CAMEL-10976: camel-spring-boot - Configure rest configuration using auto configuration
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/62e35c78 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/62e35c78 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/62e35c78 Branch: refs/heads/master Commit: 62e35c78585c3b2da63ee4c304f9a0fd335b3190 Parents: 2f9773e Author: lburgazzoli <lburgazz...@gmail.com> Authored: Thu Mar 16 18:42:46 2017 +0100 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Fri Mar 17 13:38:38 2017 +0100 ---------------------------------------------------------------------- .../camel/component/rest/RestComponent.java | 80 ++++++- .../apache/camel/model/rest/RestConstants.java | 24 ++ .../model/rest/RestPropertyDefinition.java | 8 + .../apache/camel/util/IntrospectionSupport.java | 17 ++ .../camel/component/rest/FromRestGetTest.java | 4 +- .../spring/boot/CamelRestAutoConfiguration.java | 60 +++++ .../boot/CamelRestConfigurationProperties.java | 233 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../camel/spring/boot/CamelAnnotationsTest.java | 34 +-- .../CamelAutoConfigurationPropertiesTest.java | 2 + .../spring/boot/CamelAutoConfigurationTest.java | 62 ++--- .../boot/CamelConfigurationLocationsTest.java | 2 + .../spring/boot/CamelEventNotifierTest.java | 2 + .../boot/CamelNonInvasiveCamelContextTest.java | 2 + .../apache/camel/spring/boot/CamelRestTest.java | 167 +++++++++++++ .../CamelSpringBootTemplateShutdownTest.java | 2 + .../spring/boot/CustomShutdownStrategyTest.java | 2 + .../boot/ExistingConversionServiceTest.java | 25 +- .../camel/spring/boot/NoConvertersTest.java | 2 + .../org/apache/camel/spring/boot/PlainTest.java | 2 + .../boot/SpringConverterDelegationTest.java | 34 ++- .../spring/boot/SpringTypeConverterTest.java | 2 + 22 files changed, 680 insertions(+), 87 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java index c49cee4..78518fc 100644 --- a/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java +++ b/camel-core/src/main/java/org/apache/camel/component/rest/RestComponent.java @@ -16,19 +16,27 @@ */ package org.apache.camel.component.rest; +import java.util.HashMap; +import java.util.Iterator; import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Supplier; +import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; -import org.apache.camel.impl.UriEndpointComponent; +import org.apache.camel.impl.DefaultComponent; +import org.apache.camel.model.rest.RestConstants; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.FileUtil; +import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; /** * Rest component. */ -public class RestComponent extends UriEndpointComponent { +public class RestComponent extends DefaultComponent { @Metadata(label = "common") private String componentName; @@ -37,10 +45,6 @@ public class RestComponent extends UriEndpointComponent { @Metadata(label = "producer") private String host; - public RestComponent() { - super(RestEndpoint.class); - } - @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { String restConfigurationName = getAndRemoveParameter(parameters, "componentName", String.class, componentName); @@ -49,7 +53,9 @@ public class RestComponent extends UriEndpointComponent { answer.setComponentName(restConfigurationName); answer.setApiDoc(apiDoc); - RestConfiguration config = getCamelContext().getRestConfiguration(restConfigurationName, true); + RestConfiguration config = new RestConfiguration(); + mergeConfigurations(config, findGlobalRestConfiguration()); + mergeConfigurations(config, getCamelContext().getRestConfiguration(restConfigurationName, true)); // if no explicit host was given, then fallback and use default configured host String h = resolveAndRemoveReferenceParameter(parameters, "host", String.class, host); @@ -155,4 +161,64 @@ public class RestComponent extends UriEndpointComponent { this.host = host; } + // **************************************** + // Helpers + // **************************************** + + private RestConfiguration findGlobalRestConfiguration() { + CamelContext context = getCamelContext(); + + RestConfiguration conf = CamelContextHelper.lookup(context, RestConstants.DEFAULT_REST_CONFIGURATION_ID, RestConfiguration.class); + if (conf == null) { + conf = CamelContextHelper.findByType(getCamelContext(), RestConfiguration.class); + } + + return conf; + } + + private RestConfiguration mergeConfigurations(RestConfiguration conf, RestConfiguration from) throws Exception { + if (from != null) { + Map<String, Object> map = IntrospectionSupport.getNonNullProperties(from); + + // Remove properties as they need to be manually managed + Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry<String, Object> entry = it.next(); + if (entry.getValue() instanceof Map) { + it.remove(); + } + } + + // Copy common options, will override those in conf + IntrospectionSupport.setProperties(getCamelContext(), getCamelContext().getTypeConverter(), conf, map); + + // Merge properties + mergeProperties(conf::getComponentProperties, from::getComponentProperties, conf::setComponentProperties); + mergeProperties(conf::getEndpointProperties, from::getEndpointProperties, conf::setEndpointProperties); + mergeProperties(conf::getConsumerProperties, from::getConsumerProperties, conf::setConsumerProperties); + mergeProperties(conf::getDataFormatProperties, from::getDataFormatProperties, conf::setDataFormatProperties); + mergeProperties(conf::getApiProperties, from::getApiProperties, conf::setApiProperties); + mergeProperties(conf::getCorsHeaders, from::getCorsHeaders, conf::setCorsHeaders); + } + + return conf; + } + + private <T> void mergeProperties(Supplier<Map<String, T>> base, Supplier<Map<String, T>> addons, Consumer<Map<String, T>> consumer) { + Map<String, T> baseMap = base.get(); + Map<String, T> addonsMap = addons.get(); + + if (baseMap != null || addonsMap != null) { + HashMap<String, T> result = new HashMap<>(); + if (baseMap != null) { + result.putAll(baseMap); + } + if (addonsMap != null) { + result.putAll(addonsMap); + } + + consumer.accept(result); + } + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/camel-core/src/main/java/org/apache/camel/model/rest/RestConstants.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestConstants.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestConstants.java new file mode 100644 index 0000000..17a9216 --- /dev/null +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestConstants.java @@ -0,0 +1,24 @@ +/** + * 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.model.rest; + +public final class RestConstants { + public static final String DEFAULT_REST_CONFIGURATION_ID = "rest-configuration"; + + private RestConstants() { + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/camel-core/src/main/java/org/apache/camel/model/rest/RestPropertyDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/rest/RestPropertyDefinition.java b/camel-core/src/main/java/org/apache/camel/model/rest/RestPropertyDefinition.java index 95eb84c..c1e85d0 100644 --- a/camel-core/src/main/java/org/apache/camel/model/rest/RestPropertyDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/rest/RestPropertyDefinition.java @@ -37,6 +37,14 @@ public class RestPropertyDefinition { @XmlAttribute(required = true) private String value; + public RestPropertyDefinition() { + } + + public RestPropertyDefinition(String key, String value) { + this.key = key; + this.value = value; + } + /** * Property key */ http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java b/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java index c3a7258..185ef3b 100755 --- a/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java +++ b/camel-core/src/main/java/org/apache/camel/util/IntrospectionSupport.java @@ -26,6 +26,7 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -204,6 +205,22 @@ public final class IntrospectionSupport { return isSetter(method, false); } + /** + * Will inspect the target for properties. + * <p/> + * Notice a property must have both a getter/setter method to be included. + * Notice all <tt>null</tt> values won't be included. + * + * @param target the target bean + * @return the map with found properties + */ + public static Map<String, Object> getNonNullProperties(Object target) { + Map<String, Object> properties = new HashMap<>(); + + getProperties(target, properties, null, false); + + return properties; + } /** * Will inspect the target for properties. http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java index bfafb14..9fb6cbb 100644 --- a/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/rest/FromRestGetTest.java @@ -113,8 +113,8 @@ public class FromRestGetTest extends ContextTestSupport { .get().consumes("application/json") .param().type(RestParamType.header).description("header param description1").dataType("integer").allowableValues("1", "2", "3", "4") .defaultValue("1").name("header_count").required(true) - .endParam(). - param().type(RestParamType.query).description("header param description2").dataType("string").allowableValues("a", "b", "c", "d") + .endParam() + .param().type(RestParamType.query).description("header param description2").dataType("string").allowableValues("a", "b", "c", "d") .defaultValue("b").collectionFormat(CollectionFormat.multi).name("header_letter").required(false) .endParam() .responseMessage().code(300).message("test msg").responseModel(Integer.class) http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestAutoConfiguration.java new file mode 100644 index 0000000..ef58774 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestAutoConfiguration.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.spring.boot; + +import org.apache.camel.CamelContext; +import org.apache.camel.model.rest.RestConstants; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.util.IntrospectionSupport; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnProperty(name = "camel.rest.enabled", matchIfMissing = true) +@ConditionalOnBean(CamelAutoConfiguration.class) +@AutoConfigureAfter(CamelAutoConfiguration.class) +@EnableConfigurationProperties(CamelRestConfigurationProperties.class) +public class CamelRestAutoConfiguration { + @Autowired + private CamelContext camelContext; + @Autowired + private CamelRestConfigurationProperties config; + + @Bean(name = RestConstants.DEFAULT_REST_CONFIGURATION_ID) + @ConditionalOnClass(CamelContext.class) + @ConditionalOnMissingBean + public RestConfiguration restConfiguration() throws Exception { + final RestConfiguration configuration = new RestConfiguration(); + + // Copy properties + IntrospectionSupport.setProperties( + camelContext, + camelContext.getTypeConverter(), + configuration, + IntrospectionSupport.getNonNullProperties(config) + ); + + return configuration; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestConfigurationProperties.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestConfigurationProperties.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestConfigurationProperties.java new file mode 100644 index 0000000..cf1c855 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelRestConfigurationProperties.java @@ -0,0 +1,233 @@ +/** + * 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.spring.boot; + +import java.util.Map; + +import org.apache.camel.spi.RestConfiguration; +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "camel.rest") +public class CamelRestConfigurationProperties { + // Delegate it so we do not have the CamelRestConfigurationProperties + // registered available also as RestConfiguration + private final RestConfiguration delegate; + + public CamelRestConfigurationProperties() { + this.delegate = new RestConfiguration(); + } + + public String getComponent() { + return delegate.getComponent(); + } + + public void setComponent(String componentName) { + delegate.setComponent(componentName); + } + + public String getApiComponent() { + return delegate.getApiComponent(); + } + + public void setApiComponent(String apiComponent) { + delegate.setApiComponent(apiComponent); + } + + public String getProducerComponent() { + return delegate.getProducerComponent(); + } + + public void setProducerComponent(String componentName) { + delegate.setProducerComponent(componentName); + } + + public String getProducerApiDoc() { + return delegate.getProducerApiDoc(); + } + + public void setProducerApiDoc(String producerApiDoc) { + delegate.setProducerApiDoc(producerApiDoc); + } + + public String getHost() { + return delegate.getHost(); + } + + public void setHost(String host) { + delegate.setHost(host); + } + + public String getScheme() { + return delegate.getScheme(); + } + + public void setScheme(String scheme) { + delegate.setScheme(scheme); + } + + public int getPort() { + return delegate.getPort(); + } + + public void setPort(int port) { + delegate.setPort(port); + } + + public String getContextPath() { + return delegate.getContextPath(); + } + + public void setContextPath(String contextPath) { + delegate.setContextPath(contextPath); + } + + public String getApiContextPath() { + return delegate.getApiContextPath(); + } + + public void setApiContextPath(String contextPath) { + delegate.setApiContextPath(contextPath); + } + + public String getApiContextRouteId() { + return delegate.getApiContextRouteId(); + } + + public void setApiContextRouteId(String apiContextRouteId) { + delegate.setApiContextRouteId(apiContextRouteId); + } + + public String getApiContextIdPattern() { + return delegate.getApiContextIdPattern(); + } + + public void setApiContextIdPattern(String apiContextIdPattern) { + delegate.setApiContextIdPattern(apiContextIdPattern); + } + + public boolean isApiContextListing() { + return delegate.isApiContextListing(); + } + + public void setApiContextListing(boolean apiContextListing) { + delegate.setApiContextListing(apiContextListing); + } + + public RestConfiguration.RestHostNameResolver getRestHostNameResolver() { + return delegate.getRestHostNameResolver(); + } + + public void setRestHostNameResolver(RestConfiguration.RestHostNameResolver restHostNameResolver) { + delegate.setRestHostNameResolver(restHostNameResolver); + } + + public void setRestHostNameResolver(String restHostNameResolver) { + delegate.setRestHostNameResolver(restHostNameResolver); + } + + public RestConfiguration.RestBindingMode getBindingMode() { + return delegate.getBindingMode(); + } + + public void setBindingMode(RestConfiguration.RestBindingMode bindingMode) { + delegate.setBindingMode(bindingMode); + } + + public void setBindingMode(String bindingMode) { + delegate.setBindingMode(bindingMode); + } + + public boolean isSkipBindingOnErrorCode() { + return delegate.isSkipBindingOnErrorCode(); + } + + public void setSkipBindingOnErrorCode(boolean skipBindingOnErrorCode) { + delegate.setSkipBindingOnErrorCode(skipBindingOnErrorCode); + } + + public boolean isEnableCORS() { + return delegate.isEnableCORS(); + } + + public void setEnableCORS(boolean enableCORS) { + delegate.setEnableCORS(enableCORS); + } + + public String getJsonDataFormat() { + return delegate.getJsonDataFormat(); + } + + public void setJsonDataFormat(String name) { + delegate.setJsonDataFormat(name); + } + + public String getXmlDataFormat() { + return delegate.getXmlDataFormat(); + } + + public void setXmlDataFormat(String name) { + delegate.setXmlDataFormat(name); + } + + public Map<String, Object> getComponentProperties() { + return delegate.getComponentProperties(); + } + + public void setComponentProperties(Map<String, Object> componentProperties) { + delegate.setComponentProperties(componentProperties); + } + + public Map<String, Object> getEndpointProperties() { + return delegate.getEndpointProperties(); + } + + public void setEndpointProperties(Map<String, Object> endpointProperties) { + delegate.setEndpointProperties(endpointProperties); + } + + public Map<String, Object> getConsumerProperties() { + return delegate.getConsumerProperties(); + } + + public void setConsumerProperties(Map<String, Object> consumerProperties) { + delegate.setConsumerProperties(consumerProperties); + } + + public Map<String, Object> getDataFormatProperties() { + return delegate.getDataFormatProperties(); + } + + public void setDataFormatProperties(Map<String, Object> dataFormatProperties) { + delegate.setDataFormatProperties(dataFormatProperties); + } + + public Map<String, Object> getApiProperties() { + return delegate.getApiProperties(); + } + + public void setApiProperties(Map<String, Object> apiProperties) { + delegate.setApiProperties(apiProperties); + } + + public Map<String, String> getCorsHeaders() { + return delegate.getCorsHeaders(); + } + + public void setCorsHeaders(Map<String, String> corsHeaders) { + delegate.setCorsHeaders(corsHeaders); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/main/resources/META-INF/spring.factories ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/resources/META-INF/spring.factories b/components/camel-spring-boot/src/main/resources/META-INF/spring.factories index 05fc73e..361ef6b 100644 --- a/components/camel-spring-boot/src/main/resources/META-INF/spring.factories +++ b/components/camel-spring-boot/src/main/resources/META-INF/spring.factories @@ -17,4 +17,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.apache.camel.spring.boot.CamelAutoConfiguration,\ +org.apache.camel.spring.boot.CamelRestAutoConfiguration,\ org.apache.camel.spring.boot.health.CamelHealthAutoConfiguration http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java index 3909be3..585bad2 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAnnotationsTest.java @@ -29,13 +29,19 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration -@SpringBootTest(classes = {CamelAnnotationsTest.class, CamelAnnotationsTestConfig.class}) +@SpringBootTest( + classes = { + CamelAnnotationsTest.class, + CamelAnnotationsTest.TestConfig.class + } +) public class CamelAnnotationsTest extends Assert { - @Autowired ProducerTemplate producerTemplate; @@ -49,19 +55,17 @@ public class CamelAnnotationsTest extends Assert { mockEndpoint.assertIsSatisfied(); } -} + @Configuration + public static class TestConfig { -@Configuration -class CamelAnnotationsTestConfig { - - @Bean - RoutesBuilder route() { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:test").to("mock:test"); - } - }; + @Bean + RoutesBuilder route() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:test").to("mock:test"); + } + }; + } } - } http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java index 8726b88..4f15180 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java @@ -29,8 +29,10 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = CamelAutoConfigurationPropertiesTest.class, properties = "camel.springboot.jmxEnabled=false") http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java index c0260bc..fdbbecb 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java @@ -32,20 +32,26 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.springframework.test.context.junit4.SpringRunner; -import static org.apache.camel.spring.boot.TestConfig.ROUTE_ID; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration -@SpringBootTest(classes = {TestConfig.class, CamelAutoConfigurationTest.class, RouteConfigWithCamelContextInjected.class}, -properties = { +@SpringBootTest( + classes = { + CamelAutoConfigurationTest.TestConfig.class, + CamelAutoConfigurationTest.class, + RouteConfigWithCamelContextInjected.class }, + properties = { "camel.springboot.consumerTemplateCacheSize=100", "camel.springboot.jmxEnabled=true", "camel.springboot.name=customName", - "camel.springboot.typeConversion=true"}) + "camel.springboot.typeConversion=true"} +) public class CamelAutoConfigurationTest extends Assert { // Collaborators fixtures @@ -80,7 +86,7 @@ public class CamelAutoConfigurationTest extends Assert { @Test public void shouldDetectRoutes() { // When - Route route = camelContext.getRoute(ROUTE_ID); + Route route = camelContext.getRoute(TestConfig.ROUTE_ID); // Then assertNotNull(route); @@ -164,31 +170,25 @@ public class CamelAutoConfigurationTest extends Assert { xmlAutoLoadingMock.assertIsSatisfied(); } -} - -@Configuration -class TestConfig { - - // Constants - - static final String ROUTE_ID = "testRoute"; - - // Test beans - - @Bean - RouteBuilder routeBuilder() { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:test").routeId(ROUTE_ID).to("mock:test"); - } - }; + @Configuration + public static class TestConfig { + // Constants + static final String ROUTE_ID = "testRoute"; + + // Test bean + @Bean + RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:test").routeId(ROUTE_ID).to("mock:test"); + } + }; + } + + @Bean + CamelContextConfiguration camelContextConfiguration() { + return mock(CamelContextConfiguration.class); + } } - - - @Bean - CamelContextConfiguration camelContextConfiguration() { - return mock(CamelContextConfiguration.class); - } - } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelConfigurationLocationsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelConfigurationLocationsTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelConfigurationLocationsTest.java index 8dd0624..82f61bc 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelConfigurationLocationsTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelConfigurationLocationsTest.java @@ -27,8 +27,10 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = CamelConfigurationLocationsTest.class, properties = "camel.springboot.file-configurations=file:src/test/secret/*.properties") http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelEventNotifierTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelEventNotifierTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelEventNotifierTest.java index 5f32aa8..397c989 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelEventNotifierTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelEventNotifierTest.java @@ -33,8 +33,10 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = CamelEventNotifierTest.class) http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelNonInvasiveCamelContextTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelNonInvasiveCamelContextTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelNonInvasiveCamelContextTest.java index d6fd9e8..328ccec 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelNonInvasiveCamelContextTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelNonInvasiveCamelContextTest.java @@ -28,11 +28,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ImportResource; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = { TestApplication.class, CamelNonInvasiveCamelContextTest.class }) http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelRestTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelRestTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelRestTest.java new file mode 100644 index 0000000..ab7fe56 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelRestTest.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.spring.boot; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.seda.SedaEndpoint; +import org.apache.camel.impl.ActiveMQUuidGenerator; +import org.apache.camel.spi.RestApiConsumerFactory; +import org.apache.camel.spi.RestConfiguration; +import org.apache.camel.spi.RestConsumerFactory; +import org.apache.camel.util.CamelContextHelper; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +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.springframework.test.context.junit4.SpringRunner; + + +@DirtiesContext +@RunWith(SpringRunner.class) +@EnableAutoConfiguration +@SpringBootTest( + classes = { + CamelRestTest.class, + CamelRestTest.TestConfiguration.class + }, + properties = { + "debug=false", + "camel.springboot.xml-rests=false", + "camel.springboot.xml-routes=false", + "camel.rest.enabled=true", + "camel.rest.component=dummy-rest", + "camel.rest.host=localhost" + } +) +public class CamelRestTest { + @Autowired + private CamelContext context; + + @Test + public void test() throws Exception { + ProducerTemplate template = context.createProducerTemplate(); + String result = template.requestBody("seda:get-say-hello", "test", String.class); + + Assert.assertEquals("Hello World", result); + } + + // *********************************** + // Configuration + // *********************************** + + @Configuration + public static class TestConfiguration { + @Bean(name = "dummy-rest") + public RestConsumerFactory dummyRestConsumerFactory() { + return new TestConsumerFactory(); + } + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + rest("/say/hello") + .get().to("direct:hello"); + from("direct:hello") + .transform().constant("Hello World"); + } + }; + } + } + + // *********************************** + // Rest Helpers + // *********************************** + + private static final class TestConsumerFactory implements RestConsumerFactory, RestApiConsumerFactory { + private Object dummy; + + public Object getDummy() { + return dummy; + } + + public void setDummy(Object dummy) { + this.dummy = dummy; + } + + @Override + public Consumer createConsumer( + CamelContext camelContext, + Processor processor, + String verb, + String basePath, + String uriTemplate, + String consumes, + String produces, + RestConfiguration configuration, + Map<String, Object> parameters) throws Exception { + + // just use a seda endpoint for testing purpose + String id; + if (uriTemplate != null) { + id = ActiveMQUuidGenerator.generateSanitizedId(basePath + uriTemplate); + } else { + id = ActiveMQUuidGenerator.generateSanitizedId(basePath); + } + // remove leading dash as we add that ourselves + if (id.startsWith("-")) { + id = id.substring(1); + } + + if (configuration.getConsumerProperties() != null) { + String ref = (String) configuration.getConsumerProperties().get("dummy"); + if (ref != null) { + dummy = CamelContextHelper.mandatoryLookup(camelContext, ref.substring(1)); + } + } + + SedaEndpoint seda = camelContext.getEndpoint("seda:" + verb + "-" + id, SedaEndpoint.class); + return seda.createConsumer(processor); + } + + @Override + public Consumer createApiConsumer( + CamelContext camelContext, + Processor processor, + String contextPath, + RestConfiguration configuration, + Map<String, Object> parameters) throws Exception { + + // just use a seda endpoint for testing purpose + String id = ActiveMQUuidGenerator.generateSanitizedId(contextPath); + // remove leading dash as we add that ourselves + if (id.startsWith("-")) { + id = id.substring(1); + } + + SedaEndpoint seda = camelContext.getEndpoint("seda:api:" + "-" + id, SedaEndpoint.class); + return seda.createConsumer(processor); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java index 0b7b6e0..d1cacaf 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelSpringBootTemplateShutdownTest.java @@ -24,10 +24,12 @@ import org.junit.Before; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.test.annotation.DirtiesContext; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +@DirtiesContext public class CamelSpringBootTemplateShutdownTest { CamelContext camelContext; http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CustomShutdownStrategyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CustomShutdownStrategyTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CustomShutdownStrategyTest.java index 0dd2f00..bdf8d80 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CustomShutdownStrategyTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CustomShutdownStrategyTest.java @@ -32,8 +32,10 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 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.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = CustomShutdownStrategyTest.class) http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/ExistingConversionServiceTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/ExistingConversionServiceTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/ExistingConversionServiceTest.java index 17c4a55..4137954 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/ExistingConversionServiceTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/ExistingConversionServiceTest.java @@ -27,13 +27,13 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; -import static org.apache.camel.spring.boot.ConversionServiceConfig.providedConversionService; - +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration -@SpringBootTest(classes = ConversionServiceConfig.class) +@SpringBootTest(classes = ExistingConversionServiceTest.TestConfig.class) public class ExistingConversionServiceTest extends Assert { @Autowired @@ -42,20 +42,17 @@ public class ExistingConversionServiceTest extends Assert { @Test public void shouldUseProvidedConversionService() { - assertSame(providedConversionService, conversionService); + assertSame(TestConfig.CONVERSION_SERVICE, conversionService); } -} - -@Configuration -class ConversionServiceConfig { + @Configuration + public static class TestConfig { + static final ConversionService CONVERSION_SERVICE = new DefaultConversionService(); - static ConversionService providedConversionService = new DefaultConversionService(); - - @Bean(name = "myService") - ConversionService providedConversionService() { - return providedConversionService; + @Bean(name = "myService") + ConversionService providedConversionService() { + return CONVERSION_SERVICE; + } } - } http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/NoConvertersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/NoConvertersTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/NoConvertersTest.java index e12d4a6..06a7b53 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/NoConvertersTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/NoConvertersTest.java @@ -25,8 +25,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = NoConvertersTest.class, properties = "camel.springboot.typeConversion=false") http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/PlainTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/PlainTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/PlainTest.java index d138398..004c8a4 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/PlainTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/PlainTest.java @@ -25,8 +25,10 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @SpringBootTest public class PlainTest { http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java index 807bf10..83c3332 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringConverterDelegationTest.java @@ -26,23 +26,15 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = SpringConverterDelegationTest.class, properties = "camel.springboot.typeConversion=true") public class SpringConverterDelegationTest extends Assert { - @Configuration - static class Config { - - @Bean - ConvertableConverter convertableConverter() { - return new ConvertableConverter(); - } - - } - @Autowired TypeConverter typeConverter; @@ -52,16 +44,22 @@ public class SpringConverterDelegationTest extends Assert { assertEquals("converted!", result); } -} - -class Convertable { -} + @Configuration + public static class Config { + @Bean + ConvertableConverter convertableConverter() { + return new ConvertableConverter(); + } -class ConvertableConverter implements Converter<Convertable, String> { + } - @Override - public String convert(Convertable source) { - return "converted!"; + public static class Convertable { } + public static class ConvertableConverter implements Converter<Convertable, String> { + @Override + public String convert(Convertable source) { + return "converted!"; + } + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/62e35c78/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringTypeConverterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringTypeConverterTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringTypeConverterTest.java index ac76c8a..5e14200 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringTypeConverterTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/SpringTypeConverterTest.java @@ -35,8 +35,10 @@ import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; +@DirtiesContext @RunWith(SpringRunner.class) @EnableAutoConfiguration @SpringBootTest(classes = SpringTypeConverterTest.SpringTypeConversionConfiguration.class)