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/03/22 13:17:39 UTC

[sling-org-apache-sling-testing-sling-mock] branch master updated: SLING-8325 sling-mock: Overlay ContentBuilder.resource methods with variants to create child resources

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 1d270d4  SLING-8325 sling-mock: Overlay ContentBuilder.resource methods with variants to create child resources
1d270d4 is described below

commit 1d270d41c924ac057d776223ffc5ba155aa0dd83
Author: sseifert <ss...@pro-vision.de>
AuthorDate: Fri Mar 22 14:17:29 2019 +0100

    SLING-8325 sling-mock: Overlay ContentBuilder.resource methods with variants to create child resources
---
 .../testing/mock/sling/builder/ContentBuilder.java | 33 ++++++++++++++++++++++
 .../testing/mock/sling/builder/package-info.java   |  2 +-
 .../mock/sling/builder/ContentBuilderTest.java     | 17 +++++++++++
 3 files changed, 51 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 7f8e985..7502513 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
@@ -92,6 +92,39 @@ public class ContentBuilder {
     }
 
     /**
+     * Create child resource below the given parent resource.
+     * @param resource Parent resource
+     * @param name Child resource name
+     * @return Resource object
+     */
+    public final @NotNull Resource resource(@NotNull Resource resource, @NotNull String name) {
+        return resource(resource, name, ValueMap.EMPTY);
+    }
+
+    /**
+     * Create child resource below the given parent resource.
+     * @param resource Parent resource
+     * @param name Child resource name
+     * @param properties Properties for resource.
+     * @return Resource object
+     */
+    public final @NotNull Resource resource(@NotNull Resource resource, @NotNull String name, @NotNull Map<String, Object> properties) {
+        String path = resource.getPath() + "/" + StringUtils.stripStart(name, "/");
+        return resource(path, properties);
+    }
+
+    /**
+     * Create child resource below the given parent resource.
+     * @param resource Parent resource
+     * @param name Child resource name
+     * @param properties Properties for resource.
+     * @return Resource object
+     */
+    public final @NotNull Resource resource(@NotNull Resource resource, @NotNull String name, @NotNull Object @NotNull ... properties) {
+        return resource(resource, name, MapUtil.toMap(properties));
+    }
+
+    /**
      * Ensure that a resource exists at the given path. If not, it is created
      * using <code>nt:unstructured</code> node type.
      * @param path Resource path
diff --git a/core/src/main/java/org/apache/sling/testing/mock/sling/builder/package-info.java b/core/src/main/java/org/apache/sling/testing/mock/sling/builder/package-info.java
index ed6acd2..efa0a0d 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/sling/builder/package-info.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/sling/builder/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Content builder for creating test content.
  */
-@org.osgi.annotation.versioning.Version("1.1.1")
+@org.osgi.annotation.versioning.Version("1.2")
 package org.apache.sling.testing.mock.sling.builder;
diff --git a/core/src/test/java/org/apache/sling/testing/mock/sling/builder/ContentBuilderTest.java b/core/src/test/java/org/apache/sling/testing/mock/sling/builder/ContentBuilderTest.java
index 7e8ecb5..d92d374 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/sling/builder/ContentBuilderTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/sling/builder/ContentBuilderTest.java
@@ -70,4 +70,21 @@ public class ContentBuilderTest {
         assertEquals("value1", props.get("stringProp", String.class));
     }
 
+    @Test
+    public void testChildResource() {
+        Resource resource = context.create().resource("/content/test1/resource1");
+        
+        Resource child1 = context.create().resource(resource, "child1");
+        assertNotNull(child1);
+        assertEquals("child1", child1.getName());
+        assertTrue(ResourceUtil.getValueMap(child1).isEmpty());
+        
+        Resource child2 = context.create().resource(resource, "/child2",
+                "prop1", "value1");
+        assertNotNull(child2);
+        assertEquals("child2", child2.getName());
+        ValueMap props = ResourceUtil.getValueMap(child2);
+        assertEquals("value1", props.get("prop1", String.class));
+    }
+
 }