Search in sources :

Example 6 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class ClientStubBaseTest method check404AndNoBodyGivesNotFoundException.

@Test
public void check404AndNoBodyGivesNotFoundException() throws IOException {
    HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
    Mockito.when(response.body()).thenReturn("");
    Mockito.when(response.code()).thenReturn(404);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof ResourceNotFoundException);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) Test(org.junit.Test)

Example 7 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class ClientStubBaseTest method check500AndNoBodyGivesInternalServerErrorException.

@Test
public void check500AndNoBodyGivesInternalServerErrorException() throws IOException {
    HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
    Mockito.when(response.body()).thenReturn("");
    Mockito.when(response.code()).thenReturn(500);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof InternalServerErrorException);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) Test(org.junit.Test)

Example 8 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class ClientStubBaseTest method checkBodyWithNoErrorsAnd500Status.

@Test
public void checkBodyWithNoErrorsAnd500Status() throws IOException {
    Document document = new Document();
    document.setErrors(new ArrayList<ErrorData>());
    String body = client.getObjectMapper().writeValueAsString(document);
    HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
    Mockito.when(response.body()).thenReturn(body);
    Mockito.when(response.code()).thenReturn(500);
    RuntimeException exception = stub.handleError(response);
    Assert.assertTrue(exception instanceof InternalServerErrorException);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) Document(io.crnk.core.engine.document.Document) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 9 with HttpAdapterResponse

use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.

the class ClientStubBase method execute.

protected Object execute(String url, ResponseType responseType, HttpMethod method, String requestBody) {
    try {
        HttpAdapter httpAdapter = client.getHttpAdapter();
        HttpAdapterRequest request = httpAdapter.newRequest(url, method, requestBody);
        LOGGER.debug("requesting {} {}", method, url);
        if (requestBody != null) {
            LOGGER.debug("request body: {}", requestBody);
        }
        if (method == HttpMethod.POST || method == HttpMethod.PATCH) {
            request.header("Content-Type", HttpHeaders.JSONAPI_CONTENT_TYPE + "; charset=" + HttpHeaders.DEFAULT_CHARSET);
        }
        request.header("Accept", HttpHeaders.JSONAPI_CONTENT_TYPE);
        HttpAdapterResponse response = request.execute();
        if (!response.isSuccessful()) {
            throw handleError(response);
        }
        String body = response.body();
        LOGGER.debug("response body: {}", body);
        ObjectMapper objectMapper = client.getObjectMapper();
        if (responseType != ResponseType.NONE && Resource.class.equals(resourceClass)) {
            Document document = objectMapper.readValue(body, Document.class);
            return toResourceResponse(document, objectMapper);
        } else if (responseType != ResponseType.NONE) {
            Document document = objectMapper.readValue(body, Document.class);
            ClientDocumentMapper documentMapper = client.getDocumentMapper();
            return documentMapper.fromDocument(document, responseType == ResponseType.RESOURCES);
        }
        return null;
    } catch (IOException e) {
        throw new TransportException(e);
    }
}
Also used : HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest) HttpAdapter(io.crnk.client.http.HttpAdapter) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Resource(io.crnk.core.engine.document.Resource) IOException(java.io.IOException) Document(io.crnk.core.engine.document.Document) TransportException(io.crnk.client.TransportException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)9 Test (org.junit.Test)6 Document (io.crnk.core.engine.document.Document)4 HttpAdapterRequest (io.crnk.client.http.HttpAdapterRequest)3 ErrorData (io.crnk.core.engine.document.ErrorData)3 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)3 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 HttpAdapter (io.crnk.client.http.HttpAdapter)2 ErrorDataBuilder (io.crnk.core.engine.document.ErrorDataBuilder)2 ClientException (io.crnk.client.ClientException)1 TransportException (io.crnk.client.TransportException)1 Resource (io.crnk.core.engine.document.Resource)1 Operation (io.crnk.operations.Operation)1 OperationResponse (io.crnk.operations.OperationResponse)1 ArrayList (java.util.ArrayList)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1