Updated TaskLauncherSink to be able to use SCST 2.0

* Updated tests
 - to handle additional cases
 - removed `now` invalid tests
* removed logging payload from the TaskLauncher
* handles message instead TaskLaunchRequestPOJO

resolves #385
This commit is contained in:
Glenn Renfro
2018-01-29 14:57:30 -05:00
committed by Michael Minella
parent 71b74665a0
commit 7843ba2edb
4 changed files with 87 additions and 40 deletions

14
pom.xml
View File

@@ -132,13 +132,13 @@
</modules>
<properties>
<spring-cloud-stream.version>2.0.0.M3</spring-cloud-stream.version>
<spring-cloud-deployer-spi.version>2.0.0.M1</spring-cloud-deployer-spi.version>
<spring-cloud-deployer-local.version>2.0.0.M1</spring-cloud-deployer-local.version>
<spring-cloud-stream-binder-rabbit.version>2.0.0.M3</spring-cloud-stream-binder-rabbit.version>
<spring-cloud-deployer-resource-support.version>2.0.0.M1</spring-cloud-deployer-resource-support.version>
<spring-cloud-deployer-resource-maven.version>2.0.0.M1</spring-cloud-deployer-resource-maven.version>
<spring-batch.version>4.0.0.M5</spring-batch.version>
<spring-cloud-stream.version>2.0.0.M4</spring-cloud-stream.version>
<spring-cloud-deployer-spi.version>1.3.0.RELEASE</spring-cloud-deployer-spi.version>
<spring-cloud-deployer-local.version>1.3.0.RELEASE</spring-cloud-deployer-local.version>
<spring-cloud-stream-binder-rabbit.version>2.0.0.M4</spring-cloud-stream-binder-rabbit.version>
<spring-cloud-deployer-resource-support.version>1.3.0.RELEASE</spring-cloud-deployer-resource-support.version>
<spring-cloud-deployer-resource-maven.version>1.3.0.RELEASE</spring-cloud-deployer-resource-maven.version>
<spring-batch.version>4.0.0.RELEASE</spring-batch.version>
<commons-logging.version>1.1</commons-logging.version>
<java-ee-api.version>8.0</java-ee-api.version>

View File

