Fix sonar code smells
[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.models.rapp.Rapp;
9 import com.oransc.rappmanager.models.rapp.RappState;
10 import com.oransc.rappmanager.models.rappinstance.RappInstance;
11 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
12 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
13 import com.oransc.rappmanager.sme.service.SmeDeployer;
14 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
15 import java.util.Map;
16 import java.util.UUID;
17 import org.junit.jupiter.api.Test;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
20 import org.springframework.boot.test.context.SpringBootTest;
21 import org.springframework.boot.test.mock.mockito.MockBean;
22 import org.springframework.http.HttpStatus;
23
24 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
25 @AutoConfigureMockMvc
26 class RappServiceTest {
27
28     @Autowired
29     RappService rappService;
30
31     @MockBean
32     AcmDeployer acmDeployer;
33
34     @MockBean
35     SmeDeployer smeDeployer;
36
37     @MockBean
38     SmeLifecycleManager smeLifecycleManager;
39
40     @Autowired
41     RappInstanceStateMachine rappInstanceStateMachine;
42
43     String validCsarFileLocation = "src/test/resources/";
44
45     private final String validRappFile = "valid-rapp-package.csar";
46
47     private final String invalidRappFile = "invalid-rapp-package.csar";
48
49
50     @Test
51     void testPrimeRapp() {
52         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
53                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
54         when(acmDeployer.primeRapp(any())).thenReturn(true);
55         assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
56     }
57
58     @Test
59     void testPrimeRappInvalidState() {
60         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
61                             .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build();
62         assertEquals(HttpStatus.BAD_REQUEST, rappService.primeRapp(rapp).getStatusCode());
63     }
64
65     @Test
66     void testPrimeRappAcmFailure() {
67         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
68                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
69         when(acmDeployer.primeRapp(any())).thenReturn(false);
70         assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
71     }
72
73     @Test
74     void testDeprimeRapp() {
75         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
76                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
77         when(acmDeployer.deprimeRapp(any())).thenReturn(true);
78         assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
79     }
80
81     @Test
82     void testDeprimeRappFailure() {
83         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
84                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
85         when(acmDeployer.deprimeRapp(any())).thenReturn(false);
86         assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode());
87     }
88
89     @Test
90     void testDeprimeRappInvalidState() {
91         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
92                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
93         assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
94     }
95
96     @Test
97     void testDeprimeRappActiveInstances() {
98         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
99                             .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED)
100                             .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build();
101         assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode());
102     }
103
104     @Test
105     void testDeployRappInstance() {
106         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
107                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
108         RappInstance rappInstance = new RappInstance();
109         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
110         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
111         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true);
112         assertEquals(HttpStatus.ACCEPTED, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
113     }
114
115     @Test
116     void testDeployRappInstanceFailure() {
117         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
118                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
119         RappInstance rappInstance = new RappInstance();
120         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
121         when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true);
122         when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false);
123         assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
124     }
125
126     @Test
127     void testUndeployRappInstance() {
128         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
129                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
130         RappInstance rappInstance = new RappInstance();
131         rappInstance.setState(RappInstanceState.DEPLOYED);
132         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
133         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
134         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
135         assertEquals(HttpStatus.ACCEPTED, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
136     }
137
138     @Test
139     void testUndeployRappInstanceFailure() {
140         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
141                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
142         RappInstance rappInstance = new RappInstance();
143         rappInstance.setState(RappInstanceState.DEPLOYED);
144         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
145         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
146         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
147         assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
148     }
149
150     @Test
151     void testUndeployRappInstanceInvalidStateFailure() {
152         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
153                             .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
154         RappInstance rappInstance = new RappInstance();
155         rappInstance.setState(RappInstanceState.DEPLOYING);
156         rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
157         when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true);
158         when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false);
159         assertEquals(HttpStatus.BAD_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
160     }
161 }