You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by np...@apache.org on 2018/01/29 09:09:09 UTC

[sling-org-apache-sling-pipes] 02/02: SLING-7450 add expression conf for write pipe

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

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

commit 66971e71e65fc0e350aabe01357cdfc13bcd1d49
Author: Nicolas Peltier <pe...@gmail.com>
AuthorDate: Mon Jan 29 10:08:54 2018 +0100

    SLING-7450 add expression conf for write pipe
---
 .../org/apache/sling/pipes/internal/WritePipe.java | 11 +++++++--
 .../org/apache/sling/pipes/AbstractPipeTest.java   |  6 +++--
 .../apache/sling/pipes/internal/WritePipeTest.java | 27 ++++++++++++++++++++++
 3 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/internal/WritePipe.java b/src/main/java/org/apache/sling/pipes/internal/WritePipe.java
index 0d38a8f..4cbc948 100644
--- a/src/main/java/org/apache/sling/pipes/internal/WritePipe.java
+++ b/src/main/java/org/apache/sling/pipes/internal/WritePipe.java
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.pipes.internal;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
@@ -57,9 +58,15 @@ public class WritePipe extends BasePipe {
     public WritePipe(Plumber plumber, Resource resource) throws Exception {
         super(plumber, resource);
         if (getConfiguration() == null){
-            throw new Exception("write pipe is misconfigured: it should have a configuration node");
+            String pathCandidate = getExpr();
+            if (StringUtils.isNotBlank(pathCandidate) && resolver.getResource(pathCandidate) != null){
+                confTree = resolver.getResource(pathCandidate).adaptTo(Node.class);
+            } else {
+                throw new Exception("write pipe is misconfigured: it should have a configuration node, or an expression");
+            }
+        } else {
+            confTree = getConfiguration().adaptTo(Node.class);
         }
-        confTree = getConfiguration().adaptTo(Node.class);
     }
 
 
diff --git a/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java b/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java
index 801a3cd..84bb202 100644
--- a/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/AbstractPipeTest.java
@@ -42,8 +42,10 @@ public class AbstractPipeTest {
 
     protected static final String PATH_PIPE = "/etc/pipe";
     protected static final String PATH_FRUITS = "/content/fruits";
-    protected static final String PATH_BANANA = PATH_FRUITS + "/banana";
-    protected static final String PATH_APPLE = PATH_FRUITS + "/apple";
+    protected static final String BANANA_SUFFIX = "/banana";
+    protected static final String PATH_BANANA = PATH_FRUITS + BANANA_SUFFIX;
+    protected static final String APPLE_SUFFIX = "/apple";
+    protected static final String PATH_APPLE = PATH_FRUITS + APPLE_SUFFIX;
     protected static final String PATH_PEA = PATH_APPLE + "/isnota/pea";
     protected static final String PATH_CARROT = PATH_APPLE + "/isnota/carrot";
     protected static final String SAME_COLOR = PATH_PEA + "/buttheyhavesamecolor";
diff --git a/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java b/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java
index 8dc96b4..bc206fb 100644
--- a/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/WritePipeTest.java
@@ -23,14 +23,19 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 
+import org.apache.commons.collections.IteratorUtils;
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.pipes.AbstractPipeTest;
+import org.apache.sling.pipes.ExecutionResult;
 import org.apache.sling.pipes.Pipe;
 import org.junit.Before;
 import org.junit.Test;
@@ -125,4 +130,26 @@ public class WritePipeTest extends AbstractPipeTest {
         NodeIterator children = appleNode.getNodes();
         assertTrue("Apple node should have children", children.hasNext());
     }
+
+    @Test
+    public void testReferencedSource() throws Exception {
+        String path = "/content/test/referenced/source";
+        ResourceResolver resolver = context.resourceResolver();
+        ExecutionResult result = plumber.newPipe(resolver)
+                .mkdir(path)
+                .pipe(WritePipe.RESOURCE_TYPE)
+                .expr("/content/fruits")
+                .run();
+        assertEquals("result should have 1", 1, result.size());
+        Resource root = resolver.getResource(path);
+        assertNotNull("target resource should be created", root);
+        Resource property =  root.getChild("index");
+        assertNotNull("property should be here", property);
+        assertArrayEquals("index property should be the same", new String[] {"apple","banana"}, property.adaptTo(String[].class));
+        List<Resource> resources = IteratorUtils.toList(root.listChildren());
+        List<String> children = resources.stream().map(r -> r.getPath()).collect(Collectors.toList());
+        assertEquals("there should be 2 children", 2, children.size());
+        assertTrue("first should be apple", children.get(0).endsWith(APPLE_SUFFIX));
+        assertTrue("second should be banana", children.get(1).endsWith(BANANA_SUFFIX));
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
npeltier@apache.org.