This is an automated email from the ASF dual-hosted git repository. reschke pushed a commit to branch SLING-12884 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git
commit 2a1a4662c6c5c694fe3092fd20396beb3509d525 Author: Julian Reschke <[email protected]> AuthorDate: Tue Aug 12 20:26:24 2025 +0100 SLING-12884: refactor mocking in AliasMapEntriesTest --- .../impl/mapping/AliasMapEntriesTest.java | 330 ++++++++------------- 1 file changed, 125 insertions(+), 205 deletions(-) diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java index 6923a1c0..ca800ebf 100644 --- a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java +++ b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java @@ -41,6 +41,7 @@ import org.apache.sling.api.resource.QuerySyntaxException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceUtil; +import org.apache.sling.api.resource.ValueMap; import org.apache.sling.api.resource.path.Path; import org.apache.sling.resourceresolver.impl.ResourceResolverImpl; import org.apache.sling.resourceresolver.impl.ResourceResolverMetrics; @@ -284,24 +285,16 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { boolean queryAlwaysThrows, boolean pagedQueryThrows, String... aliases) { - Resource parent = mock(Resource.class); - Resource result = mock(Resource.class); - Resource content = mock(Resource.class); - when(parent.getChildren()).thenReturn(Set.of(result)); - when(result.getParent()).thenReturn(null); // should be root - when(parent.getPath()).thenReturn("/parent"); - when(parent.getName()).thenReturn("parent"); + Resource parent = createMockedResource("/parent"); + Resource result = createMockedResource("/parent/child"); + Resource content = createMockedResource("/parent/child/jcr:content"); - when(result.getChildren()).thenReturn(Set.of(content)); - when(result.getParent()).thenReturn(withNullParent && !onJcrContent ? null : parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); + attachChildResource(parent, result); + attachChildResource(result, content); - when(content.getChildren()).thenReturn(Set.of()); + when(result.getParent()).thenReturn(withNullParent && !onJcrContent ? null : parent); when(content.getParent()).thenReturn(withNullParent && onJcrContent ? null : result); - when(content.getPath()).thenReturn("/parent/child/jcr:content"); - when(content.getName()).thenReturn("jcr:content"); Resource aliasResource = onJcrContent ? content : result; @@ -342,19 +335,17 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { @Test public void test_that_duplicate_alias_does_not_replace_first_alias() { - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource result = createMockedResource("/parent/child"); + + attachChildResource(parent, result); - final Resource result = mock(Resource.class); - when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); - final Resource secondResult = mock(Resource.class); - when(secondResult.getParent()).thenReturn(parent); - when(secondResult.getPath()).thenReturn("/parent/child2"); - when(secondResult.getName()).thenReturn("child2"); + Resource secondResult = createMockedResource("/parent/child2"); + + attachChildResource(parent, secondResult); + when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -378,32 +369,14 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { // checks that alias lists for "x" and "x/jcr:content" are merged private void internal_test_alias_on_parent_and_on_content_child(boolean cached) { - String parentPath = "/parent"; - Resource parent = mock(Resource.class, "mock for " + parentPath); - when(resourceResolver.getResource(parentPath)).thenReturn(parent); - - String nodePath = "/parent/node"; - Resource node = mock(Resource.class, "mock for " + nodePath); - when(resourceResolver.getResource(nodePath)).thenReturn(node); - - String contentPath = "/parent/node/jcr:content"; - Resource content = mock(Resource.class, "mock for " + contentPath); - when(resourceResolver.getResource(contentPath)).thenReturn(content); - - when(parent.getPath()).thenReturn(parentPath); - when(parent.getName()).thenReturn(ResourceUtil.getName(parentPath)); - when(parent.getChildren()).thenReturn(List.of(node)); - - when(node.getParent()).thenReturn(parent); - when(node.getPath()).thenReturn(nodePath); - when(node.getName()).thenReturn(ResourceUtil.getName(nodePath)); - when(node.getChildren()).thenReturn(List.of(content)); - when(node.getChild(ResourceUtil.getName(contentPath))).thenReturn(content); - when(node.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); + Resource parent = createMockedResource("/parent"); + Resource node = createMockedResource("/parent/node"); + Resource content = createMockedResource("/parent/node/jcr:content"); - when(content.getParent()).thenReturn(node); - when(content.getPath()).thenReturn(contentPath); - when(content.getName()).thenReturn(ResourceUtil.getName(contentPath)); + attachChildResource(parent, node); + attachChildResource(node, content); + + when(node.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); when(content.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "contentalias")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -492,14 +465,11 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { mapEntries = new MapEntries( resourceResolverFactory, bundleContext, eventAdmin, stringInterpolationProvider, metrics); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource result = createMockedResource("/parent/child"); + + attachChildResource(parent, result); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(result); - when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent/child", new AtomicBoolean()); @@ -515,14 +485,14 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { mapEntries = new MapEntries( resourceResolverFactory, bundleContext, eventAdmin, stringInterpolationProvider, metrics); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource result = createMockedResource("/parent/child"); + + // TODO: using attachChildResource causes the test to fail + // attachChildResource(parent, result); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); + when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent/child", new AtomicBoolean()); @@ -538,14 +508,11 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { mapEntries = new MapEntries( resourceResolverFactory, bundleContext, eventAdmin, stringInterpolationProvider, metrics); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource result = mock("/parent/child"); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); + when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); removeAlias(mapEntries, resourceResolver, "/parent", "/parent/child", NOOP); @@ -558,14 +525,10 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doAddAlias() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource result = createMockedResource("/parent/child"); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent/child", new AtomicBoolean()); @@ -578,11 +541,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); // test_that_duplicate_alias_does_not_replace_first_alias - final Resource secondResult = mock(Resource.class); - when(resourceResolver.getResource("/parent/child2")).thenReturn(secondResult); + Resource secondResult = createMockedResource("/parent/child2"); + when(secondResult.getParent()).thenReturn(parent); - when(secondResult.getPath()).thenReturn("/parent/child2"); - when(secondResult.getName()).thenReturn("child2"); when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent/child2", new AtomicBoolean()); @@ -595,11 +556,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); // testing jcr:content node - final Resource jcrContentResult = mock(Resource.class); - when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult); + Resource jcrContentResult = createMockedResource("/parent/child/jcr:content"); + when(jcrContentResult.getParent()).thenReturn(result); - when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content"); - when(jcrContentResult.getName()).thenReturn("jcr:content"); when(jcrContentResult.getValueMap()) .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent")); @@ -618,14 +577,10 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doAddAlias2() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/"); + Resource parent = createMockedResource("/"); + Resource result = createMockedResource("/parent"); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent"); - when(result.getName()).thenReturn("parent"); when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent", new AtomicBoolean()); @@ -638,11 +593,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); // test_that_duplicate_alias_does_not_replace_first_alias - final Resource secondResult = mock(Resource.class); - when(resourceResolver.getResource("/parent2")).thenReturn(secondResult); + Resource secondResult = createMockedResource("/parent2"); + when(secondResult.getParent()).thenReturn(parent); - when(secondResult.getPath()).thenReturn("/parent2"); - when(secondResult.getName()).thenReturn("parent2"); when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent2", new AtomicBoolean()); @@ -655,11 +608,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); // testing jcr:content node - final Resource jcrContentResult = mock(Resource.class); - when(resourceResolver.getResource("/parent/jcr:content")).thenReturn(jcrContentResult); + Resource jcrContentResult = createMockedResource("/parent/jcr:content"); + when(jcrContentResult.getParent()).thenReturn(result); - when(jcrContentResult.getPath()).thenReturn("/parent/jcr:content"); - when(jcrContentResult.getName()).thenReturn("jcr:content"); when(jcrContentResult.getValueMap()) .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent")); @@ -674,11 +625,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); // trying to add invalid alias path - final Resource invalidResourcePath = mock(Resource.class); - when(resourceResolver.getResource("/notallowedparent")).thenReturn(invalidResourcePath); + Resource invalidResourcePath = createMockedResource("/notallowedparent"); + when(invalidResourcePath.getParent()).thenReturn(parent); - when(invalidResourcePath.getPath()).thenReturn("/notallowedparent"); - when(invalidResourcePath.getName()).thenReturn("notallowedparent"); when(invalidResourcePath.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/notallowedparent", new AtomicBoolean()); @@ -694,14 +643,10 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doUpdateAlias() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource result = createMockedResource("/parent/child"); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); updateResource(mapEntries, "/parent/child", new AtomicBoolean()); @@ -725,11 +670,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); // testing jcr:content node update - final Resource jcrContentResult = mock(Resource.class); - when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult); + Resource jcrContentResult = createMockedResource("/parent/child/jcr:content"); + when(jcrContentResult.getParent()).thenReturn(result); - when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content"); - when(jcrContentResult.getName()).thenReturn("jcr:content"); when(jcrContentResult.getValueMap()) .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent")); when(result.getChild("jcr:content")).thenReturn(jcrContentResult); @@ -766,11 +709,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(List.of("aliasUpdated", "aliasJcrContentUpdated"), aliasMapEntry.get("child")); // add another node with different alias and check that the update doesn't break anything (see also SLING-3728) - final Resource secondResult = mock(Resource.class); - when(resourceResolver.getResource("/parent/child2")).thenReturn(secondResult); + Resource secondResult = createMockedResource("/parent/child2"); + when(secondResult.getParent()).thenReturn(parent); - when(secondResult.getPath()).thenReturn("/parent/child2"); - when(secondResult.getName()).thenReturn("child2"); when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias2")); updateResource(mapEntries, "/parent/child2", new AtomicBoolean()); @@ -811,14 +752,10 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { // check that alias map is empty assertEquals(0, aliasMap.size()); - final Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource child = createMockedResource("/parent/child"); - final Resource child = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(child); when(child.getParent()).thenReturn(parent); - when(child.getPath()).thenReturn("/parent/child"); - when(child.getName()).thenReturn("child"); when(child.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent/child", new AtomicBoolean()); @@ -862,22 +799,17 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doRemoveAlias2() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); + Resource result = createMockedResource("/parent/child"); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent/child"); - when(result.getName()).thenReturn("child"); when(result.getValueMap()).thenReturn(buildValueMap()); // testing jcr:content node removal - final Resource jcrContentResult = mock(Resource.class); + Resource jcrContentResult = createMockedResource("/parent/child/jcr:content"); when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult); + when(jcrContentResult.getParent()).thenReturn(result); - when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content"); - when(jcrContentResult.getName()).thenReturn("jcr:content"); when(jcrContentResult.getValueMap()) .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent")); when(result.getChild("jcr:content")).thenReturn(jcrContentResult); @@ -925,23 +857,17 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doRemoveAlias3() throws Exception { assertEquals(0, aliasMap.size()); - final Resource parentRsrc = mock(Resource.class); - when(parentRsrc.getPath()).thenReturn("/parent"); + Resource parentRsrc = createMockedResource("/parent"); + Resource childRsrc = createMockedResource("/parent/child"); - final Resource childRsrc = mock(Resource.class); - when(resourceResolver.getResource("/parent/child")).thenReturn(childRsrc); when(childRsrc.getParent()).thenReturn(parentRsrc); - when(childRsrc.getPath()).thenReturn("/parent/child"); - when(childRsrc.getName()).thenReturn("child"); when(childRsrc.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent/child", new AtomicBoolean()); - final Resource jcrContentResult = mock(Resource.class); - when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult); + Resource jcrContentResult = createMockedResource("/parent/child/jcr:content"); + when(jcrContentResult.getParent()).thenReturn(childRsrc); - when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content"); - when(jcrContentResult.getName()).thenReturn("jcr:content"); when(jcrContentResult.getValueMap()) .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent")); when(childRsrc.getChild("jcr:content")).thenReturn(jcrContentResult); @@ -1030,14 +956,10 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doRemoveAlias4() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/"); + Resource parent = createMockedResource("/"); + Resource result = createMockedResource("/parent"); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent"); - when(result.getName()).thenReturn("parent"); when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias")); addResource(mapEntries, "/parent", new AtomicBoolean()); @@ -1081,22 +1003,16 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doRemoveAlias5() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/"); + Resource parent = createMockedResource("/"); + Resource result = createMockedResource("/parent"); - final Resource result = mock(Resource.class); - when(resourceResolver.getResource("/parent")).thenReturn(result); when(result.getParent()).thenReturn(parent); - when(result.getPath()).thenReturn("/parent"); - when(result.getName()).thenReturn("parent"); when(result.getValueMap()).thenReturn(buildValueMap()); // testing jcr:content node removal - final Resource jcrContentResult = mock(Resource.class); - when(resourceResolver.getResource("/parent/jcr:content")).thenReturn(jcrContentResult); + Resource jcrContentResult = createMockedResource("/parent/jcr:content"); + when(jcrContentResult.getParent()).thenReturn(result); - when(jcrContentResult.getPath()).thenReturn("/parent/jcr:content"); - when(jcrContentResult.getName()).thenReturn("jcr:content"); when(jcrContentResult.getValueMap()) .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "aliasJcrContent")); when(result.getChild("jcr:content")).thenReturn(jcrContentResult); @@ -1125,52 +1041,38 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doNotRemoveAliasWhenJCRContentDeletedInParentPath() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(resourceResolver.getResource("/parent")).thenReturn(parent); + Resource parent = createMockedResource("/parent"); + when(parent.getParent()).thenReturn(parent); - when(parent.getPath()).thenReturn("/parent"); - when(parent.getName()).thenReturn("parent"); when(parent.getValueMap()).thenReturn(buildValueMap()); - final Resource container = mock(Resource.class); - when(resourceResolver.getResource("/parent/container")).thenReturn(container); + Resource container = createMockedResource("/parent/container"); + when(container.getParent()).thenReturn(parent); - when(container.getPath()).thenReturn("/parent/container"); - when(container.getName()).thenReturn("container"); when(container.getValueMap()).thenReturn(buildValueMap()); when(parent.getChild("container")).thenReturn(container); - final Resource jcrContent = mock(Resource.class); - when(resourceResolver.getResource("/parent/container/jcr:content")).thenReturn(jcrContent); + Resource jcrContent = createMockedResource("/parent/container/jcr:content"); + when(jcrContent.getParent()).thenReturn(container); - when(jcrContent.getPath()).thenReturn("/parent/container/jcr:content"); - when(jcrContent.getName()).thenReturn("jcr:content"); when(jcrContent.getValueMap()).thenReturn(buildValueMap()); when(container.getChild("jcr:content")).thenReturn(jcrContent); - final Resource childContainer = mock(Resource.class); - when(resourceResolver.getResource("/parent/container/childContainer")).thenReturn(childContainer); + Resource childContainer = createMockedResource("/parent/container/childContainer"); + when(childContainer.getParent()).thenReturn(container); - when(childContainer.getPath()).thenReturn("/parent/container/childContainer"); - when(childContainer.getName()).thenReturn("childContainer"); when(childContainer.getValueMap()).thenReturn(buildValueMap()); when(container.getChild("childContainer")).thenReturn(childContainer); - final Resource grandChild = mock(Resource.class); - when(resourceResolver.getResource("/parent/container/childContainer/grandChild")) - .thenReturn(grandChild); + Resource grandChild = createMockedResource("/parent/container/childContainer/grandChild"); + when(grandChild.getParent()).thenReturn(childContainer); - when(grandChild.getPath()).thenReturn("/parent/container/childContainer/grandChild"); - when(grandChild.getName()).thenReturn("grandChild"); when(grandChild.getValueMap()).thenReturn(buildValueMap()); when(childContainer.getChild("grandChild")).thenReturn(grandChild); - final Resource grandChildJcrContent = mock(Resource.class); - when(resourceResolver.getResource("/parent/container/childContainer/grandChild/jcr:content")) - .thenReturn(grandChildJcrContent); + Resource grandChildJcrContent = createMockedResource("/parent/container/childContainer/grandChild/jcr:content"); + when(grandChildJcrContent.getParent()).thenReturn(grandChild); - when(grandChildJcrContent.getPath()).thenReturn("/parent/container/childContainer/grandChild/jcr:content"); - when(grandChildJcrContent.getName()).thenReturn("jcr:content"); when(grandChildJcrContent.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "gc")); when(grandChild.getChild("jcr:content")).thenReturn(grandChildJcrContent); @@ -1198,27 +1100,19 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_doRemoveAliasFromSibling() throws Exception { assertEquals(0, aliasMap.size()); - Resource parent = mock(Resource.class); - when(parent.getPath()).thenReturn("/parent"); + Resource parent = createMockedResource("/parent"); - when(resourceResolver.getResource("/parent")).thenReturn(parent); when(parent.getParent()).thenReturn(parent); - when(parent.getPath()).thenReturn("/parent"); - when(parent.getName()).thenReturn("parent"); when(parent.getValueMap()).thenReturn(buildValueMap()); - final Resource child1 = mock(Resource.class); - when(resourceResolver.getResource("/parent/child1")).thenReturn(child1); + Resource child1 = createMockedResource("/parent/child1"); + when(child1.getParent()).thenReturn(parent); - when(child1.getPath()).thenReturn("/parent/child1"); - when(child1.getName()).thenReturn("child1"); when(child1.getValueMap()).thenReturn(buildValueMap()); - final Resource child1JcrContent = mock(Resource.class); - when(resourceResolver.getResource("/parent/child1/jcr:content")).thenReturn(child1JcrContent); + Resource child1JcrContent = createMockedResource("/parent/child1/jcr:content"); + when(child1JcrContent.getParent()).thenReturn(child1); - when(child1JcrContent.getPath()).thenReturn("/parent/child1/jcr:content"); - when(child1JcrContent.getName()).thenReturn("jcr:content"); when(child1JcrContent.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "test1")); when(child1.getChild("jcr:content")).thenReturn(child1JcrContent); @@ -1233,18 +1127,16 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); - final Resource child2 = mock(Resource.class); - when(resourceResolver.getResource("/parent/child2")).thenReturn(child2); - when(child2.getParent()).thenReturn(parent); + Resource child2 = createMockedResource("/parent/child2"); when(child2.getPath()).thenReturn("/parent/child2"); when(child2.getName()).thenReturn("child2"); + + when(child2.getParent()).thenReturn(parent); when(child2.getValueMap()).thenReturn(buildValueMap()); - final Resource child2JcrContent = mock(Resource.class); - when(resourceResolver.getResource("/parent/child2/jcr:content")).thenReturn(child2JcrContent); + Resource child2JcrContent = createMockedResource("/parent/child2/jcr:content"); + when(child2JcrContent.getParent()).thenReturn(child2); - when(child2JcrContent.getPath()).thenReturn("/parent/child2/jcr:content"); - when(child2JcrContent.getName()).thenReturn("jcr:content"); when(child2JcrContent.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "test2")); when(child2.getChild("jcr:content")).thenReturn(child2JcrContent); @@ -1262,11 +1154,9 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, aliasMap.size()); assertEquals(2, mapEntries.getAliasMap("/parent").size()); - final Resource child2JcrContentChild = mock(Resource.class); - when(resourceResolver.getResource("/parent/child2/jcr:content/test")).thenReturn(child2JcrContentChild); + Resource child2JcrContentChild = createMockedResource("/parent/child2/jcr:content/test"); + when(child2JcrContentChild.getParent()).thenReturn(child2); - when(child2JcrContentChild.getPath()).thenReturn("/parent/child2/jcr:content/test"); - when(child2JcrContentChild.getName()).thenReturn("test"); when(child2JcrContent.getChild("test")).thenReturn(child2JcrContentChild); removeResource(mapEntries, child2JcrContentChild.getPath(), new AtomicBoolean()); @@ -1365,4 +1255,34 @@ public class AliasMapEntriesTest extends AbstractMappingMapEntriesTest { private boolean matchesPagedQuery(String query) { return AQ_PAGED_PATTERN.matcher(query).matches(); } + + private Resource createMockedResource(String path) { + Resource result = mock(Resource.class, "mock for " + path); + + // the basics + when(result.getName()).thenReturn(ResourceUtil.getName(path)); + when(result.getPath()).thenReturn(path); + + // need to be attached later + when(result.getChildren()).thenReturn(Set.of()); + when(result.getChild(anyString())).thenReturn(null); + when(result.getParent()).thenReturn(null); + + // need to be specified later + when(result.getValueMap()).thenReturn(ValueMap.EMPTY); + + // attach to resource resolver + when(resourceResolver.getResource(path)).thenReturn(result); + + return result; + } + + private void attachChildResource(Resource parent, Resource child) { + + // TODO: support adding multiple children + when(parent.getChildren()).thenReturn(Set.of(child)); + when(parent.getChild(child.getName())).thenReturn(child); + + when(child.getParent()).thenReturn(parent); + } }
