Provide proper response message/code in delete rapp instance 31/11831/1
authoraravind.est <aravindhan.a@est.tech>
Thu, 28 Sep 2023 15:09:18 +0000 (16:09 +0100)
committeraravind.est <aravindhan.a@est.tech>
Thu, 28 Sep 2023 15:09:18 +0000 (16:09 +0100)
Fixes the delete rApp instance response messages.

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

rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappInstanceController.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 9566364..a6e83dc 100755 (executable)
@@ -24,7 +24,6 @@ import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rappinstance.DeployOrder;
 import com.oransc.rappmanager.models.rappinstance.RappInstance;
 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
-import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
 import com.oransc.rappmanager.service.RappService;
 import java.util.Map;
@@ -80,7 +79,7 @@ public class RappInstanceController {
                            rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
                            return rappInstance;
                        }).map(ResponseEntity::ok).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
-                        String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
+                        String.format(RAPP_INSTANCE_NOT_FOUND, rappInstanceId)));
     }
 
     @PutMapping("{rapp_instance_id}")
@@ -103,18 +102,10 @@ public class RappInstanceController {
     @DeleteMapping("{rapp_instance_id}")
     public ResponseEntity<Object> deleteRappInstance(@PathVariable("rapp_id") String rappId,
             @PathVariable("rapp_instance_id") UUID rappInstanceId) {
-        return rappCacheService.getRapp(rappId).map(rapp -> Pair.of(rapp, rapp.getRappInstances()))
-                       .filter(rappPair -> rappPair.getRight().containsKey(rappInstanceId) && rappPair.getRight()
-                                                                                                      .get(rappInstanceId)
-                                                                                                      .getState()
-                                                                                                      .equals(RappInstanceState.UNDEPLOYED))
-                       .map(rappPair -> {
-                           rappInstanceStateMachine.deleteRappInstance(
-                                   rappPair.getLeft().getRappInstances().get(rappInstanceId));
-                           rappPair.getLeft().getRappInstances().remove(rappInstanceId);
-                           return ResponseEntity.noContent().build();
-                       }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
-                        String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
+        return rappCacheService.getRapp(rappId).filter(rApp -> rApp.getRappInstances().containsKey(rappInstanceId))
+                       .map(rApp -> rappService.deleteRappInstance(rApp, rappInstanceId)).orElseThrow(
+                        () -> new RappHandlerException(HttpStatus.NOT_FOUND,
+                                String.format(RAPP_INSTANCE_NOT_FOUND, rappId)));
     }
 
 }
index 6df12d4..9ddcd8b 100755 (executable)
@@ -21,6 +21,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.exception.RappHandlerException;
 import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rapp.RappEvent;
 import com.oransc.rappmanager.models.rapp.RappState;
@@ -28,6 +29,7 @@ import com.oransc.rappmanager.models.rappinstance.RappInstance;
 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
 import com.oransc.rappmanager.sme.service.SmeDeployer;
+import java.util.UUID;
 import lombok.RequiredArgsConstructor;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -131,6 +133,16 @@ public class RappService {
         }
     }
 
+    public ResponseEntity<Object> deleteRappInstance(Rapp rApp, UUID rappInstanceId) {
+        if (rApp.getRappInstances().get(rappInstanceId).getState().equals(RappInstanceState.UNDEPLOYED)) {
+            rappInstanceStateMachine.deleteRappInstance(rApp.getRappInstances().get(rappInstanceId));
+            rApp.getRappInstances().remove(rappInstanceId);
+            return ResponseEntity.noContent().build();
+        }
+        throw new RappHandlerException(HttpStatus.BAD_REQUEST,
+                String.format("Unable to delete rApp instance %s as it is not in UNDEPLOYED state", rappInstanceId));
+    }
+
     public void updateRappInstanceState(Rapp rapp, RappInstance rappInstance) {
         acmDeployer.syncRappInstanceStatus(rapp.getCompositionId(), rappInstance);
     }
index 6652dba..ea9d3d0 100755 (executable)
@@ -1,11 +1,13 @@
 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 +15,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;
@@ -237,6 +240,34 @@ class RappServiceTest {
         assertEquals(HttpStatus.BAD_REQUEST, rappService.undeployRappInstance(rapp, rappInstance).getStatusCode());
     }
 
+    @Test
+    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.UNDEPLOYED);
+        HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
+        rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
+        rapp.setRappInstances(rAppInstanceMap);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        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);
+        HashMap<UUID, RappInstance> rAppInstanceMap = new HashMap<>();
+        rAppInstanceMap.put(rappInstance.getRappInstanceId(), rappInstance);
+        rapp.setRappInstances(rAppInstanceMap);
+        rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
+        assertThrows(RappHandlerException.class,
+                () -> rappService.deleteRappInstance(rapp, rappInstance.getRappInstanceId()));
+    }
+
     @Test
     void testDeleteRappSuccess() {
         Rapp rApp = Rapp.builder().rappId(UUID.randomUUID()).name("").packageName(validRappFile)