Search in sources :

Example 6 with ResourcePatch

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch 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)

Example 7 with ResourcePatch

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.

the class ResourceIdControllerTest method checkPatchResource.

private Resource checkPatchResource(Resource resource) {
    JsonPath path = pathBuilder.build("/relationIdTest/" + resource.getId());
    resource.getRelationships().put("testLookupAlways", new Relationship(schedule2Id));
    Document newDocument = createDocument(resource);
    // WHEN PATCH
    ResourcePatch sut = new ResourcePatch(resourceRegistry, new NullPropertiesProvider(), typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = sut.handle(path, emptyTaskQuery, null, newDocument);
    // THEN PATCH
    assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.OK_200);
    Resource updatedResource = taskResponse.getDocument().getSingleData().get();
    Assert.assertEquals(resource.getType(), updatedResource.getType());
    Assert.assertEquals(resource.getId(), updatedResource.getId());
    Assert.assertEquals(resource.getAttributes().get("name"), updatedResource.getAttributes().get("name"));
    Relationship relationship = updatedResource.getRelationships().get("testLookupAlways");
    Assert.assertTrue(relationship.getData().isPresent());
    Assert.assertEquals(schedule2Id, relationship.getData().get());
    // validate relationship not accessed, only id set => performance
    RelationIdTestResource entity = repository.findOne(1L, new QuerySpec(RelationIdTestResource.class));
    Assert.assertEquals(schedule2.getId(), entity.getTestLookupAlwaysId());
    Assert.assertNull(entity.getTestLookupAlways());
    return updatedResource;
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Relationship(io.crnk.core.engine.document.Relationship) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) Resource(io.crnk.core.engine.document.Resource) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) RelationshipsResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch) QuerySpec(io.crnk.core.queryspec.QuerySpec) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource)

Example 8 with ResourcePatch

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.

the class ResourcePatchTest method onGivenRequestCollectionGetShouldDenyIt.

@Test
public void onGivenRequestCollectionGetShouldDenyIt() {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/tasks/");
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    Assert.assertEquals(result, false);
}
Also used : JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 9 with ResourcePatch

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.

the class ResourcePatchTest method onUpdatedLazyRelationshipDataShouldReturnThatData.

@Test
public void onUpdatedLazyRelationshipDataShouldReturnThatData() 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 post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = post.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    Document newProjectBody = new Document();
    data = createProject();
    data.setType("projects");
    data.getRelationships().put("tasks", new Relationship(Collections.singletonList(new ResourceIdentifier(taskId.toString(), "tasks"))));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectsPath = pathBuilder.build("/projects");
    Response projectsResponse = post.handle(projectsPath, emptyProjectQuery, null, newProjectBody);
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(1);
    // update relationship and availability in response
    ResourcePatch patch = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Nullable<Object> emptyRelation = Nullable.of((Object) new ArrayList<ResourceIdentifier>());
    data.getRelationships().get("tasks").setData(emptyRelation);
    projectsResponse = patch.handle(pathBuilder.build("/projects/2"), emptyProjectQuery, null, newProjectBody);
    assertThat(projectsResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    assertThat(projectsResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(0);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 10 with ResourcePatch

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch in project crnk-framework by crnk-project.

the class ResourcePatchTest method onPatchNonExistingResourceThrowException.

@Test(expected = ResourceNotFoundException.class)
public void onPatchNonExistingResourceThrowException() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    Document taskPatch = new Document();
    data = createTask();
    taskPatch.setData(Nullable.of((Object) data));
    data.setAttribute("name", objectMapper.readTree("\"task updated\""));
    JsonPath jsonPath = pathBuilder.build("/tasks/1234567");
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
}
Also used : Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)16 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)16 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)15 Test (org.junit.Test)15 Document (io.crnk.core.engine.document.Document)14 Resource (io.crnk.core.engine.document.Resource)14 Response (io.crnk.core.engine.dispatcher.Response)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)9 Relationship (io.crnk.core.engine.document.Relationship)4 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)3 Task (io.crnk.core.mock.models.Task)3 RelationshipsResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch)2 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 ResourceGet (io.crnk.core.engine.internal.dispatcher.controller.ResourceGet)1 NullPropertiesProvider (io.crnk.core.engine.properties.NullPropertiesProvider)1 PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)1 CrnkException (io.crnk.core.exception.CrnkException)1