Search in sources :

Example 46 with FilterSpec

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());
}
Also used : TaskResource(io.crnk.activiti.resource.TaskResource) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Test(org.junit.Test)

Example 47 with FilterSpec

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");
    }
}
Also used : BadRequestException(io.crnk.core.exception.BadRequestException) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Example 48 with FilterSpec

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());
}
Also used : RelatedEntity(io.crnk.jpa.model.RelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 49 with FilterSpec

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");
}
Also used : Task(io.crnk.test.mock.models.Task) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) Span(org.springframework.cloud.sleuth.Span) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 50 with FilterSpec

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;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) Collection(java.util.Collection) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Aggregations

FilterSpec (io.crnk.core.queryspec.FilterSpec)56 QuerySpec (io.crnk.core.queryspec.QuerySpec)51 Test (org.junit.Test)40 HashSet (java.util.HashSet)14 Set (java.util.Set)13 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)12 HashMap (java.util.HashMap)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)9 TaskResource (io.crnk.activiti.resource.TaskResource)8 MetaResource (io.crnk.meta.model.resource.MetaResource)8 ScheduleApprovalProcessInstance (io.crnk.activiti.example.model.ScheduleApprovalProcessInstance)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)5 MetaAttribute (io.crnk.meta.model.MetaAttribute)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4 ResourceList (io.crnk.core.resource.list.ResourceList)3 RelatedEntity (io.crnk.jpa.model.RelatedEntity)3 ArrayList (java.util.ArrayList)3 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)2 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)2 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)2