diff --git a/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java b/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java index 2730e644394f91d2bcb1557fb815d18596103fbf..ca18568fcfc31acb919cfba0fc29b339b6e87acf 100644 --- a/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java +++ b/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java @@ -145,6 +145,6 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler { this.logger.warning(exceptionMsg, e); } - return new ResponseEntity<Object>(gson.toJson(exceptionMsg),HttpStatus.resolve(e.getError().getCode())); + return new ResponseEntity<Object>(e.getError(),HttpStatus.resolve(e.getError().getCode())); } } \ No newline at end of file diff --git a/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java b/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java index 2a74171a4d1164ceff02a0189feb7166e7c1bf41..a53c8009d56a9dfd608aad2354aa1765a6808caf 100644 --- a/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java +++ b/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java @@ -5,6 +5,7 @@ import static org.powermock.api.mockito.PowerMockito.mock; import javassist.NotFoundException; +import org.opengroup.osdu.core.common.model.http.AppError; import org.opengroup.osdu.core.common.model.http.AppException; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.springframework.http.HttpHeaders; @@ -34,7 +35,8 @@ public class GlobalExceptionMapperTests { ResponseEntity<Object> response = sut.handleAppException(exception); assertEquals(409, response.getStatusCodeValue()); //assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType().toString()); - assertEquals("\"any message\"", response.getBody()); + AppError expectedError = new AppError(409, "any reason", "any message"); + assertEquals(expectedError, response.getBody()); } @Test public void should_addLocationHeader_when_fromAppException() { @@ -55,6 +57,7 @@ public class GlobalExceptionMapperTests { //assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType().toString()); //assertEquals("any message", response.getBody()); } + @Test public void should_use404ValueInResponse_When_NotFoundExceptionIsHandledByGlobalExceptionMapper() { @@ -64,6 +67,7 @@ public class GlobalExceptionMapperTests { //assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType().toString()); //assertEquals(null, response.getBody().getMessage()); } + @Test public void should_use405ValueInResponse_When_HttpRequestMethodNotSupportedExceptionIsHandledByGlobalExceptionMapper() { HttpHeaders httpHeaders = mock(HttpHeaders.class); @@ -72,6 +76,7 @@ public class GlobalExceptionMapperTests { ResponseEntity<Object> response = sut.handleHttpRequestMethodNotSupported(exception, httpHeaders, HttpStatus.METHOD_NOT_ALLOWED, webRequest); assertEquals(405, response.getStatusCodeValue()); } + @Test public void should_useGenericValuesInResponse_When_ExceptionIsHandledByGlobalExceptionMapper() { diff --git a/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/CreateLegalTagApiAcceptanceTests.java b/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/CreateLegalTagApiAcceptanceTests.java index 4b7acdbcfc8bed7ec9f73577aa5793680942a7ee..b35fa7214b7249fc392a531b02d48fca3bceb30e 100644 --- a/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/CreateLegalTagApiAcceptanceTests.java +++ b/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/CreateLegalTagApiAcceptanceTests.java @@ -42,14 +42,15 @@ public abstract class CreateLegalTagApiAcceptanceTests extends AcceptanceBaseTes COO = "SD"; ClientResponse response = validateAccess(400); String messageResponse = legalTagUtils.getResult(response, 400, String.class); - assertEquals("\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [SD].\\\"]}\"" , messageResponse); } + assertEquals("{\"code\":400,\"reason\":\"Validation error.\",\"message\":\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [SD].\\\"]}\"}" , messageResponse); + } @Test public void should_notAllowCreationOfLegalTag_when_countryOfOriginIsSetToARestrictedCountry() throws Exception { COO = "BV"; ClientResponse response = validateAccess(400); String messageResponse = legalTagUtils.getResult(response, 400, String.class); - assertEquals("\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [BV].\\\"]}\"", messageResponse); + assertEquals("{\"code\":400,\"reason\":\"Validation error.\",\"message\":\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [BV].\\\"]}\"}", messageResponse); } @Test @@ -89,14 +90,14 @@ public abstract class CreateLegalTagApiAcceptanceTests extends AcceptanceBaseTes response = legalTagUtils.create("US", name); String error = legalTagUtils.getResult(response, 409, String.class); - assertTrue(error.startsWith("\"A LegalTag already exists for the given name")); + assertTrue(error.contains("\"message\":\"A LegalTag already exists for the given name")); } @Test public void should_return400_CreationOfALegalTag_when_countryOfOriginIsSetToDefaultResidencyRiskCountry_andThen_None_ofThe_Datatype_is_exluded_from_Dataresidency() throws Exception { ClientResponse response = legalTagUtils.create("MX", name); String messageResponse = legalTagUtils.getResult(response, 400, String.class); - assertEquals("\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [MX].\\\"]}\"", messageResponse); + assertEquals("{\"code\":400,\"reason\":\"Validation error.\",\"message\":\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [MX].\\\"]}\"}", messageResponse); } @Test @@ -158,7 +159,7 @@ public abstract class CreateLegalTagApiAcceptanceTests extends AcceptanceBaseTes public void should_return400_CreationOfALegalTag_when_countryOfOriginIsSetToEmbargoCountry_andThen_input_Datatype_is_exluded_from_Dataresidency() throws Exception { ClientResponse response = legalTagUtils.create("IR", name, "Transferred Data"); String messageResponse = legalTagUtils.getResult(response, 400, String.class); - assertEquals("\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [IR].\\\"]}\"", messageResponse); + assertEquals("{\"code\":400,\"reason\":\"Validation error.\",\"message\":\"{\\\"errors\\\":[\\\"Invalid country of origin set. It should match one of the ISO alpha 2 codes and be a country with no restriction on data residency. Found: [IR].\\\"]}\"}", messageResponse); } @Test public void should_onlyLetAMaximumOf1LegaltagBeCreated_when_tryingToCreateMultipleVersionsOfTheSameContractAtTheSameTime() throws Exception { diff --git a/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/UpdateLegalTagApiAcceptanceTests.java b/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/UpdateLegalTagApiAcceptanceTests.java index a70dd8a1b1381eb3a93b61eeec3b71aced1ce3d6..a8897627d1ce467bee36feff7172739a65ffc7e9 100644 --- a/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/UpdateLegalTagApiAcceptanceTests.java +++ b/testing/legal-test-core/src/main/java/org/opengroup/osdu/legal/acceptanceTests/UpdateLegalTagApiAcceptanceTests.java @@ -48,7 +48,7 @@ public abstract class UpdateLegalTagApiAcceptanceTests extends AcceptanceBaseTes expDate = "2010-12-31"; //set expired date ClientResponse response = legalTagUtils.send(this.getApi(), this.getHttpMethod(), legalTagUtils.accessToken(), getBody(), getQuery()); String error = legalTagUtils.getResult(response, 400, String.class); - assertEquals("\"{\\\"errors\\\":[\\\"Expiration date must be a value in the future. Given 2010-12-31\\\"]}\"", error); + assertEquals("{\"code\":400,\"reason\":\"Validation error.\",\"message\":\"{\\\"errors\\\":[\\\"Expiration date must be a value in the future. Given 2010-12-31\\\"]}\"}", error); } @Test