You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2022/05/05 16:06:05 UTC

[sling-org-apache-sling-testing-sling-mock] branch master updated: SLING-11297 - The org.apache.sling.testing.mock.sling.RRMockResourceResolverWrapper should first query its providers (#12)

This is an automated email from the ASF dual-hosted git repository.

sseifert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new 9627d02  SLING-11297 - The org.apache.sling.testing.mock.sling.RRMockResourceResolverWrapper should first query its providers (#12)
9627d02 is described below

commit 9627d024dd0fe858d8411bb1825eb4641dfc4200
Author: Radu Cotescu <17...@users.noreply.github.com>
AuthorDate: Thu May 5 18:06:00 2022 +0200

    SLING-11297 - The org.apache.sling.testing.mock.sling.RRMockResourceResolverWrapper should first query its providers (#12)
    
    * implemented the rest of the methods that read resources
    * added tests
    
    Co-authored-by: Stefan Seifert <st...@users.noreply.github.com>
---
 .../mock/sling/RRMockResourceResolverWrapper.java  | 39 ++++++++++++++++++++++
 .../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, 92 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..540cf0e 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,33 @@ class RRMockResourceResolverWrapper extends ResourceResolverWrapper implements R
         return null;
     }
 
+    @Override
+    @SuppressWarnings("unchecked")
+    public Resource getResource(Resource base, @NotNull 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 +115,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"
+    }
+}