Skip to content

Commit

Permalink
Improve ContentFragmentListImplTest tag predicate testing
Browse files Browse the repository at this point in the history
Updates the tag predicate testing to ensure that there are no
additional predicates in the actual predicate group that were
not present in the expected predicate group.

----
refs adobe#2381
  • Loading branch information
ky940819 committed Nov 23, 2022
1 parent fe38ebf commit 531ecc4
Showing 1 changed file with 33 additions and 14 deletions.
Expand Up @@ -20,6 +20,8 @@
import java.util.Map;
import javax.jcr.Session;

import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.tagging.TagConstants;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -88,7 +90,7 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
when(iterator.next()).thenReturn(resource);
when(resource.getResourceResolver()).thenReturn(leakingResourceResolverMock);
when(queryBuilderMock.createQuery(Mockito.any(PredicateGroup.class), Mockito.any(Session.class)))
.thenReturn(query);
.thenReturn(query);
}

@Test
Expand Down Expand Up @@ -145,8 +147,11 @@ void verifyQueryBuilderInteractionWhenNonExistingModelIsGiven() {
expectedPredicates.put("path", ImmutableMap.of("path", ContentFragmentListImpl.DEFAULT_DAM_PARENT_PATH));
expectedPredicates.put("type", ImmutableMap.of("type", "dam:Asset"));
expectedPredicates.put("1_property", ImmutableMap.of(
"property", "jcr:content/data/cq:model",
"value", "foobar"));
"property", "jcr:content/data/cq:model",
"value", "foobar"));
expectedPredicates.put("orderby", ImmutableMap.of(
"orderby", "@" + JcrConstants.JCR_CREATED,
"sort", Predicate.SORT_ASCENDING));

// WHEN
getModelInstanceUnderTest(NON_EXISTING_MODEL);
Expand All @@ -163,11 +168,11 @@ void verifyQueryBuilderInteractionWhenOrderByIsGiven() {
expectedPredicates.put("path", ImmutableMap.of("path", ContentFragmentListImpl.DEFAULT_DAM_PARENT_PATH));
expectedPredicates.put("type", ImmutableMap.of("type", "dam:Asset"));
expectedPredicates.put("1_property", ImmutableMap.of(
"property", "jcr:content/data/cq:model",
"value", "foobar"));
"property", "jcr:content/data/cq:model",
"value", "foobar"));
expectedPredicates.put("orderby", ImmutableMap.of(
"orderby", "@main",
"sort", "desc"));
"orderby", "@main",
"sort", "desc"));


// WHEN
Expand Down Expand Up @@ -195,11 +200,17 @@ void verifyQueryBuilderInteractionWhenPathParameterAndTagsAreGiven() {
expectedPredicates.put("path", ImmutableMap.of("path", "/content/dam/some-other-parent-path"));
expectedPredicates.put("type", ImmutableMap.of("type", "dam:Asset"));
expectedPredicates.put("1_property", ImmutableMap.of(
"property", "jcr:content/data/cq:model",
"value", "foobar"));
"property", "jcr:content/data/cq:model",
"value", "foobar"));
expectedPredicates.put("2_property", ImmutableMap.of(
"property", "jcr:content/metadata/jcr:mixinTypes",
"value", TagConstants.NT_TAGGABLE));
expectedPredicates.put("tagid", ImmutableMap.of(
"property", "jcr:content/metadata/cq:tags",
"1_value", "quux"));
"property", "jcr:content/metadata/cq:tags",
"1_value", "quux"));
expectedPredicates.put("orderby", ImmutableMap.of(
"orderby", "@" + JcrConstants.JCR_CREATED,
"sort", Predicate.SORT_ASCENDING));

// WHEN
getModelInstanceUnderTest(NON_EXISTING_MODEL_WITH_PATH_AND_TAGS);
Expand All @@ -216,8 +227,11 @@ void verifyQueryBuilderInteractionWhenMaxLimitIsGiven() {
expectedPredicates.put("path", ImmutableMap.of("path", ContentFragmentListImpl.DEFAULT_DAM_PARENT_PATH));
expectedPredicates.put("type", ImmutableMap.of("type", "dam:Asset"));
expectedPredicates.put("1_property", ImmutableMap.of(
"property", "jcr:content/data/cq:model",
"value", "foobar"));
"property", "jcr:content/data/cq:model",
"value", "foobar"));
expectedPredicates.put("orderby", ImmutableMap.of(
"orderby", "@" + JcrConstants.JCR_CREATED,
"sort", Predicate.SORT_ASCENDING));

//Expected Max Limit
String expectedLimit = "20";
Expand All @@ -242,12 +256,17 @@ private void verifyPredicateGroup(final Map<String, Map<String, String>> expecte
return false;
}

// checks length to ensure that there are no predicates in the result that aren't in the expected
if (expectedPredicates.size() != argument.size()) {
return false;
}

for (String predicateName : expectedPredicates.keySet()) {
Predicate predicate = argument.getByName(predicateName);
for (String predicateParameterName : expectedPredicates.get(predicateName).keySet()) {
String predicateParameterValue = predicate.getParameters().get(predicateParameterName);
String expectedPredicateParameterValue =
expectedPredicates.get(predicateName).get(predicateParameterName);
expectedPredicates.get(predicateName).get(predicateParameterName);
if (!predicateParameterValue.equals(expectedPredicateParameterValue)) {
return false;
}
Expand Down

0 comments on commit 531ecc4

Please sign in to comment.