Fixed SpecificationsBuilder to support immutable Collections and added a test for it
Signed-off-by: Dominik Herbst <dominik.herbst@bosch-si.com>
This commit is contained in:
@@ -28,7 +28,7 @@ public final class SpecificationsBuilder {
|
||||
* where clause.
|
||||
*
|
||||
* @param specList
|
||||
* all specification wich will combine
|
||||
* all specification which will combine
|
||||
* @return <null> if the given specification list is empty
|
||||
*/
|
||||
public static <T> Specifications<T> combineWithAnd(final List<Specification<T>> specList) {
|
||||
@@ -36,8 +36,7 @@ public final class SpecificationsBuilder {
|
||||
return null;
|
||||
}
|
||||
Specifications<T> specs = Specifications.where(specList.get(0));
|
||||
specList.remove(0);
|
||||
for (final Specification<T> specification : specList) {
|
||||
for (final Specification<T> specification : specList.subList(1, specList.size())) {
|
||||
specs = specs.and(specification);
|
||||
}
|
||||
return specs;
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.jpa.specifications;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.domain.Specifications;
|
||||
import ru.yandex.qatools.allure.annotations.Description;
|
||||
import ru.yandex.qatools.allure.annotations.Features;
|
||||
import ru.yandex.qatools.allure.annotations.Stories;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Expression;
|
||||
import javax.persistence.criteria.Path;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@Features("Unit Tests - Repository")
|
||||
@Stories("Specifications builder")
|
||||
public class SpecificationsBuilderTest {
|
||||
|
||||
@Test
|
||||
@Description("Test the combination of specs on an empty list which returns null")
|
||||
public void combineWithAndEmptyList() {
|
||||
List<Specification<Object>> specList = Collections.emptyList();
|
||||
assertThat(SpecificationsBuilder.combineWithAnd(specList)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Test the combination of specs on an immutable list with one entry")
|
||||
public void combineWithAndSingleImmutableList() {
|
||||
Specification<Object> spec = (root, query, cb) -> cb.equal(root.get("field1"), "testValue");
|
||||
List<Specification<Object>> specList = Collections.singletonList(spec);
|
||||
Specifications<Object> specifications = SpecificationsBuilder.combineWithAnd(specList);
|
||||
assertThat(specifications).as("Specifications").isNotNull();
|
||||
|
||||
// mocks to call toPredicate on specifications
|
||||
CriteriaBuilder criteriaBuilder = mock(CriteriaBuilder.class);
|
||||
final Path field1 = mock(Path.class);
|
||||
final Predicate equalPredicate = mock(Predicate.class);
|
||||
final CriteriaQuery<Object[]> query = mock(CriteriaQuery.class);
|
||||
final Root<Object> root = mock(Root.class);
|
||||
|
||||
when(criteriaBuilder.equal(any(Expression.class), anyString())).thenReturn(equalPredicate);
|
||||
when(root.get("field1")).thenReturn(field1);
|
||||
|
||||
Predicate predicate = specifications.toPredicate(root, query, criteriaBuilder);
|
||||
|
||||
assertThat(predicate).isEqualTo(equalPredicate);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Description("Test the combination of specs on a list with multiple entries")
|
||||
public void combineWithAndList() {
|
||||
Specification<Object> spec1 = (root, query, cb) -> cb.equal(root.get("field1"), "testValue1");
|
||||
Specification<Object> spec2 = (root, query, cb) -> cb.equal(root.get("field2"), "testValue2");
|
||||
|
||||
List<Specification<Object>> specList = new ArrayList<>(2);
|
||||
specList.add(spec1);
|
||||
specList.add(spec2);
|
||||
|
||||
Specifications<Object> specifications = SpecificationsBuilder.combineWithAnd(specList);
|
||||
assertThat(specifications).as("Specifications").isNotNull();
|
||||
|
||||
// mocks to call toPredicate on specifications
|
||||
CriteriaBuilder criteriaBuilder = mock(CriteriaBuilder.class);
|
||||
final Path field1 = mock(Path.class);
|
||||
final Path field2 = mock(Path.class);
|
||||
final Predicate equalPredicate1 = mock(Predicate.class);
|
||||
final Predicate equalPredicate2 = mock(Predicate.class);
|
||||
final Predicate combinedPredicate = mock(Predicate.class);
|
||||
final CriteriaQuery<Object[]> query = mock(CriteriaQuery.class);
|
||||
final Root<Object> root = mock(Root.class);
|
||||
|
||||
when(criteriaBuilder.equal(any(Path.class), eq("testValue1"))).thenReturn(equalPredicate1);
|
||||
when(criteriaBuilder.equal(any(Path.class), eq("testValue2"))).thenReturn(equalPredicate2);
|
||||
when(criteriaBuilder.and(eq(equalPredicate1), eq(equalPredicate2))).thenReturn(combinedPredicate);
|
||||
when(root.get("field1")).thenReturn(field1);
|
||||
when(root.get("field2")).thenReturn(field2);
|
||||
|
||||
Predicate predicate = specifications.toPredicate(root, query, criteriaBuilder);
|
||||
|
||||
assertThat(predicate).as("Combined predicate").isEqualTo(combinedPredicate);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user