Search in sources :

Example 1 with ResourceIdentifier

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

the class RelationshipsResourcePostTest method supportPolymorphicRelationshipTypes.

@Test
public void supportPolymorphicRelationshipTypes() {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    Long taskIdOne = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskIdTwo = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    Long taskIdThree = Long.parseLong(taskResponse.getDocument().getSingleData().get().getId());
    assertThat(taskIdOne).isNotNull();
    // Create ProjectPolymorphic object
    Document newProjectBody = new Document();
    data = new Resource();
    String type = ClassUtils.getAnnotation(ProjectPolymorphic.class, JsonApiResource.class).get().type();
    data.setType(type);
    data.getRelationships().put("task", new Relationship(new ResourceIdentifier(taskIdOne.toString(), "tasks")));
    data.getRelationships().put("tasks", new Relationship(Arrays.asList(new ResourceIdentifier(taskIdTwo.toString(), "tasks"), new ResourceIdentifier(taskIdThree.toString(), "tasks"))));
    newProjectBody.setData(Nullable.of((Object) data));
    JsonPath projectPolymorphicTypePath = pathBuilder.build("/" + type);
    // WHEN
    Response projectResponse = resourcePost.handle(projectPolymorphicTypePath, emptyProjectQuery, null, newProjectBody);
    // THEN
    assertThat(projectResponse.getDocument().getSingleData().get().getType()).isEqualTo("projects-polymorphic");
    Long projectId = Long.parseLong(projectResponse.getDocument().getSingleData().get().getId());
    assertThat(projectId).isNotNull();
    Resource projectPolymorphic = projectResponse.getDocument().getSingleData().get();
    assertNotNull(projectPolymorphic.getRelationships().get("task").getSingleData().get());
    assertNotNull(projectPolymorphic.getRelationships().get("tasks"));
    ProjectPolymorphicRepository repository = (ProjectPolymorphicRepository) resourceRegistry.getEntry(ProjectPolymorphic.class).getResourceRepository(null).getResourceRepository();
    ProjectPolymorphic projectPolymorphicObj = repository.findOne(projectId, null);
    assertNotNull(projectPolymorphicObj.getTasks());
    assertEquals(2, projectPolymorphicObj.getTasks().size());
}
Also used : Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ProjectPolymorphic(io.crnk.core.mock.models.ProjectPolymorphic) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) JsonApiResource(io.crnk.core.resource.annotations.JsonApiResource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ProjectPolymorphicRepository(io.crnk.core.mock.repository.ProjectPolymorphicRepository) 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 2 with ResourceIdentifier

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

the class ResourceGetTest method onGivenRequestResourceShouldNotLoadAutoIncludeFields.

@Test
public void onGivenRequestResourceShouldNotLoadAutoIncludeFields() throws Exception {
    // GIVEN
    Document newTaskBody = new Document();
    Resource data = createTask();
    newTaskBody.setData(Nullable.of((Object) data));
    JsonPath taskPath = pathBuilder.build("/tasks");
    ResourcePost resourcePost = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, new ArrayList<ResourceModificationFilter>());
    // WHEN -- adding a task
    Response taskResponse = resourcePost.handle(taskPath, emptyTaskQuery, null, newTaskBody);
    // THEN
    assertThat(taskResponse.getDocument().getSingleData().get()).isExactlyInstanceOf(Resource.class);
    assertThat(taskResponse.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(taskResponse.getDocument().getSingleData().get().getId()).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()).isExactlyInstanceOf(Resource.class);
    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");
    /* ------- */
    // GIVEN
    Document newTaskToProjectBody = new Document();
    ResourceIdentifier reldata = new ResourceIdentifier();
    newTaskToProjectBody.setData(Nullable.of((Object) data));
    data.setType("projects");
    data.setId("2");
    JsonPath savedTaskPath = pathBuilder.build("/tasks/" + TASK_ID + "/relationships/project");
    RelationshipsResourcePost sut = new RelationshipsResourcePost(resourceRegistry, typeParser, new ArrayList<ResourceModificationFilter>());
    // 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(TASK_ID, "project", new QueryParams());
    assertThat(project.getId()).isEqualTo(PROJECT_ID);
    // Given
    JsonPath jsonPath = pathBuilder.build("/tasks/" + TASK_ID);
    ResourceGet responseGetResp = new ResourceGet(resourceRegistry, typeParser, documentMapper);
    Map<String, Set<String>> queryParams = new HashMap<>();
    queryParams.put(RestrictedQueryParamsMembers.include.name() + "[tasks]", Collections.singleton("[\"project\"]"));
    QueryParams requestParams = new QueryParamsBuilder(new DefaultQueryParamsParser()).buildQueryParams(queryParams);
    // WHEN
    Response response = responseGetResp.handle(jsonPath, new QueryParamsAdapter(resourceRegistry.getEntry(Task.class).getResourceInformation(), requestParams, moduleRegistry), null, null);
    // THEN
    Assert.assertNotNull(response);
    assertThat(response.getDocument().getSingleData().get().getType()).isEqualTo("tasks");
    assertThat(taskResponse.getDocument().getSingleData().get().getRelationships().get("project").getData().get()).isNull();
}
Also used : Task(io.crnk.core.mock.models.Task) Resource(io.crnk.core.engine.document.Resource) 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) DefaultQueryParamsParser(io.crnk.legacy.queryParams.DefaultQueryParamsParser) Response(io.crnk.core.engine.dispatcher.Response) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Project(io.crnk.core.mock.models.Project) TaskToProjectRepository(io.crnk.core.mock.repository.TaskToProjectRepository) ResourceGet(io.crnk.core.engine.internal.dispatcher.controller.ResourceGet) QueryParamsBuilder(io.crnk.legacy.queryParams.QueryParamsBuilder) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) QueryParams(io.crnk.legacy.queryParams.QueryParams) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 3 with ResourceIdentifier

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

