camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/4] camel git commit: CAMEL-10646: jsonpath now supports pojos as map/list as well.
Date Fri, 23 Dec 2016 12:14:22 GMT
Repository: camel
Updated Branches:
  refs/heads/master f54a309d1 -> af22f502e


CAMEL-10646: jsonpath now supports pojos as map/list as well.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2baea250
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2baea250
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2baea250

Branch: refs/heads/master
Commit: 2baea2500618a7ba2f55210f47f507a9eac1bcf6
Parents: f54a309
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Fri Dec 23 11:22:21 2016 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Dec 23 11:22:21 2016 +0100

----------------------------------------------------------------------
 .../apache/camel/jsonpath/JsonPathEngine.java   |  8 +++
 .../camel/jsonpath/JsonPathLanguageTest.java    | 23 ++++++++
 .../jsonpath/JsonPathMapTransformTest.java      | 59 ++++++++++++++++++++
 3 files changed, 90 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/2baea250/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
index 58eebf3..14071d7 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
@@ -20,6 +20,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -110,6 +112,12 @@ public class JsonPathEngine {
         if (json instanceof String) {
             String str = (String) json;
             return path.read(str, configuration);
+        } else if (json instanceof Map) {
+            Map map = (Map) json;
+            return path.read(map, configuration);
+        } else if (json instanceof List) {
+            List list = (List) json;
+            return path.read(list, configuration);
         } else {
             InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
             String jsonEncoding = exchange.getIn().getHeader(JsonPathConstants.HEADER_JSON_ENCODING,
String.class);

http://git-wip-us.apache.org/repos/asf/camel/blob/2baea250/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
index 0f10864..4f837e1 100644
--- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
@@ -17,7 +17,9 @@
 package org.apache.camel.jsonpath;
 
 import java.io.File;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import com.jayway.jsonpath.Option;
 import org.apache.camel.Exchange;
@@ -74,6 +76,27 @@ public class JsonPathLanguageTest extends CamelTestSupport {
     }
 
     @Test
+    public void testExpressionPojo() throws Exception {
+        Exchange exchange = new DefaultExchange(context);
+        Map pojo = new HashMap();
+        pojo.put("kind", "full");
+        pojo.put("type", "customer");
+        exchange.getIn().setBody(pojo);
+
+        Language lan = context.resolveLanguage("jsonpath");
+        Expression exp = lan.createExpression("$.kind");
+        String kind = exp.evaluate(exchange, String.class);
+
+        assertNotNull(kind);
+        assertEquals("full", kind);
+
+        exp = lan.createExpression("$.type");
+        String type = exp.evaluate(exchange, String.class);
+        assertNotNull(type);
+        assertEquals("customer", type);
+    }
+
+    @Test
     public void testPredicate() throws Exception {
         // Test books.json file
         Exchange exchange = new DefaultExchange(context);

http://git-wip-us.apache.org/repos/asf/camel/blob/2baea250/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathMapTransformTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathMapTransformTest.java
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathMapTransformTest.java
new file mode 100644
index 0000000..1a2c3f5
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathMapTransformTest.java
@@ -0,0 +1,59 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.jsonpath;
+
+import java.io.FileInputStream;
+import java.util.List;
+import java.util.Map;
+
+import com.jayway.jsonpath.Configuration;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathMapTransformTest extends CamelTestSupport {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .transform().jsonpath("$.store.book[*].author")
+                    .to("mock:authors");
+            }
+        };
+    }
+
+    @Test
+    public void testAuthors() throws Exception {
+        getMockEndpoint("mock:authors").expectedMessageCount(1);
+
+        // should be a map
+        Object document = Configuration.defaultConfiguration().jsonProvider().parse(new FileInputStream("src/test/resources/books.json"),
"utf-8");
+        assertIsInstanceOf(Map.class, document);
+
+        template.sendBody("direct:start", document);
+
+        assertMockEndpointsSatisfied();
+
+        List<?> authors = getMockEndpoint("mock:authors").getReceivedExchanges().get(0).getIn().getBody(List.class);
+        assertEquals("Nigel Rees", authors.get(0));
+        assertEquals("Evelyn Waugh", authors.get(1));
+    }
+
+}


Mime
View raw message