From fce2f1dfd832a45e40a1ea6198edfbdf82855767 Mon Sep 17 00:00:00 2001 From: Stefan Tataru Date: Thu, 3 Oct 2024 17:23:11 +0200 Subject: [PATCH] Add jackson-avro integration test --- features/src/main/feature/camel-features.xml | 5 +- tests/features/camel-jackson-avro/pom.xml | 54 +++++++++ .../test/CamelJacksonAvroRouteSupplier.java | 104 ++++++++++++++++++ .../camel/itest/CamelJacksonAvroITest.java | 40 +++++++ tests/features/pom.xml | 1 + 5 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 tests/features/camel-jackson-avro/pom.xml create mode 100644 tests/features/camel-jackson-avro/src/main/java/org/apache/karaf/camel/test/CamelJacksonAvroRouteSupplier.java create mode 100644 tests/features/camel-jackson-avro/src/test/java/org/apache/karaf/camel/itest/CamelJacksonAvroITest.java diff --git a/features/src/main/feature/camel-features.xml b/features/src/main/feature/camel-features.xml index 1a668a2cc..c8f7d3243 100644 --- a/features/src/main/feature/camel-features.xml +++ b/features/src/main/feature/camel-features.xml @@ -1587,8 +1587,9 @@ camel-jackson guava - mvn:org.apache.commons/commons-compress/${auto-detect-version} - mvn:org.apache.avro/avro/${auto-detect-version} + mvn:commons-io/commons-io/${commons-io-version} + mvn:org.apache.commons/commons-compress/${commons-compress-version} + mvn:org.apache.avro/avro/${avro-version} mvn:org.codehaus.jackson/jackson-core-asl/${auto-detect-version} mvn:org.codehaus.jackson/jackson-mapper-asl/${auto-detect-version} mvn:com.thoughtworks.paranamer/paranamer/${auto-detect-version} diff --git a/tests/features/camel-jackson-avro/pom.xml b/tests/features/camel-jackson-avro/pom.xml new file mode 100644 index 000000000..85499a4c2 --- /dev/null +++ b/tests/features/camel-jackson-avro/pom.xml @@ -0,0 +1,54 @@ + + + + 4.0.0 + + org.apache.camel.karaf + camel-karaf-features-test + 4.8.0-SNAPSHOT + + + camel-jackson-avro-test + Apache Camel :: Karaf :: Tests :: Features :: Jackson Avro + + + + junit + junit + + + org.apache.camel + camel-jackson + ${camel-version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson2-version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-avro + ${jackson2-version} + + + \ No newline at end of file diff --git a/tests/features/camel-jackson-avro/src/main/java/org/apache/karaf/camel/test/CamelJacksonAvroRouteSupplier.java b/tests/features/camel-jackson-avro/src/main/java/org/apache/karaf/camel/test/CamelJacksonAvroRouteSupplier.java new file mode 100644 index 000000000..1cb1f7fea --- /dev/null +++ b/tests/features/camel-jackson-avro/src/main/java/org/apache/karaf/camel/test/CamelJacksonAvroRouteSupplier.java @@ -0,0 +1,104 @@ +/* + * 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.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jackson.SchemaResolver; +import org.apache.camel.model.RouteDefinition; +import org.apache.camel.model.dataformat.AvroDataFormat; +import org.apache.camel.model.dataformat.AvroLibrary; +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.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.dataformat.avro.AvroMapper; +import com.fasterxml.jackson.dataformat.avro.AvroSchema; + +@Component( + name = "karaf-camel-jackson-avro-test", + immediate = true, + service = CamelRouteSupplier.class +) +public class CamelJacksonAvroRouteSupplier extends AbstractCamelSingleFeatureResultMockBasedRouteSupplier { + + public static final String AVRO_BEAN = "avroBean"; + public static final String AVRO_SAMPLE_NAME = "Jack Aveiro"; + public static final int AVRO_SAMPLE_AGE = 99; + + @Override + protected boolean consumerEnabled() { + return false; + } + + @Override + public void configure(CamelContext context) { + AvroDataFormat avro = new AvroDataFormat(); + avro.setLibrary(AvroLibrary.Jackson); + avro.setUnmarshalType(JsonNode.class); + avro.setAutoDiscoverSchemaResolver(Boolean.TRUE.toString()); + context.getRegistry().bind(AVRO_BEAN, avro); + + try { + AvroSchema schema = new AvroMapper().schemaFor(MyData.class); + SchemaResolver resolver = ex -> schema; + context.getRegistry().bind("schema-resolver", SchemaResolver.class, resolver); + } catch (JsonMappingException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) { + producerRoute.setBody(ex -> new MyData(AVRO_SAMPLE_NAME, AVRO_SAMPLE_AGE)) + .log("Will marshal: ${body}") + .marshal(AVRO_BEAN) + .log("Marshal: ${body}") + .process(ex -> { + byte[] avroData = ex.getIn().getBody(byte[].class); + assertNotNull(avroData); + assertTrue(avroData.length > 0); + }) + .toF("mock:%s", getResultMockName()) + .log("Will unmarshal: ${body}") + .unmarshal(AVRO_BEAN) + .log("Unmarshal: ${body}") + .process(ex -> { + JsonNode data = ex.getIn().getBody(JsonNode.class); + assertEquals(AVRO_SAMPLE_NAME, data.get("name").asText()); + assertEquals(AVRO_SAMPLE_AGE, data.get("age").asInt()); + }) + .toF("mock:%s", getResultMockName()); + } + + public static class MyData { + + private String name; + + private int age; + + public MyData(String name, int age) { + this.name = name; + this.age = age; + } + } +} \ No newline at end of file diff --git a/tests/features/camel-jackson-avro/src/test/java/org/apache/karaf/camel/itest/CamelJacksonAvroITest.java b/tests/features/camel-jackson-avro/src/test/java/org/apache/karaf/camel/itest/CamelJacksonAvroITest.java new file mode 100644 index 000000000..074ddff74 --- /dev/null +++ b/tests/features/camel-jackson-avro/src/test/java/org/apache/karaf/camel/itest/CamelJacksonAvroITest.java @@ -0,0 +1,40 @@ +/* + * 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.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 CamelJacksonAvroITest extends AbstractCamelSingleFeatureResultMockBasedRouteITest { + + @Override + public void configureMock(MockEndpoint mock) { + mock.expectedMessageCount(2); + } + + @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 9a33f3bb3..608d66c25 100644 --- a/tests/features/pom.xml +++ b/tests/features/pom.xml @@ -83,6 +83,7 @@ camel-http camel-influxdb2 camel-jackson + camel-jackson-avro camel-jacksonxml camel-jcache camel-jetty