From 1d1c7b4153dd9c0be14c7188c63496ff71c3e1c2 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Mon, 9 Nov 2020 10:05:27 +0100 Subject: [PATCH] Remove Sonar code smell Change-Id: I484ccd60cd060d1989afae0156febe973e78fc02 Issue-ID: NONRTRIC-287 Signed-off-by: elinuxhenrik --- .../java/org/oransc/rappcatalogue/api/ServicesApiDelegateImpl.java | 5 +++-- .../java/org/oransc/rappcatalogue/exception/HeaderException.java | 5 +++-- .../api/GeneralRappCatalogueControllerAdvisorTest.java | 6 ++++-- .../org/oransc/rappcatalogue/api/ServicesApiDelegateImplTest.java | 7 ++++--- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImpl.java b/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImpl.java index 8c9469ac..bb3a6dc6 100644 --- a/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImpl.java +++ b/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImpl.java @@ -98,10 +98,11 @@ public class ServicesApiDelegateImpl implements ServicesApiDelegate { nativeResponse.addHeader(LOCATION_HEADER, requestURL.toString()); nativeResponse.getWriter().print(""); } else { - throw new HeaderException(LOCATION_HEADER, new Exception("Native Request or Response missing")); + throw new HeaderException(LOCATION_HEADER, serviceName, + new Exception("Native Request or Response missing")); } } catch (IOException e) { - throw new HeaderException(LOCATION_HEADER, e); + throw new HeaderException(LOCATION_HEADER, serviceName, e); } } diff --git a/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/exception/HeaderException.java b/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/exception/HeaderException.java index 90568de0..676ae1c1 100644 --- a/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/exception/HeaderException.java +++ b/r-app-catalogue/src/main/java/org/oransc/rappcatalogue/exception/HeaderException.java @@ -22,8 +22,9 @@ public class HeaderException extends RuntimeException { private static final long serialVersionUID = -7798178963078284655L; - public HeaderException(String header, Exception cause) { - super(String.format("Unable to set header %s in response. Cause: %s", header, cause.getMessage())); + public HeaderException(String header, String serviceName, Exception cause) { + super(String.format("Unable to set header %s in put response for service %s. Cause: %s", header, serviceName, + cause.getMessage())); } } diff --git a/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/GeneralRappCatalogueControllerAdvisorTest.java b/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/GeneralRappCatalogueControllerAdvisorTest.java index 1c4d4143..24afa09e 100644 --- a/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/GeneralRappCatalogueControllerAdvisorTest.java +++ b/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/GeneralRappCatalogueControllerAdvisorTest.java @@ -64,13 +64,15 @@ class GeneralRappCatalogueControllerAdvisorTest { void handleHeaderException_shouldReturnInternalServerErrorWithMessage() { GeneralRappCatalogueControllerAdvisor advisorUnderTest = new GeneralRappCatalogueControllerAdvisor(); - HeaderException exception = new HeaderException("Header", new Exception("Cause")); + String serviceName = "Service"; + HeaderException exception = new HeaderException("Header", serviceName, new Exception("Cause")); ResponseEntity response = advisorUnderTest.handleHeaderException(exception); assertThat(response.getStatusCode()).isEqualTo(INTERNAL_SERVER_ERROR); ErrorInformation body = (ErrorInformation) response.getBody(); assertThat(body.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR.value()); - assertThat(body.getDetail()).isEqualTo("Unable to set header Header in response. Cause: Cause"); + assertThat(body.getDetail()) + .isEqualTo("Unable to set header Header in put response for service " + serviceName + ". Cause: Cause"); } } diff --git a/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImplTest.java b/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImplTest.java index c19e1de2..f2f30ff6 100644 --- a/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImplTest.java +++ b/r-app-catalogue/src/test/java/org/oransc/rappcatalogue/api/ServicesApiDelegateImplTest.java @@ -155,7 +155,7 @@ class ServicesApiDelegateImplTest { } @Test - void putServiceWhenIoException_shouldThrowExceptionAndNoServiceCreated() throws Exception { + void putServiceWhenIoExceptionAddingHeader_shouldThrowExceptionAndNoServiceCreated() throws Exception { ServicesApiDelegateImpl delegateUnderTest = new ServicesApiDelegateImpl(webRequestMock); whenGetRequestUrlThenReturnUrl(); @@ -172,7 +172,8 @@ class ServicesApiDelegateImplTest { delegateUnderTest.putIndividualService(SERVICE_NAME, service); }); - assertThat(exception.getMessage()).isEqualTo("Unable to set header Location in response. Cause: Error"); + assertThat(exception.getMessage()) + .isEqualTo("Unable to set header Location in put response for service " + SERVICE_NAME + ". Cause: Error"); ResponseEntity> response = delegateUnderTest.getServices(); assertThat(response.getBody()).isEmpty(); @@ -206,7 +207,7 @@ class ServicesApiDelegateImplTest { assertThat(services).hasSize(2); List expectedServiceNames = Arrays.asList(serviceName1, serviceName2); assertThat(expectedServiceNames).contains(services.get(0).getName()) // - .contains(services.get(1).getName()); + .contains(services.get(1).getName()); } @Test -- 2.16.6