Checks for invalid ExecutionId in the TaskLifeCycleListener
resolves #115 * Fixes bug where if the user set the environment variable and commandline args a unique constraint would fire. * Updated docs * Removed deprecation * Fixed version number for integration test. Added integration tests for externally generated task executions
This commit is contained in:
@@ -136,7 +136,8 @@ public class SimpleTaskConfiguration {
|
||||
this.platformTransactionManager = taskConfigurer.getTransactionManager();
|
||||
this.taskExplorer = taskConfigurer.getTaskExplorer();
|
||||
|
||||
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, taskNameResolver(), this.applicationArguments);
|
||||
this.taskLifecycleListener = new TaskLifecycleListener(this.taskRepository, taskNameResolver(),
|
||||
this.applicationArguments, taskExplorer);
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ import org.springframework.boot.ExitCodeEvent;
|
||||
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.cloud.task.repository.TaskNameResolver;
|
||||
import org.springframework.cloud.task.repository.TaskRepository;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
@@ -75,6 +76,8 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
|
||||
|
||||
private final TaskRepository taskRepository;
|
||||
|
||||
private final TaskExplorer taskExplorer;
|
||||
|
||||
private TaskExecution taskExecution;
|
||||
|
||||
private boolean started = false;
|
||||
@@ -92,18 +95,23 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
|
||||
@Value("${spring.cloud.task.closecontext.enable:true}")
|
||||
private Boolean closeContext;
|
||||
|
||||
@Value("${spring.cloud.task.executionid:}")
|
||||
private Integer taskExecutionId;
|
||||
|
||||
/**
|
||||
* @param taskRepository The repository to record executions in.
|
||||
*/
|
||||
public TaskLifecycleListener(TaskRepository taskRepository,
|
||||
TaskNameResolver taskNameResolver,
|
||||
ApplicationArguments applicationArguments) {
|
||||
ApplicationArguments applicationArguments, TaskExplorer taskExplorer) {
|
||||
Assert.notNull(taskRepository, "A taskRepository is required");
|
||||
Assert.notNull(taskNameResolver, "A taskNameResolver is required");
|
||||
Assert.notNull(taskExplorer, "A taskExplorer is required");
|
||||
|
||||
this.taskRepository = taskRepository;
|
||||
this.taskNameResolver = taskNameResolver;
|
||||
this.applicationArguments = applicationArguments;
|
||||
this.taskExplorer = taskExplorer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,9 +195,18 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
|
||||
if(this.applicationArguments != null) {
|
||||
args = Arrays.asList(this.applicationArguments.getSourceArgs());
|
||||
}
|
||||
|
||||
this.taskExecution = this.taskRepository.createTaskExecution(
|
||||
this.taskNameResolver.getTaskName(), new Date(), args);
|
||||
if(this.taskExecutionId != null) {
|
||||
TaskExecution taskExecution = taskExplorer.getTaskExecution(this.taskExecutionId);
|
||||
Assert.notNull(taskExecution, String.format("Invalid TaskExecution, ID %s not found", this.taskExecutionId));
|
||||
Assert.isNull(taskExecution.getEndTime(), String.format(
|
||||
"Invalid TaskExecution, ID %s task is already complete", this.taskExecutionId));
|
||||
this.taskExecution = this.taskRepository.startTaskExecution(this.taskExecutionId,
|
||||
this.taskNameResolver.getTaskName(), new Date(), args);
|
||||
}
|
||||
else {
|
||||
this.taskExecution = this.taskRepository.createTaskExecution(
|
||||
this.taskNameResolver.getTaskName(), new Date(), args);
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.error("Multiple start events have been received. The first one was " +
|
||||
|
||||
@@ -82,13 +82,12 @@ public class TaskExecution {
|
||||
String errorMessage) {
|
||||
|
||||
Assert.notNull(arguments, "arguments must not be null");
|
||||
Assert.notNull(startTime, "startTime must not be null");
|
||||
this.executionId = executionId;
|
||||
this.exitCode = exitCode;
|
||||
this.taskName = taskName;
|
||||
this.exitMessage = exitMessage;
|
||||
this.arguments = new ArrayList<>(arguments);
|
||||
this.startTime = (Date)startTime.clone();
|
||||
this.startTime = (startTime != null) ? (Date)startTime.clone() : null;
|
||||
this.endTime = (endTime != null) ? (Date)endTime.clone() : null;
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
@@ -68,4 +68,26 @@ public interface TaskRepository {
|
||||
TaskExecution createTaskExecution(String taskName,
|
||||
Date startTime,List<String> arguments);
|
||||
|
||||
/**
|
||||
* Creates an empty TaskExecution with just an id provided. This is intended to be
|
||||
* utilized in systems where the request of launching a task is separate from the
|
||||
* actual start of a task (the underlying system may need to deploy the task prior to
|
||||
* launching, etc).
|
||||
*
|
||||
* @return the initial {@link TaskExecution}
|
||||
*/
|
||||
@Transactional
|
||||
TaskExecution createTaskExecution();
|
||||
|
||||
/**
|
||||
* Notifies the repository that a taskExecution has has started.
|
||||
* @param executionid to the task execution to be updated.
|
||||
* @param taskName the name that associated with the task execution.
|
||||
* @param startTime the time task began.
|
||||
* @param arguments list of key/value pairs that configure the task.
|
||||
* @return
|
||||
*/
|
||||
@Transactional
|
||||
TaskExecution startTaskExecution(long executionid, String taskName,
|
||||
Date startTime,List<String> arguments);
|
||||
}
|
||||
|
||||
@@ -74,6 +74,9 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
|
||||
private static final String CREATE_TASK_ARGUMENT = "INSERT into "
|
||||
+ "%PREFIX%EXECUTION_PARAMS(TASK_EXECUTION_ID, TASK_PARAM ) values (?, ?)";
|
||||
|
||||
private static final String START_TASK_EXECUTION = "UPDATE %PREFIX%EXECUTION set "
|
||||
+ "START_TIME = ?, TASK_NAME = ?, LAST_UPDATED = ? where TASK_EXECUTION_ID = ?";
|
||||
|
||||
private static final String CHECK_TASK_EXECUTION_EXISTS = "SELECT COUNT(*) FROM "
|
||||
+ "%PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = ?";
|
||||
|
||||
@@ -127,16 +130,31 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
|
||||
@Override
|
||||
public TaskExecution createTaskExecution(String taskName,
|
||||
Date startTime, List<String> arguments) {
|
||||
long taskExecutionId = getNextExecutionId();
|
||||
TaskExecution taskExecution = new TaskExecution(taskExecutionId, null, taskName,
|
||||
long nextExecutionId = getNextExecutionId();
|
||||
|
||||
TaskExecution taskExecution = new TaskExecution(nextExecutionId, null, taskName,
|
||||
startTime, null, null, arguments, null);
|
||||
|
||||
Object[] queryParameters = new Object[]{ taskExecutionId, startTime, taskName, new Date()};
|
||||
Object[] queryParameters = new Object[]{ nextExecutionId, startTime, taskName, new Date()};
|
||||
jdbcTemplate.update(
|
||||
getQuery(SAVE_TASK_EXECUTION),
|
||||
queryParameters,
|
||||
new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP });
|
||||
insertTaskArguments(taskExecutionId, arguments);
|
||||
insertTaskArguments(nextExecutionId, arguments);
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments) {
|
||||
TaskExecution taskExecution = new TaskExecution(executionId, null, taskName,
|
||||
startTime, null, null, arguments, null);
|
||||
|
||||
Object[] queryParameters = new Object[]{ startTime, taskName, new Date(), executionId};
|
||||
jdbcTemplate.update(
|
||||
getQuery(START_TASK_EXECUTION),
|
||||
queryParameters,
|
||||
new int[]{ Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP, Types.BIGINT });
|
||||
insertTaskArguments(executionId, arguments);
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
@@ -146,7 +164,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
|
||||
// Check if given TaskExecution's Id already exists, if none is found
|
||||
// it is invalid and an exception should be thrown.
|
||||
if (jdbcTemplate.queryForObject(getQuery(CHECK_TASK_EXECUTION_EXISTS), Integer.class,
|
||||
new Object[]{ taskExecutionId}) != 1) {
|
||||
taskExecutionId) != 1) {
|
||||
throw new IllegalStateException("Invalid TaskExecution, ID " + taskExecutionId + " not found.");
|
||||
}
|
||||
|
||||
|
||||
@@ -61,8 +61,23 @@ public class MapTaskExecutionDao implements TaskExecutionDao {
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskExecution startTaskExecution(long executionId, String taskName, Date startTime, List<String> arguments) {
|
||||
TaskExecution taskExecution= taskExecutions.get(executionId);
|
||||
|
||||
taskExecution.setTaskName(taskName);
|
||||
taskExecution.setStartTime(startTime);
|
||||
taskExecution.setArguments(arguments);
|
||||
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void completeTaskExecution(long executionId, Integer exitCode, Date endTime, String exitMessage, String errorMessage) {
|
||||
if(!this.taskExecutions.containsKey(executionId)) {
|
||||
throw new IllegalStateException("Invalid TaskExecution, ID " + executionId + " not found.");
|
||||
}
|
||||
|
||||
TaskExecution taskExecution= taskExecutions.get(executionId);
|
||||
taskExecution.setEndTime(endTime);
|
||||
taskExecution.setExitCode(exitCode);
|
||||
|
||||
@@ -43,7 +43,19 @@ public interface TaskExecutionDao {
|
||||
Date startTime, List<String> arguments);
|
||||
|
||||
/**
|
||||
* Update and existing {@link TaskExecution}.
|
||||
* Update and existing {@link TaskExecution} to mark it as started.
|
||||
*
|
||||
* @param executionId the id of the taskExecution to be updated.
|
||||
* @param taskName the name that associated with the task execution.
|
||||
* @param startTime the time task began.
|
||||
* @param arguments list of key/value pairs that configure the task.
|
||||
* @since 1.1.0
|
||||
*/
|
||||
TaskExecution startTaskExecution(long executionId, String taskName,
|
||||
Date startTime, List<String> arguments);
|
||||
|
||||
/**
|
||||
* Update and existing {@link TaskExecution} to mark it as completed.
|
||||
*
|
||||
* @param executionId the id of the taskExecution to be updated.
|
||||
* @param exitCode the status of the task upon completion.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -109,6 +110,24 @@ public class SimpleTaskRepository implements TaskRepository {
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskExecution createTaskExecution() {
|
||||
initialize();
|
||||
TaskExecution taskExecution =
|
||||
taskExecutionDao.createTaskExecution(null, null, new ArrayList<String>(0));
|
||||
logger.debug("Creating: " + taskExecution.toString());
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TaskExecution startTaskExecution(long executionid, String taskName, Date startTime, List<String> arguments) {
|
||||
initialize();
|
||||
TaskExecution taskExecution =
|
||||
taskExecutionDao.startTaskExecution(executionid, taskName, startTime, arguments);
|
||||
logger.debug("Starting: " + taskExecution.toString());
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the taskExecutionDao associated with this repository.
|
||||
* @return the taskExecutionDao
|
||||
|
||||
@@ -7,8 +7,9 @@ import org.junit.Test;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.test.OutputCapture;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
import org.springframework.cloud.task.configuration.EnableTask;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -28,6 +29,8 @@ public class TaskCoreTests {
|
||||
private static final String UPDATE_TASK_MESSAGE = "Updating: TaskExecution with executionId=";
|
||||
private static final String SUCCESS_EXIT_CODE_MESSAGE = "with the following {exitCode=0";
|
||||
private static final String EXCEPTION_EXIT_CODE_MESSAGE = "with the following {exitCode=1";
|
||||
private static final String EXCEPTION_INVALID_TASK_EXECUTION_ID =
|
||||
"java.lang.IllegalArgumentException: Invalid TaskExecution, ID 55 not found";
|
||||
private static final String ERROR_MESSAGE =
|
||||
"errorMessage='java.lang.IllegalStateException: Failed to execute CommandLineRunner";
|
||||
|
||||
@@ -88,6 +91,27 @@ public class TaskCoreTests {
|
||||
output.contains(EXCEPTION_MESSAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidExecutionId() {
|
||||
boolean exceptionFired = false;
|
||||
try {
|
||||
applicationContext = new SpringApplicationBuilder().sources(new Object[]{TaskExceptionConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class}).build().run(new String[]{
|
||||
"--spring.cloud.task.closecontext.enable=false",
|
||||
"--spring.cloud.task.name=" + TASK_NAME,
|
||||
"--spring.main.web-environment=false",
|
||||
"--spring.cloud.task.executionid=55"});
|
||||
}
|
||||
catch (ApplicationContextException exception) {
|
||||
exceptionFired = true;
|
||||
}
|
||||
assertTrue("An ApplicationContextException should have been thrown", exceptionFired);
|
||||
|
||||
String output = this.outputCapture.toString();
|
||||
assertTrue("Test results do not show the correct exception message: " + output,
|
||||
output.contains(EXCEPTION_INVALID_TASK_EXECUTION_ID));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableTask
|
||||
public static class TaskConfiguration {
|
||||
|
||||
@@ -35,10 +35,15 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.cloud.task.util.TestDefaultConfiguration;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -139,6 +144,17 @@ public class TaskLifecycleListenerTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = ApplicationContextException.class)
|
||||
public void testInvalidTaskExecutionId() {
|
||||
ConfigurableEnvironment environment = new StandardEnvironment();
|
||||
MutablePropertySources propertySources = environment.getPropertySources();
|
||||
Map myMap = new HashMap();
|
||||
myMap.put("spring.cloud.task.executionid", "55");
|
||||
propertySources.addFirst(new MapPropertySource("EnvrionmentTestPropsource", myMap));
|
||||
context.setEnvironment(environment);
|
||||
context.refresh();
|
||||
}
|
||||
|
||||
private void verifyTaskExecution(int numberOfParams, boolean update, Integer exitCode, Throwable exception) {
|
||||
|
||||
Sort sort = new Sort("id");
|
||||
|
||||
@@ -16,11 +16,16 @@
|
||||
|
||||
package org.springframework.cloud.task.repository.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
@@ -56,7 +61,24 @@ public class JdbcTaskExecutionDaoTests {
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void saveTaskExecution() {
|
||||
public void testStartTaskExecution() {
|
||||
TaskExecution expectedTaskExecution = dao.createTaskExecution(null, null,
|
||||
new ArrayList<String>(0));
|
||||
|
||||
expectedTaskExecution.setArguments(Collections.singletonList("foo=" + UUID.randomUUID().toString()));
|
||||
expectedTaskExecution.setStartTime(new Date());
|
||||
expectedTaskExecution.setTaskName(UUID.randomUUID().toString());
|
||||
|
||||
dao.startTaskExecution(expectedTaskExecution.getExecutionId(), expectedTaskExecution.getTaskName(),
|
||||
expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments());
|
||||
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
TestDBUtils.getTaskExecutionFromDB(dataSource, expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void createTaskExecution() {
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg();
|
||||
expectedTaskExecution = dao.createTaskExecution(expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(),
|
||||
expectedTaskExecution.getArguments());
|
||||
@@ -65,6 +87,16 @@ public class JdbcTaskExecutionDaoTests {
|
||||
TestDBUtils.getTaskExecutionFromDB(dataSource, expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void createEmptyTaskExecution() {
|
||||
TaskExecution expectedTaskExecution = dao.createTaskExecution(null, null,
|
||||
new ArrayList<String>(0));
|
||||
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
TestDBUtils.getTaskExecutionFromDB(dataSource, expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void completeTaskExecution() {
|
||||
|
||||
@@ -16,28 +16,72 @@
|
||||
|
||||
package org.springframework.cloud.task.repository.dao;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.cloud.task.util.TestVerifierUtils;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* Executes unit tests on MapTaskExecutionDaoTests.
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
public class MapTaskExecutionDaoTests {
|
||||
|
||||
private MapTaskExecutionDao dao;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.dao = new MapTaskExecutionDao();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartTaskExecution() {
|
||||
TaskExecution expectedTaskExecution = this.dao.createTaskExecution(null, null, new ArrayList<String>(0));
|
||||
|
||||
expectedTaskExecution.setArguments(Collections.singletonList("foo=" + UUID.randomUUID().toString()));
|
||||
expectedTaskExecution.setStartTime(new Date());
|
||||
expectedTaskExecution.setTaskName(UUID.randomUUID().toString());
|
||||
|
||||
this.dao.startTaskExecution(expectedTaskExecution.getExecutionId(), expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments());
|
||||
Map<Long, TaskExecution> taskExecutionMap = this.dao.getTaskExecutions();
|
||||
assertNotNull("taskExecutionMap must not be null", taskExecutionMap);
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
taskExecutionMap.get(expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createEmptyTaskExecution() {
|
||||
TaskExecution expectedTaskExecution = dao.createTaskExecution(null, null,
|
||||
new ArrayList<String>(0));
|
||||
|
||||
Map<Long, TaskExecution> taskExecutionMap = this.dao.getTaskExecutions();
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
taskExecutionMap.get(expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void completeTaskExecutionWithNoCreate() {
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg();
|
||||
this.dao.completeTaskExecution(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution.getExitCode(), expectedTaskExecution.getEndTime(),
|
||||
expectedTaskExecution.getExitMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveTaskExecution(){
|
||||
MapTaskExecutionDao dao = new MapTaskExecutionDao();
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg();
|
||||
expectedTaskExecution = dao.createTaskExecution(expectedTaskExecution.getTaskName(),
|
||||
expectedTaskExecution = this.dao.createTaskExecution(expectedTaskExecution.getTaskName(),
|
||||
expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments());
|
||||
Map<Long, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
|
||||
Map<Long, TaskExecution> taskExecutionMap = this.dao.getTaskExecutions();
|
||||
assertNotNull("taskExecutionMap must not be null", taskExecutionMap);
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
taskExecutionMap.get(expectedTaskExecution.getExecutionId()));
|
||||
@@ -45,14 +89,13 @@ public class MapTaskExecutionDaoTests {
|
||||
|
||||
@Test
|
||||
public void completeTaskExecution(){
|
||||
MapTaskExecutionDao dao = new MapTaskExecutionDao();
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoArg();
|
||||
expectedTaskExecution = dao.createTaskExecution(expectedTaskExecution.getTaskName(),
|
||||
expectedTaskExecution = this.dao.createTaskExecution(expectedTaskExecution.getTaskName(),
|
||||
expectedTaskExecution.getStartTime(), expectedTaskExecution.getArguments());
|
||||
dao.completeTaskExecution(expectedTaskExecution.getExecutionId(),
|
||||
this.dao.completeTaskExecution(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution.getExitCode(), expectedTaskExecution.getEndTime(),
|
||||
expectedTaskExecution.getExitMessage());
|
||||
Map<Long, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
|
||||
Map<Long, TaskExecution> taskExecutionMap = this.dao.getTaskExecutions();
|
||||
assertNotNull("taskExecutionMap must not be null", taskExecutionMap);
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
taskExecutionMap.get(expectedTaskExecution.getExecutionId()));
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -61,6 +62,17 @@ public class SimpleTaskRepositoryJdbcTests {
|
||||
@Autowired
|
||||
private TaskExplorer taskExplorer;
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testCreateEmptyExecution() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository);
|
||||
TaskExecution actualTaskExecution = TestDBUtils.getTaskExecutionFromDB(dataSource,
|
||||
expectedTaskExecution.getExecutionId());
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testCreateTaskExecutionNoParam() {
|
||||
@@ -81,6 +93,39 @@ public class SimpleTaskRepositoryJdbcTests {
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void startTaskExecutionWithParam() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository);
|
||||
|
||||
expectedTaskExecution.setArguments(Collections.singletonList("foo=" + UUID.randomUUID().toString()));
|
||||
expectedTaskExecution.setStartTime(new Date());
|
||||
expectedTaskExecution.setTaskName(UUID.randomUUID().toString());
|
||||
|
||||
TaskExecution actualTaskExecution = this.taskRepository.startTaskExecution(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(),
|
||||
expectedTaskExecution.getArguments());
|
||||
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void startTaskExecutionWithNoParam() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository);
|
||||
|
||||
expectedTaskExecution.setStartTime(new Date());
|
||||
expectedTaskExecution.setTaskName(UUID.randomUUID().toString());
|
||||
|
||||
TaskExecution actualTaskExecution = this.taskRepository.startTaskExecution(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(),
|
||||
expectedTaskExecution.getArguments());
|
||||
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testCompleteTaskExecution() {
|
||||
@@ -200,7 +245,6 @@ public class SimpleTaskRepositoryJdbcTests {
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
@DirtiesContext
|
||||
public void testCreateTaskExecutionNullEndTime(){
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -43,6 +45,14 @@ public class SimpleTaskRepositoryMapTests {
|
||||
this.taskRepository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateEmptyExecution() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository);
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
getSingleTaskExecutionFromMapRepository(expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTaskExecutionNoParam() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
@@ -59,6 +69,37 @@ public class SimpleTaskRepositoryMapTests {
|
||||
getSingleTaskExecutionFromMapRepository(expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startTaskExecutionWithParam() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository);
|
||||
|
||||
expectedTaskExecution.setArguments(Collections.singletonList("foo=" + UUID.randomUUID().toString()));
|
||||
expectedTaskExecution.setStartTime(new Date());
|
||||
expectedTaskExecution.setTaskName(UUID.randomUUID().toString());
|
||||
|
||||
TaskExecution actualTaskExecution = this.taskRepository.startTaskExecution(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(),
|
||||
expectedTaskExecution.getArguments());
|
||||
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startTaskExecutionWithNoParam() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
TaskExecutionCreator.createAndStoreEmptyTaskExecution(taskRepository);
|
||||
|
||||
expectedTaskExecution.setStartTime(new Date());
|
||||
expectedTaskExecution.setTaskName(UUID.randomUUID().toString());
|
||||
|
||||
TaskExecution actualTaskExecution = this.taskRepository.startTaskExecution(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(),
|
||||
expectedTaskExecution.getArguments());
|
||||
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompleteTaskExecution() {
|
||||
TaskExecution expectedTaskExecution =
|
||||
|
||||
@@ -30,6 +30,16 @@ import org.springframework.cloud.task.repository.TaskRepository;
|
||||
*/
|
||||
public class TaskExecutionCreator {
|
||||
|
||||
/**
|
||||
* Creates a sample TaskExecution and stores it in the taskRepository.
|
||||
*
|
||||
* @param taskRepository the taskRepository where the taskExecution should be stored.
|
||||
* @return the taskExecution created.
|
||||
*/
|
||||
public static TaskExecution createAndStoreEmptyTaskExecution(TaskRepository taskRepository) {
|
||||
return taskRepository.createTaskExecution();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a sample TaskExecution and stores it in the taskRepository.
|
||||
*
|
||||
|
||||
@@ -69,8 +69,8 @@ public class TestDefaultConfiguration implements InitializingBean {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskLifecycleListener taskHandler(){
|
||||
return new TaskLifecycleListener(taskRepository(), taskNameResolver(), applicationArguments);
|
||||
public TaskLifecycleListener taskHandler(TaskExplorer taskExplorer){
|
||||
return new TaskLifecycleListener(taskRepository(), taskNameResolver(), applicationArguments, taskExplorer);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user