camel-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CAMEL-12009) Bindy - Missing Headers from OneToMany Field
Date Wed, 15 Nov 2017 10:28:00 GMT

    [ https://issues.apache.org/jira/browse/CAMEL-12009?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16253234#comment-16253234
] 

ASF GitHub Bot commented on CAMEL-12009:
----------------------------------------

davsclaus closed pull request #2092: CAMEL-12009: Generate headers when OneToMany tag
URL: https://github.com/apache/camel/pull/2092
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
index e57acd5fa1d..f0bc611708c 100644
--- a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
+++ b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
@@ -17,6 +17,8 @@
 package org.apache.camel.dataformat.bindy;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.text.NumberFormat;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -27,6 +29,7 @@
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.dataformat.bindy.annotation.Link;
+import org.apache.camel.dataformat.bindy.annotation.OneToMany;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -93,6 +96,24 @@ private void loadModels(Class<?> root) {
                 
                 loadModels(field.getType());
             }
+
+            OneToMany oneToManyField = field.getAnnotation(OneToMany.class);
+
+            if (oneToManyField != null) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Class (OneToMany) linked: {}, Field: {}", field.getType(),
field);
+                }
+
+                Type listType = field.getGenericType();
+                Type type = ((ParameterizedType) listType).getActualTypeArguments()[0];
+                Class clazz = (Class<?>)type;
+
+                models.add(clazz);
+                modelClassNames.add(clazz.getName());
+
+                loadModels(clazz);
+            }
+
         }
     }
 
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalOneToManyWithHeadersTest.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalOneToManyWithHeadersTest.java
new file mode 100644
index 00000000000..5e360a489d1
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindyMarshalOneToManyWithHeadersTest.java
@@ -0,0 +1,96 @@
+package org.apache.camel.dataformat.bindy.csv;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.LoggingLevel;
+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.dataformat.bindy.model.simple.linkonetomany.Order;
+import org.apache.camel.dataformat.bindy.model.simple.linkonetomany.OrderItem;
+import org.apache.camel.processor.interceptor.Tracer;
+import org.junit.Test;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+
+import java.util.Arrays;
+import java.util.List;
+
+@ContextConfiguration
+public class BindyMarshalOneToManyWithHeadersTest extends AbstractJUnit4SpringContextTests
{
+
+    private static final String URI_MOCK_RESULT = "mock:result";
+    private static final String URI_MOCK_ERROR = "mock:error";
+    private static final String URI_DIRECT_START = "direct:start";
+
+    private Order order;
+    private String expected;
+
+    @Produce(uri = URI_DIRECT_START)
+    private ProducerTemplate template;
+
+    @EndpointInject(uri = URI_MOCK_RESULT)
+    private MockEndpoint result;
+
+    @Test
+    @DirtiesContext
+    public void testMarshallMessage() throws Exception {
+
+        expected = "orderNumber,customerName,sku,quantity,unitPrice\r\n" +
+                "11111,Joe Blow,abc,1,3\r\n" +
+                "11111,Joe Blow,cde,3,2\r\n";
+
+        result.expectedBodiesReceived(expected);
+
+        template.sendBody(generateModel());
+
+        result.assertIsSatisfied();
+    }
+
+    public Order generateModel() {
+
+        Order order = new Order();
+        order.setCustomerName("Joe Blow");
+        order.setOrderNumber(11111);
+
+        OrderItem oi1 = new OrderItem();
+        oi1.setSku("abc");
+        oi1.setQuantity(1);
+        oi1.setUnitPrice(3);
+
+        OrderItem oi2 = new OrderItem();
+        oi2.setSku("cde");
+        oi2.setQuantity(3);
+        oi2.setUnitPrice(2);
+
+        List<OrderItem> orderList = Arrays.asList(oi1, oi2);
+        order.setItems(orderList);
+
+        return order;
+    }
+
+    public static class ContextConfig extends RouteBuilder {
+
+        public void configure() {
+
+            Tracer tracer = new Tracer();
+            tracer.setLogLevel(LoggingLevel.ERROR);
+            tracer.setLogName("org.apache.camel.bindy");
+
+            getContext().addInterceptStrategy(tracer);
+
+            BindyCsvDataFormat camelDataFormat = new BindyCsvDataFormat(Order.class);
+            camelDataFormat.setLocale("en");
+
+            // default should errors go to mock:error
+            errorHandler(deadLetterChannel(URI_MOCK_ERROR).redeliveryDelay(0));
+
+            onException(Exception.class).maximumRedeliveries(0).handled(true);
+
+            from(URI_DIRECT_START).marshal(camelDataFormat).to(URI_MOCK_RESULT);
+        }
+
+    }
+
+}
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/linkonetomany/Order.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/linkonetomany/Order.java
new file mode 100644
index 00000000000..c08ea09f5c3
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/linkonetomany/Order.java
@@ -0,0 +1,44 @@
+package org.apache.camel.dataformat.bindy.model.simple.linkonetomany;
+
+import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.annotation.DataField;
+import org.apache.camel.dataformat.bindy.annotation.OneToMany;
+
+import java.util.List;
+
+@CsvRecord(separator = ",", generateHeaderColumns = true)
+public class Order {
+
+    @DataField(pos = 1)
+    private int orderNumber;
+
+    @DataField(pos = 2)
+    private String customerName;
+
+    @OneToMany
+    private List<OrderItem> items;
+
+    public int getOrderNumber() {
+        return orderNumber;
+    }
+
+    public void setOrderNumber(int orderNumber) {
+        this.orderNumber = orderNumber;
+    }
+
+    public String getCustomerName() {
+        return customerName;
+    }
+
+    public void setCustomerName(String customerName) {
+        this.customerName = customerName;
+    }
+
+    public List<OrderItem> getItems() {
+        return items;
+    }
+
+    public void setItems(List<OrderItem> items) {
+        this.items = items;
+    }
+}
diff --git a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/linkonetomany/OrderItem.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/linkonetomany/OrderItem.java
new file mode 100644
index 00000000000..e6d0b75b86d
--- /dev/null
+++ b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/model/simple/linkonetomany/OrderItem.java
@@ -0,0 +1,41 @@
+package org.apache.camel.dataformat.bindy.model.simple.linkonetomany;
+
+import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
+import org.apache.camel.dataformat.bindy.annotation.DataField;
+
+@CsvRecord(separator = ",", generateHeaderColumns = true)
+public class OrderItem {
+
+    @DataField(pos = 3)
+    private String sku;
+
+    @DataField(pos = 4)
+    private int quantity;
+
+    @DataField(pos = 5)
+    private int unitPrice;
+
+    public String getSku() {
+        return sku;
+    }
+
+    public void setSku(String sku) {
+        this.sku = sku;
+    }
+
+    public int getQuantity() {
+        return quantity;
+    }
+
+    public void setQuantity(int quantity) {
+        this.quantity = quantity;
+    }
+
+    public int getUnitPrice() {
+        return unitPrice;
+    }
+
+    public void setUnitPrice(int unitPrice) {
+        this.unitPrice = unitPrice;
+    }
+}
diff --git a/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindyMarshalOneToManyWithHeadersTest-context.xml
b/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindyMarshalOneToManyWithHeadersTest-context.xml
new file mode 100644
index 00000000000..44680817291
--- /dev/null
+++ b/components/camel-bindy/src/test/resources/org/apache/camel/dataformat/bindy/csv/BindyMarshalOneToManyWithHeadersTest-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.BindyMarshalOneToManyWithHeadersTest$ContextConfig"/>
+
+</beans>
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Bindy - Missing Headers from OneToMany Field
> --------------------------------------------
>
>                 Key: CAMEL-12009
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12009
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-bindy
>            Reporter: Khaled AlTurkestani
>            Priority: Minor
>
> When generating a CSV header, the code doesn't follow the @OneToMany field and thus doesn't
print its fields in the header. For example, when marshalling a list of Orders (List<Order>)
where the classes are the ones below: 
> {code}
> @CsvRecord(separator = ",", generateHeaderColumns = true)
> public class Order {
>     @DataField(pos = 1)
>     private int orderNumber;
>     @DataField(pos = 2)
>     private String customerName;
>     @OneToMany
>     private List<OrderItem> items;
> }
> {code}
> {code}
> @CsvRecord(separator = ",", generateHeaderColumns = true)
> public class OrderItem {
>     @DataField(pos = 3)
>     private String sku;
>     @DataField(pos = 4)
>     private int quantity;
>     @DataField(pos = 5)
>     private int unitPrice;
> }
> {code}
> we get the following header:
> +orderNumber,customerName+
> when, instead, we should be getting the following header:
> +orderNumber,customerName,sku,quantity,unitPrice+



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message