c50f0b4287931f7d3cc7201ae2a645bda934cab4
[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
25 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
26 @AutoConfigureMockMvc
27 class RappServiceTest {
28
29     @Autowired
30     RappService rappService;
31
32     @MockBean
33     AcmDeployer acmDeployer;
34
35     @MockBean
36     SmeDeployer smeDeployer;
37
38     @MockBean
39     DmeDeployer dmeDeployer;
40
41     @MockBean
42     SmeLifecycleManager smeLifecycleManager;
43
44     @Autowired
45     RappInstanceStateMachine rappInstanceStateMachine;
46
47     String validCsarFileLocation = "src/test/resources/";
48
49     private final String validRappFile = "valid-rapp-package.csar";
50
51
52     @Test
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());
60     }
61
62     @Test
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());
67     }
68
69     @Test
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());
77     }
78     @Test
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());
86     }
87
88
89     @Test
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());
97     }
98
99     @Test
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());
107     }
108
109     @Test
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());
117     }
118
119     @Test
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());
125     }
126
127     @Test
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());
134     }
135
136     @Test
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());
146     }
147
148     @Test
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());
158     }
159
160     @Test
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());
170     }
171
172     @Test
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());
183     }
184
185     @Test
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());
196     }
197
198     @Test
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());
209     }
210
211     @Test
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());
222     }
223 }