Search in sources :

Example 36 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier 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 37 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class ResourcePatchTest method onUnchagedLazyRelationshipDataShouldNotReturnThatData.

@Test
public void onUnchagedLazyRelationshipDataShouldNotReturnThatData() 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);
    data.getRelationships().remove("tasks");
    data.getAttributes().put("name", objectMapper.readTree("\"updated project\""));
    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("updated project");
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().isPresent()).isFalse();
}
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) 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 38 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class ResourcePostTest 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 sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response taskResponse = sut.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(taskResponse.getDocument().getSingleData().get().getId()).isNotNull();
    assertThat(taskResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample task");
    Long taskId = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    /* ------- */
    // GIVEN
    Document newProjectBody = new Document();
    data = createProject();
    data.setType("projects");
    List<ResourceIdentifier> taskIds = Collections.singletonList(new ResourceIdentifier(taskId.toString(), "tasks"));
    data.getRelationships().put("tasks", new Relationship(taskIds));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectsPath = pathBuilder.build("/projects");
    // WHEN
    Response projectsResponse = sut.handle(projectsPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectsResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    Long userId = Long.parseLong(projectsResponse.getDocument().getSingleData().get().getId());
    assertThat(userId).isNotNull();
    assertThat(projectsResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample project");
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get()).hasSize(1);
    assertThat(projectsResponse.getDocument().getSingleData().get().getRelationships().get("tasks").getCollectionData().get().get(0).getId()).isEqualTo(taskId.toString());
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyManyRelationship(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq(ResourceRelationshipModificationType.SET), Mockito.eq(taskIds));
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) 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) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 39 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class ResourcePostTest method onNewResourcesAndRelationshipShouldPersistThoseData.

@Test
public void onNewResourcesAndRelationshipShouldPersistThoseData() throws Exception {
    // GIVEN
    Document newProjectBody = new Document();
    Resource data = createProject();
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPath = pathBuilder.build("/projects");
    ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response projectResponse = sut.handle(projectPath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
    assertThat(projectResponse.getDocument().getData().get()).isExactlyInstanceOf(Resource.class);
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects");
    Resource persistedProject = projectResponse.getDocument().getSingleData().get();
    assertThat(persistedProject.getId()).isNotNull();
    assertThat(persistedProject.getAttributes().get("name").asText()).isEqualTo("sample project");
    assertThat(persistedProject.getAttributes().get("data").get("data").asText()).isEqualTo("asd");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    Mockito.verify(modificationFilter, Mockito.times(1)).modifyAttribute(Mockito.any(), Mockito.any(ResourceField.class), Mockito.eq("data"), Mockito.any());
    /* ------- */
    // GIVEN
    Document newTasksBody = new Document();
    newTasksBody.setData(Nullable.of((Object) createTask()));
    newTasksBody.getSingleData().get().getRelationships().put("project", new Relationship(new ResourceIdentifier(projectId.toString(), "projects")));
    JsonPath taskPath = pathBuilder.build("/tasks");
    // WHEN
    Response taskResponse = sut.handle(taskPath, emptyTaskQuery, null, newTasksBody);
    // THEN
    assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    String taskId = taskResponse.getDocument().getSingleData().get().getId();
    assertThat(taskId).isNotNull();
    assertThat(taskResponse.getDocument().getSingleData().get().getAttributes().get("name").asText()).isEqualTo("sample task");
    TaskRepository taskRepository = new TaskRepository();
    Task persistedTask = taskRepository.findOne(Long.parseLong(taskId), null);
    assertThat(persistedTask.getProject().getId()).isEqualTo(projectId);
}
Also used : Task(io.crnk.core.mock.models.Task) TaskRepository(io.crnk.core.mock.repository.TaskRepository) Resource(io.crnk.core.engine.document.Resource) 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) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 40 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class DocumentMapperTest method testRelationshipIncludeMultiValued.

@Test
public void testRelationshipIncludeMultiValued() {
    LazyTask task = createLazyTask(2);
    Project project1 = createProject(3, "sample project3");
    Project project2 = createProject(4, "sample project4");
    task.setProjects(Arrays.asList(project1, project2));
    QuerySpec querySpec = new QuerySpec(LazyTask.class);
    querySpec.includeRelation(Arrays.asList("projects"));
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("2", resource.getId());
    Relationship relationship = resource.getRelationships().get("projects");
    Assert.assertNotNull(relationship);
    List<ResourceIdentifier> relationshipData = relationship.getCollectionData().get();
    Assert.assertNotNull(relationshipData);
    Assert.assertEquals(2, relationshipData.size());
    Assert.assertEquals("3", relationshipData.get(0).getId());
    Assert.assertEquals("projects", relationshipData.get(0).getType());
    Assert.assertEquals("4", relationshipData.get(1).getId());
    Assert.assertEquals("projects", relationshipData.get(1).getType());
    Assert.assertFalse(document.getIncluded().isEmpty());
    List<Resource> included = document.getIncluded();
    Assert.assertEquals(2, included.size());
    Assert.assertEquals("3", included.get(0).getId());
    Assert.assertEquals("projects", included.get(0).getType());
    Assert.assertEquals("sample project3", included.get(0).getAttributes().get("name").asText());
    Assert.assertEquals("4", included.get(1).getId());
    Assert.assertEquals("projects", included.get(1).getType());
    Assert.assertEquals("sample project4", included.get(1).getAttributes().get("name").asText());
}
Also used : Project(io.crnk.core.mock.models.Project) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) LazyTask(io.crnk.core.mock.models.LazyTask) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Aggregations

ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)60 Resource (io.crnk.core.engine.document.Resource)42 Document (io.crnk.core.engine.document.Document)40 Test (org.junit.Test)33 Relationship (io.crnk.core.engine.document.Relationship)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)25 Response (io.crnk.core.engine.dispatcher.Response)14 Task (io.crnk.core.mock.models.Task)13 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)11 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)11 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)11 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)9 Serializable (java.io.Serializable)9 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)8 Project (io.crnk.core.mock.models.Project)8 ArrayList (java.util.ArrayList)8 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)6 LazyTask (io.crnk.core.mock.models.LazyTask)6 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)4