Repository: neo4j-contrib/neo4j-apoc-procedures
Pull request #32 (2016-05-10 14:36:40)

adding urlencode/urldecode


Patch:
diff --git a/src/main/java/apoc/text/Strings.java b/src/main/java/apoc/text/Strings.java
index 7494a3af5..20e387b65 100644
--- a/src/main/java/apoc/text/Strings.java
+++ b/src/main/java/apoc/text/Strings.java
@@ -7,6 +7,9 @@ import apoc.result.StringResult;
 import org.neo4j.procedure.Name;
 import org.neo4j.procedure.Procedure;
 
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.util.List;
 import java.util.regex.Pattern;
 import java.util.stream.Stream;
@@ -59,6 +62,26 @@ public class Strings {
         return Empty.stream(matched);
     }
 
+    @Procedure
+    @Description("apoc.text.urlencode(text) - return the urlencoded text")
+    public Stream<StringResult> urlencode(@Name("text") String text) {
+        try {
+            return Stream.of(new StringResult(URLEncoder.encode(text, "UTF-8")));
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException("urlencoding failed", e);
+        }
+    }
+
+    @Procedure
+    @Description("apoc.text.urldecode(text) - return the urldecoded text")
+    public Stream<StringResult> urldecode(@Name("text") String text) {
+        try {
+            return Stream.of(new StringResult(URLDecoder.decode(text, "UTF-8")));
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException("urldecoding failed", e);
+        }
+    }
+
     private static Pattern cleanPattern = Pattern.compile("[^A-Za-z0-9]+");
 
     private static String removeNonWordCharacters(String s) {


Test patch:
diff --git a/src/test/java/apoc/text/StringsTest.java b/src/test/java/apoc/text/StringsTest.java
index 2fef8bfa9..a6e796505 100644
--- a/src/test/java/apoc/text/StringsTest.java
+++ b/src/test/java/apoc/text/StringsTest.java
@@ -2,7 +2,9 @@ package apoc.text;
 
 import apoc.util.TestUtil;
 import org.junit.*;
+import org.junit.rules.ExpectedException;
 import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.graphdb.QueryExecutionException;
 import org.neo4j.test.TestGraphDatabaseFactory;
 
 import java.util.Arrays;
@@ -20,6 +22,10 @@ import static org.junit.Assert.*;
 public class StringsTest {
     private static GraphDatabaseService db;
 
+    @Rule
+    public ExpectedException thrown= ExpectedException.none();
+
+
     @BeforeClass
     public static void setUp() throws Exception
     {
@@ -197,4 +203,31 @@ public class StringsTest {
                         "CALL apoc.text.filterCleanMatches(text, 'hello_world') RETURN text",
                 row -> assertEquals("Hello World!", row.get("text")));
     }
+
+    @Test
+    public void testUrlEncode() {
+        testCall(db,
+                "CALL apoc.text.urlencode('ab cd=gh&ij?') YIELD value RETURN value",
+                row -> assertEquals("ab+cd%3Dgh%26ij%3F", row.get("value"))
+        );
+    }
+
+    @Test
+    public void testUrlDecode() {
+        testCall(db,
+                "CALL apoc.text.urldecode('ab+cd%3Dgh%26ij%3F') YIELD value RETURN value",
+                row -> assertEquals("ab cd=gh&ij?", row.get("value"))
+        );
+    }
+
+    @Test
+    public void testUrlDecodeFailure() {
+        thrown.expect(QueryExecutionException.class);
+        thrown.expectMessage("Failed to invoke procedure `apoc.text.urldecode`: Caused by: java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern");
+        testCall(db,
+                "CALL apoc.text.urldecode('ab+cd%3Dgh%26ij%3') YIELD value RETURN value",
+                row -> assertEquals("ab cd=gh&ij?", row.get("value"))
+        );
+    }
+
 }