Search in sources :

Example 66 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath 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 67 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath 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 68 with JsonPath

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

Example 69 with JsonPath

use of io.crnk.core.engine.internal.dispatcher.path.JsonPath 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 70 with JsonPath

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

Aggregations

JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)88 Test (org.junit.Test)80 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)72 Response (io.crnk.core.engine.dispatcher.Response)50 Document (io.crnk.core.engine.document.Document)46 Resource (io.crnk.core.engine.document.Resource)41 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)20 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)16 Project (io.crnk.core.mock.models.Project)14 Relationship (io.crnk.core.engine.document.Relationship)13 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)12 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)11 ResourcePath (io.crnk.core.engine.internal.dispatcher.path.ResourcePath)10 QuerySpec (io.crnk.core.queryspec.QuerySpec)10 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 Task (io.crnk.core.mock.models.Task)9 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)9 QueryParams (io.crnk.legacy.queryParams.QueryParams)9 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)8