Search in sources :

Example 1 with ErrorInformation

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());
    }
}
Also used : ErrorInformation(org.finra.herd.model.api.xml.ErrorInformation) StringEntity(org.apache.http.entity.StringEntity) MockCloseableHttpResponse(org.finra.herd.dao.impl.MockCloseableHttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MockCloseableHttpResponse(org.finra.herd.dao.impl.MockCloseableHttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 2 with ErrorInformation

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) StatusLine(org.apache.http.StatusLine) ErrorInformation(org.finra.herd.model.api.xml.ErrorInformation) ByteArrayInputStream(java.io.ByteArrayInputStream) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 3 with ErrorInformation

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);
}
Also used : ErrorInformation(org.finra.herd.model.api.xml.ErrorInformation) SQLException(java.sql.SQLException) PersistenceException(javax.persistence.PersistenceException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 4 with ErrorInformation

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));
}
Also used : ErrorInformation(org.finra.herd.model.api.xml.ErrorInformation) ActivitiException(org.activiti.engine.ActivitiException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MethodNotAllowedException(org.finra.herd.model.MethodNotAllowedException) AlreadyExistsException(org.finra.herd.model.AlreadyExistsException) SQLException(java.sql.SQLException) PersistenceException(javax.persistence.PersistenceException) ActivitiClassLoadingException(org.activiti.engine.ActivitiClassLoadingException) ELException(org.activiti.engine.impl.javax.el.ELException) Test(org.junit.Test) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest)

Example 5 with ErrorInformation

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());
}
Also used : ErrorInformation(org.finra.herd.model.api.xml.ErrorInformation) Test(org.junit.Test) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest)

Aggregations

ErrorInformation (org.finra.herd.model.api.xml.ErrorInformation)7 SQLException (java.sql.SQLException)3 PersistenceException (javax.persistence.PersistenceException)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 JAXBException (javax.xml.bind.JAXBException)2 ActivitiClassLoadingException (org.activiti.engine.ActivitiClassLoadingException)2 ActivitiException (org.activiti.engine.ActivitiException)2 ELException (org.activiti.engine.impl.javax.el.ELException)2 AlreadyExistsException (org.finra.herd.model.AlreadyExistsException)2 MethodNotAllowedException (org.finra.herd.model.MethodNotAllowedException)2 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)2 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1