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;
}
});
}
}
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);
}
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));
}
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);
}
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);
}
Aggregations