the class DocumentMapperUtilTest method toResourceId.

@Test
public void toResourceId() {
    Task task = new Task();
    task.setId(12L);
    ResourceIdentifier id = util.toResourceId(task);
    Assert.assertEquals("tasks", id.getType());
    Assert.assertEquals("12", id.getId());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Task(io.crnk.core.mock.models.Task) Test(org.junit.Test)

Example 4 with ResourceIdentifier

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

the class PerTypeIncludeBehaviorTest method includeParentChildren.

@Test
public void includeParentChildren() throws Exception {
    QuerySpec querySpec = new QuerySpec(HierarchicalTask.class);
    querySpec.includeRelation(Arrays.asList("parent", "children"));
    Document document = mapper.toDocument(toResponse(h11), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship parentRelationship = taskResource.getRelationships().get("parent");
    assertNotNull(parentRelationship);
    assertNotNull(parentRelationship.getSingleData());
    ResourceIdentifier parentResource = parentRelationship.getSingleData().get();
    assertNotNull(h1.getId().toString(), parentResource.getId());
    List<Resource> included = document.getIncluded();
    // both parent and children recursively included
    assertEquals(3, included.size());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) Test(org.junit.Test)

Example 5 with ResourceIdentifier

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

the class PerTypeIncludeBehaviorTest method includeCyclicParent.

@Test
public void includeCyclicParent() throws Exception {
    h.setParent(h1);
    QuerySpec querySpec = new QuerySpec(HierarchicalTask.class);
    querySpec.includeRelation(Arrays.asList("parent"));
    Document document = mapper.toDocument(toResponse(h1), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship parentRelationship = taskResource.getRelationships().get("parent");
    assertNotNull(parentRelationship);
    assertNotNull(parentRelationship.getSingleData());
    ResourceIdentifier parentResource = parentRelationship.getSingleData().get();
    assertNotNull(h.getId().toString(), parentResource.getId());
    List<Resource> included = document.getIncluded();
    assertEquals(1, included.size());
    assertNotNull(h.getId().toString(), included.get(0).getId());
    Relationship rootParent = included.get(0).getRelationships().get("parent");
    assertTrue(rootParent.getSingleData().isPresent());
    assertNotNull(h1.getId().toString(), rootParent.getSingleData().get().getId());
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) 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