From 7a59882396170a8922c4a6e2f637d1b339a6b140 Mon Sep 17 00:00:00 2001 From: Artsiom Viahera Date: Fri, 21 Jun 2024 14:21:09 +0200 Subject: [PATCH] #7 - Introduced list predicates --- .../avegera/predicate4j/Predicates.java | 25 + .../avegera/predicate4j/api/WhereList.java | 18 + .../avegera/predicate4j/api/WhereType.java | 3 + .../predicate4j/impl/WhereListImpl.java | 45 ++ .../predicate4j/impl/WhereTypeImpl.java | 7 + .../github/avegera/predicate4j/WhereTest.java | 618 ++++++++++++++++++ .../avegera/predicate4j/test/Customer.java | 23 + .../avegera/predicate4j/test/Product.java | 21 + 8 files changed, 760 insertions(+) create mode 100644 src/main/java/io/github/avegera/predicate4j/api/WhereList.java create mode 100644 src/main/java/io/github/avegera/predicate4j/impl/WhereListImpl.java create mode 100644 src/test/java/io/github/avegera/predicate4j/test/Customer.java create mode 100644 src/test/java/io/github/avegera/predicate4j/test/Product.java diff --git a/src/main/java/io/github/avegera/predicate4j/Predicates.java b/src/main/java/io/github/avegera/predicate4j/Predicates.java index 47a7e39..d84a5c1 100644 --- a/src/main/java/io/github/avegera/predicate4j/Predicates.java +++ b/src/main/java/io/github/avegera/predicate4j/Predicates.java @@ -1,5 +1,6 @@ package io.github.avegera.predicate4j; +import java.util.Collection; import java.util.Objects; import java.util.function.Predicate; @@ -59,4 +60,28 @@ public static Predicate isFalse() { public static Predicate notFalse() { return bool -> bool == null || bool; } + + public static > Predicate isEmpty() { + return collection -> collection == null || collection.isEmpty(); + } + + public static > Predicate notEmpty() { + return collection -> collection != null && !collection.isEmpty(); + } + + public static > Predicate hasSize(int size) { + return collection -> (collection != null ? collection.size() : 0) == size; + } + + public static > Predicate notHaveSize(int size) { + return collection -> (collection != null ? collection.size() : 0) != size; + } + + public static , R> Predicate contains(R element) { + return collection -> collection != null && collection.contains(element); + } + + public static , R> Predicate notContain(R element) { + return collection -> collection == null || !collection.contains(element); + } } \ No newline at end of file diff --git a/src/main/java/io/github/avegera/predicate4j/api/WhereList.java b/src/main/java/io/github/avegera/predicate4j/api/WhereList.java new file mode 100644 index 0000000..a697373 --- /dev/null +++ b/src/main/java/io/github/avegera/predicate4j/api/WhereList.java @@ -0,0 +1,18 @@ +package io.github.avegera.predicate4j.api; + +import java.util.List; + +public interface WhereList extends WhereObject> { + + RichPredicate isEmpty(); + + RichPredicate notEmpty(); + + RichPredicate hasSize(int size); + + RichPredicate notHaveSize(int size); + + RichPredicate contains(R element); + + RichPredicate notContain(R element); +} \ No newline at end of file diff --git a/src/main/java/io/github/avegera/predicate4j/api/WhereType.java b/src/main/java/io/github/avegera/predicate4j/api/WhereType.java index 721267e..d185067 100644 --- a/src/main/java/io/github/avegera/predicate4j/api/WhereType.java +++ b/src/main/java/io/github/avegera/predicate4j/api/WhereType.java @@ -1,8 +1,11 @@ package io.github.avegera.predicate4j.api; +import java.util.List; import java.util.function.Function; public interface WhereType { WhereBoolean booleanValue(Function mapper); + + WhereList list(Function> mapper); } \ No newline at end of file diff --git a/src/main/java/io/github/avegera/predicate4j/impl/WhereListImpl.java b/src/main/java/io/github/avegera/predicate4j/impl/WhereListImpl.java new file mode 100644 index 0000000..092a20e --- /dev/null +++ b/src/main/java/io/github/avegera/predicate4j/impl/WhereListImpl.java @@ -0,0 +1,45 @@ +package io.github.avegera.predicate4j.impl; + +import io.github.avegera.predicate4j.Predicates; +import io.github.avegera.predicate4j.api.RichPredicate; +import io.github.avegera.predicate4j.api.WhereList; + +import java.util.List; +import java.util.function.Function; + +public class WhereListImpl extends WhereObjectImpl> implements WhereList { + + public WhereListImpl(Function> mapper) { + super(mapper); + } + + @Override + public RichPredicate isEmpty() { + return getPredicate(Predicates.isEmpty()); + } + + @Override + public RichPredicate notEmpty() { + return getPredicate(Predicates.notEmpty()); + } + + @Override + public RichPredicate hasSize(int size) { + return getPredicate(Predicates.hasSize(size)); + } + + @Override + public RichPredicate notHaveSize(int size) { + return getPredicate(Predicates.notHaveSize(size)); + } + + @Override + public RichPredicate contains(R element) { + return getPredicate(Predicates.contains(element)); + } + + @Override + public RichPredicate notContain(R element) { + return getPredicate(Predicates.notContain(element)); + } +} \ No newline at end of file diff --git a/src/main/java/io/github/avegera/predicate4j/impl/WhereTypeImpl.java b/src/main/java/io/github/avegera/predicate4j/impl/WhereTypeImpl.java index ef5b888..4a99f9c 100644 --- a/src/main/java/io/github/avegera/predicate4j/impl/WhereTypeImpl.java +++ b/src/main/java/io/github/avegera/predicate4j/impl/WhereTypeImpl.java @@ -1,8 +1,10 @@ package io.github.avegera.predicate4j.impl; import io.github.avegera.predicate4j.api.WhereBoolean; +import io.github.avegera.predicate4j.api.WhereList; import io.github.avegera.predicate4j.api.WhereType; +import java.util.List; import java.util.function.Function; public class WhereTypeImpl implements WhereType { @@ -11,4 +13,9 @@ public class WhereTypeImpl implements WhereType { public WhereBoolean booleanValue(Function mapper) { return new WhereBooleanImpl<>(mapper); } + + @Override + public WhereList list(Function> mapper) { + return new WhereListImpl<>(mapper); + } } \ No newline at end of file diff --git a/src/test/java/io/github/avegera/predicate4j/WhereTest.java b/src/test/java/io/github/avegera/predicate4j/WhereTest.java index 41e3a0e..c194305 100644 --- a/src/test/java/io/github/avegera/predicate4j/WhereTest.java +++ b/src/test/java/io/github/avegera/predicate4j/WhereTest.java @@ -1,15 +1,20 @@ package io.github.avegera.predicate4j; import io.github.avegera.predicate4j.test.Address; +import io.github.avegera.predicate4j.test.Customer; import io.github.avegera.predicate4j.test.Organization; import io.github.avegera.predicate4j.test.User; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import java.util.function.Predicate; import static io.github.avegera.predicate4j.Where.where; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; class WhereTest { @@ -702,6 +707,619 @@ void mappedValueIsFalse() { } } + @Nested + @DisplayName("Predicate from method where().list(mapper)") + class WhereListImpl { + + @Nested + @DisplayName(".isEqualTo(value)") + class IsEqualTo { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is equal to provided list") + void mappedListIsEqualToProvidedList() { + List roles = asList("Admin", "User"); + Predicate predicate = where().list(Customer::roles).isEqualTo(roles); + assertThat(predicate).accepts(new Customer(roles, null)); + } + + @Test + @DisplayName("mapped list is equal to provided list with different list implementations") + void mappedListIsEqualToProvidedListWithDifferentListImplementations() { + List roles = new LinkedList<>(); + roles.add("Admin"); + roles.add("User"); + Predicate predicate = where().list(Customer::roles).isEqualTo(roles); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and provided list is null") + void mappedListIsNullAndProvidedListIsNull() { + Predicate predicate = where().list(Customer::roles).isEqualTo(null); + assertThat(predicate).accepts(new Customer(null, null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is not equal to provided list") + void mappedListIsNotEqualToProvidedList() { + Predicate predicate = where().list(Customer::roles).isEqualTo(asList("Manager", "SuperAdmin")); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is not empty and provided list is null") + void mappedListIsNotEmptyAndProvidedListIsNull() { + Predicate predicate = where().list(Customer::roles).isEqualTo(null); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is not empty and provided list is empty") + void mappedListIsNotEmptyAndProvidedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).isEqualTo(new ArrayList<>()); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and provided list is not empty") + void mappedListIsNullAndProvidedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).isEqualTo(asList("Admin", "User")); + assertThat(predicate).rejects(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is empty and provided value is not empty") + void mappedListIsEmptyAndProvidedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).isEqualTo(asList("Admin", "User")); + assertThat(predicate).rejects(new Customer(new ArrayList<>(), null)); + } + } + } + + @Nested + @DisplayName(".notEqualTo(value)") + class NotEqualTo { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is not equal to provided list") + void mappedListIsNotEqualToProvidedList() { + Predicate predicate = where().list(Customer::roles).notEqualTo(asList("Manager", "SuperAdmin")); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is not empty and provided list is null") + void mappedListIsNotEmptyAndProvidedListIsNull() { + Predicate predicate = where().list(Customer::roles).notEqualTo(null); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is not empty and provided list is empty") + void mappedListIsNotEmptyAndProvidedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).notEqualTo(new ArrayList<>()); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and provided list is not empty") + void mappedListIsNullAndProvidedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).notEqualTo(asList("Admin", "User")); + assertThat(predicate).accepts(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is empty and provided value is not empty") + void mappedListIsEmptyAndProvidedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).notEqualTo(asList("Admin", "User")); + assertThat(predicate).accepts(new Customer(new ArrayList<>(), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is equal to provided list") + void mappedListIsEqualToProvidedList() { + List roles = asList("Admin", "User"); + Predicate predicate = where().list(Customer::roles).notEqualTo(roles); + assertThat(predicate).rejects(new Customer(roles, null)); + } + + @Test + @DisplayName("mapped list is equal to provided list with different list implementations") + void mappedListIsEqualToProvidedListWithDifferentListImplementations() { + List roles = new LinkedList<>(); + roles.add("Admin"); + roles.add("User"); + Predicate predicate = where().list(Customer::roles).notEqualTo(roles); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and provided list is null") + void mappedListIsNullAndProvidedListIsNull() { + Predicate predicate = where().list(Customer::roles).notEqualTo(null); + assertThat(predicate).rejects(new Customer(null, null)); + } + } + } + + @Nested + @DisplayName(".isNull()") + class IsNull { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is null") + void mappedListIsNull() { + Predicate predicate = where().list(Customer::roles).isNull(); + assertThat(predicate).accepts(new Customer(null, null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is empty") + void mappedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).isNull(); + assertThat(predicate).rejects(new Customer(new ArrayList<>(), null)); + } + + @Test + @DisplayName("mapped list is not empty") + void mappedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).isNull(); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + } + } + + @Nested + @DisplayName(".notNull()") + class NotNull { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is empty") + void mappedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).notNull(); + assertThat(predicate).accepts(new Customer(new ArrayList<>(), null)); + } + + @Test + @DisplayName("mapped list is not empty") + void mappedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).notNull(); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is null") + void mappedListIsNull() { + Predicate predicate = where().list(Customer::roles).notNull(); + assertThat(predicate).rejects(new Customer(null, null)); + } + } + } + + @Nested + @DisplayName(".isInstanceOf(clazz)") + class IsInstanceOf { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is instance of provided class") + void mappedListIsInstanceOfProvidedClass() { + List list = new ArrayList<>(); + list.add("Admin"); + list.add("User"); + Predicate predicate = where().list(Customer::roles).isInstanceOf(ArrayList.class); + assertThat(predicate).accepts(new Customer(list, null)); + } + + @Test + @DisplayName("mapped list is null and provided class is null") + void mappedListIsNullAndProvidedClassIsNull() { + Predicate predicate = where().list(Customer::roles).isInstanceOf(null); + assertThat(predicate).rejects(new Customer(null, null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is not instance of provided class") + void mappedListIsNotInstanceOfProvidedClass() { + Predicate predicate = where().list(Customer::roles).isInstanceOf(LinkedList.class); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and provided class is not null") + void mappedListIsNullAndProvidedClassIsNotNull() { + Predicate predicate = where().list(Customer::roles).isInstanceOf(List.class); + assertThat(predicate).rejects(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is not null and provided class is null") + void mappedListIsNotNullAndProvidedClassIsNull() { + Predicate predicate = where().list(Customer::roles).isInstanceOf(null); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and provided class is null") + void mappedListIsNullAndProvidedClassIsNull() { + Predicate predicate = where().list(Customer::roles).isInstanceOf(null); + assertThat(predicate).rejects(new Customer(null, null)); + } + } + } + + @Nested + @DisplayName(".notInstanceOf(clazz)") + class NotInstanceOf { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is not instance of provided class") + void mappedListIsNotInstanceOfProvidedClass() { + Predicate predicate = where().list(Customer::roles).notInstanceOf(LinkedList.class); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is not null and provided class is null") + void mappedListIsNotNullAndProvidedClassIsNull() { + Predicate predicate = where().list(Customer::roles).notInstanceOf(null); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and provided class is not null") + void mappedListIsNullAndProvidedClassIsNotNull() { + Predicate predicate = where().list(Customer::roles).notInstanceOf(ArrayList.class); + assertThat(predicate).accepts(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is null and provided class is null") + void mappedListIsNullAndProvidedClassIsNull() { + Predicate predicate = where().list(Customer::roles).notInstanceOf(null); + assertThat(predicate).accepts(new Customer(null, null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is instance of provided class") + void mappedListIsInstanceOfProvidedClass() { + List list = new ArrayList<>(); + list.add("Admin"); + list.add("User"); + Predicate predicate = where().list(Customer::roles).notInstanceOf(ArrayList.class); + assertThat(predicate).rejects(new Customer(list, null)); + } + } + } + + + @Nested + @DisplayName(".isEmpty()") + class IsEmpty { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is null") + void mappedListIsNull() { + Predicate predicate = where().list(Customer::roles).isEmpty(); + assertThat(predicate).accepts(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is empty") + void mappedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).isEmpty(); + assertThat(predicate).accepts(new Customer(new ArrayList<>(), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is not empty") + void mappedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).isEmpty(); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + } + } + + @Nested + @DisplayName(".notEmpty()") + class NotEmpty { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list is not empty") + void mappedListIsNotEmpty() { + Predicate predicate = where().list(Customer::roles).notEmpty(); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list is null") + void mappedListIsNull() { + Predicate predicate = where().list(Customer::roles).notEmpty(); + assertThat(predicate).rejects(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is empty") + void mappedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).notEmpty(); + assertThat(predicate).rejects(new Customer(new ArrayList<>(), null)); + } + } + } + + @Nested + @DisplayName(".hasSize(size)") + class HasSize { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list has the specified size") + void mappedListHasSpecifiedSize() { + Predicate predicate = where().list(Customer::roles).hasSize(2); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is empty and specified size is zero") + void mappedListIsEmptyAndSpecifiedSizeIsZero() { + Predicate predicate = where().list(Customer::roles).hasSize(0); + assertThat(predicate).accepts(new Customer(new ArrayList<>(), null)); + } + + @Test + @DisplayName("mapped list is null and specified size is zero") + void mappedListIsNullAndSpecifiedSizeIsZero() { + Predicate predicate = where().list(Customer::roles).hasSize(0); + assertThat(predicate).accepts(new Customer(new ArrayList<>(), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list has a different size than specified") + void mappedListHasDifferentSize() { + Predicate predicate = where().list(Customer::roles).hasSize(3); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is empty and size is not zero") + void mappedListIsEmptyAndSizeIsNotZero() { + Predicate predicate = where().list(Customer::roles).hasSize(1); + assertThat(predicate).rejects(new Customer(new ArrayList<>(), null)); + } + + @Test + @DisplayName("mapped list is null and size is not zero") + void mappedListIsNullAndSizeIsNotZero() { + Predicate predicate = where().list(Customer::roles).hasSize(2); + assertThat(predicate).rejects(new Customer(null, null)); + } + } + } + + @Nested + @DisplayName(".notHaveSize(size)") + class NotHaveSize { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list does not have the specified size") + void mappedListDoesNotHaveSpecifiedSize() { + Predicate predicate = where().list(Customer::roles).notHaveSize(3); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null and specified size is not zero") + void mappedListIsNullAndSpecifiedSizeIsNotZero() { + Predicate predicate = where().list(Customer::roles).notHaveSize(2); + assertThat(predicate).accepts(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is empty and size is not zero") + void mappedListIsEmptyAndSizeIsNotZero() { + Predicate predicate = where().list(Customer::roles).notHaveSize(1); + assertThat(predicate).accepts(new Customer(new ArrayList<>(), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list has the specified size") + void mappedListHasSpecifiedSize() { + Predicate predicate = where().list(Customer::roles).notHaveSize(2); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is empty and specified size is zero") + void mappedListIsEmptyAndSpecifiedSizeIsZero() { + Predicate predicate = where().list(Customer::roles).notHaveSize(0); + assertThat(predicate).rejects(new Customer(new ArrayList<>(), null)); + } + + @Test + @DisplayName("mapped list is null and specified size is zero") + void mappedListIsNullAndSpecifiedSizeIsZero() { + Predicate predicate = where().list(Customer::roles).notHaveSize(0); + assertThat(predicate).rejects(new Customer(new ArrayList<>(), null)); + } + } + } + + @Nested + @DisplayName(".contains(element)") + class Contains { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list contains the specified element") + void mappedListContainsSpecifiedElement() { + Predicate predicate = where().list(Customer::roles).contains("Admin"); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list does not contain the specified element") + void mappedListDoesNotContainSpecifiedElement() { + Predicate predicate = where().list(Customer::roles).contains("Manager"); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null") + void mappedListIsNull() { + Predicate predicate = where().list(Customer::roles).contains("Admin"); + assertThat(predicate).rejects(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is empty") + void mappedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).contains("Admin"); + assertThat(predicate).rejects(new Customer(new ArrayList<>(), null)); + } + } + } + + @Nested + @DisplayName(".notContain(element)") + class NotContain { + + @Nested + @DisplayName("returns true when") + class ReturnsTrueWhen { + + @Test + @DisplayName("mapped list does not contain the specified element") + void mappedListDoesNotContainSpecifiedElement() { + Predicate predicate = where().list(Customer::roles).notContain("Manager"); + assertThat(predicate).accepts(new Customer(asList("Admin", "User"), null)); + } + + @Test + @DisplayName("mapped list is null") + void mappedListIsNull() { + Predicate predicate = where().list(Customer::roles).notContain("Admin"); + assertThat(predicate).accepts(new Customer(null, null)); + } + + @Test + @DisplayName("mapped list is empty") + void mappedListIsEmpty() { + Predicate predicate = where().list(Customer::roles).notContain("Admin"); + assertThat(predicate).accepts(new Customer(new ArrayList<>(), null)); + } + } + + @Nested + @DisplayName("returns false when") + class ReturnsFalseWhen { + + @Test + @DisplayName("mapped list contains the specified element") + void mappedListContainsSpecifiedElement() { + Predicate predicate = where().list(Customer::roles).notContain("Admin"); + assertThat(predicate).rejects(new Customer(asList("Admin", "User"), null)); + } + } + } + } + @Nested @DisplayName("Logical conjunction of") class LogicalConjunctionOf { diff --git a/src/test/java/io/github/avegera/predicate4j/test/Customer.java b/src/test/java/io/github/avegera/predicate4j/test/Customer.java new file mode 100644 index 0000000..4b030aa --- /dev/null +++ b/src/test/java/io/github/avegera/predicate4j/test/Customer.java @@ -0,0 +1,23 @@ +package io.github.avegera.predicate4j.test; + +import java.util.List; + +public class Customer { + + private final List roles; + + private final List products; + + public Customer(List roles, List products) { + this.roles = roles; + this.products = products; + } + + public List roles() { + return roles; + } + + public List products() { + return products; + } +} \ No newline at end of file diff --git a/src/test/java/io/github/avegera/predicate4j/test/Product.java b/src/test/java/io/github/avegera/predicate4j/test/Product.java new file mode 100644 index 0000000..dbe1e1e --- /dev/null +++ b/src/test/java/io/github/avegera/predicate4j/test/Product.java @@ -0,0 +1,21 @@ +package io.github.avegera.predicate4j.test; + +public class Product { + + private final String name; + + private final boolean active; + + public Product(String name, boolean active) { + this.name = name; + this.active = active; + } + + public String name() { + return name; + } + + public boolean active() { + return active; + } +}