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());
}
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();
}
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());
}
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());
}
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());
}
Aggregations