use of io.crnk.core.queryspec.FilterSpec in project crnk-framework by crnk-project.
the class TaskResourceRepositoryTest method checkLEPriority.
@Test
public void checkLEPriority() {
QuerySpec querySpec = new QuerySpec(ApproveTask.class);
querySpec.addFilter(new FilterSpec(Arrays.asList("priority"), FilterOperator.LE, task.getPriority() + 1));
Assert.assertEquals(1, taskRepository.findAll(querySpec).size());
querySpec = new QuerySpec(TaskResource.class);
querySpec.addFilter(new FilterSpec(Arrays.asList("priority"), FilterOperator.LE, task.getPriority() - 1));
Assert.assertEquals(0, taskRepository.findAll(querySpec).size());
}
use of io.crnk.core.queryspec.FilterSpec in project crnk-framework by crnk-project.
the class ActivitiRepositoryBase method checkFilter.
protected void checkFilter(Object resource, boolean applyAsDefault) {
QuerySpec enforcementSpec = new QuerySpec((String) null);
for (FilterSpec baseFilter : baseFilters) {
enforcementSpec.addFilter(baseFilter);
// apply as default if possible
List<String> attributePath = baseFilter.getAttributePath();
if (attributePath.size() == 1 && baseFilter.getOperator() == FilterOperator.EQ) {
String attributeName = baseFilter.getAttributePath().get(0);
Object actualValue = PropertyUtils.getProperty(resource, attributeName);
Object expectedValue = baseFilter.getValue();
if (actualValue == null) {
PropertyUtils.setProperty(resource, attributeName, expectedValue);
}
}
}
if (enforcementSpec.apply(Arrays.asList(resource)).isEmpty()) {
throw new BadRequestException("resource does not belong to this repository");
}
}
use of io.crnk.core.queryspec.FilterSpec in project crnk-framework by crnk-project.
the class JpaRelationshipRepositoryTestBase method testFindManyTargetWithUnmatchedFilter.
@Test
public void testFindManyTargetWithUnmatchedFilter() throws InstantiationException, IllegalAccessException {
QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
querySpec.addFilter(new FilterSpec(Arrays.asList("id"), FilterOperator.EQ, 9999));
Iterable<RelatedEntity> relatedEntities = repo.findManyTargets(1L, TestEntity.ATTR_oneRelatedValue, querySpec);
Iterator<RelatedEntity> iterator = relatedEntities.iterator();
Assert.assertFalse(iterator.hasNext());
}
use of io.crnk.core.queryspec.FilterSpec in project crnk-framework by crnk-project.
the class SleuthModuleTest method testFindAll.
@Test
public void testFindAll() {
Task task = new Task();
task.setId(13L);
task.setName("myTask");
QuerySpec querySpec = new QuerySpec(Task.class);
querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "doe"));
taskRepo.findAll(querySpec);
// check client call and link span
ArgumentCaptor<Span> clientSpanCaptor = ArgumentCaptor.forClass(Span.class);
List<Span> clientSpans = clientSpanCaptor.getAllValues();
Span callSpan = clientSpans.get(0);
Assert.assertEquals("get", callSpan.getName());
Assert.assertTrue(callSpan.toString().contains("\"cs\""));
Assert.assertTrue(callSpan.toString().contains("\"cr\""));
// check server local span
Assert.assertEquals(1, reportedSpans.spans.size());
Span repositorySpan = reportedSpans.spans.get(0);
Assert.assertEquals("crnk:get:/tasks", repositorySpan.getName());
Assert.assertTrue(repositorySpan.toString().contains("\"lc\""));
assertBinaryAnnotation(repositorySpan, "lc", "crnk");
assertBinaryAnnotation(repositorySpan, "crnk.query", "?filter[tasks][name][EQ]=doe");
assertBinaryAnnotation(repositorySpan, "crnk.results", "0");
assertBinaryAnnotation(repositorySpan, "crnk.status", "OK");
}
use of io.crnk.core.queryspec.FilterSpec in project crnk-framework by crnk-project.
the class GetFromOppositeStrategy method findTargets.
@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
RegistryEntry sourceEntry = context.getSourceEntry();
ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
RegistryEntry targetEntry = context.getTargetEntry(field);
ResourceInformation targetInformation = targetEntry.getResourceInformation();
ResourceField oppositeField = Objects.requireNonNull(targetInformation.findFieldByUnderlyingName(field.getOppositeName()));
QuerySpec idQuerySpec = querySpec.duplicate();
idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeField.getUnderlyingName(), sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
idQuerySpec.includeRelation(Arrays.asList(oppositeField.getUnderlyingName()));
ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
JsonApiResponse response = targetAdapter.findAll(context.createQueryAdapter(idQuerySpec));
Collection<D> results = (Collection<D>) response.getEntity();
MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {
@Override
protected List<D> newList() {
return new DefaultResourceList<>();
}
};
Set<I> sourceIdSet = new HashSet<>();
for (I sourceId : sourceIds) {
sourceIdSet.add(sourceId);
}
for (D result : results) {
handleTarget(bulkResult, result, sourceIdSet, oppositeField, sourceInformation);
}
return bulkResult;
}
Aggregations