Provide proper response message/code in delete rapp instance
[nonrtric/plt/rappmanager.git] / rapp-manager-application / src / test / java / com / oransc / rappmanager / service / RappServiceTest.java
1 package com.oransc.rappmanager.service;
2
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;
7
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;
19 import java.util.Map;
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;
28
29 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
30 @AutoConfigureMockMvc
31 class RappServiceTest {
32
33     @Autowired
34     RappService rappService;
35
36     @MockBean
37     AcmDeployer acmDeployer;
38
39     @MockBean
40     SmeDeployer smeDeployer;
41
42     @MockBean
43     DmeDeployer dmeDeployer;
44
45     @MockBean
46     SmeLifecycleManager smeLifecycleManager;
47
48     @Autowired
49     RappInstanceStateMachine rappInstanceStateMachine;
50
51     String validCsarFileLocation = "src/test/resources/";
52
53     private final String validRappFile = "valid-rapp-package.csar";
54
55
56     @Test
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());
64     }
65
66     @Test
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());
71     }
72
73     @Test
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());
81     }
82
83     @Test
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());
91     }
92
93
94     @Test
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());
102     }
103
104     @Test
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());
112     }
113
114     @Test
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());
122     }
123
124     @Test
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());
130     }
131
132     @Test
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());
139     }
140
141     @Test
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());
151     }
152
153     @Test
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());
163     }
164
165     @Test
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());
175     }
176
177     @Test
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());
189     }
190
191     @Test
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());
202     }
203
204     @Test
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());
215     }
216
217     @Test
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());
228     }
229
230     @Test
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());
241     }
242
243     @Test
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());
255     }
256
257     @Test
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()));
269     }
270
271     @Test
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());
276     }
277
278     @Test
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());
287     }
288
289     @Test
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());
302     }
303 }