Add Error messages in rest response 24/11624/2
authoraravind.est <aravindhan.a@est.tech>
Wed, 16 Aug 2023 15:40:01 +0000 (16:40 +0100)
committerJohn Keeney <john.keeney@est.tech>
Mon, 21 Aug 2023 15:04:19 +0000 (15:04 +0000)
Include messages on the http response in the controller.

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

rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappController.java
rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappInstanceController.java
rapp-manager-application/src/test/java/com/oransc/rappmanager/rest/RappControllerTest.java
rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/ErrorResponse.java [new file with mode: 0755]
rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/ExceptionControllerHandler.java [new file with mode: 0755]
rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/RappHandlerException.java [new file with mode: 0755]

index 5376c07..8cfbddc 100755 (executable)
@@ -21,6 +21,7 @@ package com.oransc.rappmanager.rest;
 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
 import com.oransc.rappmanager.models.cache.RappCacheService;
 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
 import com.oransc.rappmanager.configuration.RappManagerConfiguration;
 import com.oransc.rappmanager.models.cache.RappCacheService;
 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
+import com.oransc.rappmanager.models.exception.RappHandlerException;
 import com.oransc.rappmanager.models.rapp.PrimeOrder;
 import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
 import com.oransc.rappmanager.models.rapp.PrimeOrder;
 import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
@@ -35,6 +36,7 @@ import java.util.Optional;
 import lombok.RequiredArgsConstructor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import lombok.RequiredArgsConstructor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -65,7 +67,8 @@ public class RappController {
 
     @GetMapping("{rapp_id}")
     public ResponseEntity<Rapp> getRapp(@PathVariable("rapp_id") String rappId) {
 
     @GetMapping("{rapp_id}")
     public ResponseEntity<Rapp> getRapp(@PathVariable("rapp_id") String rappId) {
-        return rappCacheService.getRapp(rappId).map(ResponseEntity::ok).orElse(ResponseEntity.badRequest().build());
+        return rappCacheService.getRapp(rappId).map(ResponseEntity::ok).orElseThrow(
+                () -> new RappHandlerException(HttpStatus.NOT_FOUND, "rApp '" + rappId + "' not found."));
     }
 
     @PostMapping("{rapp_id}")
     }
 
     @PostMapping("{rapp_id}")
@@ -85,7 +88,7 @@ public class RappController {
             return ResponseEntity.accepted().build();
         } else {
             logger.info("Invalid Rapp package for {}", rappId);
             return ResponseEntity.accepted().build();
         } else {
             logger.info("Invalid Rapp package for {}", rappId);
-            return ResponseEntity.badRequest().build();
+            throw new RappHandlerException(HttpStatus.BAD_REQUEST, "Invalid rApp package.");
         }
     }
 
         }
     }
 
@@ -98,7 +101,8 @@ public class RappController {
                             .filter(primeOrder -> primeOrder.equals(PrimeOrder.PRIME))
                             .map(primeOrder -> rappService.primeRapp(rapp))
                             .orElseGet(() -> rappService.deprimeRapp(rapp)))
                             .filter(primeOrder -> primeOrder.equals(PrimeOrder.PRIME))
                             .map(primeOrder -> rappService.primeRapp(rapp))
                             .orElseGet(() -> rappService.deprimeRapp(rapp)))
-                       .orElse(ResponseEntity.notFound().build());
+                       .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+                               "rApp '" + rappId + "' not found."));
         // @formatter:on
     }
 
         // @formatter:on
     }
 
@@ -111,7 +115,8 @@ public class RappController {
                    rappCacheService.deleteRapp(rapp);
                    return ResponseEntity.ok().build();
                })
                    rappCacheService.deleteRapp(rapp);
                    return ResponseEntity.ok().build();
                })
-               .orElse(ResponseEntity.notFound().build());
+               .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+                               "rApp '" + rappId + "' not found."));
         // @formatter:on
     }
 }
         // @formatter:on
     }
 }
