b45c289fdac814cbedbb3d3b6382343a44126129
[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.mockito.ArgumentMatchers.any;
5 import static org.mockito.Mockito.when;
6
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;
16 import java.util.Map;
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;
24 import org.springframework.http.ResponseEntity;
25
26 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
27 @AutoConfigureMockMvc
28 class RappServiceTest {
29
30     @Autowired
31     RappService rappService;
32
33     @MockBean
34     AcmDeployer acmDeployer;
35
36     @MockBean
37     SmeDeployer smeDeployer;
38
39     @MockBean
40     DmeDeployer dmeDeployer;
41
42     @MockBean
43     SmeLifecycleManager smeLifecycleManager;
44
45     @Autowired
46     RappInstanceStateMachine rappInstanceStateMachine;
47
48     String validCsarFileLocation = "src/test/resources/";
49
50     private final String validRappFile = "valid-rapp-package.csar";
51
52
53     @Test
54     void testPrimeRapp() {
55         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
56                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
57         when(acmDeployer.primeRapp(any())).thenReturn(true);
58         when(dmeDeployer.primeRapp(any())).thenReturn(true);
59         assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
60         assertEquals(RappState.PRIMED, rapp.getState());
61     }
62
63     @Test
64     void testPrimeRappInvalidState() {
65         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
66                             .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build();
67         assertEquals(HttpStatus.BAD_REQUEST, rappService.primeRapp(rapp).getStatusCode());
68     }
69
70     @Test
71     void testPrimeRappAcmFailure() {
72         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
73                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
74         when(acmDeployer.primeRapp(any())).thenReturn(false);
75         when(dmeDeployer.primeRapp(any())).thenReturn(true);
76         assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
77         assertEquals(RappState.COMMISSIONED, rapp.getState());
78     }
79
80     @Test
81     void testPrimeRappDmeFailure() {
82         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
83                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
84         when(acmDeployer.primeRapp(any())).thenReturn(true);
85         when(dmeDeployer.primeRapp(any())).thenReturn(false);
86         assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
87         assertEquals(RappState.COMMISSIONED, rapp.getState());
88     }
89
90
91     @Test
92     void testDeprimeRapp() {
93         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
94                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
95         when(acmDeployer.deprimeRapp(any())).thenReturn(true);
96         when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
97         assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
98         assertEquals(RappState.COMMISSIONED, rapp.getState());
99     }
100
101     @Test
102     void testDeprimeRappAcmFailure() {
103         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
104                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
105         when(acmDeployer.deprimeRapp(any())).thenReturn(false);
106         when(dmeDeployer.deprimeRapp(any())).thenReturn(true);
107         assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
108         assertEquals(RappState.PRIMED, rapp.getState());
109     }
110
111     @Test
112     void testDeprimeRappDmeFailure() {
113         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
114                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
115         when(acmDeployer.deprimeRapp(any())).thenReturn(true);
116         when(dmeDeployer.deprimeRapp(any())).thenReturn(false);
117         assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
118         assertEquals(RappState.PRIMED, rapp.getState());
119     }
120
121     @Test
122     void testDeprimeRappInvalidState() {
123         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
124                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
125         assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
126         assertEquals(RappState.COMMISSIONED, rapp.getState());
127     }
128
129     @Test
130     void testDeprimeRappActiveInstances() {
131         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
132                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED)
133                             .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
134         assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
135         assertEquals(RappState.PRIMED, rapp.getState());
136     }
137
138     @Test
139     void testDeployRappInstance() {
140         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
141                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
142         RappInstance rappInstance = new RappInstance();
143         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
144         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
145         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
146         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
147         assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
148     }
149
150     @Test
151     void testDeployRappInstanceFailure() {
152         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
153                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
154         RappInstance rappInstance = new RappInstance();
155         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
156         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
157         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
158         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true);
159         assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
160     }
161
162     @Test
163     void testDeployRappInstanceDmeFailure() {
164         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
165                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
166         RappInstance rappInstance = new RappInstance();
167         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
168         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
169         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
170         when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(false);
171         assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
172     }
173
174     @Test
175     void testDeployRappInstanceFailureWithState() {
176         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
177                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
178         RappInstance rappInstance = new RappInstance();
179         RappInstanceState rappInstanceState = RappInstanceState.DEPLOYED;
180         rappInstance.setState(rappInstanceState);
181         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
182         ResponseEntity<String> responseEntity = rappService.deployRappInstance(rapp, rappInstance);
183         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
184         assertEquals("State transition from " + rappInstanceState + " to DEPLOYED is not permitted.",
185                 responseEntity.getBody());
186     }
187
188     @Test
189     void testUndeployRappInstance() {
190         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
191                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
192         RappInstance rappInstance = new RappInstance();
193         rappInstance.setState(RappInstanceState.DEPLOYED);
194         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
195         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
196         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
197         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
198         assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
199     }
200
201     @Test
202     void testUndeployRappInstanceFailure() {
203         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
204                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
205         RappInstance rappInstance = new RappInstance();
206         rappInstance.setState(RappInstanceState.DEPLOYED);
207         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
208         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
209         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
210         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
211         assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
212     }
213
214     @Test
215     void testUndeployRappInstanceDmeFailure() {
216         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
217                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
218         RappInstance rappInstance = new RappInstance();
219         rappInstance.setState(RappInstanceState.DEPLOYED);
220         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
221         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
222         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
223         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
224         assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
225     }
226
227     @Test
228     void testUndeployRappInstanceInvalidStateFailure() {
229         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
230                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
231         RappInstance rappInstance = new RappInstance();
232         rappInstance.setState(RappInstanceState.DEPLOYING);
233         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
234         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
235         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
236         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
237         assertEquals(HttpStatus.BAD_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
238     }
239
240     @Test
241     void testDeleteRappSuccess() {
242         Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
243                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
244         assertEquals(HttpStatus.OK, rappService.deleteRapp(rApp).getStatusCode());
245     }
246
247     @Test
248     void testDeleteRappFailureWithState() {
249         String rAppName = "rAppInPrimed";
250         Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
251                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
252         ResponseEntity<String> responseEntity = rappService.deleteRapp(rApp);
253         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
254         assertEquals("Unable to delete '" + rAppName + "' as the rApp is not in COMMISSIONED state.",
255                 responseEntity.getBody());
256     }
257
258     @Test
259     void testDeleteRappFailureWithInstances() {
260         String rAppName = "rAppWithInstances";
261         Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
262                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
263         RappInstance rappInstance = new RappInstance();
264         rappInstance.setState(RappInstanceState.DEPLOYED);
265         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
266         rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance));
267         ResponseEntity<String> responseEntity = rappService.deleteRapp(rApp);
268         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
269         assertEquals("Unable to delete '" + rAppName + "' as there are active rApp instances.",
270                 responseEntity.getBody());
271     }
272 }