Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding allOf validation and test cases #438

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/am/ik/yavi/constraint/CollectionConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static am.ik.yavi.core.NullAs.VALID;
import static am.ik.yavi.core.ViolationMessage.Default.COLLECTION_CONTAINS;
import static am.ik.yavi.core.ViolationMessage.Default.COLLECTION_UNIQUE;
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_ALL_OF;

public class CollectionConstraint<T, L extends Collection<E>, E>
extends ContainerConstraintBase<T, L, CollectionConstraint<T, L, E>> {
Expand Down Expand Up @@ -69,6 +70,22 @@ public CollectionConstraint<T, L, E> unique() {
return this;
}

public CollectionConstraint<T, L, E> allOf(Collection<? extends E> values) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allOf sounds misleading. It seems that contains is more appropriate.

AssertJ's Collection assert method
https://assertj.github.io/doc/#assertj-core-group-contains

contains

Copy link
Author

@ShivKJ ShivKJ Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@making using contains can be a misnomer. It does not explain whether any or all items need to be contained. How about containsAll?

this.predicates().add(ConstraintPredicate.withViolatedValue(collection -> {
final Set<E> missingValues = new HashSet<>(values);

if (collection instanceof Set)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use brackets

missingValues.removeAll(collection);
else
missingValues.removeAll(new HashSet<>(collection));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to wrap it in a Set?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It becomes efficient. If you check removeAll implementation in AbstractSet, items are checked in collection so using set will be efficient. See below method for removeAll in AbstractSet.

    public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        boolean modified = false;

        if (size() > c.size()) {
            for (Object e : c)
                modified |= remove(e);
        } else {
            for (Iterator<?> i = iterator(); i.hasNext(); ) {
                if (c.contains(i.next())) {
                    i.remove();
                    modified = true;
                }
            }
        }
        return modified;
    }


return missingValues.isEmpty() ? Optional.empty() : Optional.of(new ViolatedValue(missingValues));
}, OBJECT_ALL_OF, () -> new Object[]{values.toString()}, VALID));

return this;
}


@Override
protected ToIntFunction<L> size() {
return Collection::size;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/am/ik/yavi/core/ViolationMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum Default implements ViolationMessage {
OBJECT_EQUAL_TO("object.equalTo", "\"{0}\" must be equal to {1}"), //
OBJECT_ONE_OF("object.oneOf", "\"{0}\" must be one of the following values: {1}"), //
OBJECT_NOT_ONE_OF("object.notOneOf", "\"{0}\" must not be one of the following values: {1}"), //
OBJECT_ALL_OF("object.allOf", "\"{0}\" must be have of all of the following values: {1}, missing values: {2}"), //
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object. prefix is not correct in this case

CONTAINER_NOT_EMPTY("container.notEmpty", "\"{0}\" must not be empty"), //
CONTAINER_LESS_THAN("container.lessThan", "The size of \"{0}\" must be less than {1}. The given size is {2}"), //
CONTAINER_LESS_THAN_OR_EQUAL("container.lessThanOrEqual",
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/am/ik/yavi/constraint/CollectionConstraintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ void unique() {
assertThat(predicate.test(Arrays.asList("a", "b", "c", "b"))).isFalse();
}

@Test
void allOf() {
Predicate<List<String>> predicate = retrievePredicate(c -> c.allOf(Arrays.asList("a", "b", "c")));
assertThat(predicate.test(Arrays.asList("a", "b", "c", "d"))).isTrue();
assertThat(predicate.test(Arrays.asList("a", "b", "c"))).isTrue();
assertThat(predicate.test(Arrays.asList("a", "b"))).isFalse();
assertThat(predicate.test(Arrays.asList("a"))).isFalse();
assertThat(predicate.test(Arrays.asList("a", "b", "d"))).isFalse();
assertThat(predicate.test(Arrays.asList("x", "y"))).isFalse();
assertThat(predicate.test(Arrays.asList())).isFalse();
}

private static Predicate<List<String>> retrievePredicate(
Function<CollectionConstraint<List<String>, List<String>, String>, CollectionConstraint<List<String>, List<String>, String>> constraint) {
return constraint.apply(new CollectionConstraint<>()).predicates().peekFirst().predicate();
Expand Down