X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=rapp-manager-application%2Fsrc%2Ftest%2Fjava%2Fcom%2Foransc%2Frappmanager%2Fservice%2FRappServiceTest.java;h=9dc4987c4c0284dfb2f798bf715a9c803e2a82d3;hb=a46c269e0972357c54378520a4bba531bf7bb12d;hp=b45c289fdac814cbedbb3d3b6382343a44126129;hpb=905289b22b41eca7b4a2e317c078a8dd8fca5ad6;p=nonrtric%2Fplt%2Frappmanager.git diff --git a/rapp-manager-application/src/test/java/com/oransc/rappmanager/service/RappServiceTest.java b/rapp-manager-application/src/test/java/com/oransc/rappmanager/service/RappServiceTest.java index b45c289..9dc4987 100755 --- a/rapp-manager-application/src/test/java/com/oransc/rappmanager/service/RappServiceTest.java +++ b/rapp-manager-application/src/test/java/com/oransc/rappmanager/service/RappServiceTest.java @@ -1,11 +1,32 @@ +/*- + * ============LICENSE_START====================================================================== + * Copyright (C) 2023 Nordix Foundation. All rights reserved. + * Copyright (C) 2023-2024 OpenInfra Foundation Europe. All rights reserved. + * =============================================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END======================================================================== + */ + package com.oransc.rappmanager.service; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import com.oransc.rappmanager.acm.service.AcmDeployer; import com.oransc.rappmanager.dme.service.DmeDeployer; +import com.oransc.rappmanager.models.exception.RappHandlerException; import com.oransc.rappmanager.models.rapp.Rapp; import com.oransc.rappmanager.models.rapp.RappState; import com.oransc.rappmanager.models.rappinstance.RappInstance; @@ -13,6 +34,7 @@ import com.oransc.rappmanager.models.rappinstance.RappInstanceState; import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine; import com.oransc.rappmanager.sme.service.SmeDeployer; import com.oransc.rappmanager.sme.service.SmeLifecycleManager; +import java.util.HashMap; import java.util.Map; import java.util.UUID; import org.junit.jupiter.api.Test; @@ -21,7 +43,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc @@ -39,6 +60,9 @@ class RappServiceTest { @MockBean DmeDeployer dmeDeployer; + @MockBean + DeploymentArtifactsService deploymentArtifactsService; + @MockBean SmeLifecycleManager smeLifecycleManager; @@ -49,6 +73,8 @@ class RappServiceTest { private final String validRappFile = "valid-rapp-package.csar"; + private final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted."; + @Test void testPrimeRapp() { @@ -56,6 +82,8 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); when(acmDeployer.primeRapp(any())).thenReturn(true); when(dmeDeployer.primeRapp(any())).thenReturn(true); + when(smeDeployer.primeRapp(any())).thenReturn(true); + when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true); assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode()); assertEquals(RappState.PRIMED, rapp.getState()); } @@ -64,16 +92,24 @@ class RappServiceTest { void testPrimeRappInvalidState() { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.PRIMING).build(); - assertEquals(HttpStatus.BAD_REQUEST, rappService.primeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); + assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.PRIMING, RappState.PRIMED), + rappHandlerException.getMessage()); + assertEquals(RappState.PRIMING, rapp.getState()); } @Test void testPrimeRappAcmFailure() { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); + when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true); when(acmDeployer.primeRapp(any())).thenReturn(false); when(dmeDeployer.primeRapp(any())).thenReturn(true); - assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.COMMISSIONED, rapp.getState()); } @@ -81,12 +117,25 @@ class RappServiceTest { void testPrimeRappDmeFailure() { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); + when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(true); when(acmDeployer.primeRapp(any())).thenReturn(true); when(dmeDeployer.primeRapp(any())).thenReturn(false); - assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.COMMISSIONED, rapp.getState()); } + @Test + void testPrimeRappDeployArtifactFailure() { + Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) + .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); + when(deploymentArtifactsService.configureDeploymentArtifacts(any())).thenReturn(false); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); + assertEquals(RappState.COMMISSIONED, rapp.getState()); + } @Test void testDeprimeRapp() { @@ -94,6 +143,7 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); when(acmDeployer.deprimeRapp(any())).thenReturn(true); when(dmeDeployer.deprimeRapp(any())).thenReturn(true); + when(smeDeployer.deprimeRapp(any())).thenReturn(true); assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode()); assertEquals(RappState.COMMISSIONED, rapp.getState()); } @@ -104,7 +154,9 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); when(acmDeployer.deprimeRapp(any())).thenReturn(false); when(dmeDeployer.deprimeRapp(any())).thenReturn(true); - assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.PRIMED, rapp.getState()); } @@ -114,7 +166,9 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); when(acmDeployer.deprimeRapp(any())).thenReturn(true); when(dmeDeployer.deprimeRapp(any())).thenReturn(false); - assertEquals(HttpStatus.OK, rappService.deprimeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.PRIMED, rapp.getState()); } @@ -122,7 +176,11 @@ class RappServiceTest { void testDeprimeRappInvalidState() { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); - assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); + assertEquals(String.format(STATE_TRANSITION_NOT_PERMITTED, RappState.COMMISSIONED, RappState.COMMISSIONED), + rappHandlerException.getMessage()); assertEquals(RappState.COMMISSIONED, rapp.getState()); } @@ -131,7 +189,9 @@ class RappServiceTest { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.PRIMED) .rappInstances(Map.of(UUID.randomUUID(), new RappInstance())).build(); - assertEquals(HttpStatus.BAD_REQUEST, rappService.deprimeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); assertEquals(RappState.PRIMED, rapp.getState()); } @@ -156,19 +216,9 @@ class RappServiceTest { when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true); when(smeDeployer.deployRappInstance(any(), any())).thenReturn(false); when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(true); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode()); - } - - @Test - void testDeployRappInstanceDmeFailure() { - Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) - .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); - RappInstance rappInstance = new RappInstance(); - rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId()); - when(acmDeployer.deployRappInstance(any(), any())).thenReturn(true); - when(smeDeployer.deployRappInstance(any(), any())).thenReturn(true); - when(dmeDeployer.deployRappInstance(any(), any())).thenReturn(false); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); } @Test @@ -176,13 +226,15 @@ class RappServiceTest { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); RappInstance rappInstance = new RappInstance(); - RappInstanceState rappInstanceState = RappInstanceState.DEPLOYED; - rappInstance.setState(rappInstanceState); + rappInstance.setState(RappInstanceState.DEPLOYED); rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId()); - ResponseEntity responseEntity = rappService.deployRappInstance(rapp, rappInstance); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - assertEquals("State transition from " + rappInstanceState + " to DEPLOYED is not permitted.", - responseEntity.getBody()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deployRappInstance(rapp, rappInstance)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); + assertEquals(String.format("Unable to deploy rApp instance %s as it is not in UNDEPLOYED state", + rappInstance.getRappInstanceId()), rappHandlerException.getMessage()); + assertEquals(RappState.PRIMED, rapp.getState()); + } @Test @@ -208,33 +260,57 @@ class RappServiceTest { when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true); when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false); when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); } @Test - void testUndeployRappInstanceDmeFailure() { + void testUndeployRappInstanceInvalidStateFailure() { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); RappInstance rappInstance = new RappInstance(); - rappInstance.setState(RappInstanceState.DEPLOYED); + rappInstance.setState(RappInstanceState.DEPLOYING); rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId()); when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true); - when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(true); - when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(false); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode()); + when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false); + when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); } @Test - void testUndeployRappInstanceInvalidStateFailure() { + void testDeleteRappInstance() { Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); RappInstance rappInstance = new RappInstance(); - rappInstance.setState(RappInstanceState.DEPLOYING); + rappInstance.setState(RappInstanceState.UNDEPLOYED); + HashMap rAppInstanceMap = new HashMap<>(); + rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance); + rapp.setRappInstances(rAppInstanceMap); rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId()); - when(acmDeployer.undeployRappInstance(any(), any())).thenReturn(true); - when(smeDeployer.undeployRappInstance(any(), any())).thenReturn(false); - when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true); - assertEquals(HttpStatus.BAD_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode()); + assertEquals(HttpStatus.NO_CONTENT, + rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()).getStatusCode()); + } + + @Test + void testDeleteRappInstanceFailure() { + Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) + .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); + RappInstance rappInstance = new RappInstance(); + rappInstance.setState(RappInstanceState.DEPLOYED); + UUID rappInstanceId = rappInstance.getRappInstanceId(); + HashMap rAppInstanceMap = new HashMap<>(); + rAppInstanceMap.put(rappInstanceId, rappInstance); + rapp.setRappInstances(rAppInstanceMap); + rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deleteRappInstance(rapp, rappInstanceId)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); + assertEquals(String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", + rappInstance.getRappInstanceId()), rappHandlerException.getMessage()); + assertEquals(RappState.PRIMED, rapp.getState()); } @Test @@ -249,10 +325,12 @@ class RappServiceTest { String rAppName = "rAppInPrimed"; Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile) .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); - ResponseEntity responseEntity = rappService.deleteRapp(rApp); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - assertEquals("Unable to delete '" + rAppName + "' as the rApp is not in COMMISSIONED state.", - responseEntity.getBody()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); + assertEquals(String.format("Unable to delete %s as the rApp is not in COMMISSIONED state.", rAppName), + rappHandlerException.getMessage()); + assertEquals(RappState.PRIMED, rApp.getState()); } @Test @@ -264,9 +342,11 @@ class RappServiceTest { rappInstance.setState(RappInstanceState.DEPLOYED); rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId()); rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance)); - ResponseEntity responseEntity = rappService.deleteRapp(rApp); - assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); - assertEquals("Unable to delete '" + rAppName + "' as there are active rApp instances.", - responseEntity.getBody()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deleteRapp(rApp)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); + assertEquals(String.format("Unable to delete %s as there are active rApp instances.", rAppName), + rappHandlerException.getMessage()); + assertEquals(RappState.PRIMED, rApp.getState()); } }