index 20fbdb0..20bfc6a 100755 (executable)
 
 package com.oransc.rappmanager.rest;
 
 
 package com.oransc.rappmanager.rest;
 
-import com.oransc.rappmanager.models.rappinstance.DeployOrder;
+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.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.rappinstance.RappInstance;
 import com.oransc.rappmanager.models.rappinstance.RappInstanceDeployOrder;
 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
-import com.oransc.rappmanager.models.cache.RappCacheService;
 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
 import com.oransc.rappmanager.service.RappService;
 import java.util.Map;
 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
 import com.oransc.rappmanager.service.RappService;
 import java.util.Map;
@@ -31,6 +32,7 @@ import java.util.Optional;
 import java.util.UUID;
 import lombok.RequiredArgsConstructor;
 import org.apache.commons.lang3.tuple.Pair;
 import java.util.UUID;
 import lombok.RequiredArgsConstructor;
 import org.apache.commons.lang3.tuple.Pair;
+import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -52,8 +54,8 @@ public class RappInstanceController {
 
     @GetMapping
     public ResponseEntity<Map<UUID, RappInstance>> getAllRappInstances(@PathVariable("rapp_id") String rappId) {
 
     @GetMapping
     public ResponseEntity<Map<UUID, RappInstance>> getAllRappInstances(@PathVariable("rapp_id") String rappId) {
-        return rappCacheService.getRapp(rappId).map(Rapp::getRappInstances).map(ResponseEntity::ok)
-                       .orElse(ResponseEntity.notFound().build());
+        return rappCacheService.getRapp(rappId).map(Rapp::getRappInstances).map(ResponseEntity::ok).orElseThrow(
+                () -> new RappHandlerException(HttpStatus.NOT_FOUND, "No instance found for rApp '" + rappId + "'."));
     }
 
     @PostMapping
     }
 
     @PostMapping
@@ -63,7 +65,7 @@ public class RappInstanceController {
             rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
             rapp.getRappInstances().put(rappInstance.getRappInstanceId(), rappInstance);
             return ResponseEntity.ok(rappInstance);
             rappInstanceStateMachine.onboardRappInstance(rappInstance.getRappInstanceId());
             rapp.getRappInstances().put(rappInstance.getRappInstanceId(), rappInstance);
             return ResponseEntity.ok(rappInstance);
-        }).orElse(ResponseEntity.notFound().build());
+        }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND, "rApp '" + rappId + "' not found."));
     }
 
     @GetMapping("{rapp_instance_id}")
     }
 
     @GetMapping("{rapp_instance_id}")
@@ -76,7 +78,8 @@ public class RappInstanceController {
                            RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
                            rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
                            return rappInstance;
                            RappInstance rappInstance = rappPair.getLeft().getRappInstances().get(rappInstanceId);
                            rappInstance.setState(rappInstanceStateMachine.getRappInstanceState(rappInstanceId));
                            return rappInstance;
-                       }).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
+                       }).map(ResponseEntity::ok).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+                        "No instance found for rApp '" + rappId + "'."));
     }
 
     @PutMapping("{rapp_instance_id}")
     }
 
     @PutMapping("{rapp_instance_id}")
@@ -91,7 +94,8 @@ public class RappInstanceController {
                         .filter(deployOrder -> deployOrder.equals(DeployOrder.DEPLOY))
                         .map(primeOrder -> rappService.deployRappInstance(rappPair.getLeft(), rappPair.getRight()))
                         .orElseGet(() -> rappService.undeployRappInstance(rappPair.getLeft(), rappPair.getRight())))
                         .filter(deployOrder -> deployOrder.equals(DeployOrder.DEPLOY))
                         .map(primeOrder -> rappService.deployRappInstance(rappPair.getLeft(), rappPair.getRight()))
                         .orElseGet(() -> rappService.undeployRappInstance(rappPair.getLeft(), rappPair.getRight())))
