Search in sources :

Example 86 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class DtoMappingTest method setupModule.

@Override
protected void setupModule(JpaModule module, boolean server) {
    super.setupModule(module, server);
    if (server) {
        EntityManager entityManager = module.getEntityManager();
        QuerydslExpressionFactory<QTestEntity> basicComputedValueFactory = new QuerydslExpressionFactory<QTestEntity>() {

            @Override
            public Expression<String> getExpression(QTestEntity parent, JPAQuery<?> jpaQuery) {
                return parent.stringValue.upper();
            }
        };
        QuerydslExpressionFactory<QTestEntity> complexComputedValueFactory = new QuerydslExpressionFactory<QTestEntity>() {

            @Override
            public Expression<Long> getExpression(QTestEntity parent, JPAQuery<?> jpaQuery) {
                QTestEntity root = QTestEntity.testEntity;
                QTestEntity sub = new QTestEntity("subquery");
                return JPAExpressions.select(sub.id.count()).from(sub).where(sub.id.lt(root.id));
            }
        };
        QuerydslQueryFactory queryFactory = (QuerydslQueryFactory) module.getQueryFactory();
        queryFactory.registerComputedAttribute(TestEntity.class, TestDTO.ATTR_COMPUTED_UPPER_STRING_VALUE, String.class, basicComputedValueFactory);
        queryFactory.registerComputedAttribute(TestEntity.class, TestDTO.ATTR_COMPUTED_NUMBER_OF_SMALLER_IDS, Long.class, complexComputedValueFactory);
        module.addRepository(JpaRepositoryConfig.builder(TestEntity.class, TestDTO.class, new TestDTOMapper(entityManager)).build());
        module.addRepository(JpaRepositoryConfig.builder(RelatedEntity.class, RelatedDTO.class, new RelatedDTOMapper(entityManager)).build());
        module.addFilter(new JpaRepositoryFilterBase() {

            @Override
            public <T> JpaQuery<T> filterQuery(Object repository, QuerySpec querySpec, JpaQuery<T> query) {
                query.setDistinct(true);
                return query;
            }
        });
    }
}
Also used : QuerydslQueryFactory(io.crnk.jpa.query.querydsl.QuerydslQueryFactory) JPAQuery(com.querydsl.jpa.impl.JPAQuery) JpaQuery(io.crnk.jpa.query.JpaQuery) QuerydslExpressionFactory(io.crnk.jpa.query.querydsl.QuerydslExpressionFactory) EntityManager(javax.persistence.EntityManager) QTestEntity(io.crnk.jpa.model.QTestEntity) JpaRepositoryFilterBase(io.crnk.jpa.JpaRepositoryFilterBase) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 87 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class DefaultQuerySpecDeserializerTestBase method testFilterByMany.

@Test
public void testFilterByMany() {
    QuerySpec expectedSpec = new QuerySpec(Task.class);
    expectedSpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, new HashSet<>(Arrays.asList("value1", "value2"))));
    Map<String, Set<String>> params = new HashMap<>();
    params.put("filter[tasks][name][EQ]", new HashSet<>(Arrays.asList("value1", "value2")));
    QuerySpec actualSpec = deserializer.deserialize(taskInformation, params);
    Assert.assertEquals(expectedSpec, actualSpec);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) HashSet(java.util.HashSet) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 88 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class DefaultQuerySpecDeserializerTestBase method testIngoreParseException.

@Test
public void testIngoreParseException() {
    Map<String, Set<String>> params = new HashMap<>();
    add(params, "filter[id]", "NotAnId");
    deserializer.setIgnoreParseExceptions(true);
    QuerySpec querySpec = deserializer.deserialize(taskInformation, params);
    Assert.assertEquals(Task.class, querySpec.getResourceClass());
    Assert.assertEquals(Arrays.asList("id"), querySpec.getFilters().get(0).getAttributePath());
    Assert.assertEquals("NotAnId", querySpec.getFilters().get(0).getValue());
    Assert.assertNull(querySpec.getRelatedSpecs().get(Project.class));
}
Also used : Project(io.crnk.core.mock.models.Project) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 89 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class DefaultQuerySpecDeserializerTestBase method testFilterByOne.

@Test
public void testFilterByOne() {
    QuerySpec expectedSpec = new QuerySpec(Task.class);
    expectedSpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "value"));
    Map<String, Set<String>> params = new HashMap<>();
    add(params, "filter[tasks][name][EQ]", "value");
    QuerySpec actualSpec = deserializer.deserialize(taskInformation, params);
    Assert.assertEquals(expectedSpec, actualSpec);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Example 90 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class DefaultQuerySpecDeserializerTestBase method testPaging.

@Test
public void testPaging() {
    QuerySpec expectedSpec = new QuerySpec(Task.class);
    expectedSpec.setPagingSpec(new OffsetLimitPagingSpec(1L, 2L));
    Map<String, Set<String>> params = new HashMap<>();
    add(params, "page[offset]", "1");
    add(params, "page[limit]", "2");
    QuerySpec actualSpec = deserializer.deserialize(taskInformation, params);
    Assert.assertEquals(expectedSpec, actualSpec);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) OffsetLimitPagingSpec(io.crnk.core.queryspec.pagingspec.OffsetLimitPagingSpec) HashMap(java.util.HashMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractQuerySpecTest(io.crnk.core.queryspec.AbstractQuerySpecTest) Test(org.junit.Test)

Aggregations

QuerySpec (io.crnk.core.queryspec.QuerySpec)306 Test (org.junit.Test)233 FilterSpec (io.crnk.core.queryspec.FilterSpec)51 Document (io.crnk.core.engine.document.Document)45 Resource (io.crnk.core.engine.document.Resource)43 Set (java.util.Set)39 HashMap (java.util.HashMap)37 HashSet (java.util.HashSet)36 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)34 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)32 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)32 Task (io.crnk.test.mock.models.Task)32 Project (io.crnk.core.mock.models.Project)28 Relationship (io.crnk.core.engine.document.Relationship)26 Task (io.crnk.core.mock.models.Task)26 TestEntity (io.crnk.jpa.model.TestEntity)26 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)25 Serializable (java.io.Serializable)24 RelatedEntity (io.crnk.jpa.model.RelatedEntity)21 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)20