From 7843ba2edb3d9e9a92b54467fed367986bd0a9a3 Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Mon, 29 Jan 2018 14:57:30 -0500 Subject: [PATCH] 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 --- pom.xml | 14 +-- spring-cloud-task-samples/tasksink/pom.xml | 6 +- .../cloud/task/launcher/TaskLauncherSink.java | 10 +- .../task/launcher/TaskLauncherSinkTests.java | 97 ++++++++++++++----- 4 files changed, 87 insertions(+), 40 deletions(-) diff --git a/pom.xml b/pom.xml index 558d509f..acfab3dd 100755 --- a/pom.xml +++ b/pom.xml @@ -132,13 +132,13 @@ - 2.0.0.M3 - 2.0.0.M1 - 2.0.0.M1 - 2.0.0.M3 - 2.0.0.M1 - 2.0.0.M1 - 4.0.0.M5 + 2.0.0.M4 + 1.3.0.RELEASE + 1.3.0.RELEASE + 2.0.0.M4 + 1.3.0.RELEASE + 1.3.0.RELEASE + 4.0.0.RELEASE 1.1 8.0 diff --git a/spring-cloud-task-samples/tasksink/pom.xml b/spring-cloud-task-samples/tasksink/pom.xml index 86de9126..252174a3 100644 --- a/spring-cloud-task-samples/tasksink/pom.xml +++ b/spring-cloud-task-samples/tasksink/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 2.0.0.M5 + 2.0.0.RC1 @@ -42,13 +42,13 @@ org.springframework.cloud spring-cloud-starter-stream-rabbit - 2.0.0.M3 + 2.0.0.M4 compile org.springframework.cloud spring-cloud-stream-test-support - 2.0.0.M3 + 2.0.0.M4 test diff --git a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java index 4a932e25..b07331c2 100644 --- a/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java +++ b/spring-cloud-task-stream/src/main/java/org/springframework/cloud/task/launcher/TaskLauncherSink.java @@ -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()); diff --git a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherSinkTests.java b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherSinkTests.java index 041f2fe9..4c558b5f 100644 --- a/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherSinkTests.java +++ b/spring-cloud-task-stream/src/test/java/org/springframework/cloud/task/launcher/TaskLauncherSinkTests.java @@ -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 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 commandLineArgs, String applicationName) throws Exception { + TaskConfiguration.TestTaskLauncher testTaskLauncher = context.getBean(TaskConfiguration.TestTaskLauncher.class); + String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs, applicationName); + GenericMessage message = new GenericMessage<>(stringRequest); + + this.sink.input().send(message); + return testTaskLauncher; } - private TaskConfiguration.TestTaskLauncher launchTask(String artifactURL, - List commandLineArgs, String applicationName) { + private TaskConfiguration.TestTaskLauncher launchTaskByteArray(String artifactURL, + List commandLineArgs, String applicationName) throws Exception { TaskConfiguration.TestTaskLauncher testTaskLauncher = context.getBean(TaskConfiguration.TestTaskLauncher.class); + String stringRequest = getStringTaskLaunchRequest(artifactURL, commandLineArgs, applicationName); + GenericMessage message = new GenericMessage<>(stringRequest.getBytes()); + this.sink.input().send(message); + return testTaskLauncher; + } + + private String getStringTaskLaunchRequest(String artifactURL, + List 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 commandLineArgs, String applicationName) throws Exception { + TaskConfiguration.TestTaskLauncher testTaskLauncher = + context.getBean(TaskConfiguration.TestTaskLauncher.class); TaskLaunchRequest request = new TaskLaunchRequest(artifactURL, commandLineArgs, properties, null, applicationName); GenericMessage message = new GenericMessage<>(request); + this.sink.input().send(message); return testTaskLauncher; }