use of io.crnk.core.engine.document.Relationship in project crnk-framework by crnk-project.
the class ClientResourceUpsert method setRelationsField.
@Override
protected void setRelationsField(Object newResource, RegistryEntry registryEntry, Map.Entry<String, Relationship> property, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
Relationship relationship = property.getValue();
if (!relationship.getData().isPresent()) {
ObjectNode links = relationship.getLinks();
if (links != null) {
// create proxy to lazy load relations
String fieldName = property.getKey();
ResourceInformation resourceInformation = registryEntry.getResourceInformation();
ResourceField field = resourceInformation.findRelationshipFieldByName(fieldName);
Class elementType = field.getElementType();
Class collectionClass = field.getType();
JsonNode relatedNode = links.get("related");
if (relatedNode != null) {
String url = null;
if (relatedNode.has(SerializerUtil.HREF)) {
JsonNode hrefNode = relatedNode.get(SerializerUtil.HREF);
if (hrefNode != null) {
url = hrefNode.asText().trim();
}
} else {
url = relatedNode.asText().trim();
}
Object proxy = proxyFactory.createCollectionProxy(elementType, collectionClass, url);
field.getAccessor().setValue(newResource, proxy);
}
}
} else {
// set elements
super.setRelationsField(newResource, registryEntry, property, queryAdapter, parameterProvider);
}
}
use of io.crnk.core.engine.document.Relationship 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.Relationship in project crnk-framework by crnk-project.
the class FieldResourceGetTest method onGivenIncludeRequestFieldResourcesGetShouldHandleIt.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onGivenIncludeRequestFieldResourcesGetShouldHandleIt() throws Exception {
// get repositories
ResourceRepositoryAdapter userRepo = resourceRegistry.getEntry(User.class).getResourceRepository(null);
ResourceRepositoryAdapter projectRepo = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
RelationshipRepositoryAdapter relRepositoryUserToProject = resourceRegistry.getEntry(User.class).getRelationshipRepository("assignedProjects", null);
RelationshipRepositoryAdapter relRepositoryProjectToTask = resourceRegistry.getEntry(Project.class).getRelationshipRepository("tasks", null);
ResourceInformation userInfo = resourceRegistry.getEntry(User.class).getResourceInformation();
ResourceInformation projectInfo = resourceRegistry.getEntry(Project.class).getResourceInformation();
ResourceField includedTaskField = projectInfo.findRelationshipFieldByName("includedTask");
ResourceField assignedProjectsField = userInfo.findRelationshipFieldByName("assignedProjects");
// setup test data
User user = new User();
user.setId(1L);
userRepo.create(user, emptyUserQuery);
Project project = new Project();
project.setId(2L);
projectRepo.create(project, emptyProjectQuery);
Task task = new Task();
task.setId(3L);
taskRepo.create(task, emptyTaskQuery);
relRepositoryUserToProject.setRelations(user, Collections.singletonList(project.getId()), assignedProjectsField, emptyProjectQuery);
relRepositoryProjectToTask.setRelation(project, task.getId(), includedTaskField, emptyTaskQuery);
Map<String, Set<String>> params = new HashMap<String, Set<String>>();
addParams(params, "include[projects]", "includedTask");
QueryParams queryParams = queryParamsBuilder.buildQueryParams(params);
QueryAdapter queryAdapter = new QueryParamsAdapter(projectInfo, queryParams, moduleRegistry);
JsonPath jsonPath = pathBuilder.build("/users/1/assignedProjects");
FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
Response response = sut.handle(jsonPath, queryAdapter, null, null);
// THEN
Assert.assertNotNull(response);
Assert.assertNotNull(response.getDocument().getData());
List<Resource> entityList = ((List<Resource>) response.getDocument().getData().get());
Assert.assertEquals(true, entityList.size() > 0);
Assert.assertEquals("projects", entityList.get(0).getType());
Resource returnedProject = entityList.get(0);
Assert.assertEquals(project.getId().toString(), returnedProject.getId());
Relationship relationship = returnedProject.getRelationships().get("includedTask");
Assert.assertNotNull(relationship);
Assert.assertEquals(task.getId().toString(), relationship.getSingleData().get().getId());
}
use of io.crnk.core.engine.document.Relationship 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.Relationship 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