-                   .orElse(ResponseEntity.notFound().build());
+                   .orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+                           "rApp instance '" + rappInstanceId + "' not found."));
         //@formatter:on
     }
 
         //@formatter:on
     }
 
@@ -108,7 +112,8 @@ public class RappInstanceController {
                                    rappPair.getLeft().getRappInstances().get(rappInstanceId));
                            rappPair.getLeft().getRappInstances().remove(rappInstanceId);
                            return ResponseEntity.noContent().build();
                                    rappPair.getLeft().getRappInstances().get(rappInstanceId));
                            rappPair.getLeft().getRappInstances().remove(rappInstanceId);
                            return ResponseEntity.noContent().build();
-                       }).orElse(ResponseEntity.notFound().build());
+                       }).orElseThrow(() -> new RappHandlerException(HttpStatus.NOT_FOUND,
+                        "rApp instance '" + rappInstanceId + "' not found."));
     }
 
 }
     }
 
 }
index 9ff7c8f..50d62fa 100755 (executable)
@@ -8,11 +8,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.oransc.rappmanager.acm.service.AcmDeployer;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.oransc.rappmanager.acm.service.AcmDeployer;
+import com.oransc.rappmanager.models.cache.RappCacheService;
 import com.oransc.rappmanager.models.rapp.PrimeOrder;
 import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
 import com.oransc.rappmanager.models.rapp.RappState;
 import com.oransc.rappmanager.models.rapp.PrimeOrder;
 import com.oransc.rappmanager.models.rapp.Rapp;
 import com.oransc.rappmanager.models.rapp.RappPrimeOrder;
 import com.oransc.rappmanager.models.rapp.RappState;
-import com.oransc.rappmanager.models.cache.RappCacheService;
 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
 import java.io.File;
 import java.io.FileInputStream;
 import com.oransc.rappmanager.sme.service.SmeLifecycleManager;
 import java.io.File;
 import java.io.FileInputStream;
@@ -79,7 +79,7 @@ public class RappControllerTest {
     @Test
     void testGetInvalidRapp() throws Exception {
         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
     @Test
     void testGetInvalidRapp() throws Exception {
         mockMvc.perform(MockMvcRequestBuilders.get("/rapps/{rapp_id}", UUID.randomUUID()))
-                .andExpect(status().isBadRequest());
+                .andExpect(status().isNotFound());
     }
 
     @Test
     }
 
     @Test
diff --git a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/ErrorResponse.java b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/ErrorResponse.java
new file mode 100755 (executable)
index 0000000..9a48c05
--- /dev/null
@@ -0,0 +1,9 @@
+package com.oransc.rappmanager.models.exception;
+
+import lombok.Value;
+
+@Value
+public class ErrorResponse {
+
+    String message;
+}
diff --git a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/ExceptionControllerHandler.java b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/ExceptionControllerHandler.java
new file mode 100755 (executable)
index 0000000..1d66173
--- /dev/null
@@ -0,0 +1,15 @@
+package com.oransc.rappmanager.models.exception;
+
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+@RestControllerAdvice
+public class ExceptionControllerHandler {
+
+    @ExceptionHandler(RappHandlerException.class)
+    public ResponseEntity<ErrorResponse> handleRappHandlerException(RappHandlerException rappHandlerException) {
+        return ResponseEntity.status(rappHandlerException.getStatusCode())
+                       .body(new ErrorResponse(rappHandlerException.getMessage()));
+    }
+}
diff --git a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/RappHandlerException.java b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/exception/RappHandlerException.java
new file mode 100755 (executable)
index 0000000..565b719
--- /dev/null
@@ -0,0 +1,17 @@
+package com.oransc.rappmanager.models.exception;
+
+import lombok.Getter;
+import org.springframework.http.HttpStatusCode;
+
+@Getter
+public class RappHandlerException extends RuntimeException {
+
+    private final HttpStatusCode statusCode;
+
+    public RappHandlerException(HttpStatusCode statusCode, String message) {
+        super(message);
+        this.statusCode = statusCode;
+
+    }
+
+}