use of io.crnk.jpa.JpaRepositoryFilterBase 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.jpa.JpaRepositoryFilterBase in project crnk-framework by crnk-project.
the class JpaListenerTest method testaAddRemove.
@Test
public void testaAddRemove() throws InstantiationException, IllegalAccessException {
JpaRepositoryFilterBase filter = Mockito.spy(new JpaRepositoryFilterBase());
module.addFilter(filter);
Assert.assertEquals(1, module.getFilters().size());
module.removeFilter(filter);
Assert.assertEquals(0, module.getFilters().size());
}
use of io.crnk.jpa.JpaRepositoryFilterBase in project crnk-framework by crnk-project.
the class JpaListenerTest method test.
@SuppressWarnings("unchecked")
@Test
public void test() throws InstantiationException, IllegalAccessException {
JpaRepositoryFilterBase filter = Mockito.spy(new JpaRepositoryFilterBase());
module.addFilter(filter);
QuerySpec querySpec = new QuerySpec(TestEntity.class);
ResourceList<TestEntity> list = repo.findAll(querySpec);
Assert.assertEquals(5, list.size());
Mockito.verify(filter, Mockito.times(1)).filterQuerySpec(Mockito.eq(repo), Mockito.eq(querySpec));
Mockito.verify(filter, Mockito.times(1)).filterResults(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.eq(list));
Mockito.verify(filter, Mockito.times(1)).filterExecutor(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.any(JpaQueryExecutor.class));
Mockito.verify(filter, Mockito.times(1)).filterTuples(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.anyList());
Mockito.verify(filter, Mockito.times(1)).filterQuery(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.any(JpaQuery.class));
}
Aggregations