use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class ClientStubBase method handleError.
protected RuntimeException handleError(HttpAdapterResponse response) throws IOException {
ErrorResponse errorResponse = null;
String body = response.body();
String contentType = response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE);
if (body.length() > 0 && contentType != null && contentType.toLowerCase().contains(HttpHeaders.JSONAPI_CONTENT_TYPE)) {
ObjectMapper objectMapper = client.getObjectMapper();
Document document = objectMapper.readValue(body, Document.class);
if (document.getErrors() != null && !document.getErrors().isEmpty()) {
errorResponse = new ErrorResponse(document.getErrors(), response.code());
}
}
if (errorResponse == null) {
errorResponse = new ErrorResponse(null, response.code());
}
ExceptionMapperRegistry exceptionMapperRegistry = client.getExceptionMapperRegistry();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(errorResponse);
if (mapper.isPresent()) {
Throwable throwable = mapper.get().fromErrorResponse(errorResponse);
if (throwable instanceof RuntimeException) {
return (RuntimeException) throwable;
} else {
return new ClientException(response.code(), response.message(), throwable);
}
} else {
return new ClientException(response.code(), response.message());
}
}
use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class RelationshipRepositoryStubImpl method executeWithId.
private void executeWithId(String requestUrl, HttpMethod method, Object targetId) {
Document document = new Document();
ResourceIdentifier resourceIdentifier = sourceResourceInformation.toResourceIdentifier(targetId);
document.setData(Nullable.of((Object) resourceIdentifier));
doExecute(requestUrl, method, document);
}
use of io.crnk.core.engine.document.Document 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.core.engine.document.Document in project crnk-framework by crnk-project.
the class RelationshipsResourceDeleteTest method onExistingToOneRelationshipShouldRemoveIt.
@Test
public void onExistingToOneRelationshipShouldRemoveIt() throws Exception {
// GIVEN
Document newTaskBody = new Document();
Resource data = createTask();
newTaskBody.setData(Nullable.of((Object) data));
data.setType("tasks");
JsonPath taskPath = pathBuilder.build("/tasks");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
// WHEN -- adding a task
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
// THEN
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskId).isNotNull();
/* ------- */
// GIVEN
Document newProjectBody = new Document();
data = createProject();
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPath = pathBuilder.build("/projects");
// WHEN -- adding a project
Response projectResponse = resourcePost.handle(projectPath, emptyProjectQuery, null, newProjectBody);
// THEN
assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
assertThat(projectResponse.getDocument().getSingleData().get().getId()).isNotNull();
assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
/* ------- */
// GIVEN
Document newTaskToProjectBody = new Document();
data = new Resource();
newTaskToProjectBody.setData(Nullable.of((Object) data));
data.setType("projects");
data.setId(projectId.toString());
JsonPath projectRelationPath = pathBuilder.build("/tasks/" + taskId + "/relationships/project");
RelationshipsResourcePost relationshipsResourcePost = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
// WHEN -- adding a relation between task and project
Response projectRelationshipResponse = relationshipsResourcePost.handle(projectRelationPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(projectRelationshipResponse).isNotNull();
// THEN
TaskToProjectRepository taskToProjectRepository = new TaskToProjectRepository();
Project project = taskToProjectRepository.findOneTarget(taskId, "project", REQUEST_PARAMS);
assertThat(project.getId()).isEqualTo(projectId);
/* ------- */
// GIVEN
RelationshipsResourceDelete sut = new RelationshipsResourceDelete(resourceRegistry, typeParser, modificationFilters);
// WHEN -- removing a relation between task and project
Response result = sut.handle(projectRelationPath, emptyProjectQuery, null, newTaskToProjectBody);
assertThat(result).isNotNull();
taskToProjectRepository.removeRelations("project");
// THEN
assertThat(result.getHttpStatus()).isEqualTo(HttpStatus.NO_CONTENT_204);
Project nullProject = taskToProjectRepository.findOneTarget(taskId, "project", REQUEST_PARAMS);
assertThat(nullProject).isNull();
}
use of io.crnk.core.engine.document.Document in project crnk-framework by crnk-project.
the class RelationshipsResourcePatchTest method supportPolymorphicRelationshipTypes.
@Test
public void supportPolymorphicRelationshipTypes() {
// GIVEN
Document newTaskBody = new Document();
Resource data = new Resource();
data.setType(ClassUtils.getAnnotation(Task.class, JsonApiResource.class).get().type());
newTaskBody.setData(Nullable.of((Object) data));
JsonPath taskPath = pathBuilder.build("/tasks");
ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
Long taskIdOne = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskIdOne).isNotNull();
taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
Long taskIdTwo = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskIdOne).isNotNull();
taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
Long taskIdThree = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
assertThat(taskIdOne).isNotNull();
newTaskBody = new Document();
// Create ProjectPolymorphic object
Document newProjectBody = new Document();
data = new Resource();
String type = ClassUtils.getAnnotation(ProjectPolymorphic.class, JsonApiResource.class).get().type();
data.setType(type);
data.getRelationships().put("task", new Relationship(new ResourceIdentifier(taskIdOne.toString(), "tasks")));
data.getRelationships().put("tasks", new Relationship(Arrays.asList(new ResourceIdentifier(taskIdTwo.toString(), "tasks"), new ResourceIdentifier(taskIdThree.toString(), "tasks"))));
newProjectBody.setData(Nullable.of((Object) data));
JsonPath projectPolymorphicTypePath = pathBuilder.build("/" + type);
Response projectResponse = resourcePost.handle(projectPolymorphicTypePath, emptyProjectQuery, null, newProjectBody);
assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
Resource projectPolymorphic = projectResponse.getDocument().getSingleData().get();
assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
ProjectPolymorphicRepository resourceRepository = (ProjectPolymorphicRepository) resourceRegistry.getEntry(ProjectPolymorphic.class).getResourceRepository(null).getResourceRepository();
ProjectPolymorphic projectPolymorphicObj = resourceRepository.findOne(projectId, null);
assertEquals(2, projectPolymorphicObj.getTasks().size());
projectPolymorphicTypePath = pathBuilder.build("/" + type + "/" + projectPolymorphic.getId());
ResourcePatch resourcePatch = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
data = newProjectBody.getSingleData().get();
data.setId(projectId.toString());
projectPolymorphic.setId(Long.toString(projectId));
data.getRelationships().get("tasks").setData(Nullable.of((Object) new ArrayList<ResourceIdentifier>()));
// WHEN
Response baseResponseContext = resourcePatch.handle(projectPolymorphicTypePath, new QuerySpecAdapter(new QuerySpec(ProjectPolymorphic.class), resourceRegistry), null, newProjectBody);
assertThat(baseResponseContext.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
projectId = Long.parseLong(baseResponseContext.getDocument().getSingleData().get().getId());
assertThat(projectId).isNotNull();
projectPolymorphic = baseResponseContext.getDocument().getSingleData().get();
assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
projectPolymorphicObj = resourceRepository.findOne(projectId, null);
assertEquals(0, projectPolymorphicObj.getTasks().size());
}
Aggregations