Adding controller exception handling 33/133033/1
authorwaqas.ikram <waqas.ikram@est.tech>
Thu, 26 Jan 2023 11:29:08 +0000 (11:29 +0000)
committerwaqas.ikram <waqas.ikram@est.tech>
Thu, 26 Jan 2023 11:37:56 +0000 (11:37 +0000)
and fixing code smells

Change-Id: Icd5c64b910eeae0a063466f3f996e0292d02a2f9
Issue-ID: SO-4068
Signed-off-by: waqas.ikram <waqas.ikram@est.tech>
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-application/src/main/java/org/onap/so/cnfm/lcm/app/DefaultToShortClassNameBeanNameGenerator.java
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesController.java
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmControllerExceptionHandler.java [new file with mode: 0644]
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmOpOccControllerExceptionHandler.java [new file with mode: 0644]
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmOpOccStatusNotFoundException.java [new file with mode: 0644]
so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesControllerTest.java

index 2683b03..b260fc3 100644 (file)
@@ -34,8 +34,9 @@ public class DefaultToShortClassNameBeanNameGenerator extends AnnotationBeanName
 
     @Override
     protected String buildDefaultBeanName(final BeanDefinition definition) {
-        if (definition.getBeanClassName() != null) {
-            return ClassUtils.getShortName(definition.getBeanClassName());
+        final String beanClassName = definition.getBeanClassName();
+        if (beanClassName != null) {
+            return ClassUtils.getShortName(beanClassName);
         }
         logger.warn("Bean class name is not specified...");
         return null;
index 8c25d20..257f5fc 100644 (file)
@@ -25,10 +25,9 @@ import java.util.Optional;
 import javax.ws.rs.core.MediaType;
 import org.onap.so.cnfm.lcm.lifecycle.AsLcmOperationOccurrenceManager;
 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
-import org.onap.so.cnfm.lcm.model.ErrorDetails;
+import org.onap.so.cnfm.lcm.rest.exceptions.AsLcmOpOccStatusNotFoundException;
 import org.slf4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -53,7 +52,7 @@ public class AsLcmOperationOccurrencesController {
 
     @GetMapping(value = "/as_lcm_op_occs/{asLcmOpOccId}",
             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
-    public ResponseEntity<?> getOperationStatus(@PathVariable("asLcmOpOccId") final String asLcmOpOccId) {
+    public ResponseEntity<AsLcmOpOcc> getOperationStatus(@PathVariable("asLcmOpOccId") final String asLcmOpOccId) {
         logger.info("Received request to retrieve operation status for asLcmOpOccId: {}", asLcmOpOccId);
         final Optional<AsLcmOpOcc> optionalAsLcmOpOccs =
                 asLcmOperationOccurrenceManager.getAsLcmOperationOccurrence(asLcmOpOccId);
@@ -66,6 +65,6 @@ public class AsLcmOperationOccurrencesController {
 
         final String errorMessage = "Unable to retrieve operation occurrence status for asLcmOpOccId: " + asLcmOpOccId;
         logger.error(errorMessage);
-        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorDetails().detail(errorMessage));
+        throw new AsLcmOpOccStatusNotFoundException(errorMessage);
     }
 }
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmControllerExceptionHandler.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmControllerExceptionHandler.java
new file mode 100644 (file)
index 0000000..4de5118
--- /dev/null
@@ -0,0 +1,58 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.so.cnfm.lcm.rest.exceptions;
+
+import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.AsRequestProcessingException;
+import org.onap.so.cnfm.lcm.model.ErrorDetails;
+import org.onap.so.cnfm.lcm.rest.AsLifecycleManagementController;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+
+/**
+ *
+ * @author Waqas Ikram (waqas.ikram@est.tech)
+ *
+ */
+@ControllerAdvice(assignableTypes = AsLifecycleManagementController.class)
+public class AsLcmControllerExceptionHandler {
+
+    @ExceptionHandler(AsRequestProcessingException.class)
+    public ResponseEntity<ErrorDetails> handleAsRequestProcessingException(
+            final AsRequestProcessingException asRequestProcessingException) {
+        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
+                .body(getErrorDetails(asRequestProcessingException));
+    }
+
+    @ExceptionHandler(Exception.class)
+    public ResponseEntity<ErrorDetails> handleAsRequestProcessingException(final Exception exception) {
+        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
+                new ErrorDetails().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).detail(exception.getMessage()));
+    }
+
+    private ErrorDetails getErrorDetails(AsRequestProcessingException asRequestProcessingException) {
+        if (asRequestProcessingException.getErrorDetails() != null) {
+            return asRequestProcessingException.getErrorDetails();
+        }
+        return new ErrorDetails().status(HttpStatus.INTERNAL_SERVER_ERROR.value())
+                .detail(asRequestProcessingException.getMessage());
+    }
+}
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmOpOccControllerExceptionHandler.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmOpOccControllerExceptionHandler.java
new file mode 100644 (file)
index 0000000..022407d
--- /dev/null
@@ -0,0 +1,50 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.so.cnfm.lcm.rest.exceptions;
+
+import org.onap.so.cnfm.lcm.model.ErrorDetails;
+import org.onap.so.cnfm.lcm.rest.AsLcmOperationOccurrencesController;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+
+/**
+ *
+ * @author Waqas Ikram (waqas.ikram@est.tech)
+ *
+ */
+@ControllerAdvice(assignableTypes = AsLcmOperationOccurrencesController.class)
+public class AsLcmOpOccControllerExceptionHandler {
+
+    @ExceptionHandler(AsLcmOpOccStatusNotFoundException.class)
+    public ResponseEntity<ErrorDetails> handleAsLcmOpOccStatusNotFoundException(
+            final AsLcmOpOccStatusNotFoundException exception) {
+        return ResponseEntity.status(HttpStatus.NOT_FOUND)
+                .body(new ErrorDetails().status(HttpStatus.NOT_FOUND.value()).detail(exception.getMessage()));
+    }
+
+    @ExceptionHandler(Exception.class)
+    public ResponseEntity<ErrorDetails> handleAsLcmOpOccException(final Exception exception) {
+        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
+                new ErrorDetails().status(HttpStatus.INTERNAL_SERVER_ERROR.value()).detail(exception.getMessage()));
+    }
+
+}
diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmOpOccStatusNotFoundException.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/main/java/org/onap/so/cnfm/lcm/rest/exceptions/AsLcmOpOccStatusNotFoundException.java
new file mode 100644 (file)
index 0000000..90846ab
--- /dev/null
@@ -0,0 +1,44 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2023 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.so.cnfm.lcm.rest.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+/**
+ *
+ * @author Waqas Ikram (waqas.ikram@est.tech)
+ *
+ */
+@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
+public class AsLcmOpOccStatusNotFoundException extends RuntimeException {
+
+    private static final long serialVersionUID = 1706676614379799737L;
+
+    public AsLcmOpOccStatusNotFoundException(String message) {
+        super(message);
+    }
+
+    @Override
+    public synchronized Throwable fillInStackTrace() {
+        return this;
+    }
+
+}
index c3dd4d0..4f1a0b6 100644 (file)
@@ -36,6 +36,7 @@ import org.onap.so.cnfm.lcm.database.beans.OperationStateEnum;
 import org.onap.so.cnfm.lcm.database.beans.State;
 import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider;
 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
+import org.onap.so.cnfm.lcm.model.ErrorDetails;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.boot.test.web.client.TestRestTemplate;
@@ -61,6 +62,8 @@ import com.google.gson.Gson;
 @ActiveProfiles("test")
 public class AsLcmOperationOccurrencesControllerTest {
 
+    private static final String RANDOM_UUID = UUID.randomUUID().toString();
+
     private static final String AS_LCM_OP_OCCS = "/as_lcm_op_occs/";
 
     @LocalServerPort
@@ -93,18 +96,26 @@ public class AsLcmOperationOccurrencesControllerTest {
         assertNotNull(responseEntity.getBody());
     }
 
+    @Test
+    public void testGetOperationStatus_invalidAsLcmOpOccId_returnsErrorDetails() {
+        final String baseUrl = getAsLcmBaseUrl() + AS_LCM_OP_OCCS + "123";
+        final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders());
+        final ResponseEntity<ErrorDetails> responseEntity =
+                testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, ErrorDetails.class);
+        assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
+        assertTrue(responseEntity.hasBody());
+        assertNotNull(responseEntity.getBody());
+    }
 
     private String getAsLcmBaseUrl() {
         return "http://localhost:" + port + Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL;
     }
 
-
     private String addDummyAsLcmOpOccToDatabase() {
         final LocalDateTime now = LocalDateTime.now();
-        final AsInst asInst = new AsInst().name("name").asdId(UUID.randomUUID().toString())
-                .status(State.NOT_INSTANTIATED).asdInvariantId(UUID.randomUUID().toString()).statusUpdatedTime(now)
-                .asApplicationName("asApplicationName").asApplicationVersion("asApplicationVersion")
-                .asProvider("asProvider").serviceInstanceId(UUID.randomUUID().toString())
+        final AsInst asInst = new AsInst().name("name").asdId(RANDOM_UUID).status(State.NOT_INSTANTIATED)
+                .asdInvariantId(RANDOM_UUID).statusUpdatedTime(now).asApplicationName("asApplicationName")
+                .asApplicationVersion("asApplicationVersion").asProvider("asProvider").serviceInstanceId(RANDOM_UUID)
                 .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion")
                 .tenantId("tenantId");