X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=rapp-manager-application%2Fsrc%2Ftest%2Fjava%2Fcom%2Foransc%2Frappmanager%2Fservice%2FRappServiceTest.java;h=4154b6c3a2073553eb8f1b6d6e0d359b8cf431d4;hb=c8d173152d53450283b8a0f77f4644d315e57a91;hp=ea9d3d01d9f5f8fbbcf88bfc0f1570d03ff9d0d8;hpb=23d4ff14d9d361656b690bcfe317a68bda6b9275;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 ea9d3d0..4154b6c 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,3 +1,22 @@ +/*- + * ============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; @@ -24,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 @@ -52,6 +70,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() { @@ -59,6 +79,7 @@ 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); assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode()); assertEquals(RappState.PRIMED, rapp.getState()); } @@ -67,7 +88,12 @@ 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 @@ -76,7 +102,9 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); when(acmDeployer.primeRapp(any())).thenReturn(false); when(dmeDeployer.primeRapp(any())).thenReturn(true); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.COMMISSIONED, rapp.getState()); } @@ -86,7 +114,9 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); when(acmDeployer.primeRapp(any())).thenReturn(true); when(dmeDeployer.primeRapp(any())).thenReturn(false); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.primeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.primeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.COMMISSIONED, rapp.getState()); } @@ -97,6 +127,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()); } @@ -107,7 +138,9 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); when(acmDeployer.deprimeRapp(any())).thenReturn(false); when(dmeDeployer.deprimeRapp(any())).thenReturn(true); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.PRIMED, rapp.getState()); } @@ -117,7 +150,9 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); when(acmDeployer.deprimeRapp(any())).thenReturn(true); when(dmeDeployer.deprimeRapp(any())).thenReturn(false); - assertEquals(HttpStatus.BAD_GATEWAY, rappService.deprimeRapp(rapp).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.deprimeRapp(rapp)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); assertEquals(RappState.PRIMED, rapp.getState()); } @@ -125,7 +160,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()); } @@ -134,7 +173,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()); } @@ -159,19 +200,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 @@ -179,13 +210,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 @@ -211,20 +244,9 @@ 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()); - } - - @Test - void testUndeployRappInstanceDmeFailure() { - Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile) - .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); - RappInstance rappInstance = new RappInstance(); - rappInstance.setState(RappInstanceState.DEPLOYED); - 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()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance)); + assertEquals(HttpStatus.BAD_GATEWAY, rappHandlerException.getStatusCode()); } @Test @@ -237,7 +259,9 @@ 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_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode()); + RappHandlerException rappHandlerException = + assertThrows(RappHandlerException.class, () -> rappService.undeployRappInstance(rapp, rappInstance)); + assertEquals(HttpStatus.BAD_REQUEST, rappHandlerException.getStatusCode()); } @Test @@ -260,12 +284,17 @@ class RappServiceTest { .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build(); RappInstance rappInstance = new RappInstance(); rappInstance.setState(RappInstanceState.DEPLOYED); + UUID rappInstanceId = rappInstance.getRappInstanceId(); HashMap rAppInstanceMap = new HashMap<>(); - rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance); + rAppInstanceMap.put(rappInstanceId, rappInstance); rapp.setRappInstances(rAppInstanceMap); rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId()); - assertThrows(RappHandlerException.class, - () -> rappService.deleteRappInstance(rapp, 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 @@ -280,10 +309,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 @@ -295,9 +326,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()); } }