From b75fe3c7ce231c86cd4c6d052da453d02809c8f9 Mon Sep 17 00:00:00 2001 From: vasraz Date: Fri, 14 Oct 2022 11:44:03 +0100 Subject: [PATCH] Move DefaultExceptionMapper to be used widely Signed-off-by: Vasyl Razinkov Change-Id: I759b023cc37fa8c5eed8b01210bed4e91edb57cb Issue-ID: SDC-4189 --- .../src/main/webapp/WEB-INF/beans-services.xml | 2 +- .../sdc/common}/errors/DefaultExceptionMapper.java | 9 +--- .../errorCodesToResponseStatusMapping.json | 0 .../common}/errors/DefaultExceptionMapperTest.java | 52 +++++++++++----------- 4 files changed, 29 insertions(+), 34 deletions(-) rename openecomp-be/{api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests => lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common}/errors/DefaultExceptionMapper.java (92%) rename openecomp-be/{api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest => lib/openecomp-common-lib}/src/main/resources/errorCodesToResponseStatusMapping.json (100%) rename openecomp-be/{api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests => lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common}/errors/DefaultExceptionMapperTest.java (68%) diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml b/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml index 8c005f0169..9c2aa51a28 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/onboarding-rest-war/src/main/webapp/WEB-INF/beans-services.xml @@ -90,7 +90,7 @@ - + diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/DefaultExceptionMapper.java similarity index 92% rename from openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java rename to openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/DefaultExceptionMapper.java index 22d6d90d8a..a059434709 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/DefaultExceptionMapper.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/DefaultExceptionMapper.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.openecomp.sdcrests.errors; +package org.openecomp.sdc.common.errors; import com.fasterxml.jackson.databind.JsonMappingException; import java.util.ArrayList; @@ -31,13 +31,6 @@ import org.apache.commons.collections4.CollectionUtils; import org.hibernate.validator.internal.engine.path.PathImpl; import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.core.utilities.json.JsonUtil; -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.common.errors.ErrorCategory; -import org.openecomp.sdc.common.errors.ErrorCode; -import org.openecomp.sdc.common.errors.ErrorCodeAndMessage; -import org.openecomp.sdc.common.errors.GeneralErrorBuilder; -import org.openecomp.sdc.common.errors.JsonMappingErrorBuilder; -import org.openecomp.sdc.common.errors.ValidationErrorBuilder; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json b/openecomp-be/lib/openecomp-common-lib/src/main/resources/errorCodesToResponseStatusMapping.json similarity index 100% rename from openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json rename to openecomp-be/lib/openecomp-common-lib/src/main/resources/errorCodesToResponseStatusMapping.json diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java b/openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/errors/DefaultExceptionMapperTest.java similarity index 68% rename from openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java rename to openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/errors/DefaultExceptionMapperTest.java index 680c3409ad..45c511706d 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java +++ b/openecomp-be/lib/openecomp-common-lib/src/test/java/org/openecomp/sdc/common/errors/DefaultExceptionMapperTest.java @@ -17,69 +17,71 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.openecomp.sdcrests.errors; -import static org.junit.Assert.assertEquals; +package org.openecomp.sdc.common.errors; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.fasterxml.jackson.databind.JsonMappingException; import java.util.HashSet; import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import javax.ws.rs.core.Response; -import com.fasterxml.jackson.databind.JsonMappingException; import org.hibernate.validator.internal.engine.path.PathImpl; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.openecomp.sdc.common.errors.CoreException; -import org.openecomp.sdc.common.errors.ErrorCategory; -import org.openecomp.sdc.common.errors.ErrorCode; +import org.mockito.junit.jupiter.MockitoExtension; import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder; -@RunWith(MockitoJUnitRunner.class) -public class DefaultExceptionMapperTest { +@ExtendWith(MockitoExtension.class) +class DefaultExceptionMapperTest { private static final String TEST_MESSAGE = "Test message"; @Mock private ConstraintViolation constraintViolation; - private PathImpl path = PathImpl.createRootPath(); + private final PathImpl path = PathImpl.createRootPath(); @Test - public void shouldMapCoreExceptionToResponse() { + void shouldMapCoreExceptionToResponse() { DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper(); ErrorCode errorCode = new ErrorCodeBuilder().withId("VSP_NOT_FOUND").withCategory(ErrorCategory.APPLICATION).build(); CoreException exception = new CoreException(errorCode); - Response response = defaultExceptionMapper.toResponse(exception); - assertEquals(response.getStatus(), 404); + try (final Response response = defaultExceptionMapper.toResponse(exception)) { + assertEquals(404, response.getStatus()); + } } @Test - public void shouldMapConstraintViolationExceptionToResponse() { + void shouldMapConstraintViolationExceptionToResponse() { Mockito.when(constraintViolation.getPropertyPath()).thenReturn(path); DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper(); Set> violations = new HashSet<>(); violations.add(constraintViolation); ConstraintViolationException exception = new ConstraintViolationException(TEST_MESSAGE, violations); - Response response = defaultExceptionMapper.toResponse(exception); - assertEquals(response.getStatus(), 417); + try (final Response response = defaultExceptionMapper.toResponse(exception)) { + assertEquals(417, response.getStatus()); + } } @Test - public void shouldMapJsonMappingExceptionToResponse() { + void shouldMapJsonMappingExceptionToResponse() { DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper(); JsonMappingException exception = new JsonMappingException(TEST_MESSAGE); - Response response = defaultExceptionMapper.toResponse(exception); - assertEquals(response.getStatus(), 417); + try (final Response response = defaultExceptionMapper.toResponse(exception)) { + assertEquals(417, response.getStatus()); + } } @Test - public void shouldMapOtherExceptionToResponse() { + void shouldMapOtherExceptionToResponse() { DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper(); Exception exception = new Exception(TEST_MESSAGE); - Response response = defaultExceptionMapper.toResponse(exception); - assertEquals(response.getStatus(), 500); + try (final Response response = defaultExceptionMapper.toResponse(exception)) { + assertEquals(500, response.getStatus()); + } } -} \ No newline at end of file +} -- 2.16.6