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

@@ -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();
}
}
}