You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2024/09/27 07:52:23 UTC

(camel-karaf) branch main updated: Ref #506: Add jacksonxml integration test (#507)

This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new 994d4c5a5 Ref #506: Add jacksonxml integration test (#507)
994d4c5a5 is described below

commit 994d4c5a5c55b8df636a45a9eb9f0051e2300b3b
Author: Stefan Tataru <st...@gmail.com>
AuthorDate: Fri Sep 27 09:52:19 2024 +0200

    Ref #506: Add jacksonxml integration test (#507)
---
 tests/features/camel-jacksonxml/pom.xml            |  59 ++++++++++++
 .../camel/test/CamelJacksonxmlRouteSupplier.java   | 106 +++++++++++++++++++++
 .../karaf/camel/itest/CamelJacksonxmlITest.java    |  48 ++++++++++
 tests/features/pom.xml                             |   1 +
 4 files changed, 214 insertions(+)

diff --git a/tests/features/camel-jacksonxml/pom.xml b/tests/features/camel-jacksonxml/pom.xml
new file mode 100644
index 000000000..83449eb7a
--- /dev/null
+++ b/tests/features/camel-jacksonxml/pom.xml
@@ -0,0 +1,59 @@
+<?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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.camel.karaf</groupId>
+        <artifactId>camel-karaf-features-test</artifactId>
+        <version>4.8.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-jacksonxml-test</artifactId>
+    <name>Apache Camel :: Karaf :: Tests :: Features :: Jacksonxml</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-core</artifactId>
+            <version>${jackson2-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>${jackson2-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-annotations</artifactId>
+            <version>${jackson2-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.fasterxml.jackson.dataformat</groupId>
+            <artifactId>jackson-dataformat-xml</artifactId>
+            <version>${jackson2-version}</version>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/tests/features/camel-jacksonxml/src/main/java/org/apache/karaf/camel/test/CamelJacksonxmlRouteSupplier.java b/tests/features/camel-jacksonxml/src/main/java/org/apache/karaf/camel/test/CamelJacksonxmlRouteSupplier.java
new file mode 100644
index 000000000..6178a02b9
--- /dev/null
+++ b/tests/features/camel-jacksonxml/src/main/java/org/apache/karaf/camel/test/CamelJacksonxmlRouteSupplier.java
@@ -0,0 +1,106 @@
+/*
+ * 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.karaf.camel.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.karaf.camel.itests.AbstractCamelSingleFeatureResultMockBasedRouteSupplier;
+import org.apache.karaf.camel.itests.CamelRouteSupplier;
+import org.osgi.service.component.annotations.Component;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
+
+@Component(
+        name = "karaf-camel-jacksonxml-test",
+        immediate = true,
+        service = CamelRouteSupplier.class
+)
+public class CamelJacksonxmlRouteSupplier extends AbstractCamelSingleFeatureResultMockBasedRouteSupplier {
+
+    public static final String XML_SAMPLE_NAME = "Jack";
+    public static final int XML_SAMPLE_AGE = 9;
+
+    @Override
+    protected boolean consumerEnabled() {
+        return false;
+    }
+
+    @JsonInclude(JsonInclude.Include.NON_NULL)
+    public static class MyData {
+        private String name;
+        private String nikname;
+        private int age;
+
+        // empty constructor is needed by default
+        public MyData() {
+        };
+
+        // getters and setters
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public String getNikname() {
+            return nikname;
+        }
+
+        public void setNikname(String nikname) {
+            this.nikname = nikname;
+        }
+
+        public int getAge() {
+            return age;
+        }
+
+        public void setAge(int age) {
+            this.age = age;
+        }
+    }
+
+    @Override
+    protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) {
+        producerRoute.log("Will unmarshal: ${body}")
+        .unmarshal().jacksonXml(MyData.class)
+        .log("Unmarshal: ${body}")
+        .process(ex -> {
+            MyData data = ex.getIn().getBody(MyData.class);
+            assertEquals(XML_SAMPLE_NAME, data.getName());
+            assertNull(data.getNikname());
+            assertEquals(XML_SAMPLE_AGE, data.getAge());
+        })
+        .log("Will marshal: ${body}")
+        .marshal().jacksonXml()
+        .log("Marshal: ${body}")
+        .process(ex -> {
+            String data = ex.getIn().getBody(String.class);
+
+            XmlMapper xmlMapper = new XmlMapper();
+            JsonNode jsonNode = xmlMapper.readTree(data);
+            assertEquals(XML_SAMPLE_NAME, jsonNode.get("name").asText());
+            assertNull(jsonNode.get("nikname"));
+            assertEquals(XML_SAMPLE_AGE, jsonNode.get("age").asInt());
+        }).toF("mock:%s", getResultMockName());
+    }
+}
\ No newline at end of file
diff --git a/tests/features/camel-jacksonxml/src/test/java/org/apache/karaf/camel/itest/CamelJacksonxmlITest.java b/tests/features/camel-jacksonxml/src/test/java/org/apache/karaf/camel/itest/CamelJacksonxmlITest.java
new file mode 100644
index 000000000..d3effac5a
--- /dev/null
+++ b/tests/features/camel-jacksonxml/src/test/java/org/apache/karaf/camel/itest/CamelJacksonxmlITest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed 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.karaf.camel.itest;
+
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.karaf.camel.itests.AbstractCamelSingleFeatureResultMockBasedRouteITest;
+import org.apache.karaf.camel.itests.CamelKarafTestHint;
+import org.apache.karaf.camel.test.CamelJacksonxmlRouteSupplier;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+@CamelKarafTestHint
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class CamelJacksonxmlITest extends AbstractCamelSingleFeatureResultMockBasedRouteITest {
+
+    private static final String XML_SAMPLE = "<MyData xmlns=\"\"><name>%s</name><age>%d</age></MyData>"
+            .formatted(CamelJacksonxmlRouteSupplier.XML_SAMPLE_NAME, CamelJacksonxmlRouteSupplier.XML_SAMPLE_AGE);
+
+    @Override
+    public String getBodyToSend() {
+        return XML_SAMPLE;
+    }
+
+    @Override
+    public void configureMock(MockEndpoint mock) {
+        mock.expectedBodiesReceived(XML_SAMPLE);
+    }
+
+    @Test
+    public void testResultMock() throws Exception {
+        assertMockEndpointsSatisfied();
+    }
+}
\ No newline at end of file
diff --git a/tests/features/pom.xml b/tests/features/pom.xml
index 9951c8d74..2e7c9564d 100644
--- a/tests/features/pom.xml
+++ b/tests/features/pom.xml
@@ -82,6 +82,7 @@
         <module>camel-hazelcast</module>
         <module>camel-http</module>
         <module>camel-influxdb2</module>
+        <module>camel-jacksonxml</module>
         <module>camel-jcache</module>
         <module>camel-jetty</module>
         <module>camel-jms</module>