1 package com.oransc.rappmanager.service;
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.mockito.ArgumentMatchers.any;
5 import static org.mockito.Mockito.when;
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;
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;
24 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
26 class RappServiceTest {
29 RappService rappService;
32 AcmDeployer acmDeployer;
35 SmeDeployer smeDeployer;
38 SmeLifecycleManager smeLifecycleManager;
41 RappInstanceStateMachine rappInstanceStateMachine;
43 String validCsarFileLocation = "src/test/resources/";
45 private final String validRappFile = "valid-rapp-package.csar";
47 private final String invalidRappFile = "invalid-rapp-package.csar";
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());