use of io.crnk.core.engine.query.QueryAdapter 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.query.QueryAdapter in project crnk-framework by crnk-project.
the class AbstractIncludeBehaviorTest method setup.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setup() {
super.setup();
ResourceRepositoryAdapter hierarchicalTaskRepository = resourceRegistry.findEntry(HierarchicalTask.class).getResourceRepository(null);
h = new HierarchicalTask();
h.setId(1L);
h.setName("");
h0 = new HierarchicalTask();
h0.setId(2L);
h0.setName("0");
h0.setParent(h);
h1 = new HierarchicalTask();
h1.setId(3L);
h1.setName("1");
h1.setParent(h);
h11 = new HierarchicalTask();
h11.setId(4L);
h11.setName("11");
h11.setParent(h1);
h.setChildren(Arrays.asList(h0, h1));
h0.setChildren(new ArrayList<HierarchicalTask>());
h1.setChildren(Arrays.asList(h11));
h11.setChildren(new ArrayList<HierarchicalTask>());
QueryAdapter emptyQueryAdapter = new QuerySpecAdapter(new QuerySpec(HierarchicalTask.class), resourceRegistry);
hierarchicalTaskRepository.create(h, emptyQueryAdapter);
hierarchicalTaskRepository.create(h0, emptyQueryAdapter);
hierarchicalTaskRepository.create(h1, emptyQueryAdapter);
hierarchicalTaskRepository.create(h11, emptyQueryAdapter);
}
use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class RelationshipRepositoryAdapter method setRelation.
@SuppressWarnings("rawtypes")
public JsonApiResponse setRelation(T source, J targetId, ResourceField field, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
Object source = request.getEntity();
Serializable targetId = request.getId();
ResourceField field = request.getRelationshipField();
QueryAdapter queryAdapter = request.getQueryAdapter();
if (isAnnotated) {
((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName(), queryAdapter);
} else if (relationshipRepository instanceof RelationshipRepositoryV2) {
((RelationshipRepositoryV2) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
} else {
((RelationshipRepository) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.PATCH, source, queryAdapter, Arrays.asList(targetId), field);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class RelationshipRepositoryAdapter method addRelations.
@SuppressWarnings("rawtypes")
public JsonApiResponse addRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
Object source = request.getEntity();
Iterable<?> targetIds = request.getIds();
ResourceField field = request.getRelationshipField();
QueryAdapter queryAdapter = request.getQueryAdapter();
if (isAnnotated) {
((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
} else if (relationshipRepository instanceof RelationshipRepositoryV2) {
((RelationshipRepositoryV2) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
} else {
((RelationshipRepository) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.POST, source, queryAdapter, targetIds, field);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.
the class ResourceRepositoryAdapter method delete.
public JsonApiResponse delete(I id, QueryAdapter queryAdapter) {
RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {
@SuppressWarnings("rawtypes")
@Override
protected JsonApiResponse invoke(RepositoryFilterContext context) {
RepositoryRequestSpec request = context.getRequest();
QueryAdapter queryAdapter = request.getQueryAdapter();
Serializable id = request.getId();
if (isAnnotated) {
((AnnotatedResourceRepositoryAdapter) resourceRepository).delete(id, queryAdapter);
} else if (resourceRepository instanceof ResourceRepositoryV2) {
((ResourceRepositoryV2) resourceRepository).delete(id);
} else {
((ResourceRepository) resourceRepository).delete(id);
}
return new JsonApiResponse();
}
};
RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forDelete(moduleRegistry, resourceInformation, queryAdapter, id);
return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Aggregations