Search in sources :

Example 91 with Resource

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

the class ResourcePostTest method onNewInheritedResourceShouldPersistThisResource.

@Test
// TODO
@Ignore
public void onNewInheritedResourceShouldPersistThisResource() throws Exception {
    // GIVEN
    Document newMemorandumBody = new Document();
    Resource data = new Resource();
    newMemorandumBody.setData(Nullable.of((Object) data));
    data.setType("memoranda");
    data.setAttribute("title", objectMapper.readTree("\"sample title\""));
    data.setAttribute("body", objectMapper.readTree("\"sample body\""));
    JsonPath projectPath = pathBuilder.build("/documents");
    ResourcePost sut = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    Response memorandumResponse = sut.handle(projectPath, emptyMemorandumQuery, null, newMemorandumBody);
    // 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("sample title");
    assertThat(persistedMemorandum.getAttributes().get("body").asText()).isEqualTo("sample body");
}
Also used : 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) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) Ignore(org.junit.Ignore) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 92 with Resource

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

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

the class ResourcePostTest method onPostingReadOnlyFieldReturnBadRequestWithFailBehavior.

@Test
public void onPostingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath path = pathBuilder.build("/tasks");
    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;
        }
    };
    ResourcePost sut = new ResourcePost(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    // WHEN
    try {
        sut.handle(path, 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) 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 94 with Resource

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

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

Resource (io.crnk.core.engine.document.Resource)130 Document (io.crnk.core.engine.document.Document)86 Test (org.junit.Test)85 Relationship (io.crnk.core.engine.document.Relationship)48 Response (io.crnk.core.engine.dispatcher.Response)43 QuerySpec (io.crnk.core.queryspec.QuerySpec)43 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)42 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)41 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)37 Task (io.crnk.core.mock.models.Task)32 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)31 Project (io.crnk.core.mock.models.Project)24 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)17 LazyTask (io.crnk.core.mock.models.LazyTask)15 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)14 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)14 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)13 ResourceField (io.crnk.core.engine.information.resource.ResourceField)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)11 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10