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-12009: Generate headers when OneToMany tag
Date Wed, 15 Nov 2017 10:27:27 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 1c32a84a91014e3c495c33ae9b6051e84e32d0b4
Author: Khaled AlTurkestani <khaled@ooma.com>
AuthorDate: Tue Nov 14 17:16:57 2017 -0800

    CAMEL-12009: Generate headers when OneToMany tag
    
    Includes unit test
---
 .../dataformat/bindy/BindyAbstractFactory.java     | 21 +++++
 .../csv/BindyMarshalOneToManyWithHeadersTest.java  | 96 ++++++++++++++++++++++
 .../bindy/model/simple/linkonetomany/Order.java    | 44 ++++++++++
 .../model/simple/linkonetomany/OrderItem.java      | 41 +++++++++
 ...indyMarshalOneToManyWithHeadersTest-context.xml | 34 ++++++++
 5 files changed, 236 insertions(+)

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 7beea26..d2510dd 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;
@@ -26,6 +28,7 @@ import java.util.Map;
 import java.util.Set;
 
 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;
@@ -92,6 +95,24 @@ public abstract class BindyAbstractFactory implements BindyFactory {
                 
                 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 0000000..5e360a4
--- /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 0000000..c08ea09
--- /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 0000000..e6d0b75
--- /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 0000000..4468081
--- /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

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <commits@camel.apache.org>.

Mime
View raw message