This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 578c1ab camel-zookeeper: use AvailablePortFinderPropertiesFunction to avoid hardcoded ports (#3586) 578c1ab is described below commit 578c1ab6858ff54c0660841a8ce805b292a987ff Author: Luca Burgazzoli <lburgazz...@users.noreply.github.com> AuthorDate: Tue Feb 18 14:33:49 2020 +0100 camel-zookeeper: use AvailablePortFinderPropertiesFunction to avoid hardcoded ports (#3586) * camel-zookeeper: use AvailablePortFinderPropertiesFunction to avoid hardcoded ports in spring xml tests * camel-zookeeper: disable stdout logging for tests --- .../cloud/SpringZooKeeperServiceCallRouteTest.java | 43 +++++++++++----------- ...erviceRegistrationWithServiceComponentTest.java | 7 +++- .../src/test/resources/log4j2.properties | 2 +- .../cloud/SpringZooKeeperServiceCallRouteTest.xml | 12 +++--- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.java b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.java index 54795c0..6f4b3b6 100644 --- a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.java +++ b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.java @@ -16,10 +16,10 @@ */ package org.apache.camel.component.zookeeper.cloud; -import java.util.Collections; - +import org.apache.camel.CamelContext; +import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.component.zookeeper.ZooKeeperContainer; -import org.apache.camel.test.testcontainers.ContainerPropertiesFunction; +import org.apache.camel.test.AvailablePortFinderPropertiesFunction; import org.apache.camel.test.testcontainers.spring.ContainerAwareSpringTestSupport; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; @@ -30,10 +30,8 @@ import org.apache.curator.x.discovery.ServiceDiscoveryBuilder; import org.apache.curator.x.discovery.ServiceInstance; import org.apache.curator.x.discovery.details.JsonInstanceSerializer; import org.junit.Test; -import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.context.support.AbstractApplicationContext; -import org.springframework.context.support.GenericApplicationContext; -import org.springframework.core.io.ClassPathResource; +import org.springframework.context.support.ClassPathXmlApplicationContext; import org.testcontainers.containers.GenericContainer; public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTestSupport { @@ -42,12 +40,23 @@ public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTes private CuratorFramework curator; private ServiceDiscovery<ZooKeeperServiceDiscovery.MetaData> discovery; + private AvailablePortFinderPropertiesFunction function; // *********************** // Setup / tear down // *********************** @Override + protected CamelContext createCamelContext() throws Exception { + final CamelContext context = super.createCamelContext(); + final PropertiesComponent pc = (PropertiesComponent) context.getPropertiesComponent(); + + pc.addFunction(function); + + return context; + } + + @Override public GenericContainer createContainer() { return new ZooKeeperContainer(); } @@ -56,6 +65,8 @@ public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTes public void doPreSetup() throws Exception { super.doPreSetup(); + function = new AvailablePortFinderPropertiesFunction(); + curator = CuratorFrameworkFactory.builder() .connectString(getContainerHost(ZooKeeperContainer.CONTAINER_NAME) + ":" + getContainerPort(ZooKeeperContainer.CONTAINER_NAME, ZooKeeperContainer.CLIENT_PORT)) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) @@ -73,7 +84,7 @@ public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTes discovery.registerService( ServiceInstance.<ZooKeeperServiceDiscovery.MetaData>builder() .address("127.0.0.1") - .port(9011) + .port(Integer.parseInt(function.apply("service-1"))) .name(SERVICE_NAME) .id("service-1") .build()); @@ -81,7 +92,7 @@ public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTes discovery.registerService( ServiceInstance.<ZooKeeperServiceDiscovery.MetaData>builder() .address("127.0.0.1") - .port(9012) + .port(Integer.parseInt(function.apply("service-2"))) .name(SERVICE_NAME) .id("service-2") .build()); @@ -89,7 +100,7 @@ public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTes discovery.registerService( ServiceInstance.<ZooKeeperServiceDiscovery.MetaData>builder() .address("127.0.0.1") - .port(9013) + .port(Integer.parseInt(function.apply("service-3"))) .name(SERVICE_NAME) .id("service-3") .build()); @@ -110,7 +121,7 @@ public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTes @Test public void testServiceCall() throws Exception { getMockEndpoint("mock:result").expectedMessageCount(3); - getMockEndpoint("mock:result").expectedBodiesReceivedInAnyOrder("ping 9011", "ping 9012", "ping 9013"); + getMockEndpoint("mock:result").expectedBodiesReceivedInAnyOrder("ping svc1", "ping svc2", "ping svc3"); template.sendBody("direct:start", "ping"); template.sendBody("direct:start", "ping"); @@ -125,16 +136,6 @@ public class SpringZooKeeperServiceCallRouteTest extends ContainerAwareSpringTes @Override protected AbstractApplicationContext createApplicationContext() { - GenericApplicationContext applicationContext = new GenericApplicationContext(); - applicationContext.getBeanFactory().registerSingleton( - "zkProperties", - new ContainerPropertiesFunction(Collections.singletonList(getContainer(ZooKeeperContainer.CONTAINER_NAME)))); - - XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(applicationContext); - xmlReader.loadBeanDefinitions(new ClassPathResource("org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.xml")); - - applicationContext.refresh(); - - return applicationContext; + return new ClassPathXmlApplicationContext("org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.xml"); } } diff --git a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/ZooKeeperServiceRegistrationWithServiceComponentTest.java b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/ZooKeeperServiceRegistrationWithServiceComponentTest.java index eba9656..85dac20 100644 --- a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/ZooKeeperServiceRegistrationWithServiceComponentTest.java +++ b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cloud/ZooKeeperServiceRegistrationWithServiceComponentTest.java @@ -39,8 +39,11 @@ public class ZooKeeperServiceRegistrationWithServiceComponentTest extends ZooKee return new RouteBuilder() { @Override public void configure() throws Exception { - fromF("service:%s:jetty:http://0.0.0.0:%d/service/endpoint?service.type=zookeeper", SERVICE_NAME, SERVICE_PORT).routeId(SERVICE_ID).routeGroup(SERVICE_NAME) - .noAutoStartup().to("log:service-registry?level=INFO"); + fromF("service:%s:jetty:http://0.0.0.0:%d/service/endpoint?service.type=zookeeper", SERVICE_NAME, SERVICE_PORT) + .routeId(SERVICE_ID) + .routeGroup(SERVICE_NAME) + .noAutoStartup() + .to("log:service-registry?level=INFO"); } }; } diff --git a/components/camel-zookeeper/src/test/resources/log4j2.properties b/components/camel-zookeeper/src/test/resources/log4j2.properties index 8041ed2..a05da2e 100644 --- a/components/camel-zookeeper/src/test/resources/log4j2.properties +++ b/components/camel-zookeeper/src/test/resources/log4j2.properties @@ -48,5 +48,5 @@ logger.springframework.name = org.springframework logger.springframework.level = WARN rootLogger.level = INFO -rootLogger.appenderRef.stdout.ref = out +#rootLogger.appenderRef.stdout.ref = out rootLogger.appenderRef.file.ref = file diff --git a/components/camel-zookeeper/src/test/resources/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.xml b/components/camel-zookeeper/src/test/resources/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.xml index 6345443..f622a04 100644 --- a/components/camel-zookeeper/src/test/resources/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.xml +++ b/components/camel-zookeeper/src/test/resources/org/apache/camel/component/zookeeper/cloud/SpringZooKeeperServiceCallRouteTest.xml @@ -40,23 +40,23 @@ </route> <route> - <from uri="jetty:http://localhost:9011"/> + <from uri="jetty:http://localhost:{{available-port:service-1}}"/> <transform> - <simple>${body} 9011</simple> + <simple>${body} svc1</simple> </transform> </route> <route> - <from uri="jetty:http://localhost:9012"/> + <from uri="jetty:http://localhost:{{available-port:service-2}}"/> <transform> - <simple>${body} 9012</simple> + <simple>${body} svc2</simple> </transform> </route> <route> - <from uri="jetty:http://localhost:9013"/> + <from uri="jetty:http://localhost:{{available-port:service-3}}"/> <transform> - <simple>${body} 9013</simple> + <simple>${body} svc3</simple> </transform> </route>