This is an automated email from the ASF dual-hosted git repository. radu pushed a commit to branch issue/SLING-11297 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit c300e34c5df606062cbe47adc63249d027709b71 Author: Radu Cotescu <[email protected]> AuthorDate: Thu May 5 16:06:29 2022 +0200 SLING-11297 - The org.apache.sling.testing.mock.sling.RRMockResourceResolverWrapper should first query its providers * implemented the rest of the methods that read resources * added tests --- .../mock/sling/RRMockResourceResolverWrapper.java | 38 ++++++++++++++++++++++ .../sling/junit/SlingContextDefaultRRTypeTest.java | 21 ++++++++++++ junit4/src/test/resources/test-content/parent.json | 6 ++++ .../mock/sling/junit5/SlingContextTest.java | 20 ++++++++++++ junit5/src/test/resources/test-content/parent.json | 6 ++++ 5 files changed, 91 insertions(+) diff --git a/core/src/main/java/org/apache/sling/testing/mock/sling/RRMockResourceResolverWrapper.java b/core/src/main/java/org/apache/sling/testing/mock/sling/RRMockResourceResolverWrapper.java index f6a98ea..db157e4 100644 --- a/core/src/main/java/org/apache/sling/testing/mock/sling/RRMockResourceResolverWrapper.java +++ b/core/src/main/java/org/apache/sling/testing/mock/sling/RRMockResourceResolverWrapper.java @@ -69,6 +69,32 @@ class RRMockResourceResolverWrapper extends ResourceResolverWrapper implements R return null; } + @Override + public Resource getResource(Resource base, String path) { + if (resourceProviders.isEmpty()) { + return super.getResource(base, path); + } + String normalizedPath = ResourceUtil.normalize(base.getPath() + "/" + path); + if (normalizedPath != null) { + ResourceProvider resourceProvider = getMatchingResourceProvider(normalizedPath); + if (resourceProvider != null) { + return resourceProvider.getResource(this, normalizedPath, ResourceContext.EMPTY_CONTEXT, null); + } + return super.getResource(path); + } + return null; + } + + // duplicated method from MockResourceResolver to ensure resources from resource providers are respected as well + @Override + public Resource getParent(@NotNull Resource child) { + final String parentPath = ResourceUtil.getParent(child.getPath()); + if (parentPath == null) { + return null; + } + return this.getResource(parentPath); + } + @Override @SuppressWarnings("unchecked") public @NotNull Iterator<Resource> listChildren(@NotNull Resource parent) { @@ -88,6 +114,18 @@ class RRMockResourceResolverWrapper extends ResourceResolverWrapper implements R return super.listChildren(parent); } + @Override + public @NotNull Iterable<Resource> getChildren(@NotNull Resource parent) { + return () -> listChildren(parent); + } + + @Override + public boolean hasChildren(@NotNull Resource resource) { + return listChildren(resource).hasNext(); + } + + + private ResourceProvider getMatchingResourceProvider(String path) { ResourceProvider provider = resourceProviders.get(path); if (provider != null) { diff --git a/junit4/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextDefaultRRTypeTest.java b/junit4/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextDefaultRRTypeTest.java index 3ac4bb2..40a1c9b 100644 --- a/junit4/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextDefaultRRTypeTest.java +++ b/junit4/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextDefaultRRTypeTest.java @@ -18,8 +18,11 @@ */ package org.apache.sling.testing.mock.sling.junit; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import org.apache.sling.api.resource.Resource; import org.junit.Rule; import org.junit.Test; @@ -33,4 +36,22 @@ public class SlingContextDefaultRRTypeTest { assertNotNull(context.request()); } + @Test + public void testResourceOperationsOnMountedFolder() { + String root = context.uniqueRoot().content() + "/test-content"; + context.load().folderJson("src/test/resources/test-content", root); + + Resource parent = context.resourceResolver().getResource(root + "/parent"); + assertNotNull( "Expected to resolve the 'parent' resource.", parent); + assertNotNull("Expected to resolver the 'child' resource.", parent.getChild("child")); + + Resource uniqueRoot = context.resourceResolver().getParent(parent); + assertNotNull("Expected to resolve the unique root.", uniqueRoot); + assertEquals("The resolved unique root is not identical to the created unique root.", root, uniqueRoot.getPath()); + + assertTrue("Expected to see a list of children.", context.resourceResolver().listChildren(parent).hasNext()); + assertTrue("Expected to get a list of children.", context.resourceResolver().getChildren(parent).iterator().hasNext()); + assertTrue("Expected to see a list of children.", parent.hasChildren()); + } + } diff --git a/junit4/src/test/resources/test-content/parent.json b/junit4/src/test/resources/test-content/parent.json new file mode 100644 index 0000000..5b0881a --- /dev/null +++ b/junit4/src/test/resources/test-content/parent.json @@ -0,0 +1,6 @@ +{ + "jcr:primaryType": "nt:unstructured", + "child" : { + "jcr:primaryType": "nt:unstructured" + } +} diff --git a/junit5/src/test/java/org/apache/sling/testing/mock/sling/junit5/SlingContextTest.java b/junit5/src/test/java/org/apache/sling/testing/mock/sling/junit5/SlingContextTest.java index 485d9a7..a39204c 100644 --- a/junit5/src/test/java/org/apache/sling/testing/mock/sling/junit5/SlingContextTest.java +++ b/junit5/src/test/java/org/apache/sling/testing/mock/sling/junit5/SlingContextTest.java @@ -19,6 +19,8 @@ package org.apache.sling.testing.mock.sling.junit5; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.sling.api.resource.Resource; import org.apache.sling.testing.mock.sling.ResourceResolverType; @@ -55,6 +57,24 @@ class SlingContextTest { assertEquals("myValue", model.getProp1()); } + @Test + void testResourceOperationsOnMountedFolder(SlingContext context) { + String root = context.uniqueRoot().content() + "/test-content"; + context.load().folderJson("src/test/resources/test-content", root); + + Resource parent = context.resourceResolver().getResource(root + "/parent"); + assertNotNull(parent, "Expected to resolve the 'parent' resource."); + assertNotNull(parent.getChild("child"), "Expected to resolver the 'child' resource."); + + Resource uniqueRoot = context.resourceResolver().getParent(parent); + assertNotNull(uniqueRoot, "Expected to resolve the unique root"); + assertEquals(root, uniqueRoot.getPath(), "The resolved unique root is not identical to the created unique root."); + + assertTrue(context.resourceResolver().listChildren(parent).hasNext(), "Expected to get a list of children."); + assertTrue(context.resourceResolver().getChildren(parent).iterator().hasNext(), "Expected to get a list of children."); + assertTrue(parent.hasChildren(), "Expected to get a list of children."); + } + @AfterEach void tearDown(SlingContext context) throws Exception { Resource resource = context.resourceResolver().getResource("/content/test"); diff --git a/junit5/src/test/resources/test-content/parent.json b/junit5/src/test/resources/test-content/parent.json new file mode 100644 index 0000000..5b0881a --- /dev/null +++ b/junit5/src/test/resources/test-content/parent.json @@ -0,0 +1,6 @@ +{ + "jcr:primaryType": "nt:unstructured", + "child" : { + "jcr:primaryType": "nt:unstructured" + } +}
