use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.
the class OkHttpRequest method execute.
@Override
public HttpAdapterResponse execute() throws IOException {
Request request = builder.build();
Response response = client.newCall(request).execute();
return new OkHttpResponse(response);
}
use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.
the class OperationsCall method execute.
public void execute() {
List<Operation> operations = new ArrayList<>();
for (QueuedOperation queuedOperation : queuedOperations) {
operations.add(queuedOperation.operation);
}
HttpAdapter adapter = client.getCrnk().getHttpAdapter();
ObjectMapper mapper = client.getCrnk().getObjectMapper();
try {
String operationsJson = mapper.writer().writeValueAsString(operations.toArray(new Operation[operations.size()]));
String url = client.getCrnk().getServiceUrlProvider().getUrl() + "/operations";
HttpAdapterRequest request = adapter.newRequest(url, HttpMethod.PATCH, operationsJson);
request.header(HttpHeaders.HTTP_CONTENT_TYPE, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
request.header(HttpHeaders.HTTP_HEADER_ACCEPT, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
HttpAdapterResponse response = request.execute();
int status = response.code();
if (status != 200) {
// general issue, status of individual operations is important.
throw new InternalServerErrorException("patch execution failed with status " + status);
}
String json = response.body();
responses = Arrays.asList(mapper.readValue(json, OperationResponse[].class));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkCheckedException.
@Test
public void checkCheckedException() throws IOException {
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn("");
Mockito.when(response.code()).thenReturn(599);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof ClientException);
Assert.assertTrue(exception.getCause() instanceof IOException);
}
use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkBodyWithErrors.
@Test
public void checkBodyWithErrors() throws IOException {
Document document = new Document();
ErrorData errorData = new ErrorDataBuilder().setCode("404").setDetail("detail").build();
document.setErrors(Arrays.asList(errorData));
String body = client.getObjectMapper().writeValueAsString(document);
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn(body);
Mockito.when(response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn(HttpHeaders.JSONAPI_CONTENT_TYPE);
Mockito.when(response.code()).thenReturn(404);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof ResourceNotFoundException);
Assert.assertEquals("detail", exception.getMessage());
}
use of io.crnk.client.http.HttpAdapterResponse in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkBodyWithErrorsButInvalidContentType.
@Test
public void checkBodyWithErrorsButInvalidContentType() throws IOException {
Document document = new Document();
ErrorData errorData = new ErrorDataBuilder().setCode("404").setDetail("detail").build();
document.setErrors(Arrays.asList(errorData));
String body = client.getObjectMapper().writeValueAsString(document);
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn(body);
Mockito.when(response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE)).thenReturn("not json api");
Mockito.when(response.code()).thenReturn(404);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof ResourceNotFoundException);
Assert.assertNull(exception.getMessage());
}
Aggregations