Add Application Name to TaskLaunchRequest for launched task
resolves #238
This commit is contained in:
committed by
Michael Minella
parent
cc1f971f3e
commit
4a1a2c3c88
@@ -34,6 +34,8 @@ import static org.junit.Assert.assertTrue;
|
||||
public class TaskLaunchRequestTests {
|
||||
public static final String URI = "http://myURI";
|
||||
|
||||
public static final String APP_NAME = "MY_APP_NAME";
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
List<String> args = new ArrayList<>();
|
||||
@@ -42,46 +44,50 @@ public class TaskLaunchRequestTests {
|
||||
map.put("bar", "baz");
|
||||
|
||||
TaskLaunchRequest request = new TaskLaunchRequest(URI,
|
||||
Collections.EMPTY_LIST,
|
||||
Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP);
|
||||
Collections.EMPTY_LIST, Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP, null);
|
||||
TaskLaunchRequest request2 = new TaskLaunchRequest(URI,
|
||||
Collections.EMPTY_LIST,
|
||||
Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP);
|
||||
Collections.EMPTY_LIST, Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP, null);
|
||||
assertFalse(request.equals(null));
|
||||
assertFalse(request.equals("nope"));
|
||||
assertTrue(request.equals(request));
|
||||
assertTrue(request.equals(request2));
|
||||
TaskLaunchRequest requestDiff = new TaskLaunchRequest("http://oops",
|
||||
Collections.EMPTY_LIST,
|
||||
Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP);
|
||||
Collections.EMPTY_LIST, Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP, null);
|
||||
assertFalse(request.equals(requestDiff));
|
||||
|
||||
requestDiff = new TaskLaunchRequest(URI, args, Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP, null);
|
||||
assertFalse(request.equals(requestDiff));
|
||||
|
||||
requestDiff = new TaskLaunchRequest(URI,
|
||||
args,
|
||||
Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP);
|
||||
assertFalse(request.equals(requestDiff));
|
||||
|
||||
requestDiff = new TaskLaunchRequest(URI,
|
||||
null, null, null);
|
||||
null, null, null, null);
|
||||
assertTrue(request.equals(requestDiff));
|
||||
|
||||
requestDiff = new TaskLaunchRequest(URI,
|
||||
Collections.EMPTY_LIST,
|
||||
map,
|
||||
Collections.EMPTY_MAP);
|
||||
requestDiff = new TaskLaunchRequest(URI, Collections.EMPTY_LIST, map,
|
||||
Collections.EMPTY_MAP, null);
|
||||
assertFalse(request.equals(requestDiff));
|
||||
|
||||
requestDiff = new TaskLaunchRequest(URI,
|
||||
Collections.EMPTY_LIST,
|
||||
Collections.EMPTY_MAP,
|
||||
map);
|
||||
requestDiff = new TaskLaunchRequest(URI, Collections.EMPTY_LIST,
|
||||
Collections.EMPTY_MAP, map, null);
|
||||
assertFalse(request.equals(requestDiff));
|
||||
|
||||
assertEquals(request.hashCode(), request.hashCode());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplicationName() {
|
||||
TaskLaunchRequest request = new TaskLaunchRequest(URI,
|
||||
Collections.EMPTY_LIST, Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP, null);
|
||||
assertTrue(request.getApplicationName().startsWith("Task-"));
|
||||
|
||||
request = new TaskLaunchRequest(URI,
|
||||
Collections.EMPTY_LIST, Collections.EMPTY_MAP,
|
||||
Collections.EMPTY_MAP, APP_NAME);
|
||||
assertEquals(APP_NAME, request.getApplicationName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,23 +26,27 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.deployer.spi.task.LaunchState;
|
||||
import org.springframework.cloud.stream.annotation.Bindings;
|
||||
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.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = {TaskLauncherSinkApplication.class, TaskConfiguration.class} )
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = {TaskLauncherSinkApplication.class, TaskConfiguration.class} )
|
||||
public class TaskLauncherSinkTests {
|
||||
|
||||
private final static String TASK_NAME_PREFIX = "Task-";
|
||||
|
||||
private final static String APP_NAME = "MY_APP_NAME";
|
||||
|
||||
private final static String PARAM1 = "FOO";
|
||||
|
||||
private final static String PARAM2 = "BAR";
|
||||
@@ -61,7 +65,6 @@ public class TaskLauncherSinkTests {
|
||||
private ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
@Bindings(TaskLauncherSink.class)
|
||||
private Sink sink;
|
||||
|
||||
@Before
|
||||
@@ -72,29 +75,43 @@ public class TaskLauncherSinkTests {
|
||||
|
||||
@Test
|
||||
public void testSuccessWithParams() {
|
||||
|
||||
List<String> commandLineArgs = new ArrayList<>();
|
||||
commandLineArgs.add(PARAM1);
|
||||
commandLineArgs.add(PARAM2);
|
||||
|
||||
TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTask(VALID_URL, commandLineArgs);
|
||||
TaskConfiguration.TestTaskLauncher testTaskLauncher =
|
||||
launchTask(VALID_URL, commandLineArgs, null);
|
||||
|
||||
assertEquals(LaunchState.complete, testTaskLauncher.status(DEFAULT_STATUS).getState());
|
||||
assertEquals(2, testTaskLauncher.getCommandlineArguments().size());
|
||||
assertEquals(testTaskLauncher.getCommandlineArguments().get(0), PARAM1);
|
||||
assertEquals(testTaskLauncher.getCommandlineArguments().get(1), PARAM2);
|
||||
assertEquals(PARAM1, testTaskLauncher.getCommandlineArguments().get(0));
|
||||
assertEquals(PARAM2, testTaskLauncher.getCommandlineArguments().get(1));
|
||||
assertTrue(testTaskLauncher.getApplicationName().startsWith(TASK_NAME_PREFIX));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccessWithAppName() {
|
||||
TaskConfiguration.TestTaskLauncher testTaskLauncher =
|
||||
launchTask(VALID_URL, null, APP_NAME);
|
||||
|
||||
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);
|
||||
TaskConfiguration.TestTaskLauncher testTaskLauncher =
|
||||
launchTask(VALID_URL, null, null);
|
||||
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);
|
||||
TaskConfiguration.TestTaskLauncher testTaskLauncher = launchTask(
|
||||
INVALID_URL, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -108,14 +125,17 @@ public class TaskLauncherSinkTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNoTaskLauncher() {
|
||||
TaskLauncherSink sink = new TaskLauncherSink();
|
||||
sink.taskLauncherSink(new TaskLaunchRequest(VALID_URL, null, properties, null));
|
||||
sink.taskLauncherSink(new TaskLaunchRequest(VALID_URL, null, properties,
|
||||
null, null));
|
||||
}
|
||||
|
||||
private TaskConfiguration.TestTaskLauncher launchTask(String artifactURL, List<String> commandLineArgs) {
|
||||
private TaskConfiguration.TestTaskLauncher launchTask(String artifactURL,
|
||||
List<String> commandLineArgs, String applicationName) {
|
||||
TaskConfiguration.TestTaskLauncher testTaskLauncher =
|
||||
context.getBean(TaskConfiguration.TestTaskLauncher.class);
|
||||
|
||||
TaskLaunchRequest request = new TaskLaunchRequest(artifactURL, commandLineArgs, properties, null);
|
||||
TaskLaunchRequest request = new TaskLaunchRequest(artifactURL,
|
||||
commandLineArgs, properties, null, applicationName);
|
||||
GenericMessage<TaskLaunchRequest> message = new GenericMessage<>(request);
|
||||
this.sink.input().send(message);
|
||||
return testTaskLauncher;
|
||||
|
||||
@@ -45,10 +45,13 @@ public class TaskConfiguration {
|
||||
|
||||
private List<String> commandlineArguments;
|
||||
|
||||
private String applicationName;
|
||||
|
||||
@Override
|
||||
public String launch(AppDeploymentRequest request) {
|
||||
state = LaunchState.complete;
|
||||
this.commandlineArguments = request.getCommandlineArguments();
|
||||
this.applicationName = request.getDefinition().getName();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -65,5 +68,9 @@ public class TaskConfiguration {
|
||||
public List<String> getCommandlineArguments() {
|
||||
return commandlineArguments;
|
||||
}
|
||||
|
||||
public String getApplicationName() {
|
||||
return applicationName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user