Search in sources :

Example 31 with Document

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());
    }
}
Also used : ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ClientException(io.crnk.client.ClientException) Document(io.crnk.core.engine.document.Document) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 32 with Document

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);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Document(io.crnk.core.engine.document.Document)

Example 33 with 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());
}
Also used : ErrorDataBuilder(io.crnk.core.engine.document.ErrorDataBuilder) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) Document(io.crnk.core.engine.document.Document) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 34 with Document

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();
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) TaskToProjectRepository(io.crnk.core.mock.repository.TaskToProjectRepository) Resource(io.crnk.core.engine.document.Resource) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) RelationshipsResourceDelete(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceDelete) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 35 with Document

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());
}
Also used : Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) ProjectPolymorphicRepository(io.crnk.core.mock.repository.ProjectPolymorphicRepository) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ProjectPolymorphic(io.crnk.core.mock.models.ProjectPolymorphic) Relationship(io.crnk.core.engine.document.Relationship) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) QuerySpec(io.crnk.core.queryspec.QuerySpec) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

Document (io.crnk.core.engine.document.Document)131 Test (org.junit.Test)95 Resource (io.crnk.core.engine.document.Resource)87 Response (io.crnk.core.engine.dispatcher.Response)56 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)47 QuerySpec (io.crnk.core.queryspec.QuerySpec)45 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)40 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)40 Relationship (io.crnk.core.engine.document.Relationship)39 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)35 Task (io.crnk.core.mock.models.Task)34 Project (io.crnk.core.mock.models.Project)27 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)25 LazyTask (io.crnk.core.mock.models.LazyTask)17 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)11 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)10