1 package com.oransc.rappmanager.service;
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.mockito.ArgumentMatchers.any;
5 import static org.mockito.Mockito.when;
7 import com.oransc.rappmanager.acm.service.AcmDeployer;
8 import com.oransc.rappmanager.dme.service.DmeDeployer;
9 import com.oransc.rappmanager.models.rapp.Rapp;
10 import com.oransc.rappmanager.models.rapp.RappState;
11 import com.oransc.rappmanager.models.rappinstance.RappInstance;
12 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
13 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
14 import com.oransc.rappmanager.sme.service.SmeDeployer;
15 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
17 import java.util.UUID;
18 import org.junit.jupiter.api.Test;
19 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
21 import org.springframework.boot.test.context.SpringBootTest;
22 import org.springframework.boot.test.mock.mockito.MockBean;
23 import org.springframework.http.HttpStatus;
25 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
27 class RappServiceTest {
30 RappService rappService;
33 AcmDeployer acmDeployer;
36 SmeDeployer smeDeployer;
39 DmeDeployer dmeDeployer;
42 SmeLifecycleManager smeLifecycleManager;
45 RappInstanceStateMachine rappInstanceStateMachine;
47 String validCsarFileLocation = "src/test/resources/";
49 private final String validRappFile = "valid-rapp-package.csar";
53 void testPrimeRapp() {
54 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
55 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
56 when(acmDeployer.primeRapp(any())).thenReturn(true);
57 when(dmeDeployer.primeRapp(any())).thenReturn(true);
58 assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
59 assertEquals(RappState.PRIMED, rapp.getState());
63 void testPrimeRappInvalidState() {
64 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
65 .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build();
66 assertEquals(HttpStatus.BAD_REQUEST, rappService.primeRapp(rapp).getStatusCode());
70 void testPrimeRappAcmFailure() {
71 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
72 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
73 when(acmDeployer.primeRapp(any())).thenReturn(false);
74 when(dmeDeployer.primeRapp(any())).thenReturn(true);
75 assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
76 assertEquals(RappState.COMMISSIONED, rapp.getState());
79 void testPrimeRappDmeFailure() {
80 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
81 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
82 when(acmDeployer.primeRapp(any())).thenReturn(true);
83 when(dmeDeployer.primeRapp(any())).thenReturn(false);
84 assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
85 assertEquals(RappState.COMMISSIONED, rapp.getState());
90 void testDeprimeRapp() {
91 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
92 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
93 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
94 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
95 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
96 assertEquals(RappState.COMMISSIONED, rapp.getState());
100 void testDeprimeRappAcmFailure() {
101 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
102 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
103 when(acmDeployer.deprimeRapp(any())).thenReturn(false);
104 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
105 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
106 assertEquals(RappState.PRIMED, rapp.getState());
110 void testDeprimeRappDmeFailure() {
111 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
112 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
113 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
114 when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
115 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
116 assertEquals(RappState.PRIMED, rapp.getState());
120 void testDeprimeRappInvalidState() {
121 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
122 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
123 assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
124 assertEquals(RappState.COMMISSIONED, rapp.getState());
128 void testDeprimeRappActiveInstances() {
129 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
130 .packageLocation(validCsarFileLocation).state(RappState.PRIMED)
131 .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
132 assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
133 assertEquals(RappState.PRIMED, rapp.getState());
137 void testDeployRappInstance() {
138 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
139 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
140 RappInstance rappInstance = new RappInstance();
141 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
142 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
143 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
144 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
145 assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
149 void testDeployRappInstanceFailure() {
150 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
151 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
152 RappInstance rappInstance = new RappInstance();
153 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
154 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
155 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
156 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
157 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
161 void testDeployRappInstanceDmeFailure() {
162 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
163 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
164 RappInstance rappInstance = new RappInstance();
165 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
166 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
167 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
168 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(false);
169 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
173 void testUndeployRappInstance() {
174 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
175 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
176 RappInstance rappInstance = new RappInstance();
177 rappInstance.setState(RappInstanceState.DEPLOYED);
178 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
179 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
180 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
181 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
182 assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
186 void testUndeployRappInstanceFailure() {
187 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
188 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
189 RappInstance rappInstance = new RappInstance();
190 rappInstance.setState(RappInstanceState.DEPLOYED);
191 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
192 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
193 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
194 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
195 assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
199 void testUndeployRappInstanceDmeFailure() {
200 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
201 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
202 RappInstance rappInstance = new RappInstance();
203 rappInstance.setState(RappInstanceState.DEPLOYED);
204 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
205 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
206 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
207 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
208 assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
212 void testUndeployRappInstanceInvalidStateFailure() {
213 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
214 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
215 RappInstance rappInstance = new RappInstance();
216 rappInstance.setState(RappInstanceState.DEPLOYING);
217 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
218 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
219 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
220 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
221 assertEquals(HttpStatus.BAD_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());