From 5c4b817e27a3dd766d102bca26bf46ddd312a31a Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 24 Sep 2015 08:49:26 -0400 Subject: [PATCH] INTSAMPLES-146 Fix Kafka tests and some polishing JIRA: https://jira.spring.io/browse/INTSAMPLES-146 --- .gitignore | 1 + basic/barrier/pom.xml | 10 +- basic/kafka/pom.xml | 122 ++++++++++++++++++ .../samples/barrier/ApplicationTests.java | 7 +- .../samples/barrier/KafkaRunning.java | 78 +++++++++++ 5 files changed, 211 insertions(+), 7 deletions(-) create mode 100644 basic/kafka/pom.xml create mode 100644 basic/kafka/src/test/java/org/springframework/integration/samples/barrier/KafkaRunning.java diff --git a/.gitignore b/.gitignore index 1b719783..60bc2257 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ activemq-data /.gradle build/ +/classes diff --git a/basic/barrier/pom.xml b/basic/barrier/pom.xml index c00aa33f..be3c1267 100644 --- a/basic/barrier/pom.xml +++ b/basic/barrier/pom.xml @@ -86,14 +86,14 @@ compile - org.springframework.integration - spring-integration-amqp - 4.2.0.RELEASE + org.springframework.boot + spring-boot-starter-amqp compile - org.springframework.boot - spring-boot-starter-amqp + org.springframework.integration + spring-integration-amqp + 4.2.0.RELEASE compile diff --git a/basic/kafka/pom.xml b/basic/kafka/pom.xml new file mode 100644 index 00000000..d72a2763 --- /dev/null +++ b/basic/kafka/pom.xml @@ -0,0 +1,122 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 1.3.0.M5 + + org.springframework.integration.samples + kafka + 4.1.0.BUILD-SNAPSHOT + Apache Kafka Sample + Apache Kafka Sample + http://projects.spring.io/spring-integration + + SpringIO + https://spring.io + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + garyrussell + Gary Russell + grussell@pivotal.io + + project lead + + + + markfisher + Mark Fisher + mfisher@pivotal.io + + project founder and lead emeritus + + + + ghillert + Gunnar Hillert + ghillert@pivotal.io + + + abilan + Artem Bilan + abilan@pivotal.io + + + + scm:git:scm:git:git://github.com/spring-projects/spring-integration-samples.git + scm:git:scm:git:ssh://git@github.com:spring-projects/spring-integration-samples.git + https://github.com/spring-projects/spring-integration-samples + + + + org.springframework.integration + spring-integration-core + 4.2.0.RELEASE + compile + + + junit + junit + 4.11 + test + + + org.hamcrest + hamcrest-all + 1.3 + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-integration + compile + + + org.springframework.integration + spring-integration-kafka + 1.2.2.BUILD-SNAPSHOT + compile + + + org.springframework + spring-test + 4.2.0.RELEASE + test + + + org.mockito + mockito-core + 1.9.5 + test + + + + + repo.spring.io.milestone + Spring Framework Maven Milestone Repository + https://repo.spring.io/libs-milestone + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/basic/kafka/src/test/java/org/springframework/integration/samples/barrier/ApplicationTests.java b/basic/kafka/src/test/java/org/springframework/integration/samples/barrier/ApplicationTests.java index 3d693e9c..a7998d69 100644 --- a/basic/kafka/src/test/java/org/springframework/integration/samples/barrier/ApplicationTests.java +++ b/basic/kafka/src/test/java/org/springframework/integration/samples/barrier/ApplicationTests.java @@ -15,8 +15,7 @@ package org.springframework.integration.samples.barrier; * limitations under the License. */ - - +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; @@ -26,12 +25,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Gary Russell + * @author Artem Bilan * @since 4.2 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) public class ApplicationTests { + @ClassRule + public static KafkaRunning kafkaRunning = new KafkaRunning(); + @Test public void contextLoads() { } diff --git a/basic/kafka/src/test/java/org/springframework/integration/samples/barrier/KafkaRunning.java b/basic/kafka/src/test/java/org/springframework/integration/samples/barrier/KafkaRunning.java new file mode 100644 index 00000000..b77d0c43 --- /dev/null +++ b/basic/kafka/src/test/java/org/springframework/integration/samples/barrier/KafkaRunning.java @@ -0,0 +1,78 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.samples.barrier; + +import org.I0Itec.zkclient.ZkClient; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assume; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import org.springframework.integration.kafka.core.BrokerAddress; +import org.springframework.integration.kafka.core.ZookeeperConnectDefaults; + +import com.gs.collections.impl.utility.ListIterate; + +import kafka.cluster.Broker; +import kafka.utils.ZKStringSerializer$; +import kafka.utils.ZkUtils; +import scala.collection.JavaConversions; +import scala.collection.Seq; + +/** + * * A rule that prevents integration tests from failing if the Kafka server is not running or not + * accessible. If the Kafka server is not running in the background all the tests here will simply be skipped because + * of a violated assumption (showing as successful). + * The rule can be declared as static so that it only has to check once for all tests in the enclosing test case, but + * there isn't a lot of overhead in making it non-static. + * + * @author Dave Syer + * @author Artem Bilan + * @author Gary Russell + * @author Marius Bogoevici + * @since 4.2 + */ +public class KafkaRunning extends TestWatcher { + + private static final String ZOOKEEPER_CONNECT_STRING = ZookeeperConnectDefaults.ZK_CONNECT; + + private static final Log logger = LogFactory.getLog(KafkaRunning.class); + + @Override + public Statement apply(Statement base, Description description) { + try { + ZkClient zkClient = new ZkClient(ZOOKEEPER_CONNECT_STRING, 1000, 1000, ZKStringSerializer$.MODULE$); + Seq allBrokersInCluster = ZkUtils.getAllBrokersInCluster(zkClient); + BrokerAddress[] brokerAddresses = ListIterate + .collect(JavaConversions.asJavaList(allBrokersInCluster), + broker -> new BrokerAddress(broker.host(), broker.port())) + .toArray(new BrokerAddress[allBrokersInCluster.size()]); + if (brokerAddresses.length == 0) { + throw new IllegalStateException("No running Kafka brokers"); + } + } + catch (Exception e) { + logger.warn("Not executing tests because basic connectivity test failed"); + Assume.assumeNoException(e); + } + + return super.apply(base, description); + } + +}