Fixed sonar issues in policy-pap 34/133434/3
authorSuresh Charan <suresh.charan@bell.ca>
Mon, 27 Feb 2023 18:14:07 +0000 (13:14 -0500)
committerSuresh Charan <suresh.charan@bell.ca>
Tue, 28 Feb 2023 01:05:49 +0000 (20:05 -0500)
Reduced technical debt by fixing sonar issues

Issue-ID: POLICY-4536
Change-Id: I3f4ff8cae7d22c5d2d062e98475592425e3c98c4
Signed-off-by: Suresh Charan <suresh.charan@bell.ca>
main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java
main/src/main/java/org/onap/policy/pap/main/rest/stub/StubUtils.java
main/src/test/java/org/onap/policy/pap/main/service/PolicyStatusServiceTest.java

index 4d14211..dc4a030 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2021,2023 Nordix Foundation.
  *  Modifications Copyright (C) 2019-2021 AT&T Intellectual Property.
- *  Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2021-2023 Bell Canada. 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.
@@ -159,10 +159,8 @@ public class PdpStatusMessageHandler extends PdpMessageGenerator {
                 return true;
             }
 
-            if (exceptionClazz.isInstance(thrown)) {
-                if (isDuplicateKeyException(thrown.getCause(), exceptionClazz)) {
-                    return true;
-                }
+            if (exceptionClazz.isInstance(thrown) && isDuplicateKeyException(thrown.getCause(), exceptionClazz)) {
+                return true;
             }
 
             thrown = thrown.getCause();
index 11917e4..f63fc1d 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019-2023 Nordix Foundation.
  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
- *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
+ *  Modifications Copyright (C) 2021, 2023 Bell Canada. 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.
@@ -45,7 +45,7 @@ import org.springframework.web.bind.annotation.RestController;
 @Profile("default")
 public class PdpGroupStateChangeControllerV1 extends PapRestControllerV1 implements PdpGroupStateChangeControllerV1Api {
 
-    private static final Logger logger = LoggerFactory.getLogger(PdpGroupHealthCheckControllerV1.class);
+    private static final Logger logger = LoggerFactory.getLogger(PdpGroupStateChangeControllerV1.class);
     private final PdpGroupStateChangeProvider provider;
 
     /**
index d2ab15f..1f4f10f 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2023 Nordix Foundation.
+ *  Modifications Copyright (C) 2023 Bell Canada.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -44,6 +45,9 @@ import org.springframework.stereotype.Service;
 @Profile("stub")
 class StubUtils {
     private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
+    private static final String APPLICATION_JSON = "application/json";
+    private static final String SERIALIZE_RESPONSE_FAILURE_MSG =
+            "Couldn't serialize response for content type application/json";
     private final HttpServletRequest request;
     private static final String ACCEPT = "Accept";
     private static final String PAP_DB =
@@ -52,14 +56,14 @@ class StubUtils {
 
     <T> ResponseEntity<T> getStubbedResponse(Class<T> clazz) {
         var accept = request.getHeader(ACCEPT);
-        if (accept != null && accept.contains("application/json")) {
+        if (accept != null && accept.contains(APPLICATION_JSON)) {
             final var resource = new ClassPathResource(PAP_DB);
             try (var inputStream = resource.getInputStream()) {
                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
                 var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
                 return new ResponseEntity<>(targetObject, HttpStatus.OK);
             } catch (IOException e) {
-                log.error("Couldn't serialize response for content type application/json", e);
+                log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
             }
         }
@@ -68,14 +72,14 @@ class StubUtils {
 
     <T> ResponseEntity<List<T>> getStubbedResponseList(Class<T> clazz) {
         var accept = request.getHeader(ACCEPT);
-        if (accept != null && accept.contains("application/json")) {
+        if (accept != null && accept.contains(APPLICATION_JSON)) {
             final var resource = new ClassPathResource(PAP_DB);
             try (var inputStream = resource.getInputStream()) {
                 final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
                 var targetObject = Arrays.asList(JSON_TRANSLATOR.fromJson(string, clazz));
                 return new ResponseEntity<>(targetObject, HttpStatus.OK);
             } catch (IOException e) {
-                log.error("Couldn't serialize response for content type application/json", e);
+                log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
             }
         }
@@ -84,7 +88,7 @@ class StubUtils {
 
     ResponseEntity<Map<String, Object>> getStubbedResponseMap() {
         var accept = request.getHeader(ACCEPT);
-        if (accept != null && accept.contains("application/json")) {
+        if (accept != null && accept.contains(APPLICATION_JSON)) {
             final var resource = new ClassPathResource(PAP_DB);
             try (var inputStream = resource.getInputStream()) {
                 Map<String, Object> map = new HashMap<>();
@@ -93,7 +97,7 @@ class StubUtils {
                         JSON_TRANSLATOR.fromJson(string, Object.class));
                 return new ResponseEntity<>(map, HttpStatus.OK);
             } catch (IOException e) {
-                log.error("Couldn't serialize response for content type application/json", e);
+                log.error(SERIALIZE_RESPONSE_FAILURE_MSG, e);
                 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
             }
         }
@@ -102,7 +106,7 @@ class StubUtils {
 
     ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> getStubbedResponseStatistics() {
         var accept = request.getHeader(ACCEPT);
-        if (accept != null && accept.contains("application/json")) {
+        if (accept != null && accept.contains(APPLICATION_JSON)) {
             Map<String, Map<String, List<PdpStatistics>>> map = new HashMap<>();
             return new ResponseEntity<>(map, HttpStatus.OK);
         }
index aeb2fdb..6166580 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2022 Bell Canada. All rights reserved.
+ *  Copyright (C) 2022-2023 Bell Canada. All rights reserved.
  *  Modifications Copyright (C) 2022 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -141,7 +141,7 @@ public class PolicyStatusServiceTest extends CommonPapRestServer {
 
         // Test delete
         policyStatusService.cudPolicyStatus(null, null, List.of(status));
-        assertThat(policyStatusService.getAllPolicyStatus()).hasSize(0);
+        assertThat(policyStatusService.getAllPolicyStatus()).isEmpty();
     }
 
     private List<PdpPolicyStatus> createStatusList() {