1 package com.oransc.rappmanager.service;
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertThrows;
5 import static org.mockito.ArgumentMatchers.any;
6 import static org.mockito.Mockito.when;
8 import com.oransc.rappmanager.acm.service.AcmDeployer;
9 import com.oransc.rappmanager.dme.service.DmeDeployer;
10 import com.oransc.rappmanager.models.exception.RappHandlerException;
11 import com.oransc.rappmanager.models.rapp.Rapp;
12 import com.oransc.rappmanager.models.rapp.RappState;
13 import com.oransc.rappmanager.models.rappinstance.RappInstance;
14 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
15 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
16 import com.oransc.rappmanager.sme.service.SmeDeployer;
17 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
18 import java.util.HashMap;
20 import java.util.UUID;
21 import org.junit.jupiter.api.Test;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
24 import org.springframework.boot.test.context.SpringBootTest;
25 import org.springframework.boot.test.mock.mockito.MockBean;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.http.ResponseEntity;
29 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
31 class RappServiceTest {
34 RappService rappService;
37 AcmDeployer acmDeployer;
40 SmeDeployer smeDeployer;
43 DmeDeployer dmeDeployer;
46 SmeLifecycleManager smeLifecycleManager;
49 RappInstanceStateMachine rappInstanceStateMachine;
51 String validCsarFileLocation = "src/test/resources/";
53 private final String validRappFile = "valid-rapp-package.csar";
57 void testPrimeRapp() {
58 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
59 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
60 when(acmDeployer.primeRapp(any())).thenReturn(true);
61 when(dmeDeployer.primeRapp(any())).thenReturn(true);
62 assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
63 assertEquals(RappState.PRIMED, rapp.getState());
67 void testPrimeRappInvalidState() {
68 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
69 .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build();
70 assertEquals(HttpStatus.BAD_REQUEST, rappService.primeRapp(rapp).getStatusCode());
74 void testPrimeRappAcmFailure() {
75 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
76 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
77 when(acmDeployer.primeRapp(any())).thenReturn(false);
78 when(dmeDeployer.primeRapp(any())).thenReturn(true);
79 assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode());
80 assertEquals(RappState.COMMISSIONED, rapp.getState());
84 void testPrimeRappDmeFailure() {
85 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
86 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
87 when(acmDeployer.primeRapp(any())).thenReturn(true);
88 when(dmeDeployer.primeRapp(any())).thenReturn(false);
89 assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode());
90 assertEquals(RappState.COMMISSIONED, rapp.getState());
95 void testDeprimeRapp() {
96 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
97 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
98 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
99 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
100 assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
101 assertEquals(RappState.COMMISSIONED, rapp.getState());
105 void testDeprimeRappAcmFailure() {
106 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
107 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
108 when(acmDeployer.deprimeRapp(any())).thenReturn(false);
109 when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
110 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode());
111 assertEquals(RappState.PRIMED, rapp.getState());
115 void testDeprimeRappDmeFailure() {
116 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
117 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
118 when(acmDeployer.deprimeRapp(any())).thenReturn(true);
119 when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
120 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode());
121 assertEquals(RappState.PRIMED, rapp.getState());
125 void testDeprimeRappInvalidState() {
126 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
127 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
128 assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
129 assertEquals(RappState.COMMISSIONED, rapp.getState());
133 void testDeprimeRappActiveInstances() {
134 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
135 .packageLocation(validCsarFileLocation).state(RappState.PRIMED)
136 .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
137 assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
138 assertEquals(RappState.PRIMED, rapp.getState());
142 void testDeployRappInstance() {
143 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
144 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
145 RappInstance rappInstance = new RappInstance();
146 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
147 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
148 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
149 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
150 assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
154 void testDeployRappInstanceFailure() {
155 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
156 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
157 RappInstance rappInstance = new RappInstance();
158 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
159 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
160 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
161 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
162 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
166 void testDeployRappInstanceDmeFailure() {
167 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
168 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
169 RappInstance rappInstance = new RappInstance();
170 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
171 when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
172 when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
173 when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(false);
174 assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
178 void testDeployRappInstanceFailureWithState() {
179 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
180 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
181 RappInstance rappInstance = new RappInstance();
182 RappInstanceState rappInstanceState = RappInstanceState.DEPLOYED;
183 rappInstance.setState(rappInstanceState);
184 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
185 ResponseEntity<String> responseEntity = rappService.deployRappInstance(rapp, rappInstance);
186 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
187 assertEquals("State transition from " + rappInstanceState + " to DEPLOYED is not permitted.",
188 responseEntity.getBody());
192 void testUndeployRappInstance() {
193 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
194 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
195 RappInstance rappInstance = new RappInstance();
196 rappInstance.setState(RappInstanceState.DEPLOYED);
197 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
198 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
199 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
200 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
201 assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
205 void testUndeployRappInstanceFailure() {
206 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
207 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
208 RappInstance rappInstance = new RappInstance();
209 rappInstance.setState(RappInstanceState.DEPLOYED);
210 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
211 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
212 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
213 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
214 assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
218 void testUndeployRappInstanceDmeFailure() {
219 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
220 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
221 RappInstance rappInstance = new RappInstance();
222 rappInstance.setState(RappInstanceState.DEPLOYED);
223 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
224 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
225 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
226 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
227 assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
231 void testUndeployRappInstanceInvalidStateFailure() {
232 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
233 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
234 RappInstance rappInstance = new RappInstance();
235 rappInstance.setState(RappInstanceState.DEPLOYING);
236 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
237 when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
238 when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
239 when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
240 assertEquals(HttpStatus.BAD_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
244 void testDeleteRappInstance() {
245 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
246 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
247 RappInstance rappInstance = new RappInstance();
248 rappInstance.setState(RappInstanceState.UNDEPLOYED);
249 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
250 rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
251 rapp.setRappInstances(rAppInstanceMap);
252 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
253 assertEquals(HttpStatus.NO_CONTENT,
254 rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()).getStatusCode());
258 void testDeleteRappInstanceFailure() {
259 Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
260 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
261 RappInstance rappInstance = new RappInstance();
262 rappInstance.setState(RappInstanceState.DEPLOYED);
263 HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
264 rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
265 rapp.setRappInstances(rAppInstanceMap);
266 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
267 assertThrows(RappHandlerException.class,
268 () -> rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()));
272 void testDeleteRappSuccess() {
273 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
274 .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
275 assertEquals(HttpStatus.OK, rappService.deleteRapp(rApp).getStatusCode());
279 void testDeleteRappFailureWithState() {
280 String rAppName = "rAppInPrimed";
281 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
282 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
283 ResponseEntity<String> responseEntity = rappService.deleteRapp(rApp);
284 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
285 assertEquals("Unable to delete '" + rAppName + "' as the rApp is not in COMMISSIONED state.",
286 responseEntity.getBody());
290 void testDeleteRappFailureWithInstances() {
291 String rAppName = "rAppWithInstances";
292 Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
293 .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
294 RappInstance rappInstance = new RappInstance();
295 rappInstance.setState(RappInstanceState.DEPLOYED);
296 rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
297 rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance));
298 ResponseEntity<String> responseEntity = rappService.deleteRapp(rApp);
299 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
300 assertEquals("Unable to delete '" + rAppName + "' as there are active rApp instances.",
301 responseEntity.getBody());