Search in sources :

Example 36 with Relationship

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

the class ResourceIdControllerTest method checkPost.

private Resource checkPost() throws IOException {
    // GIVEN
    Resource resource = createResource();
    resource.getRelationships().put("testLookupAlways", new Relationship(schedule3Id));
    Document newDocument = createDocument(resource);
    JsonPath postPath = pathBuilder.build("/relationIdTest");
    // WHEN POST
    ResourcePost sut = new ResourcePost(resourceRegistry, new NullPropertiesProvider(), typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = sut.handle(postPath, emptyTaskQuery, null, newDocument);
    // THEN POST
    assertThat(taskResponse.getHttpStatus()).isEqualTo(HttpStatus.CREATED_201);
    Resource createdResource = taskResponse.getDocument().getSingleData().get();
    Assert.assertEquals(resource.getType(), createdResource.getType());
    Assert.assertEquals(resource.getId(), createdResource.getId());
    Assert.assertEquals(resource.getAttributes().get("name"), createdResource.getAttributes().get("name"));
    Relationship relationship = createdResource.getRelationships().get("testLookupAlways");
    Assert.assertTrue(relationship.getData().isPresent());
    Assert.assertEquals(schedule3Id, relationship.getData().get());
    // validate relationship not accessed, only id set => performance
    RelationIdTestResource entity = repository.findOne(1L, new QuerySpec(RelationIdTestResource.class));
    Assert.assertEquals(schedule3.getId(), entity.getTestLookupAlwaysId());
    Assert.assertNull(entity.getTestLookupAlways());
    return createdResource;
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider) 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) RelationIdTestResource(io.crnk.core.mock.models.RelationIdTestResource)

Example 37 with Relationship

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

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

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

the class DocumentMapperTest method testConvergingInclusionPaths.

@Test
public void testConvergingInclusionPaths() {
    Task task1 = createTask(1, "other task");
    Task task2 = createTask(2, "other task");
    Project project1 = new Project();
    project1.setName("someProject");
    project1.setId(3L);
    project1.setTasks(Arrays.asList(task1, task2));
    Project project2 = new Project();
    project2.setName("someProject");
    project2.setId(2L);
    task1.setProject(project1);
    task1.setProjectsInit(Arrays.asList(project2));
    // come back/converge to same task
    project1.setTask(task2);
    project2.setTask(task2);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project", "task"));
    querySpec.includeRelation(Arrays.asList("projectsInit", "task"));
    QuerySpecAdapter queryAdapter = (QuerySpecAdapter) toAdapter(querySpec);
    Document document = mapper.toDocument(toResponse(task1), queryAdapter);
    Resource resource = document.getSingleData().get();
    Assert.assertEquals("1", resource.getId());
    Assert.assertEquals("tasks", resource.getType());
    Relationship projectRel = resource.getRelationships().get("project");
    Assert.assertNotNull(projectRel.getLinks());
    Assert.assertTrue(projectRel.getData().isPresent());
    Assert.assertNotNull(projectRel.getData().get());
    Relationship projectsRel = resource.getRelationships().get("projectsInit");
    Assert.assertNotNull(projectsRel.getLinks());
    Assert.assertTrue(projectsRel.getData().isPresent());
    Assert.assertNotNull(projectsRel.getData().get());
    Assert.assertEquals(1, projectsRel.getCollectionData().get().size());
    Assert.assertEquals(3, document.getIncluded().size());
    List<Resource> included = document.getIncluded();
    Resource projectResource2 = included.get(0);
    Resource projectResource3 = included.get(1);
    Assert.assertTrue(projectResource2.getRelationships().get("task").getData().isPresent());
    Assert.assertTrue(projectResource3.getRelationships().get("task").getData().isPresent());
}
Also used : Project(io.crnk.core.mock.models.Project) LazyTask(io.crnk.core.mock.models.LazyTask) Task(io.crnk.core.mock.models.Task) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 40 with Relationship

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

Relationship (io.crnk.core.engine.document.Relationship)54 Resource (io.crnk.core.engine.document.Resource)48 Document (io.crnk.core.engine.document.Document)39 Test (org.junit.Test)39 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)26 Task (io.crnk.core.mock.models.Task)22 Response (io.crnk.core.engine.dispatcher.Response)14 Project (io.crnk.core.mock.models.Project)14 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)13 LazyTask (io.crnk.core.mock.models.LazyTask)12 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)10 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)10 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)9 HierarchicalTask (io.crnk.core.mock.models.HierarchicalTask)8 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)7 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)4