camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] 01/02: CAMEL-12321: camel-bindy - Allow to configure unmarshal to always return a list type
Date Mon, 05 Mar 2018 11:11:49 GMT
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 53807e6634482a2e5318a7e96e1fd14cb3a0ed6b
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Mon Mar 5 11:52:31 2018 +0100

    CAMEL-12321: camel-bindy - Allow to configure unmarshal to always return a list type
---
 .../src/main/docs/eips/setOutHeader-eip.adoc       |  3 +-
 .../org/apache/camel/builder/DataFormatClause.java | 15 ++++++++++
 .../camel/model/dataformat/BindyDataFormat.java    | 16 ++++++++++
 .../src/main/docs/bindy-dataformat.adoc            |  3 +-
 .../dataformat/bindy/BindyAbstractDataFormat.java  | 11 ++++++-
 .../bindy/csv/BindyComplexCsvUnmarshallTest.java   | 30 ++++++++++++++++---
 ...plexCsvUnmarshallUnwrapSingleInstanceTest.java} | 33 ++++++++++++---------
 ...vUnmarshallUnwrapSingleInstanceTest-context.xml | 34 ++++++++++++++++++++++
 .../BindyCsvDataFormatConfiguration.java           | 13 +++++++++
 .../BindyFixedLengthDataFormatConfiguration.java   | 13 +++++++++
 .../BindyKeyValuePairDataFormatConfiguration.java  | 13 +++++++++
 11 files changed, 164 insertions(+), 20 deletions(-)

diff --git a/camel-core/src/main/docs/eips/setOutHeader-eip.adoc b/camel-core/src/main/docs/eips/setOutHeader-eip.adoc
index a9d88fc..d80f8fa 100644
--- a/camel-core/src/main/docs/eips/setOutHeader-eip.adoc
+++ b/camel-core/src/main/docs/eips/setOutHeader-eip.adoc
@@ -1,3 +1,4 @@
+[[setOutHeader-eip]]
 == Set Out Header EIP (deprecated)
 == Set Header EIP
 
@@ -45,4 +46,4 @@ And the same example using XML:
         <to uri="direct:b"/>
     </route>
 </camelContext>
-----
+----
\ No newline at end of file
diff --git a/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java b/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
index 6fe6512..e60bf3f 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
@@ -188,6 +188,21 @@ public class DataFormatClause<T extends ProcessorDefinition<?>>
{
     }
 
     /**
+     * Uses the Bindy data format
+     *
+     * @param type      the type of bindy data format to use
+     * @param classType the POJO class type
+     * @param unwrapSingleInstance whether unmarshal should unwrap if there is a single instance
in the result
+     */
+    public T bindy(BindyType type, Class<?> classType, boolean unwrapSingleInstance)
{
+        BindyDataFormat bindy = new BindyDataFormat();
+        bindy.setType(type);
+        bindy.setClassType(classType);
+        bindy.setUnwrapSingleInstance(unwrapSingleInstance);
+        return dataFormat(bindy);
+    }
+
+    /**
      * Uses the Boon data format
      *
      * @param classType the POJO class type
diff --git a/camel-core/src/main/java/org/apache/camel/model/dataformat/BindyDataFormat.java
b/camel-core/src/main/java/org/apache/camel/model/dataformat/BindyDataFormat.java
index 3951845..4826f91 100644
--- a/camel-core/src/main/java/org/apache/camel/model/dataformat/BindyDataFormat.java
+++ b/camel-core/src/main/java/org/apache/camel/model/dataformat/BindyDataFormat.java
@@ -44,6 +44,8 @@ public class BindyDataFormat extends DataFormatDefinition {
     private String classType;
     @XmlAttribute
     private String locale;
+    @XmlAttribute @Metadata(defaultValue = "true")
+    private Boolean unwrapSingleInstance;
     @XmlTransient
     private Class<?> clazz;
 
@@ -93,6 +95,17 @@ public class BindyDataFormat extends DataFormatDefinition {
         this.locale = locale;
     }
 
+    public Boolean getUnwrapSingleInstance() {
+        return unwrapSingleInstance;
+    }
+
+    /**
+     * When unmarshalling should a single instance be unwrapped and returned instead of wrapped
in a <tt>java.util.List</tt>.
+     */
+    public void setUnwrapSingleInstance(Boolean unwrapSingleInstance) {
+        this.unwrapSingleInstance = unwrapSingleInstance;
+    }
+
     protected DataFormat createDataFormat(RouteContext routeContext) {
         if (classType == null && clazz == null) {
             throw new IllegalArgumentException("Either packages or classType must be specified");
@@ -120,6 +133,9 @@ public class BindyDataFormat extends DataFormatDefinition {
     protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext)
{
         setProperty(camelContext, dataFormat, "locale", locale);
         setProperty(camelContext, dataFormat, "classType", clazz);
+        if (unwrapSingleInstance != null) {
+            setProperty(camelContext, dataFormat, "unwrapSingleInstance", unwrapSingleInstance);
+        }
     }
 
 }
\ No newline at end of file
diff --git a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
index 538bd71..dacf65b 100644
--- a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
+++ b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
@@ -56,7 +56,7 @@ class names instead of package names now.
 ### Options
 
 // dataformat options: START
-The Bindy dataformat supports 4 options which are listed below.
+The Bindy dataformat supports 5 options which are listed below.
 
 
 
@@ -66,6 +66,7 @@ The Bindy dataformat supports 4 options which are listed below.
 | type |  | BindyType | Whether to use csv, fixed or key value pairs mode. The default value
is either Csv or KeyValue depending on chosen dataformat.
 | classType |  | String | Name of model class to use.
 | locale |  | String | To configure a default locale to use, such as us for united states.
To use the JVM platform default locale then use the name default
+| unwrapSingleInstance | true | Boolean | When unmarshalling should a single instance be
unwrapped and returned instead of wrapped in a java.util.List.
 | contentTypeHeader | false | Boolean | Whether the data format should set the Content-Type
header with the type from the data format if the data format is capable of doing so. For example
application/xml for data formats marshalling to XML, or application/json for data formats
marshalling to JSon etc.
 |===
 // dataformat options: END
diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java
index 8ae6dce..c1096c0 100644
--- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractDataFormat.java
@@ -47,6 +47,7 @@ public abstract class BindyAbstractDataFormat extends ServiceSupport implements
     private BindyAbstractFactory modelFactory;
     private Class<?> classType;
     private CamelContext camelContext;
+    private boolean unwrapSingleInstance = true;
 
     public BindyAbstractDataFormat() {
     }
@@ -71,6 +72,14 @@ public abstract class BindyAbstractDataFormat extends ServiceSupport implements
         this.locale = locale;
     }
 
