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 2019/08/01 15:21:50 UTC

[sling-org-apache-sling-testing-sling-mock] branch master updated: SLING-8609 sling-mock: Support creating nested resources with ContentBuilder

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 2dbadff  SLING-8609 sling-mock: Support creating nested resources with ContentBuilder
2dbadff is described below

commit 2dbadff63f9652e4e558be8b41a8b983717c0de9
Author: sseifert <ss...@pro-vision.de>
AuthorDate: Thu Aug 1 17:20:42 2019 +0200

    SLING-8609 sling-mock: Support creating nested resources with ContentBuilder
---
 .../testing/mock/sling/builder/ContentBuilder.java | 39 +++++++++++++++++++++-
 .../AbstractSlingCrudResourceResolverTest.java     | 24 +++++++++++++
 2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/sling/testing/mock/sling/builder/ContentBuilder.java b/core/src/main/java/org/apache/sling/testing/mock/sling/builder/ContentBuilder.java
index 7502513..f4f907f 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/sling/builder/ContentBuilder.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/sling/builder/ContentBuilder.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.testing.mock.sling.builder;
 
+import java.util.HashMap;
 import java.util.Map;
 
 import org.apache.commons.lang3.StringUtils;
@@ -71,13 +72,49 @@ public class ContentBuilder {
         if (parentPath == null) {
             throw new IllegalArgumentException("Path has no parent: " + path);
         }
+        
+        // check if properties map contains maps representing child resources
+        Map<String,Object> propertiesWihtoutChildren;
+        Map<String,Map<String,Object>> children = getChildMaps(properties);
+        if (!children.isEmpty()) {
+            propertiesWihtoutChildren = new HashMap<>();
+            for (Map.Entry<String, Object> entry : properties.entrySet()) {
+                if (!children.containsKey(entry.getKey())) {
+                    propertiesWihtoutChildren.put(entry.getKey(), entry.getValue());
+                }
+            }
+        }
+        else {
+            propertiesWihtoutChildren = properties;
+        }
+        
+        // create resource
         Resource parentResource = ensureResourceExists(parentPath);
         String name = ResourceUtil.getName(path);
+        Resource newResource;
         try {
-            return resourceResolver.create(parentResource, name, properties);
+            newResource = resourceResolver.create(parentResource, name, propertiesWihtoutChildren);
         } catch (PersistenceException ex) {
             throw new RuntimeException("Unable to create resource at " + path, ex);
         }
+        
+        // create child resources
+        for (Map.Entry<String,Map<String,Object>> entry : children.entrySet()) {
+            resource(newResource, entry.getKey(), entry.getValue());
+        }
+        
+        return newResource;
+    }
+    
+    @SuppressWarnings("unchecked")
+    private Map<String,Map<String,Object>> getChildMaps(Map<String,Object> properties) {
+        Map<String,Map<String,Object>> result = new HashMap<>();
+        for (Map.Entry<String, Object> entry : properties.entrySet()) {
+            if (entry.getValue() instanceof Map) {
+                result.put(entry.getKey(), (Map)entry.getValue());
+            }
+        }
+        return result;
     }
 
     /**
diff --git a/core/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractSlingCrudResourceResolverTest.java b/core/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractSlingCrudResourceResolverTest.java
index a075908..c8a8c01 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractSlingCrudResourceResolverTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractSlingCrudResourceResolverTest.java
@@ -292,4 +292,28 @@ public abstract class AbstractSlingCrudResourceResolverTest {
         assertFalse(context.resourceResolver().hasChanges());
     }
 
+    @Test
+    public void testCreateNestedResources() throws IOException {
+        Resource nested = context.create().resource(getTestRootResource().getPath() + "/nested", 
+                ImmutableMap.<String,Object>of(
+                        "prop1", "value1",
+                        "child1", ImmutableMap.<String,Object>of(
+                            "prop2","value2",
+                            "child2", ImmutableMap.<String,Object>of(
+                                "prop3", "value3")
+                            )
+                        ));
+
+        assertNotNull(nested);
+        assertEquals("value1", nested.getValueMap().get("prop1", String.class));
+
+        Resource child1 = nested.getChild("child1");
+        assertNotNull(child1);
+        assertEquals("value2", child1.getValueMap().get("prop2", String.class));
+
+        Resource child2 = child1.getChild("child2");
+        assertNotNull(child2);
+        assertEquals("value3", child2.getValueMap().get("prop3", String.class));
+    }
+
 }