Add proper error messages on delete rApp response 19/11819/1
authoraravind.est <aravindhan.a@est.tech>
Mon, 25 Sep 2023 13:06:45 +0000 (14:06 +0100)
committeraravind.est <aravindhan.a@est.tech>
Mon, 25 Sep 2023 13:06:45 +0000 (14:06 +0100)
Delete rApp provides the response based on the state of rApp and the existence of rApp instances.

Issue-ID: NONRTRIC-913
Signed-off-by: aravind.est <aravindhan.a@est.tech>
Change-Id: Ic9714a2e507e7c0a2035313ec3d3b6ee8bfddfdf

rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappController.java
rapp-manager-application/src/main/java/com/oransc/rappmanager/service/RappService.java
rapp-manager-application/src/test/java/com/oransc/rappmanager/service/RappServiceTest.java

index 7510a5c..287e297 100755 (executable)
@@ -108,14 +108,10 @@ public class RappController {
     }
 
     @DeleteMapping("{rapp_id}")
-    public ResponseEntity<Object> deleteRapp(@PathVariable("rapp_id") String rappId) {
+    public ResponseEntity<String> deleteRapp(@PathVariable("rapp_id") String rappId) {
         // @formatter:off
         return rappCacheService.getRapp(rappId)
-               .filter(rapp -> rapp.getRappInstances().isEmpty() && rapp.getState().equals(RappState.COMMISSIONED))
-               .map(rapp -> {
-                   rappCacheService.deleteRapp(rapp);
-                   return ResponseEntity.ok().build();
-               })
+               .map(rappService::deleteRapp)
                .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
                        String.format(RAPP_NOT_FOUND, rappId)));
         // @formatter:on
index 1b9fb68..925d5d3 100755 (executable)
@@ -20,6 +20,7 @@ package com.oransc.rappmanager.service;
 
 import com.oransc.rappmanager.acm.service.AcmDeployer;
 import com.oransc.rappmanager.dme.service.DmeDeployer;
+import com.oransc.rappmanager.models.cache.RappCacheService;
 import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rapp.RappEvent;
 import com.oransc.rappmanager.models.rapp.RappState;
@@ -40,6 +41,7 @@ public class RappService {
     private final SmeDeployer smeDeployer;
     private final DmeDeployer dmeDeployer;
     private final RappInstanceStateMachine rappInstanceStateMachine;
+    private final RappCacheService rappCacheService;
     private static final String STATE_TRANSITION_NOT_PERMITTED = "State transition from %s to %s is not permitted.";
 
     public ResponseEntity<String> primeRapp(Rapp rapp) {
@@ -78,6 +80,21 @@ public class RappService {
         }
     }
 
+    public ResponseEntity<String> deleteRapp(Rapp rApp) {
+        if (rApp.getRappInstances().isEmpty() && rApp.getState().equals(RappState.COMMISSIONED)) {
+            rappCacheService.deleteRapp(rApp);
+            return ResponseEntity.ok().build();
+        } else {
+            if (!rApp.getRappInstances().isEmpty()) {
+                return ResponseEntity.badRequest()
+                               .body("Unable to delete '" + rApp.getName() + "' as there are active rApp instances.");
+            } else {
+                return ResponseEntity.badRequest().body("Unable to delete '" + rApp.getName()
+                                                                + "' as the rApp is not in COMMISSIONED state.");
+            }
+        }
+    }
+
     public ResponseEntity<String> deployRappInstance(Rapp rapp, RappInstance rappInstance) {
         if (rappInstance.getState().equals(RappInstanceState.UNDEPLOYED)) {
             rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.DEPLOYING);
index c50f0b4..b45c289 100755 (executable)
@@ -21,6 +21,7 @@ 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
@@ -75,6 +76,7 @@ class RappServiceTest {
         assertEquals(HttpStatus.OK, rappService.primeRapp(rapp).getStatusCode());
         assertEquals(RappState.COMMISSIONED, rapp.getState());
     }
+
     @Test
     void testPrimeRappDmeFailure() {
         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
@@ -169,6 +171,20 @@ class RappServiceTest {
         assertEquals(HttpStatus.BAD_GATEWAY, rappService.deployRappInstance(rapp, rappInstance).getStatusCode());
     }
 
+    @Test
+    void testDeployRappInstanceFailureWithState() {
+        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);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        ResponseEntity<String> responseEntity = rappService.deployRappInstance(rapp, rappInstance);
+        assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
+        assertEquals("State transition from " + rappInstanceState + " to DEPLOYED is not permitted.",
+                responseEntity.getBody());
+    }
+
     @Test
     void testUndeployRappInstance() {
         Rapp rapp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
@@ -220,4 +236,37 @@ class RappServiceTest {
         when(dmeDeployer.undeployRappInstance(any(), any())).thenReturn(true);
         assertEquals(HttpStatus.BAD_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
     }
+
+    @Test
+    void testDeleteRappSuccess() {
+        Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)
+                            .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build();
+        assertEquals(HttpStatus.OK, rappService.deleteRapp(rApp).getStatusCode());
+    }
+
+    @Test
+    void testDeleteRappFailureWithState() {
+        String rAppName = "rAppInPrimed";
+        Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
+                            .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
+        ResponseEntity<String> 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());
+    }
+
+    @Test
+    void testDeleteRappFailureWithInstances() {
+        String rAppName = "rAppWithInstances";
+        Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name(rAppName).packageName(validRappFile)
+                            .packageLocation(validCsarFileLocation).state(RappState.PRIMED).build();
+        RappInstance rappInstance = new RappInstance();
+        rappInstance.setState(RappInstanceState.DEPLOYED);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        rApp.setRappInstances(Map.of(rappInstance.getRappInstanceId(), rappInstance));
+        ResponseEntity<String> responseEntity = rappService.deleteRapp(rApp);
+        assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
+        assertEquals("Unable to delete '" + rAppName + "' as there are active rApp instances.",
+                responseEntity.getBody());
+    }
 }