Search in sources :

Example 11 with ResourcePatch

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

the class ResourcePatchTest method patchNestedAttribute.

@Test
public void patchNestedAttribute() throws Exception {
    // GIVEN
    Document newProjectBody = new Document();
    Resource data = createProject();
    data.getAttributes().remove("data");
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/projects");
    // WHEN
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Response projectResponse = resourcePost.handle(taskPath, emptyProjectQuery, null, newProjectBody);
    Resource savedProject = projectResponse.getDocument().getSingleData().get();
    assertThat(savedProject.getType()).isEqualTo("projects");
    assertThat(projectResponse.getDocument().getSingleData().get().getAttributes().get("data")).isNull();
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    // GIVEN
    data = new Resource();
    data.setType("projects");
    data.setId(savedProject.getId());
    data.setAttribute("data", objectMapper.readTree("{\"data\" : \"updated data\"}"));
    Document projectPatch = new Document();
    projectPatch.setData(Nullable.of((Object) data));
    JsonPath jsonPath = pathBuilder.build("/projects/" + projectId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyProjectQuery, null, projectPatch);
    // THEN
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample " + "project");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("data").get("data").asText()).isEqualTo("updated data");
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) 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) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 12 with ResourcePatch

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

the class ResourcePatchTest method onGivenRequestResourceGetShouldAcceptIt.

@Test
public void onGivenRequestResourceGetShouldAcceptIt() {
    // GIVEN
    JsonPath jsonPath = pathBuilder.build("/tasks/1");
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    boolean result = sut.isAcceptable(jsonPath, REQUEST_TYPE);
    // THEN
    Assert.assertEquals(result, true);
}
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 13 with ResourcePatch

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

the class ResourcePatchTest method onInheritedResourceShouldUpdateInheritedResource.

@Test
// TODO inhertiance/resourceregistry
@org.junit.Ignore
public void onInheritedResourceShouldUpdateInheritedResource() throws Exception {
    // GIVEN
    Document memorandumBody = new Document();
    Resource data = new Resource();
    memorandumBody.setData(Nullable.of((Object) data));
    data.setType("memoranda");
    data.setAttribute("title", objectMapper.readTree("\"some title\""));
    data.setAttribute("body", objectMapper.readTree("\"sample body\""));
    JsonPath documentsPath = pathBuilder.build("/documents");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response taskResponse = resourcePost.handle(documentsPath, emptyMemorandumQuery, null, memorandumBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("memoranda");
    Long memorandumId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(memorandumId).isNotNull();
    // --------------------------
    // GIVEN
    memorandumBody = new Document();
    data = new Resource();
    memorandumBody.setData(Nullable.of((Object) data));
    data.setType("memoranda");
    data.setAttribute("title", objectMapper.readTree("\"new title\""));
    data.setAttribute("body", objectMapper.readTree("\"new body\""));
    JsonPath documentPath = pathBuilder.build("/documents/" + memorandumId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response memorandumResponse = sut.handle(documentPath, emptyMemorandumQuery, null, memorandumBody);
    // THEN
    assertThat(memorandumResponse.getDocument().getSingleData().get().getType()).isEqualTo("memoranda");
    Resource persistedMemorandum = memorandumResponse.getDocument().getSingleData().get();
    assertThat(persistedMemorandum.getId()).isNotNull();
    assertThat(persistedMemorandum.getAttributes().get("title").asText()).isEqualTo("new title");
    assertThat(persistedMemorandum.getAttributes().get("body").asText()).isEqualTo("new body");
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) 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) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 14 with ResourcePatch

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

the class ResourcePatchTest method onResourceRelationshipNullifiedShouldSaveIt.

@Test
public void onResourceRelationshipNullifiedShouldSaveIt() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    // WHEN
    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 taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskId).isNotNull();
    // GIVEN
    Document taskPatch = new Document();
    data = createTask();
    data.setAttribute("name", objectMapper.readTree("\"task updated\""));
    data.getRelationships().put("project", null);
    taskPatch.setData(Nullable.of((Object) data));
    JsonPath jsonPath = pathBuilder.build("/tasks/" + taskId);
    ResourcePatch sut = new ResourcePatch(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response response = sut.handle(jsonPath, emptyTaskQuery, null, taskPatch);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(response.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("task updated");
    assertThat(response.getDocument().getSingleData().get().getRelationships().get("project").getData().get()).isNull();
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) 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) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 15 with ResourcePatch

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

the class ResourcePatchTest method onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior.

@Test
public void onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath postPath = pathBuilder.build("/tasks");
    ResourcePost post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    post.handle(postPath, emptyTaskQuery, null, requestDocument);
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (CrnkProperties.RESOURCE_FIELD_IMMUTABLE_WRITE_BEHAVIOR.equals(key)) {
                return ResourceFieldImmutableWriteBehavior.FAIL.toString();
            }
            return null;
        }
    };
    ResourcePatch sut = new ResourcePatch(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    // WHEN
    try {
        JsonPath patchPath = pathBuilder.build("/tasks/" + data.getId());
        sut.handle(patchPath, emptyTaskQuery, null, requestDocument);
        Assert.fail("should not be allowed to update read-only field");
    } catch (ForbiddenException e) {
        Assert.assertEquals("field 'readOnlyValue' cannot be modified", e.getMessage());
    }
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) ForbiddenException(io.crnk.core.exception.ForbiddenException) 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) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) 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