This is an automated email from the ASF dual-hosted git repository. ffang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git
The following commit(s) were added to refs/heads/main by this push: new a980349 [CAMEL-17746]add tests in camel-gson-starter (#455) a980349 is described below commit a98034901d6d60df838fbcf7553a145e2827d695 Author: Freeman(Yue) Fang <freeman.f...@gmail.com> AuthorDate: Fri Mar 4 21:20:07 2022 -0500 [CAMEL-17746]add tests in camel-gson-starter (#455) --- .../component/gson/springboot/ExcludeAge.java | 27 ++++ .../component/gson/springboot/ExcludeWeight.java | 27 ++++ .../gson/springboot/GsonConcurrentTest.java | 114 ++++++++++++++ .../gson/springboot/GsonFieldNamePolicyTest.java | 102 +++++++++++++ .../gson/springboot/GsonJsonDataFormatTest.java | 91 +++++++++++ .../gson/springboot/GsonMarshalExclusionTest.java | 167 +++++++++++++++++++++ .../gson/springboot/GsonMarshalListTest.java | 114 ++++++++++++++ .../component/gson/springboot/GsonMarshalTest.java | 128 ++++++++++++++++ .../GsonUnmarshalTypeFromHeaderTest.java | 83 ++++++++++ .../component/gson/springboot/PersonPojo.java | 48 ++++++ .../springboot/SpringGsonFieldNamePolicyTest.java | 89 +++++++++++ .../springboot/SpringGsonJsonDataFormatTest.java | 116 ++++++++++++++ .../SpringGsonUnmarshalTypeFromHeaderTest.java | 60 ++++++++ .../camel/component/gson/springboot/TestPojo.java | 45 ++++++ .../gson/springboot/TestPojoExclusion.java | 82 ++++++++++ .../routes/SpringGsonFieldNamePolicyTest.xml | 35 +++++ .../routes/SpringGsonJsonDataFormatTest.xml | 51 +++++++ .../SpringGsonUnmarshalTypeFromHeaderTest.xml | 32 ++++ 18 files changed, 1411 insertions(+) diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/ExcludeAge.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/ExcludeAge.java new file mode 100644 index 0000000..705dc24 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/ExcludeAge.java @@ -0,0 +1,27 @@ +/* + * 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.camel.component.gson.springboot; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.FIELD }) +public @interface ExcludeAge { +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/ExcludeWeight.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/ExcludeWeight.java new file mode 100644 index 0000000..c5cad43 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/ExcludeWeight.java @@ -0,0 +1,27 @@ +/* + * 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.camel.component.gson.springboot; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.FIELD }) +public @interface ExcludeWeight { +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonConcurrentTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonConcurrentTest.java new file mode 100644 index 0000000..9daf90d5 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonConcurrentTest.java @@ -0,0 +1,114 @@ +/* + * 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.camel.component.gson.springboot; + + +import static org.apache.camel.test.junit5.TestSupport.body; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.dataformat.JsonLibrary; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + GsonConcurrentTest.class, + GsonConcurrentTest.TestConfiguration.class + } +) +public class GsonConcurrentTest { + + + @Autowired + ProducerTemplate template; + + @EndpointInject("mock:result") + MockEndpoint mock; + + + @Test + public void testNoConcurrentProducers() throws Exception { + doSendMessages(1, 1); + } + + @Test + public void testConcurrentProducers() throws Exception { + doSendMessages(10, 5); + } + + private void doSendMessages(int files, int poolSize) throws Exception { + mock.reset(); + mock.expectedMessageCount(files); + mock.assertNoDuplicates(body()); + + ExecutorService executor = Executors.newFixedThreadPool(poolSize); + for (int i = 0; i < files; i++) { + final int index = i; + executor.submit(new Callable<Object>() { + public Object call() throws Exception { + TestPojo pojo = new TestPojo(); + pojo.setName("Hi " + index); + + template.sendBody("direct:start", pojo); + return null; + } + }); + } + + mock.assertIsSatisfied(); + executor.shutdownNow(); + } + + // ************************************* + // Config + // ************************************* + + @Configuration + public static class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").marshal().json(JsonLibrary.Gson).to("log:marshalled").to("direct:marshalled"); + + from("direct:marshalled").unmarshal().json(JsonLibrary.Gson, TestPojo.class).to("mock:result"); + } + }; + } + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonFieldNamePolicyTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonFieldNamePolicyTest.java new file mode 100644 index 0000000..64a0cc5 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonFieldNamePolicyTest.java @@ -0,0 +1,102 @@ +/* + * 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.camel.component.gson.springboot; + + + +import com.google.gson.FieldNamingPolicy; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.gson.GsonDataFormat; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + GsonFieldNamePolicyTest.class, + GsonFieldNamePolicyTest.TestConfiguration.class + } +) +public class GsonFieldNamePolicyTest { + + + @Autowired + ProducerTemplate template; + + + + @Test + public void testUnmarshalPojo() { + String json = "{\"id\":\"123\",\"first_name\":\"Donald\",\"last_name\":\"Duck\"}"; + PersonPojo pojo = template.requestBody("direct:backPojo", json, PersonPojo.class); + assertNotNull(pojo); + + assertEquals(123, pojo.getId()); + assertEquals("Donald", pojo.getFirstName()); + assertEquals("Duck", pojo.getLastName()); + } + + @Test + public void testMarshalPojo() { + PersonPojo pojo = new PersonPojo(); + pojo.setId(123); + pojo.setFirstName("Donald"); + pojo.setLastName("Duck"); + + String expected = "{\"id\":123,\"first_name\":\"Donald\",\"last_name\":\"Duck\"}"; + String json = template.requestBody("direct:inPojo", pojo, String.class); + assertEquals(expected, json); + } + + // ************************************* + // Config + // ************************************* + + @Configuration + public static class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + GsonDataFormat formatPojo = new GsonDataFormat(); + formatPojo.setUnmarshalType(PersonPojo.class); + formatPojo.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); + + from("direct:inPojo").marshal(formatPojo); + from("direct:backPojo").unmarshal(formatPojo); + } + }; + } + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonJsonDataFormatTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonJsonDataFormatTest.java new file mode 100644 index 0000000..2d3297b --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonJsonDataFormatTest.java @@ -0,0 +1,91 @@ +/* + * 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.camel.component.gson.springboot; + + +import java.util.Map; + +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.model.dataformat.JsonLibrary; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + GsonJsonDataFormatTest.class, + GsonJsonDataFormatTest.TestConfiguration.class + } +) +public class GsonJsonDataFormatTest { + + + @Autowired + ProducerTemplate template; + + @EndpointInject("mock:reversePojo") + MockEndpoint mock; + + + @Test + public void testUnmarshalMap() { + Map<?, ?> unmarshalled = template.requestBody("direct:json", + "{\"pointsOfSale\":{\"pointOfSale\":{\"prodcut\":\"newpad\"}}}", Map.class); + Map<?, ?> map1 = (Map<?, ?>) unmarshalled.get("pointsOfSale"); + Map<?, ?> map2 = (Map<?, ?>) map1.get("pointOfSale"); + assertEquals("newpad", map2.get("prodcut"), "Don't get the right value"); + } + + // ************************************* + // Config + // ************************************* + + @Configuration + public static class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:in").marshal().json(JsonLibrary.Gson); + from("direct:back").unmarshal().json(JsonLibrary.Gson).to("mock:reverse"); + + from("direct:inPojo").marshal().json(JsonLibrary.Gson); + from("direct:backPojo").unmarshal().json(JsonLibrary.Gson, TestPojo.class).to("mock:reversePojo"); + + from("direct:json").unmarshal().json(JsonLibrary.Gson, Map.class); + } + }; + } + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalExclusionTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalExclusionTest.java new file mode 100644 index 0000000..210c867 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalExclusionTest.java @@ -0,0 +1,167 @@ +/* + * 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.camel.component.gson.springboot; + + +import java.util.Arrays; + +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.gson.GsonDataFormat; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + GsonMarshalExclusionTest.class, + GsonMarshalExclusionTest.TestConfiguration.class + } +) +public class GsonMarshalExclusionTest { + + @Autowired + CamelContext context; + + @Autowired + ProducerTemplate template; + + @EndpointInject("mock:reversePojoExcludeWeight") + MockEndpoint mockWeight; + + @EndpointInject("mock:reversePojoExcludeAge") + MockEndpoint mockAge; + + + @Test + public void testMarshalAndUnmarshalPojoWithExclusion() throws Exception { + + TestPojoExclusion in = new TestPojoExclusion(); + + + mockWeight.expectedMessageCount(1); + mockWeight.message(0).body().isInstanceOf(TestPojoExclusion.class); + mockWeight.message(0).body().isEqualTo(in); + + Object marshalled = template.requestBody("direct:inPojoExcludeWeight", in); + String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled); + assertEquals("{\"age\":30,\"height\":190}", marshalledAsString); + + template.sendBody("direct:backPojoExcludeWeight", marshalled); + + mockWeight.assertIsSatisfied(); + } + + @Test + public void testMarshalAndUnmarshalPojoWithAnotherExclusion() throws Exception { + + TestPojoExclusion in = new TestPojoExclusion(); + + mockAge.expectedMessageCount(1); + mockAge.message(0).body().isInstanceOf(TestPojoExclusion.class); + mockAge.message(0).body().isEqualTo(in); + + Object marshalled = template.requestBody("direct:inPojoExcludeAge", in); + String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled); + assertEquals("{\"height\":190,\"weight\":70}", marshalledAsString); + + template.sendBody("direct:backPojoExcludeAge", marshalled); + + mockAge.assertIsSatisfied(); + } + + /** + * Strategy to exclude {@link ExcludeWeight} annotated fields + */ + protected static class WeightExclusionStrategy implements ExclusionStrategy { + + @Override + public boolean shouldSkipField(FieldAttributes f) { + return f.getAnnotation(ExcludeWeight.class) != null; + } + + @Override + public boolean shouldSkipClass(Class<?> clazz) { + return false; + } + } + + //START SNIPPET: strategy + /** + * Strategy to exclude {@link ExcludeAge} annotated fields + */ + protected static class AgeExclusionStrategy implements ExclusionStrategy { + + @Override + public boolean shouldSkipField(FieldAttributes f) { + return f.getAnnotation(ExcludeAge.class) != null; + } + + @Override + public boolean shouldSkipClass(Class<?> clazz) { + return false; + } + } + //END SNIPPET: strategy + + // ************************************* + // Config + // ************************************* + + @Configuration + public static class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + + GsonDataFormat weightExclusionFormat = new GsonDataFormat(TestPojoExclusion.class); + weightExclusionFormat.setExclusionStrategies(Arrays.<ExclusionStrategy> asList(new WeightExclusionStrategy())); + from("direct:inPojoExcludeWeight").marshal(weightExclusionFormat); + from("direct:backPojoExcludeWeight").unmarshal(weightExclusionFormat).to("mock:reversePojoExcludeWeight"); + + //START SNIPPET: format + GsonDataFormat ageExclusionFormat = new GsonDataFormat(TestPojoExclusion.class); + ageExclusionFormat.setExclusionStrategies(Arrays.<ExclusionStrategy> asList(new AgeExclusionStrategy())); + from("direct:inPojoExcludeAge").marshal(ageExclusionFormat); + //END SNIPPET: format + from("direct:backPojoExcludeAge").unmarshal(ageExclusionFormat).to("mock:reversePojoExcludeAge"); + } + }; + } + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalListTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalListTest.java new file mode 100644 index 0000000..b6e86bc --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalListTest.java @@ -0,0 +1,114 @@ +/* + * 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.camel.component.gson.springboot; + + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +import com.google.gson.reflect.TypeToken; + +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.gson.GsonDataFormat; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + GsonMarshalListTest.class, + GsonMarshalListTest.TestConfiguration.class + } +) +public class GsonMarshalListTest { + + + @Autowired + ProducerTemplate template; + + @EndpointInject("mock:reversePojo") + MockEndpoint mock; + + + @Test + public void testMarshalAndUnmarshalPojo() throws Exception { + + List<TestPojo> inList = new ArrayList<>(); + + TestPojo in1 = new TestPojo(); + in1.setName("Camel1"); + + TestPojo in2 = new TestPojo(); + in2.setName("Camel2"); + + inList.add(in1); + inList.add(in2); + + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(List.class); + mock.message(0).body().isEqualTo(inList); + + String marshalled = template.requestBody("direct:inPojo", inList, String.class); + assertEquals("[{\"name\":\"Camel1\"},{\"name\":\"Camel2\"}]", marshalled); + + template.sendBody("direct:backPojo", marshalled); + + mock.assertIsSatisfied(); + } + + + // ************************************* + // Config + // ************************************* + + @Configuration + public static class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + + GsonDataFormat formatPojo = new GsonDataFormat(); + Type genericType = new TypeToken<List<TestPojo>>() { + }.getType(); + formatPojo.setUnmarshalGenericType(genericType); + + from("direct:inPojo").marshal(formatPojo); + from("direct:backPojo").unmarshal(formatPojo).to("mock:reversePojo"); + } + }; + } + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalTest.java new file mode 100644 index 0000000..ece5556 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonMarshalTest.java @@ -0,0 +1,128 @@ +/* + * 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.camel.component.gson.springboot; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.gson.GsonDataFormat; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + GsonMarshalTest.class, + GsonMarshalTest.TestConfiguration.class + } +) +public class GsonMarshalTest { + + @Autowired + CamelContext context; + + @Autowired + ProducerTemplate template; + + + @EndpointInject("mock:reverse") + MockEndpoint mock; + + @EndpointInject("mock:reversePojo") + MockEndpoint mockPojo; + + + @Test + public void testMarshalAndUnmarshalMap() throws Exception { + Map<String, String> in = new HashMap<>(); + in.put("name", "Camel"); + + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(Map.class); + mock.message(0).body().isEqualTo(in); + + Object marshalled = template.requestBody("direct:in", in); + String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled); + assertEquals("{\"name\":\"Camel\"}", marshalledAsString); + + template.sendBody("direct:back", marshalled); + + mock.assertIsSatisfied(); + } + + @Test + public void testMarshalAndUnmarshalPojo() throws Exception { + TestPojo in = new TestPojo(); + in.setName("Camel"); + + + mockPojo.expectedMessageCount(1); + mockPojo.message(0).body().isInstanceOf(TestPojo.class); + mockPojo.message(0).body().isEqualTo(in); + + Object marshalled = template.requestBody("direct:inPojo", in); + String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled); + assertEquals("{\"name\":\"Camel\"}", marshalledAsString); + + template.sendBody("direct:backPojo", marshalled); + + mockPojo.assertIsSatisfied(); + } + // ************************************* + // Config + // ************************************* + + @Configuration + public static class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + GsonDataFormat format = new GsonDataFormat(); + + from("direct:in").marshal(format); + from("direct:back").unmarshal(format).to("mock:reverse"); + + GsonDataFormat formatPojo = new GsonDataFormat(TestPojo.class); + + from("direct:inPojo").marshal(formatPojo); + from("direct:backPojo").unmarshal(formatPojo).to("mock:reversePojo"); + } + }; + } + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonUnmarshalTypeFromHeaderTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonUnmarshalTypeFromHeaderTest.java new file mode 100644 index 0000000..e075345 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/GsonUnmarshalTypeFromHeaderTest.java @@ -0,0 +1,83 @@ +/* + * 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.camel.component.gson.springboot; + + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.gson.GsonConstants; +import org.apache.camel.model.dataformat.JsonLibrary; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + GsonUnmarshalTypeFromHeaderTest.class, + GsonUnmarshalTypeFromHeaderTest.TestConfiguration.class + } +) +public class GsonUnmarshalTypeFromHeaderTest { + + + @Autowired + ProducerTemplate template; + + + + + @Test + public void testUnmarshalTypeFromHeader() { + String body = "{\"name\":\"my-name\"}"; + String unmarshallType = "org.apache.camel.component.gson.springboot.TestPojo"; + TestPojo pojo = template.requestBodyAndHeader("direct:unmarshalTypeFromHeader", body, GsonConstants.UNMARSHAL_TYPE, + unmarshallType, TestPojo.class); + assertNotNull(pojo); + assertEquals("my-name", pojo.getName()); + } + + // ************************************* + // Config + // ************************************* + + @Configuration + public static class TestConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:unmarshalTypeFromHeader").unmarshal().json(JsonLibrary.Gson); + } + }; + } + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/PersonPojo.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/PersonPojo.java new file mode 100644 index 0000000..15cd9f7 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/PersonPojo.java @@ -0,0 +1,48 @@ +/* + * 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.camel.component.gson.springboot; + +public class PersonPojo { + + private int id; + private String firstName; + private String lastName; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonFieldNamePolicyTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonFieldNamePolicyTest.java new file mode 100644 index 0000000..901175a --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonFieldNamePolicyTest.java @@ -0,0 +1,89 @@ +/* + * 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.camel.component.gson.springboot; + + +import com.google.gson.FieldNamingPolicy; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.gson.GsonDataFormat; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + SpringGsonFieldNamePolicyTest.class + }, + properties = { + "camel.springboot.routes-include-pattern=file:src/test/resources/routes/SpringGsonFieldNamePolicyTest.xml"} + +) +public class SpringGsonFieldNamePolicyTest { + + + @Autowired + ProducerTemplate template; + + + + + + + @Bean(name = "gson") + GsonDataFormat getGsonDataFormat() { + GsonDataFormat gsonDataformat = new GsonDataFormat(); + gsonDataformat.setUnmarshalTypeName("org.apache.camel.component.gson.springboot.PersonPojo"); + gsonDataformat.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); + return gsonDataformat; + } + + @Test + public void testUnmarshalPojo() { + String json = "{\"id\":\"123\",\"first_name\":\"Donald\",\"last_name\":\"Duck\"}"; + PersonPojo pojo = template.requestBody("direct:backPojo", json, PersonPojo.class); + assertNotNull(pojo); + + assertEquals(123, pojo.getId()); + assertEquals("Donald", pojo.getFirstName()); + assertEquals("Duck", pojo.getLastName()); + } + + @Test + public void testMarshalPojo() { + PersonPojo pojo = new PersonPojo(); + pojo.setId(123); + pojo.setFirstName("Donald"); + pojo.setLastName("Duck"); + + String expected = "{\"id\":123,\"first_name\":\"Donald\",\"last_name\":\"Duck\"}"; + String json = template.requestBody("direct:inPojo", pojo, String.class); + assertEquals(expected, json); + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonJsonDataFormatTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonJsonDataFormatTest.java new file mode 100644 index 0000000..1929c66 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonJsonDataFormatTest.java @@ -0,0 +1,116 @@ +/* + * 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.camel.component.gson.springboot; + + + +import org.apache.camel.CamelContext; +import org.apache.camel.EndpointInject; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.gson.GsonDataFormat; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + SpringGsonJsonDataFormatTest.class + }, + properties = { + "camel.springboot.routes-include-pattern=file:src/test/resources/routes/SpringGsonJsonDataFormatTest.xml"} + +) +public class SpringGsonJsonDataFormatTest { + + @Autowired + private CamelContext context; + + @Autowired + ProducerTemplate template; + + @EndpointInject("mock:reversePojo") + MockEndpoint mock; + + + + @Bean(name = "pretty") + GsonDataFormat getPrettyGsonDataFormat() { + GsonDataFormat gsonDataformat = new GsonDataFormat(); + gsonDataformat.setPrettyPrint(true); + gsonDataformat.setUnmarshalTypeName("org.apache.camel.component.gson.springboot.TestPojo"); + return gsonDataformat; + } + + @Bean(name = "gson") + GsonDataFormat getGsonDataFormat() { + GsonDataFormat gsonDataformat = new GsonDataFormat(); + gsonDataformat.setUnmarshalTypeName("org.apache.camel.component.gson.springboot.TestPojo"); + return gsonDataformat; + } + + @Test + public void testMarshalAndUnmarshalPojo() throws Exception { + TestPojo in = new TestPojo(); + in.setName("Camel"); + + mock.reset(); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(TestPojo.class); + mock.message(0).body().isEqualTo(in); + + Object marshalled = template.requestBody("direct:inPojo", in); + String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled); + assertEquals("{\"name\":\"Camel\"}", marshalledAsString); + + template.sendBody("direct:backPojo", marshalled); + + mock.assertIsSatisfied(); + } + + @Test + public void testMarshalAndUnmarshalPojoWithPrettyPrint() throws Exception { + TestPojo in = new TestPojo(); + in.setName("Camel"); + mock.reset(); + mock.expectedMessageCount(1); + mock.message(0).body().isInstanceOf(TestPojo.class); + mock.message(0).body().isEqualTo(in); + + Object marshalled = template.requestBody("direct:inPretty", in); + String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled); + String expected = "{\n" + + " \"name\": \"Camel\"" + + "\n}"; + assertEquals(expected, marshalledAsString); + + template.sendBody("direct:backPretty", marshalled); + + mock.assertIsSatisfied(); + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonUnmarshalTypeFromHeaderTest.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonUnmarshalTypeFromHeaderTest.java new file mode 100644 index 0000000..f23953b --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/SpringGsonUnmarshalTypeFromHeaderTest.java @@ -0,0 +1,60 @@ +/* + * 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.camel.component.gson.springboot; + + + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.apache.camel.test.spring.junit5.CamelSpringBootTest; + + +@DirtiesContext +@CamelSpringBootTest +@SpringBootTest( + classes = { + CamelAutoConfiguration.class, + SpringGsonUnmarshalTypeFromHeaderTest.class + }, + properties = { + "camel.springboot.routes-include-pattern=file:src/test/resources/routes/SpringGsonUnmarshalTypeFromHeaderTest.xml"} + +) +public class SpringGsonUnmarshalTypeFromHeaderTest { + + + @Autowired + ProducerTemplate template; + + + + @Test + public void testUnmarshalTypeFromHeader() { + TestPojo pojo = template.requestBody("direct:springUnmarshalTypeFromHeader", "{\"name\":\"my-name\"}", TestPojo.class); + assertNotNull(pojo); + assertEquals("my-name", pojo.getName()); + } + +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/TestPojo.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/TestPojo.java new file mode 100644 index 0000000..90f32ff --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/TestPojo.java @@ -0,0 +1,45 @@ +/* + * 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.camel.component.gson.springboot; + +public class TestPojo { + + private String name; + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public boolean equals(Object obj) { + return this.name.equals(((TestPojo) obj).getName()); + } + + @Override + public int hashCode() { + return name != null ? name.hashCode() : 0; + } + + @Override + public String toString() { + return "TestPojo[" + name + "]"; + } +} diff --git a/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/TestPojoExclusion.java b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/TestPojoExclusion.java new file mode 100644 index 0000000..8281108 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/java/org/apache/camel/component/gson/springboot/TestPojoExclusion.java @@ -0,0 +1,82 @@ +/* + * 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.camel.component.gson.springboot; + + +public class TestPojoExclusion { + + @ExcludeAge + private int age = 30; + + private int height = 190; + + @ExcludeWeight + private int weight = 70; + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getWeight() { + return weight; + } + + public void setWeight(int weight) { + this.weight = weight; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + TestPojoExclusion that = (TestPojoExclusion) o; + + if (age != that.age) { + return false; + } + if (height != that.height) { + return false; + } + return weight == that.weight; + + } + + @Override + public int hashCode() { + int result = age; + result = 31 * result + height; + result = 31 * result + weight; + return result; + } +} diff --git a/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonFieldNamePolicyTest.xml b/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonFieldNamePolicyTest.xml new file mode 100644 index 0000000..97a7e93 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonFieldNamePolicyTest.xml @@ -0,0 +1,35 @@ +<?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. + +--> + + + <routes xmlns="http://camel.apache.org/schema/spring"> + + <route> + <from uri="direct:inPojo"/> + <marshal><custom ref="gson"/></marshal> + </route> + + <route> + <from uri="direct:backPojo"/> + <unmarshal><custom ref="gson"/></unmarshal> + </route> + + </routes> + diff --git a/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonJsonDataFormatTest.xml b/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonJsonDataFormatTest.xml new file mode 100644 index 0000000..8feec5e --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonJsonDataFormatTest.xml @@ -0,0 +1,51 @@ +<?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. + +--> + <routes id="camel" xmlns="http://camel.apache.org/schema/spring"> + + <!-- we define the json jackson data formats to be used --> + <dataFormats> + <!-- this one uses our own TestPojo class as unmarshal type --> + <json id="gson" library="Gson" unmarshalType="org.apache.camel.component.gson.TestPojo"/> + <json id="pretty" library="Gson" prettyPrint="true" unmarshalType="org.apache.camel.component.gson.TestPojo"/> + </dataFormats> + + <route> + <from uri="direct:inPojo"/> + <marshal><custom ref="gson"/></marshal> + </route> + + <route> + <from uri="direct:backPojo"/> + <unmarshal><custom ref="gson"/></unmarshal> + <to uri="mock:reversePojo"/> + </route> + + <route> + <from uri="direct:inPretty"/> + <marshal><custom ref="pretty"/></marshal> + </route> + + <route> + <from uri="direct:backPretty"/> + <unmarshal><custom ref="pretty"/></unmarshal> + <to uri="mock:reversePojo"/> + </route> + + </routes> diff --git a/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonUnmarshalTypeFromHeaderTest.xml b/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonUnmarshalTypeFromHeaderTest.xml new file mode 100644 index 0000000..c713a96 --- /dev/null +++ b/components-starter/camel-gson-starter/src/test/resources/routes/SpringGsonUnmarshalTypeFromHeaderTest.xml @@ -0,0 +1,32 @@ +<?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. + +--> + <routes id="camel" xmlns="http://camel.apache.org/schema/spring"> + + <route> + <from uri="direct:springUnmarshalTypeFromHeader"/> + <setHeader name="CamelGsonUnmarshalType"> + <constant>org.apache.camel.component.gson.springboot.TestPojo</constant> + </setHeader> + <unmarshal> + <json library="Gson" /> + </unmarshal> + </route> + + </routes>