@@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M5</version>
<version>2.0.0.RC1</version>
<relativePath />
</parent>
@@ -42,13 +42,13 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<version>2.0.0.M3</version>
<version>2.0.0.M4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<version>2.0.0.M3</version>
<version>2.0.0.M4</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -50,17 +50,17 @@ public class TaskLauncherSink {
/**
* Launches a task upon the receipt of a valid TaskLaunchRequest.
* @param request is a TaskLaunchRequest containing the information required to launch
* @param taskLaunchRequest is a TaskLaunchRequest containing the information required to launch
* a task.
*/
@ServiceActivator(inputChannel = Sink.INPUT)
public void taskLauncherSink(TaskLaunchRequest request) {
launchTask(request);
public void taskLauncherSink(TaskLaunchRequest taskLaunchRequest) throws Exception{
launchTask(taskLaunchRequest);
}
private void launchTask(TaskLaunchRequest taskLaunchRequest) {
Assert.notNull(this.taskLauncher, "TaskLauncher has not been initialized");
logger.info("Launching Task for the following resource " + taskLaunchRequest);
logger.info("Launching Task for the following uri " + taskLaunchRequest.getUri());
Resource resource = this.resourceLoader.getResource(taskLaunchRequest.getUri());
AppDefinition definition = new AppDefinition(taskLaunchRequest.getApplicationName(), taskLaunchRequest.getEnvironmentProperties());
AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, taskLaunchRequest.getDeploymentProperties(), taskLaunchRequest.getCommandlineArguments());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -32,7 +33,6 @@ import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.task.launcher.configuration.TaskConfiguration;
import org.springframework.cloud.task.launcher.util.TaskLauncherSinkApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.junit4.SpringRunner;
@@ -74,14 +74,22 @@ public class TaskLauncherSinkTests {
}
@Test
public void testSuccessWithParams() {
public void testSuccessWithParams() throws Exception {
List<String> commandLineArgs = new ArrayList<>();
commandLineArgs.add(PARAM1);
commandLineArgs.add(PARAM2);
TaskConfiguration.TestTaskLauncher testTaskLauncher =
launchTask(VALID_URL, commandLineArgs, null);
TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL, commandLineArgs, null);
verifySuccessWithParams(testTaskLauncher);
testTaskLauncher = launchTaskByteArray(VALID_URL, commandLineArgs, null);
verifySuccessWithParams(testTaskLauncher);
testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, commandLineArgs, null);
verifySuccessWithParams(testTaskLauncher);
}
private void verifySuccessWithParams(TaskConfiguration.TestTaskLauncher testTaskLauncher) {
assertEquals(LaunchState.complete, testTaskLauncher.status(DEFAULT_STATUS).getState());
assertEquals(2, testTaskLauncher.getCommandlineArguments().size());
assertEquals(PARAM1, testTaskLauncher.getCommandlineArguments().get(0));
@@ -90,53 +98,92 @@ public class TaskLauncherSinkTests {
}
@Test
public void testSuccessWithAppName() {
TaskConfiguration.TestTaskLauncher testTaskLauncher =
launchTask(VALID_URL, null, APP_NAME);
public void testSuccessWithAppName() throws Exception {
TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL, null, APP_NAME);
verifySuccessWithAppName(testTaskLauncher);
testTaskLauncher = launchTaskByteArray(VALID_URL, null, APP_NAME);
verifySuccessWithAppName(testTaskLauncher);
testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, null, APP_NAME);
verifySuccessWithAppName(testTaskLauncher);
}
@Test
public void testInvalidJar() throws Exception{
TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskTaskLaunchRequest(
INVALID_URL, null, APP_NAME);
verifySuccessWithAppName(testTaskLauncher);
}
private void verifySuccessWithAppName(TaskConfiguration.TestTaskLauncher testTaskLauncher) {
assertEquals(LaunchState.complete, testTaskLauncher.status(DEFAULT_STATUS).getState());
assertEquals(0, testTaskLauncher.getCommandlineArguments().size());
assertEquals(APP_NAME, testTaskLauncher.getApplicationName());
}
@Test
public void testSuccessNoParams() {
TaskConfiguration.TestTaskLauncher testTaskLauncher =
launchTask(VALID_URL, null, null);
public void testSuccessNoParams() throws Exception {
TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTaskString(VALID_URL, null, null);
verifySuccessWithNoParams(testTaskLauncher);
testTaskLauncher = launchTaskByteArray(VALID_URL, null, null);
verifySuccessWithNoParams(testTaskLauncher);
testTaskLauncher = launchTaskTaskLaunchRequest(VALID_URL, null, null);
verifySuccessWithNoParams(testTaskLauncher);
}
private void verifySuccessWithNoParams(TaskConfiguration.TestTaskLauncher testTaskLauncher) {
assertEquals(LaunchState.complete, testTaskLauncher.status(DEFAULT_STATUS).getState());
assertEquals(0, testTaskLauncher.getCommandlineArguments().size());
assertTrue(testTaskLauncher.getApplicationName().startsWith(TASK_NAME_PREFIX));
}
@Test(expected = MessageHandlingException.class)
public void testInvalidJar() {
TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTask(
INVALID_URL, null, null);
}
@Test
public void testNoRun() {
TaskConfiguration.TestTaskLauncher testTaskLauncher =
context.getBean(TaskConfiguration.TestTaskLauncher.class);
assertEquals(LaunchState.unknown, testTaskLauncher.status(DEFAULT_STATUS).getState());
}
@Test(expected = IllegalArgumentException.class)
public void testNoTaskLauncher() {
TaskLauncherSink sink = new TaskLauncherSink();
sink.taskLauncherSink(new TaskLaunchRequest(VALID_URL, null, properties,
null, null));
private TaskConfiguration.TestTaskLauncher launchTaskString(String artifactURL,
List<String> commandLineArgs, String applicationName) throws Exception {
TaskConfiguration.TestTaskLauncher testTaskLauncher = context.getBean(TaskConfiguration.TestTaskLauncher.class);
String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs, applicationName);
GenericMessage<String> message = new GenericMessage<>(stringRequest);
this.sink.input().send(message);
return testTaskLauncher;
}
private TaskConfiguration.TestTaskLauncher launchTask(String artifactURL,
List<String> commandLineArgs, String applicationName) {
private TaskConfiguration.TestTaskLauncher launchTaskByteArray(String artifactURL,
List<String> commandLineArgs, String applicationName) throws Exception {
TaskConfiguration.TestTaskLauncher testTaskLauncher =
context.getBean(TaskConfiguration.TestTaskLauncher.class);
String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs, applicationName);
GenericMessage<byte[]> message = new GenericMessage<>(stringRequest.getBytes());
this.sink.input().send(message);
return testTaskLauncher;
}
private String getStringTaskLaunchRequest(String artifactURL,
List<String> commandLineArgs, String applicationName) throws Exception {
TaskLaunchRequest request = new TaskLaunchRequest(artifactURL,
commandLineArgs, properties, null, applicationName);
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(request);
}
private TaskConfiguration.TestTaskLauncher launchTaskTaskLaunchRequest(String artifactURL,
List<String> commandLineArgs, String applicationName) throws Exception {
TaskConfiguration.TestTaskLauncher testTaskLauncher =
context.getBean(TaskConfiguration.TestTaskLauncher.class);
TaskLaunchRequest request = new TaskLaunchRequest(artifactURL,
commandLineArgs, properties, null, applicationName);
GenericMessage<TaskLaunchRequest> message = new GenericMessage<>(request);
this.sink.input().send(message);
return testTaskLauncher;
}