use of org.finra.herd.model.api.xml.ErrorInformation in project herd by FINRAOS.
the class DataBridgeWebClientTest method testGetBusinessObjectDataStorageFilesCreateResponse400Throws.
@Test
public void testGetBusinessObjectDataStorageFilesCreateResponse400Throws() throws Exception {
int expectedStatusCode = 400;
String expectedReasonPhrase = "testReasonPhrase";
String expectedErrorMessage = "testErrorMessage";
ErrorInformation errorInformation = new ErrorInformation();
errorInformation.setStatusCode(expectedStatusCode);
errorInformation.setMessage(expectedErrorMessage);
errorInformation.setStatusDescription(expectedReasonPhrase);
String requestContent = xmlHelper.objectToXml(errorInformation);
CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, expectedStatusCode, expectedReasonPhrase), false);
httpResponse.setEntity(new StringEntity(requestContent));
try {
dataBridgeWebClient.getBusinessObjectDataStorageFilesCreateResponse(httpResponse);
Assert.fail("expected HttpErrorResponseException, but no exception was thrown");
} catch (Exception e) {
assertEquals("thrown exception type", HttpErrorResponseException.class, e.getClass());
HttpErrorResponseException httpErrorResponseException = (HttpErrorResponseException) e;
assertEquals("httpErrorResponseException responseMessage", expectedErrorMessage, httpErrorResponseException.getResponseMessage());
assertEquals("httpErrorResponseException statusCode", expectedStatusCode, httpErrorResponseException.getStatusCode());
assertEquals("httpErrorResponseException statusDescription", expectedReasonPhrase, httpErrorResponseException.getStatusDescription());
assertEquals("httpErrorResponseException message", "Failed to add storage files", httpErrorResponseException.getMessage());
}
}
use of org.finra.herd.model.api.xml.ErrorInformation in project herd by FINRAOS.
the class DataBridgeWebClient method processXmlHttpResponse.
/**
* Extracts an instance of the specified object class from the registration server response.
*
* @param response the HTTP response received from the registration server.
* @param actionDescription the description of the action being performed with the registration server (to be used in an error message).
* @param responseClass the class of the object expected to be returned by the registration server.
*
* @return the BusinessObjectData object extracted from the registration server response.
*/
protected Object processXmlHttpResponse(CloseableHttpResponse response, String actionDescription, Class<?>... responseClass) {
StatusLine responseStatusLine = response.getStatusLine();
Object responseObject = null;
String xmlResponse = "";
HttpErrorResponseException errorException = null;
try {
if (responseStatusLine.getStatusCode() == 200) {
// Request is successfully handled by the Server.
xmlResponse = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8.name());
InputStream inputStream = new ByteArrayInputStream(xmlResponse.getBytes(StandardCharsets.UTF_8));
// Un-marshall the response to the specified object class.
JAXBContext responseContext = JAXBContext.newInstance(responseClass);
Unmarshaller responseUnmarshaller = responseContext.createUnmarshaller();
responseObject = responseUnmarshaller.unmarshal(inputStream);
} else {
// Handle erroneous HTTP response.
xmlResponse = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8.name());
InputStream inputStream = new ByteArrayInputStream(xmlResponse.getBytes(StandardCharsets.UTF_8));
// Un-marshall response to the ErrorInformation object.
JAXBContext responseContext = JAXBContext.newInstance(ErrorInformation.class);
Unmarshaller responseUnmarshaller = responseContext.createUnmarshaller();
ErrorInformation errorInfo = (ErrorInformation) responseUnmarshaller.unmarshal(inputStream);
errorException = new HttpErrorResponseException("Failed to " + actionDescription, errorInfo.getStatusCode(), errorInfo.getStatusDescription(), errorInfo.getMessage());
}
} catch (IOException | JAXBException e) {
LOGGER.warn("Failed to get or process HTTP response from the registration server.", e);
LOGGER.warn(String.format(" HTTP Response Status: %s", responseStatusLine));
LOGGER.warn(String.format(" HTTP Response: %s", xmlResponse));
errorException = new HttpErrorResponseException("Failed to " + actionDescription, responseStatusLine.getStatusCode(), responseStatusLine.getReasonPhrase(), xmlResponse);
} finally {
try {
response.close();
} catch (Exception ex) {
LOGGER.warn("Unable to close HTTP response.", ex);
}
}
// If we populated a response exception, then throw it to the caller.
if (errorException != null) {
throw errorException;
}
// Return the response.
return responseObject;
}
use of org.finra.herd.model.api.xml.ErrorInformation in project herd by FINRAOS.
the class HerdErrorInformationExceptionHandlerTest method validatePersistenceException.
/**
* Validates that calling handlePersistenceException with a SQL exception with the given SQL state code and error code wrapped in a
* {@link PersistenceException} returns an {@link ErrorInformation} with a {@link HttpStatus#BAD_REQUEST}.
*
* @param sqlState - {@link SQLException#getSQLState()}
* @param errorCode - {@link SQLException#getErrorCode()}
*/
private void validatePersistenceException(String sqlState, int errorCode) {
SQLException sqlException = new SQLException("Test Reason", sqlState, errorCode, null);
PersistenceException persistenceException = getPersistenceException(sqlException);
ErrorInformation errorInformation = exceptionHandler.handlePersistenceException(persistenceException, new MockHttpServletResponse());
validateErrorInformation(errorInformation, HttpStatus.BAD_REQUEST, false);
}
use of org.finra.herd.model.api.xml.ErrorInformation in project herd by FINRAOS.
the class HerdErrorInformationExceptionHandlerTest method testGetErrorWithCause.
@Test
public void testGetErrorWithCause() throws Exception {
Exception ex = new Exception();
ErrorInformation errorInformation = exceptionHandler.handleBadRequestException(ex);
assertTrue(errorInformation.getMessageDetails().size() == 0);
ex = new Exception(new Exception("cause_1_exception", new Exception("cause_2_exception")));
errorInformation = exceptionHandler.handleBadRequestException(ex);
assertTrue(errorInformation.getMessageDetails().size() == 2);
assertEquals("cause_1_exception", errorInformation.getMessageDetails().get(0));
assertEquals("cause_2_exception", errorInformation.getMessageDetails().get(1));
}
use of org.finra.herd.model.api.xml.ErrorInformation in project herd by FINRAOS.
the class HerdErrorInformationExceptionHandlerTest method testHandleInternalServerErrorNoErrorMessage.
@Test
public void testHandleInternalServerErrorNoErrorMessage() throws Exception {
// Get the error information for an exception that has no message. In this case, the message is the exception class name.
ErrorInformation errorInformation = exceptionHandler.handleInternalServerErrorException(new NullPointerException());
// Validate the error status information, but not the message.
validateErrorInformation(errorInformation, HttpStatus.INTERNAL_SERVER_ERROR, false);
// Validate that the error message is the class name.
assertEquals(NullPointerException.class.getName(), errorInformation.getMessage());
}
Aggregations