Base Task Sink Case with Tests works

In process of migrating batch-events to streambridge

BatchEvents Migrated

Integration tests for TaskSink now work

Batch Event Integration Tests updated

Added tests to batch events

Event integration tests added

Updated with the last bit of tests

baseline polishing
This commit is contained in:
Glenn Renfro
2021-11-08 20:02:21 -05:00
parent f74d38a47f
commit 78f27ef26d
39 changed files with 1160 additions and 1135 deletions

View File

@@ -65,12 +65,6 @@
<artifactId>spring-cloud-stream-binder-rabbit-test-support</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support-internal</artifactId>
<version>${spring.cloud.stream}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-task</artifactId>
@@ -80,24 +74,13 @@
<artifactId>hsqldb</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${test.containers.version}</version>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring.cloud.stream}</version>
<type>test-jar</type>
<classifier>test-binder</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>rabbitmq</artifactId>
<version>${test.rabbit.containers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.rnorth.duct-tape</groupId>
<artifactId>duct-tape</artifactId>
<version>${test.ducttape.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -47,7 +47,7 @@ public class BatchEventsApplication {
SpringApplication.run(BatchEventsApplication.class, args);
}
@Configuration(proxyBeanMethods = false)
@Configuration
public static class JobConfiguration {
private static final int DEFAULT_CHUNK_COUNT = 3;

View File

@@ -16,66 +16,92 @@
package io.spring.cloud;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.assertj.core.api.BDDAssertions;
import org.junit.jupiter.api.Tag;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.RabbitMQContainer;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.stream.binder.test.OutputDestination;
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.cloud.task.batch.listener.support.JobExecutionEvent;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.cloud.task.batch.listener.support.TaskEventProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
@Tag("DockerRequired")
public class BatchEventsApplicationTests {
private static final String TASK_NAME = "taskEventTest";
static {
GenericContainer rabbitmq = new RabbitMQContainer("rabbitmq:3.8.9")
.withExposedPorts(5672);
rabbitmq.start();
final Integer mappedPort = rabbitmq.getMappedPort(5672);
System.setProperty("spring.rabbitmq.test.port", mappedPort.toString());
rabbitPort = mappedPort.toString();
private ConfigurableApplicationContext applicationContext;
private final ObjectMapper objectMapper = new ObjectMapper();
private final TaskEventProperties taskEventProperties = new TaskEventProperties();
@BeforeEach
public void setup() {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
// Count for two job execution events per task
static CountDownLatch jobExecutionLatch = new CountDownLatch(2);
private static String rabbitPort;
@Test
public void testExecution() throws Exception {
SpringApplication
.run(JobExecutionListenerBinding.class, "--spring.main.web-environment=false");
SpringApplication.run(BatchEventsApplication.class, "--server.port=0",
"--spring.cloud.stream.bindings.output.producer.requiredGroups=testgroup",
"--spring.jmx.default-domain=fakedomain",
"--spring.main.webEnvironment=false",
"--spring.rabbitmq.port=" + rabbitPort);
assertThat(jobExecutionLatch.await(60, TimeUnit.SECONDS))
.as("The latch did not count down to zero before timeout").isTrue();
}
@EnableBinding(Sink.class)
@PropertySource("classpath:io/spring/task/listener/job-listener-sink-channel.properties")
@Configuration(proxyBeanMethods = false)
public static class JobExecutionListenerBinding {
@StreamListener(Sink.INPUT)
public void receive(JobExecutionEvent execution) {
BDDAssertions.then(execution.getJobInstance().getJobName())
.isEqualTo("job").as("Job name should be job");
jobExecutionLatch.countDown();
@AfterEach
public void tearDown() {
if (this.applicationContext != null && this.applicationContext.isActive()) {
this.applicationContext.close();
}
}
@Test
public void testExecution() throws Exception {
List<Message<byte[]>> result = testListener(
taskEventProperties.getJobExecutionEventBindingName(), 1);
JobExecutionEvent jobExecutionEvent = this.objectMapper.readValue(result.get(0).getPayload(),
JobExecutionEvent.class);
assertThat(jobExecutionEvent.getJobInstance().getJobName())
.isEqualTo("job").as("Job name should be job");
}
private String[] getCommandLineParams(boolean enableFailJobConfig) {
String jobConfig = enableFailJobConfig ?
"--spring.cloud.task.test.enable-job-configuration=true" :
"--spring.cloud.task.test.enable-fail-job-configuration=true";
return new String[]{"--spring.cloud.task.closecontext_enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false",
"--spring.cloud.stream.defaultBinder=rabbit",
"--spring.cloud.stream.bindings.task-events.destination=test",
jobConfig,
"foo=" + UUID.randomUUID()};
}
private List<Message<byte[]>> testListener(String bindingName, int numberToRead) {
List<Message<byte[]>> results = new ArrayList<>();
this.applicationContext = new SpringApplicationBuilder()
.sources(TestChannelBinderConfiguration
.getCompleteConfiguration(BatchEventsTestApplication.class)).web(WebApplicationType.NONE).build()
.run(getCommandLineParams(true));
OutputDestination target = this.applicationContext.getBean(OutputDestination.class);
for (int i = 0; i < numberToRead; i++) {
results.add(target.receive(10000, bindingName));
}
return results;
}
@SpringBootApplication
@Import({BatchEventsApplication.class})
public static class BatchEventsTestApplication {
}
}

View File

@@ -58,13 +58,16 @@
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring-cloud-stream}</version>
<type>test-jar</type>
<classifier>test-binder</classifier>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 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.
@@ -18,14 +18,15 @@ package io.spring;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.task.launcher.TaskLaunchRequest;
import org.springframework.integration.annotation.Transformer;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.util.StringUtils;
/**
@@ -34,41 +35,43 @@ import org.springframework.util.StringUtils;
*
* @author Glenn Renfro
*/
@EnableBinding(Processor.class)
@EnableConfigurationProperties(TaskProcessorProperties.class)
@Configuration
public class TaskProcessor {
@Autowired
private TaskProcessorProperties processorProperties;
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object setupRequest(String message) {
Map<String, String> properties = new HashMap<>();
if (StringUtils.hasText(this.processorProperties.getDataSourceUrl())) {
properties
.put("spring_datasource_url", this.processorProperties
.getDataSourceUrl());
}
if (StringUtils
.hasText(this.processorProperties.getDataSourceDriverClassName())) {
properties.put("spring_datasource_driverClassName", this.processorProperties
.getDataSourceDriverClassName());
}
if (StringUtils.hasText(this.processorProperties.getDataSourceUserName())) {
properties.put("spring_datasource_username", this.processorProperties
.getDataSourceUserName());
}
if (StringUtils.hasText(this.processorProperties.getDataSourcePassword())) {
properties.put("spring_datasource_password", this.processorProperties
.getDataSourcePassword());
}
properties.put("payload", message);
@Bean
public Function<Message<String>, Message<TaskLaunchRequest>> processRequest() {
return (messagePayload) -> {
String message = messagePayload.getPayload();
Map<String, String> properties = new HashMap<>();
if (StringUtils.hasText(this.processorProperties.getDataSourceUrl())) {
properties
.put("spring_datasource_url", this.processorProperties
.getDataSourceUrl());
}
if (StringUtils
.hasText(this.processorProperties.getDataSourceDriverClassName())) {
properties.put("spring_datasource_driverClassName", this.processorProperties
.getDataSourceDriverClassName());
}
if (StringUtils.hasText(this.processorProperties.getDataSourceUserName())) {
properties.put("spring_datasource_username", this.processorProperties
.getDataSourceUserName());
}
if (StringUtils.hasText(this.processorProperties.getDataSourcePassword())) {
properties.put("spring_datasource_password", this.processorProperties
.getDataSourcePassword());
}
properties.put("payload", message);
TaskLaunchRequest request = new TaskLaunchRequest(
this.processorProperties.getUri(), null, properties, null,
this.processorProperties.getApplicationName());
TaskLaunchRequest request = new TaskLaunchRequest(
this.processorProperties.getUri(), null, properties, null,
this.processorProperties.getApplicationName());
return new GenericMessage<>(request);
return MessageBuilder.withPayload(request).build();
};
}
}

View File

@@ -0,0 +1,3 @@
maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot
spring.cloud.stream.function.bindings.processRequest-in-0=input
spring.cloud.stream.function.bindings.processRequest-out-0=output

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 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.
@@ -17,57 +17,89 @@
package io.spring;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.stream.binder.test.InputDestination;
import org.springframework.cloud.stream.binder.test.OutputDestination;
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.cloud.task.launcher.TaskLaunchRequest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Glenn Renfro
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = TaskProcessorApplication.class)
public class TaskProcessorApplicationTests {
private static final String DEFAULT_PAYLOAD = "hello";
@Autowired
protected Processor channels;
private final ObjectMapper objectMapper = new ObjectMapper();
private ConfigurableApplicationContext applicationContext;
@Autowired
protected MessageCollector collector;
@BeforeEach
public void setup() {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
private ObjectMapper mapper = new ObjectMapper();
@AfterEach
public void tearDown() {
if (this.applicationContext != null && this.applicationContext.isActive()) {
this.applicationContext.close();
}
}
@Test
public void test() throws InterruptedException, IOException {
this.channels.input().send(new GenericMessage<Object>(DEFAULT_PAYLOAD));
public void test() throws IOException {
Map<String, String> properties = new HashMap();
properties.put("payload", DEFAULT_PAYLOAD);
TaskLaunchRequest expectedRequest = new TaskLaunchRequest(
"maven://org.springframework.cloud.task.app:"
+ "timestamp-task:jar:1.0.1.RELEASE", null, properties,
null, null);
Message<String> result = (Message<String>) this.collector
.forChannel(this.channels.output())
.take();
TaskLaunchRequest tlq = this.mapper
.readValue(result.getPayload(), TaskLaunchRequest.class);
List<Message<byte[]>> result = testListener("output", 1);
TaskLaunchRequest tlq = objectMapper.readValue(result.get(0).getPayload(), TaskLaunchRequest.class);
assertThat(tlq).isEqualTo(expectedRequest);
}
private List<Message<byte[]>> testListener(String bindingName, int numberToRead) {
List<Message<byte[]>> results = new ArrayList<>();
this.applicationContext = new SpringApplicationBuilder()
.sources(TestChannelBinderConfiguration
.getCompleteConfiguration(TaskProcessorTestApplication.class)).web(WebApplicationType.NONE)
.run();
InputDestination input = this.applicationContext.getBean(InputDestination.class);
OutputDestination target = this.applicationContext.getBean(OutputDestination.class);
input.send(new GenericMessage<>(DEFAULT_PAYLOAD.getBytes(StandardCharsets.UTF_8)));
for (int i = 0; i < numberToRead; i++) {
results.add(target.receive(10000, bindingName));
}
return results;
}
@SpringBootApplication
@Import({TaskProcessor.class})
public static class TaskProcessorTestApplication {
}
}

View File

@@ -23,6 +23,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipInstall>true</skipInstall>
<spring-cloud-stream>3.2.0-SNAPSHOT</spring-cloud-stream>
<spring-cloud-deployer-version>2.7.0</spring-cloud-deployer-version>
</properties>
<dependencyManagement>
@@ -55,13 +56,21 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-deployer-local</artifactId>
<version>${spring-cloud-deployer-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring-cloud-stream}</version>
<type>test-jar</type>
<classifier>test-binder</classifier>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -1 +1,4 @@
maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot
spring.cloud.stream.function.bindings.taskLauncherSink-in-0=input
spring.cloud.stream.bindings.input.destination=taskLauncherSinkExchange

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2021 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.
@@ -16,7 +16,6 @@
package io.spring;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -29,7 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.deployer.spi.core.AppDeploymentRequest;
import org.springframework.cloud.deployer.spi.task.TaskLauncher;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.cloud.task.launcher.TaskLaunchRequest;
import org.springframework.context.ApplicationContext;
import org.springframework.messaging.support.GenericMessage;
@@ -49,11 +48,10 @@ public class TaskSinkApplicationTests {
ApplicationContext context;
@Autowired
private Sink sink;
private StreamBridge streamBridge;
@Test
public void testLaunch() throws IOException {
assertThat(this.sink.input()).isNotNull();
public void testLaunch() {
TaskLauncher testTaskLauncher =
this.context.getBean(TaskLauncher.class);
@@ -65,8 +63,7 @@ public class TaskSinkApplicationTests {
+ "timestamp-task:jar:1.0.1.RELEASE", null, properties,
null, null);
GenericMessage<TaskLaunchRequest> message = new GenericMessage<>(request);
this.sink.input().send(message);
this.streamBridge.send("taskLauncherSink-in-0", message);
ArgumentCaptor<AppDeploymentRequest> deploymentRequest = ArgumentCaptor
.forClass(AppDeploymentRequest.class);