Commit 4cc0a535 authored by Matija Obreza's avatar Matija Obreza

Merge branch '38-filters-buildpredicate-returns-null' into 'master'

Resolve "Filters buildPredicate() returns null"

Closes #38

See merge request genesys-pgr/application-blocks!71
parents 1c924ed7 cdbb5f44
......@@ -39,6 +39,8 @@
<version>1.1.3</version>
<executions>
<execution>
<id>generate-source-entities</id>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
......@@ -51,6 +53,21 @@
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
<execution>
<id>generate-test-entities</id>
<phase>generate-test-sources</phase>
<goals>
<goal>test-process</goal>
</goals>
<configuration>
<includes>
<!-- List packages to be processed -->
<include>org.genesys.blocks.model</include>
</includes>
<outputDirectory>target/generated-test-sources/querydsl</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
......
......@@ -39,6 +39,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.CollectionPathBase;
......@@ -84,7 +85,8 @@ public abstract class SuperModelFilter<T extends SuperModelFilter<T, R>, R> {
* @return the predicate
*/
public Predicate buildPredicate() {
return ExpressionUtils.allOf(collectPredicates());
Predicate collected = ExpressionUtils.allOf(collectPredicates());
return collected == null ? new BooleanBuilder() : collected;
}
/**
......
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.model;
import java.util.List;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.VersionedModelFilter;
import com.querydsl.core.types.Predicate;
public class VersionedEntityFilter extends VersionedModelFilter<VersionedEntityFilter, VersionedEntity> {
public StringFilter name;
public List<Predicate> collectPredicates() {
return collectPredicates(QVersionedEntity.versionedEntity);
}
public List<Predicate> collectPredicates(QVersionedEntity versionedEntity) {
final List<Predicate> predicates = super.collectPredicates(versionedEntity, versionedEntity._super._super);
if (name != null) {
predicates.add(name.buildQuery(versionedEntity.name));
}
return predicates;
}
}
......@@ -17,10 +17,11 @@ package org.genesys.blocks.persistence;
import org.genesys.blocks.model.VersionedEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
/**
* The Interface VersionedEntityRepository.
*/
public interface VersionedEntityRepository extends JpaRepository<VersionedEntity, Long> {
public interface VersionedEntityRepository extends JpaRepository<VersionedEntity, Long>, QuerydslPredicateExecutor<VersionedEntity> {
}
......@@ -15,14 +15,15 @@
*/
package org.genesys.blocks.tests.model;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import org.genesys.blocks.model.VersionedEntity;
import org.genesys.blocks.model.VersionedEntityFilter;
import org.genesys.blocks.tests.ServiceTest;
import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
/**
......@@ -75,6 +76,26 @@ public class VersionedModelTest extends ServiceTest {
assertThat(entity.isActive(), is(false));
}
@Test
public void testEmptyFilter() {
VersionedEntityFilter filter = new VersionedEntityFilter();
Page<VersionedEntity> page = versionedEntityRepository.findAll(filter.buildPredicate(), PageRequest.of(0, 10));
assertThat(page, not(nullValue()));
}
@Test
public void testStringFilter() {
VersionedEntity entity = versionedEntityRepository.save(createEntity());
VersionedEntityFilter filter = new VersionedEntityFilter();
filter.id().add(entity.getId());
Page<VersionedEntity> page = versionedEntityRepository.findAll(filter.buildPredicate(), PageRequest.of(0, 10));
assertThat(page, not(nullValue()));
assertThat(page.getContent().size(), is(1));
assertThat(page.getContent().get(0), is(entity));
}
/**
* Creates the entity.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment