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 2021/01/15 15:49:19 UTC

[sling-org-apache-sling-pipes] branch master updated: SLING-10065 fix json servlet UT

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


The following commit(s) were added to refs/heads/master by this push:
     new f6bb1ce  SLING-10065 fix json servlet UT
f6bb1ce is described below

commit f6bb1cef0fb3f8b2e1091c81febd88f753a776b8
Author: Nicolas Peltier <np...@apache.org>
AuthorDate: Fri Jan 15 16:48:59 2021 +0100

    SLING-10065 fix json servlet UT
---
 .../sling/pipes/internal/CommandExecutorImpl.java  |  3 +-
 .../pipes/internal/CommandExecutorImplTest.java    | 39 +++++++++++++++-------
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
index 231ca60..4dc200e 100644
--- a/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
+++ b/src/main/java/org/apache/sling/pipes/internal/CommandExecutorImpl.java
@@ -64,7 +64,6 @@ import static org.apache.commons.lang3.StringUtils.EMPTY;
 import static org.apache.sling.pipes.internal.CommandUtil.keyValuesToArray;
 import static org.apache.sling.pipes.internal.CommandUtil.writeToMap;
 
-import javax.json.Json;
 import javax.json.JsonException;
 import javax.servlet.Servlet;
 
@@ -425,7 +424,7 @@ public class CommandExecutorImpl extends AbstractPlumberServlet implements Comma
                 currentToken.options = getOptions(Arrays.copyOfRange(options, 1, options.length));
             }
             List<String> subTokens = getSpaceSeparatedTokens(options[0]);
-            if (subTokens.size() > 0) {
+            if (! subTokens.isEmpty()) {
                 currentToken.pipeKey = subTokens.get(0);
                 if (subTokens.size() > 1) {
                     currentToken.args = subTokens.subList(1, subTokens.size());
diff --git a/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java b/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
index b25fb74..0bab6fb 100644
--- a/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
+++ b/src/test/java/org/apache/sling/pipes/internal/CommandExecutorImplTest.java
@@ -45,6 +45,9 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import javax.json.JsonArray;
+import javax.json.JsonObject;
+import javax.json.JsonString;
 import javax.servlet.ServletException;
 
 public class CommandExecutorImplTest extends AbstractPipeTest {
@@ -169,7 +172,7 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
         assertNotNull(result);
     }
 
-    String testServlet(Map<String,Object> params) throws ServletException, IOException {
+    String testRawServlet(Map<String,Object> params) throws IOException {
         MockSlingHttpServletRequest request = context.request();
         MockSlingHttpServletResponse response = context.response();
         request.setParameterMap(params);
@@ -183,12 +186,15 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
         assertEquals(200, response.getStatus());
         return response.getOutputAsString();
     }
+    JsonObject testServlet(Map<String,Object> params) throws ServletException, IOException {
+        return (JsonObject) JsonUtil.parse(testRawServlet(params));
+    }
 
     @Test
     public void testHelp() throws ServletException, IOException {
         Map<String, Object> params = new HashMap<>();
         params.put(CommandExecutorImpl.REQ_PARAM_HELP, "blah");
-        String response = testServlet(params);
+        String response = testRawServlet(params);
         assertTrue(StringUtils.isNotBlank(response));
     }
 
@@ -196,8 +202,9 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
     public void testSimpleCommandServlet() throws IOException, ServletException {
         Map<String, Object> params = new HashMap<>();
         params.put(CommandExecutorImpl.REQ_PARAM_CMD, "echo /content | mkdir foo | write type=bar");
-        String response = testServlet(params);
-        assertEquals("{\"items\":[\"/content/foo\"],\"size\":1}\n", response);
+        JsonObject response = testServlet(params);
+        assertEquals(1, response.getJsonNumber("size").intValue());
+        assertEquals("/content/foo", response.getJsonArray("items").getString(0));
     }
 
     @Test
@@ -216,24 +223,32 @@ public class CommandExecutorImplTest extends AbstractPipeTest {
         assertEquals ("echo /content | write one=foo nested/two=foo nested/three=foo", cmdList.get(3));
     }
 
+    String[] getItemsArray(JsonObject response) {
+        JsonArray jsonItems = response.getJsonArray("items");
+        List<String> items = jsonItems.stream().map(o -> ((JsonString)o).getString()).collect(Collectors.toList());
+        return items.toArray(new String[jsonItems.size()]);
+    }
+
     @Test
     public void testChainedCommand() throws IOException, ServletException {
         Map<String, Object> params = new HashMap<>();
         params.put(CommandExecutorImpl.REQ_PARAM_FILE, IOUtils.toString(getClass().getResourceAsStream("/chainedCommand"
             + ".txt"), "UTF-8"));
-        String response = testServlet(params);
-        assertEquals("{\"items\":[\"/content/fruits/banana/isnota/pea\",\"/content/fruits/banana/isnota/carrot\","
-            + "\"/content/fruits/apple/isnota/pea\",\"/content/fruits/apple/isnota/plum\","
-            + "\"/content/fruits/apple/isnota/carrot\"],\"size\":5}\n", response);
+        JsonObject response = testServlet(params);
+        assertEquals(5, response.getJsonNumber("size").intValue());
+        assertArrayEquals(new String[]{ "/content/fruits/banana/isnota/pea","/content/fruits/banana/isnota/carrot",
+            "/content/fruits/apple/isnota/pea","/content/fruits/apple/isnota/plum",
+            "/content/fruits/apple/isnota/carrot"}, getItemsArray(response));
     }
 
     @Test
     public void testFileCommandServlet() throws IOException, ServletException {
         Map<String, Object> params = new HashMap<>();
         params.put(CommandExecutorImpl.REQ_PARAM_FILE, IOUtils.toString(getClass().getResourceAsStream("/testcommand"
-            + ".txt"), "UTF-8"));
-        String response = testServlet(params);
-        assertEquals("{\"items\":[\"/content/beatles/john\",\"/content/beatles/paul\","
-            + "\"/content/beatles/georges\",\"/content/beatles/ringo\",\"/content/beatles/ringo/jcr:content\"],\"size\":5}\n", response);
+                + ".txt"), "UTF-8"));
+        JsonObject response = testServlet(params);
+        assertEquals(5, response.getJsonNumber("size").intValue());
+        assertArrayEquals(new String[]{"/content/beatles/john", "/content/beatles/paul", "/content/beatles/georges",
+                "/content/beatles/ringo", "/content/beatles/ringo/jcr:content"}, getItemsArray(response));
     }
 }
\ No newline at end of file