Add integration test for task events

This resolves #126

Use RedisConnectionFactory from RedisTestSupport

Add sub-project for integration tests

 - Since there are overlaps with the integration tests and the unit tests running together (using redis binder vs test support binder), it is better to move the integration tests
to a separate sub-project

Little cleanup
This commit is contained in:
Ilayaperumal Gopinathan
2016-04-12 11:24:45 +05:30
committed by Glenn Renfro
parent 1cd18497b8
commit ced5a3aa87
5 changed files with 146 additions and 6 deletions

View File

@@ -72,6 +72,7 @@
<module>spring-cloud-task-docs</module>
<module>spring-cloud-task-batch</module>
<module>spring-cloud-task-stream</module>
<module>spring-cloud-task-integration-tests</module>
</modules>
<properties>

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-task-parent</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<name>spring-cloud-task-integration-tests</name>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-integration-tests</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-stream</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support-internal</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-redis</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,98 @@
/*
* Copyright 2016 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.cloud.task.listener;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.binder.redis.config.RedisServiceAutoConfiguration;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.test.junit.redis.RedisTestSupport;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Michael Minella
* @author Ilayaperumal Gopinathan
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration({TaskEventTests.ListenerBinding.class})
public class TaskEventTests {
@ClassRule
public static RedisTestSupport redisTestSupport = new RedisTestSupport();
// Count for two task execution events per task
static CountDownLatch latch = new CountDownLatch(2);
private static final String TASK_NAME = "taskEventTest";
@Test
public void testTaskEventListener() throws Exception {
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder().sources(new Object[] {TaskEventsConfiguration.class,
TaskEventAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
RedisServiceAutoConfiguration.class}).build().run(new String[] {"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=redis",
"--spring.cloud.stream.bindings.task-events.destination=test"});
assertNotNull(applicationContext.getBean("taskEventListener"));
assertNotNull(applicationContext.getBean(TaskEventAutoConfiguration.TaskEventChannels.class));
assertTrue(latch.await(1, TimeUnit.SECONDS));
}
@Configuration
@EnableTask
public static class TaskEventsConfiguration {
}
@EnableBinding(Sink.class)
@PropertySource("classpath:/org/springframework/cloud/task/listener/sink-channel.properties")
@EnableAutoConfiguration
public static class ListenerBinding {
@StreamListener(Sink.INPUT)
public void receive(TaskExecution execution) {
assertTrue(String.format("Task name should be '%s'", TASK_NAME), execution.getTaskName().equals(TASK_NAME));
latch.countDown();
}
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return redisTestSupport.getResource();
}
}
}

View File

@@ -0,0 +1 @@
spring.cloud.stream.bindings.input.destination=test

View File

@@ -35,12 +35,12 @@ public class TaskEventTests {
public void testDefaultConfiguration() {
ConfigurableApplicationContext applicationContext =
SpringApplication.run(new Object[] {TaskEventsConfiguration.class,
TaskEventAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
TestSupportBinderAutoConfiguration.class},
new String[] {"--spring.cloud.task.closecontext.enable=false",
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=test"});
TaskEventAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
TestSupportBinderAutoConfiguration.class},
new String[] {"--spring.cloud.task.closecontext.enable=false",
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=test"});
assertNotNull(applicationContext.getBean("taskEventListener"));
assertNotNull(applicationContext.getBean(TaskEventAutoConfiguration.TaskEventChannels.class));