+    public boolean isUnwrapSingleInstance() {
+        return unwrapSingleInstance;
+    }
+
+    public void setUnwrapSingleInstance(boolean unwrapSingleInstance) {
+        this.unwrapSingleInstance = unwrapSingleInstance;
+    }
+
     public BindyAbstractFactory getFactory() throws Exception {
         if (modelFactory == null) {
             FormatFactory formatFactory = createFormatFactory();
@@ -165,7 +174,7 @@ public abstract class BindyAbstractDataFormat extends ServiceSupport implements
                 }
             }
             // if there is only 1 then dont return a list
-            if (answer.size() == 1) {
+            if (isUnwrapSingleInstance() && answer.size() == 1) {
                 return answer.get(0);
             } else {
                 return answer;
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
index 59f7ec5..1a30209 100644
--- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
@@ -16,17 +16,23 @@
  */
 package org.apache.camel.dataformat.bindy.csv;
 
+import java.util.List;
+
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Assert;
 import org.junit.Test;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
 
 @ContextConfiguration
 public class BindyComplexCsvUnmarshallTest extends AbstractJUnit4SpringContextTests {
+
+    private static final Class<?> TYPE = org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Order.class;
+
     @Produce(uri = "direct:start")
     protected ProducerTemplate template;
 
@@ -37,23 +43,39 @@ public class BindyComplexCsvUnmarshallTest extends AbstractJUnit4SpringContextTe
                             + ",,,D,,BE12345678,SELL,,,,08-01-2009\r\n" + ",,,D,ISIN,BE12345678,,,,,08-01-2009\r\n"
+ ",,,D,ISIN,LU123456789,,,,,\r\n"
                             + "10,A8,Pauline,M,ISIN,XD12345678,SELL,Share,2500,USD,08-01-2009\r\n"
+ "10,A9,Pauline,M,ISIN,XD12345678,BUY,Share,2500.45,USD,08-01-2009";
 
+    private String singleRecord = "01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009";
+
     @EndpointInject(uri = "mock:result")
     private MockEndpoint resultEndpoint;
 
     @Test
     public void testUnMarshallMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(1);
+        resultEndpoint.message(0).body().isInstanceOf(List.class);
+
         template.sendBody(record);
+
+        resultEndpoint.assertIsSatisfied();
+
+        // there should be 13 element in the list
+        List list = resultEndpoint.getReceivedExchanges().get(0).getIn().getBody(List.class);
+        Assert.assertEquals(13, list.size());
+
+        resultEndpoint.reset();
+
+        // now single test
         resultEndpoint.expectedMessageCount(1);
+        resultEndpoint.message(0).body().isInstanceOf(TYPE);
+
+        template.sendBody(singleRecord);
+
         resultEndpoint.assertIsSatisfied();
     }
 
     public static class ContextConfig extends RouteBuilder {
         public void configure() {
-            Class<?> type =
-                org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Order.class;
-
             from("direct:start")
-                .unmarshal(new BindyCsvDataFormat(type))
+                .unmarshal(new BindyCsvDataFormat(TYPE))
                 .to("mock:result");
         }
     }
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallUnwrapSingleInstanceTest.java
similarity index 56%
copy from components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
copy to components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallUnwrapSingleInstanceTest.java
index 59f7ec5..a7c3e5d 100644
--- a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallTest.java
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallUnwrapSingleInstanceTest.java
@@ -16,44 +16,51 @@
  */
 package org.apache.camel.dataformat.bindy.csv;
 
+import java.util.List;
+
 import org.apache.camel.EndpointInject;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.model.dataformat.BindyType;
+import org.junit.Assert;
 import org.junit.Test;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
 
 @ContextConfiguration
-public class BindyComplexCsvUnmarshallTest extends AbstractJUnit4SpringContextTests {
+public class BindyComplexCsvUnmarshallUnwrapSingleInstanceTest extends AbstractJUnit4SpringContextTests
{
+
+    private static final Class<?> TYPE = org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Order.class;
+
     @Produce(uri = "direct:start")
     protected ProducerTemplate template;
 
-    private String record = "01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009\r\n"
+ "02,A1,,Preud'Homme,ISIN,XD12345678,BUY,,2500,USD,08-01-2009\r\n"
-                            + "03,A2,Jacques,,,BE12345678,SELL,,1500,EUR,08-01-2009\r\n"
+ "04,A3,Michel,Dupond,,,BUY,,2500,USD,08-01-2009\r\n"
-                            + "05,A4,Annie,Dutronc,ISIN,BE12345678,,,1500,EUR,08-01-2009\r\n"
+ "06,A5,Andr" + "\uc3a9" + ",Rieux,ISIN,XD12345678,SELL,Share,,USD,08-01-2009\r\n"
-                            + "07,A6,Myl" + "\uc3a8" + "ne,Farmer,ISIN,BE12345678,BUY,1500,,,08-01-2009\r\n"
+ "08,A7,Eva,Longoria,ISIN,XD12345678,SELL,Share,2500,USD,\r\n"
-                            + ",,,D,,BE12345678,SELL,,,,08-01-2009\r\n" + ",,,D,ISIN,BE12345678,,,,,08-01-2009\r\n"
+ ",,,D,ISIN,LU123456789,,,,,\r\n"
-                            + "10,A8,Pauline,M,ISIN,XD12345678,SELL,Share,2500,USD,08-01-2009\r\n"
+ "10,A9,Pauline,M,ISIN,XD12345678,BUY,Share,2500.45,USD,08-01-2009";
+    private String singleRecord = "01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009\r\n";
 
     @EndpointInject(uri = "mock:result")
     private MockEndpoint resultEndpoint;
 
     @Test
-    public void testUnMarshallMessage() throws Exception {
-        template.sendBody(record);
+    public void testUnMarshallSingleMessage() throws Exception {
         resultEndpoint.expectedMessageCount(1);
+        // should still be a list as we turned off unwrap
+        resultEndpoint.message(0).body().isInstanceOf(List.class);
+
+        template.sendBody(singleRecord);
+
         resultEndpoint.assertIsSatisfied();
+
+        // there should be 1 element in the list
+        List list = resultEndpoint.getReceivedExchanges().get(0).getIn().getBody(List.class);
+        Assert.assertEquals(1, list.size());
     }
 
     public static class ContextConfig extends RouteBuilder {
         public void configure() {
-            Class<?> type =
-                org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Order.class;
-
             from("direct:start")
-                .unmarshal(new BindyCsvDataFormat(type))
+                .unmarshal().bindy(BindyType.Csv, TYPE, false)
                 .to("mock:result");
         }
     }
diff --git a/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallUnwrapSingleInstanceTest-context.xml
b/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallUnwrapSingleInstanceTest-context.xml
new file mode 100644
index 0000000..6fc2203
--- /dev/null
+++ b/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindyComplexCsvUnmarshallUnwrapSingleInstanceTest-context.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="
+     http://www.springframework.org/schema/beans
+     http://www.springframework.org/schema/beans/spring-beans.xsd
+     http://camel.apache.org/schema/spring
+     http://camel.apache.org/schema/spring/camel-spring.xsd">
+     
+	<camelContext xmlns="http://camel.apache.org/schema/spring">
+		<routeBuilder ref="myBuilder" /> 
+	</camelContext>
+	
+	<bean id="myBuilder" class="org.apache.camel.dataformat.bindy.csv.BindyComplexCsvUnmarshallUnwrapSingleInstanceTest$ContextConfig"/>
+	
+</beans>
\ No newline at end of file
diff --git a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatConfiguration.java
b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatConfiguration.java
index 969b567..711a32d 100644
--- a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/csv/springboot/BindyCsvDataFormatConfiguration.java
@@ -47,6 +47,11 @@ public class BindyCsvDataFormatConfiguration
      */
     private String locale;
     /**
+     * When unmarshalling should a single instance be unwrapped and returned
+     * instead of wrapped in a java.util.List.
+     */
+    private Boolean unwrapSingleInstance = true;
+    /**
      * Whether the data format should set the Content-Type header with the type
      * from the data format if the data format is capable of doing so. For
      * example application/xml for data formats marshalling to XML, or
@@ -78,6 +83,14 @@ public class BindyCsvDataFormatConfiguration
         this.locale = locale;
     }
 
+    public Boolean getUnwrapSingleInstance() {
+        return unwrapSingleInstance;
+    }
+
+    public void setUnwrapSingleInstance(Boolean unwrapSingleInstance) {
+        this.unwrapSingleInstance = unwrapSingleInstance;
+    }
+
     public Boolean getContentTypeHeader() {
         return contentTypeHeader;
     }
diff --git a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatConfiguration.java
b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatConfiguration.java
index c2eeaa6..f6b78a0 100644
--- a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/fixed/springboot/BindyFixedLengthDataFormatConfiguration.java
@@ -47,6 +47,11 @@ public class BindyFixedLengthDataFormatConfiguration
      */
     private String locale;
     /**
+     * When unmarshalling should a single instance be unwrapped and returned
+     * instead of wrapped in a java.util.List.
+     */
+    private Boolean unwrapSingleInstance = true;
+    /**
      * Whether the data format should set the Content-Type header with the type
      * from the data format if the data format is capable of doing so. For
      * example application/xml for data formats marshalling to XML, or
@@ -78,6 +83,14 @@ public class BindyFixedLengthDataFormatConfiguration
         this.locale = locale;
     }
 
+    public Boolean getUnwrapSingleInstance() {
+        return unwrapSingleInstance;
+    }
+
+    public void setUnwrapSingleInstance(Boolean unwrapSingleInstance) {
+        this.unwrapSingleInstance = unwrapSingleInstance;
+    }
+
     public Boolean getContentTypeHeader() {
         return contentTypeHeader;
     }
diff --git a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatConfiguration.java
b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatConfiguration.java
index 592ebe3..8d42447 100644
--- a/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatConfiguration.java
+++ b/platforms/spring-boot/components-starter/camel-bindy-starter/src/main/java/org/apache/camel/dataformat/bindy/kvp/springboot/BindyKeyValuePairDataFormatConfiguration.java
@@ -47,6 +47,11 @@ public class BindyKeyValuePairDataFormatConfiguration
      */
     private String locale;
     /**
+     * When unmarshalling should a single instance be unwrapped and returned
+     * instead of wrapped in a java.util.List.
+     */
+    private Boolean unwrapSingleInstance = true;
+    /**
      * Whether the data format should set the Content-Type header with the type
      * from the data format if the data format is capable of doing so. For
      * example application/xml for data formats marshalling to XML, or
@@ -78,6 +83,14 @@ public class BindyKeyValuePairDataFormatConfiguration
         this.locale = locale;
     }
 
+    public Boolean getUnwrapSingleInstance() {
+        return unwrapSingleInstance;
+    }
+
+    public void setUnwrapSingleInstance(Boolean unwrapSingleInstance) {
+        this.unwrapSingleInstance = unwrapSingleInstance;
+    }
+
     public Boolean getContentTypeHeader() {
         return contentTypeHeader;
     }

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

Mime
View raw message