Search in sources :

Example 21 with ResourcePost

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

the class RelationshipsResourcePostTest method onExistingResourcesShouldAddToOneRelationship.

@Test
public void onExistingResourcesShouldAddToOneRelationship() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    newTaskBody.setData(Nullable.of((Object) createTask()));
    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();
    newProjectBody.setData(Nullable.of((Object) createProject()));
    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();
    newTaskToProjectBody.setData(Nullable.of((Object) createProject(Long.toString(projectId))));
    JsonPath savedTaskPath = pathBuilder.build("/tasks/" + taskId + "/relationships/project");
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
    // WHEN -- adding a relation between task and project
    Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
    assertThat(projectRelationshipResponse).isNotNull();
    // THEN
    TaskToProjectRepository taskToProjectRepository = new TaskToProjectRepository();
    Project project = taskToProjectRepository.findOneTarget(taskId, "project", new QueryParams());
    assertThat(project.getId()).isEqualTo(projectId);
    ResourceIdentifier projectResourceId = new ResourceIdentifier(projectId.toString(), "projects");
// TODO properly implement ResourceIdentifier vs Resource in relationship repositories
// Mockito.validate(modificationFilter, Mockito.times(1)).modifyOneRelationship(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq(projectResourceId));
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) TaskToProjectRepository(io.crnk.core.mock.repository.TaskToProjectRepository) QueryParams(io.crnk.legacy.queryParams.QueryParams) 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) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 22 with ResourcePost

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

the class RelationshipsResourcePostTest method onExistingResourcesShouldAddToManyRelationship.

@Test
public void onExistingResourcesShouldAddToManyRelationship() throws Exception {
    // GIVEN
    Document newUserBody = new Document();
    Resource data = createUser();
    newUserBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/users");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, OBJECT_MAPPER, documentMapper, modificationFilters);
    // WHEN -- adding a user
    Response taskResponse = resourcePost.handle(taskPath, emptyUserQuery, null, newUserBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("users");
    Long userId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(userId).isNotNull();
    /* ------- */
    // GIVEN
    Document newProjectBody = new Document();
    data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    data.setType("projects");
    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) Collections.singletonList(data)));
    data.setType("projects");
    data.setId(projectId.toString());
    JsonPath savedTaskPath = pathBuilder.build("/users/" + userId + "/relationships/assignedProjects");
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, modificationFilters);
    // WHEN -- adding a relation between user and project
    Response projectRelationshipResponse = sut.handle(savedTaskPath, emptyProjectQuery, null, newTaskToProjectBody);
    assertThat(projectRelationshipResponse).isNotNull();
    // THEN
    UserToProjectRepository userToProjectRepository = new UserToProjectRepository();
    Project project = userToProjectRepository.findOneTarget(userId, "assignedProjects", new QuerySpec(Project.class));
    assertThat(project.getId()).isEqualTo(projectId);
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) UserToProjectRepository(io.crnk.core.mock.repository.UserToProjectRepository) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) 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) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) RelationshipsResourcePost(io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 23 with ResourcePost

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePost 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 24 with ResourcePost

use of io.crnk.core.engine.internal.dispatcher.controller.ResourcePost 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 25 with ResourcePost

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

Aggregations

ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)40 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)39 Test (org.junit.Test)39 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)36 Document (io.crnk.core.engine.document.Document)35 Resource (io.crnk.core.engine.document.Resource)31 Response (io.crnk.core.engine.dispatcher.Response)30 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)11 Project (io.crnk.core.mock.models.Project)11 Relationship (io.crnk.core.engine.document.Relationship)10 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)9 RelationshipsResourcePost (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePost)8 QuerySpec (io.crnk.core.queryspec.QuerySpec)8 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)7 JsonApiResource (io.crnk.core.resource.annotations.JsonApiResource)7 TaskToProjectRepository (io.crnk.core.mock.repository.TaskToProjectRepository)6 QueryParams (io.crnk.legacy.queryParams.QueryParams)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4 RelationshipsResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourcePatch)4 ResourcePath (io.crnk.core.engine.internal.dispatcher.path.ResourcePath)4