SCT-96 Update TaskRepository to be additive only

resolves spring-cloud/spring-cloud-task#96
This commit is contained in:
Glenn Renfro
2016-03-01 13:06:42 -05:00
committed by Michael Minella
parent 83836e1eea
commit dc26f7c98d
18 changed files with 244 additions and 165 deletions

View File

@@ -153,7 +153,8 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
this.applicationFailedEvent.getException()).getExitMessage());
}
taskExecution.setExitMessage(invokeOnTaskEnd(taskExecution).getExitMessage());
taskRepository.update(taskExecution);
taskRepository.completeTaskExecution(taskExecution.getExecutionId(), taskExecution.getExitCode(),
taskExecution.getEndTime(), taskExecution.getExitMessage());
}
else {
logger.error("An event to end a task has been received for a task that has " +
@@ -170,11 +171,8 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
args = Arrays.asList(this.applicationArguments.getSourceArgs());
}
this.taskExecution = new TaskExecution(this.taskRepository.getNextExecutionId(),
null, this.taskNameResolver.getTaskName(), new Date(), null, null,
args);
this.taskRepository.createTaskExecution(this.taskExecution);
this.taskExecution = this.taskRepository.createTaskExecution(
this.taskNameResolver.getTaskName(), new Date(), args);
}
else {
logger.error("Multiple start events have been received. The first one was " +

View File

@@ -88,7 +88,7 @@ public class TaskExecution {
}
public Integer getExitCode() {
return exitCode;
return (exitCode == null) ? 0 : exitCode;
}
public void setExitCode(Integer exitCode) {

View File

@@ -16,6 +16,9 @@
package org.springframework.cloud.task.repository;
import java.util.Date;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -27,23 +30,28 @@ import org.springframework.transaction.annotation.Transactional;
public interface TaskRepository {
/**
* Notifies the repository that a taskExecution needs to be updated.
* Notifies the repository that a taskExecution has completed.
*
* @param taskExecution taskExecution to be updated
* @param executionId to the task execution to be updated.
* @param exitCode to be stored for this task.
* @param endTime designated when the task completed.
* @param exitMessage to be stored for the task.
* @return the updated {@link TaskExecution}
*/
public void update(TaskExecution taskExecution);
@Transactional
TaskExecution completeTaskExecution(long executionId, Integer exitCode, Date endTime,
String exitMessage);
/**
* Notifies the repository that a taskExecution needs to be created.
*
* @param taskExecution taskExecution to be recorded
* @param taskName the name that associated with the task execution.
* @param startTime the time task began.
* @param parameters list of key/value pairs that configure the task.
* @return the initial {@link TaskExecution}
*/
@Transactional
public void createTaskExecution(TaskExecution taskExecution);
TaskExecution createTaskExecution(String taskName,
Date startTime,List<String> parameters);
/**
* Retrieves the next available execution id for a task execution.
* @return long containing the executionId.
*/
public long getNextExecutionId();
}

View File

@@ -63,9 +63,8 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
public static final String TASK_NAME_WHERE_CLAUSE = "where TASK_NAME = ? ";
private static final String SAVE_TASK_EXECUTION = "INSERT into %PREFIX%EXECUTION"
+ "(TASK_EXECUTION_ID, START_TIME, END_TIME, "
+ "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED)"
+ "values (?, ?, ?, ?, ?, ?, ?)";
+ "(TASK_EXECUTION_ID, START_TIME, TASK_NAME, LAST_UPDATED)"
+ "values (?, ?, ?, ?)";
private static final String CREATE_TASK_PARAMETER = "INSERT into "
+ "%PREFIX%EXECUTION_PARAMS(TASK_EXECUTION_ID, TASK_PARAM ) values (?, ?)";
@@ -74,8 +73,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
+ "%PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = ?";
private static final String UPDATE_TASK_EXECUTION = "UPDATE %PREFIX%EXECUTION set "
+ "START_TIME = ?, END_TIME = ?, TASK_NAME = ?, EXIT_CODE = ?, "
+ "EXIT_MESSAGE = ?, LAST_UPDATED = ? "
+ "END_TIME = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, LAST_UPDATED = ? "
+ "where TASK_EXECUTION_ID = ?";
private static final String GET_EXECUTION_BY_ID = "SELECT TASK_EXECUTION_ID, " +
@@ -119,36 +117,38 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
}
@Override
public void saveTaskExecution(TaskExecution taskExecution) {
Object[] parameters = new Object[]{ taskExecution.getExecutionId(),
taskExecution.getStartTime(), taskExecution.getEndTime(),
taskExecution.getTaskName(), taskExecution.getExitCode(),
taskExecution.getExitMessage(), new Date()};
public TaskExecution createTaskExecution(String taskName,
Date startTime, List<String> parameters) {
long taskExecutionId = getNextExecutionId();
TaskExecution taskExecution = new TaskExecution(taskExecutionId, null, taskName,
startTime, null, null, parameters);
Object[] queryParameters = new Object[]{ taskExecutionId, startTime, taskName, new Date()};
jdbcTemplate.update(
getQuery(SAVE_TASK_EXECUTION),
parameters,
new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.TIMESTAMP,
Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.TIMESTAMP });
insertTaskParameters(taskExecution.getExecutionId(), taskExecution.getParameters());
queryParameters,
new int[]{ Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, Types.TIMESTAMP });
insertTaskParameters(taskExecutionId, parameters);
return taskExecution;
}
@Override
public void updateTaskExecution(TaskExecution taskExecution) {
public void completeTaskExecution(long taskExecutionId, Integer exitCode, Date endTime,
String exitMessage) {
// 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[]{ taskExecution.getExecutionId() }) != 1) {
throw new IllegalStateException("Invalid TaskExecution, ID " + taskExecution.getExecutionId() + " not found.");
new Object[]{ taskExecutionId}) != 1) {
throw new IllegalStateException("Invalid TaskExecution, ID " + taskExecutionId + " not found.");
}
Object[] parameters = new Object[]{ taskExecution.getStartTime(), taskExecution.getEndTime(),
taskExecution.getTaskName(), taskExecution.getExitCode(),
taskExecution.getExitMessage(), new Date(), taskExecution.getExecutionId()};
Object[] parameters = new Object[]{ endTime, exitCode, exitMessage, new Date(),
taskExecutionId};
jdbcTemplate.update(
getQuery(UPDATE_TASK_EXECUTION),
parameters,
new int[]{ Types.TIMESTAMP, Types.TIMESTAMP, Types.VARCHAR, Types.INTEGER,
Types.VARCHAR, Types.TIMESTAMP, Types.BIGINT});
new int[]{ Types.TIMESTAMP, Types.INTEGER, Types.VARCHAR, Types.TIMESTAMP,
Types.BIGINT});
}
/**

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.task.repository.dao;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -48,13 +49,21 @@ public class MapTaskExecutionDao implements TaskExecutionDao {
}
@Override
public void saveTaskExecution(TaskExecution taskExecution) {
taskExecutions.put(taskExecution.getExecutionId(), taskExecution);
public TaskExecution createTaskExecution(String taskName,
Date startTime, List<String> parameters) {
long taskExecutionId = getNextExecutionId();
TaskExecution taskExecution = new TaskExecution(taskExecutionId, null, taskName,
startTime, null, null, parameters);
taskExecutions.put(taskExecutionId, taskExecution);
return taskExecution;
}
@Override
public void updateTaskExecution(TaskExecution taskExecution) {
taskExecutions.put(taskExecution.getExecutionId(), taskExecution);
public void completeTaskExecution(long executionId, Integer exitCode, Date endTime, String exitMessage) {
TaskExecution taskExecution= taskExecutions.get(executionId);
taskExecution.setEndTime(endTime);
taskExecution.setExitCode(exitCode);
taskExecution.setExitMessage(exitMessage);
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.task.repository.dao;
import java.util.Date;
import java.util.List;
import org.springframework.cloud.task.repository.TaskExecution;
@@ -32,16 +33,23 @@ public interface TaskExecutionDao {
/**
* Save a new {@link TaskExecution}.
*
* @param taskExecution the taskExecution to be stored.
* @param taskName the name that associated with the task execution.
* @param startTime the time task began.
* @param parameters list of key/value pairs that configure the task.
* @return A fully qualified {@linkTaskExecution} instance.
*/
void saveTaskExecution(TaskExecution taskExecution);
TaskExecution createTaskExecution( String taskName,
Date startTime, List<String> parameters);
/**
* Update and existing {@link TaskExecution}.
*
* @param taskExecution the taskExecution to be updated.
* @param executionId the id of the taskExecution to be updated.
* @param exitCode the status of the task upon completion.
* @param endTime the time the task completed.
* @param exitMessage the message assigned to the task upon completion.
*/
void updateTaskExecution(TaskExecution taskExecution);
void completeTaskExecution(long executionId, Integer exitCode, Date endTime, String exitMessage);
/**
* Retrieves a task execution from the task repository.
@@ -107,9 +115,4 @@ public interface TaskExecutionDao {
public Page<TaskExecution> findAll(Pageable pageable);
/**
* Retrieves the next available execution id for a task execution.
* @return long containing the executionId.
*/
public long getNextExecutionId();
}

View File

@@ -16,6 +16,9 @@
package org.springframework.cloud.task.repository.support;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,26 +52,32 @@ public class SimpleTaskRepository implements TaskRepository {
}
@Override
public void update(TaskExecution taskExecution) {
public TaskExecution completeTaskExecution(long executionId, Integer exitCode, Date endTime,
String exitMessage) {
initialize();
validateTaskExecution(taskExecution);
taskExecutionDao.updateTaskExecution(taskExecution);
logger.info("Updating: " + taskExecution.toString());
validateExitInformation(executionId, exitCode, endTime);
exitMessage = trimExitMessage(exitMessage);
taskExecutionDao.completeTaskExecution(executionId, exitCode, endTime, exitMessage);
logger.debug("Updating: TaskExecution with executionId="+executionId
+ " with the following {"
+ "exitCode=" + exitCode
+ ", endTime=" + endTime
+ ", exitMessage='" + exitMessage + '\''
+ '}');
return taskExecutionDao.getTaskExecution(executionId);
}
@Override
public void createTaskExecution(TaskExecution taskExecution) {
public TaskExecution createTaskExecution(String taskName,
Date startTime,List<String> parameters) {
initialize();
validateTaskExecution(taskExecution);
taskExecutionDao.saveTaskExecution(taskExecution);
validateCreateInformation(startTime, taskName);
TaskExecution taskExecution =
taskExecutionDao.createTaskExecution(taskName, startTime, parameters);
logger.info("Creating: " + taskExecution.toString());
}
@Override
public long getNextExecutionId() {
initialize();
return taskExecutionDao.getNextExecutionId();
return taskExecution;
}
/**
@@ -93,23 +102,30 @@ public class SimpleTaskRepository implements TaskRepository {
}
/**
* Validate TaskExecution. At a minimum a startTime.
*
* @param taskExecution the taskExecution to be evaluagted.
* Validate startTime and taskName are valid.
*/
private void validateTaskExecution(TaskExecution taskExecution) {
Assert.notNull(taskExecution, "taskExecution should not be null");
Assert.notNull(taskExecution.getStartTime(), "TaskExecution start time cannot be null.");
private void validateCreateInformation(Date startTime, String taskName) {
Assert.notNull(startTime, "TaskExecution start time cannot be null.");
if (taskExecution.getTaskName() != null &&
taskExecution.getTaskName().length() > MAX_TASK_NAME_SIZE) {
if (taskName != null &&
taskName.length() > MAX_TASK_NAME_SIZE) {
throw new IllegalArgumentException("TaskName length exceeds "
+ MAX_TASK_NAME_SIZE + " characters");
}
//Trim the exit message
if(taskExecution.getExitMessage() != null &&
taskExecution.getExitMessage().length() > MAX_EXIT_MESSAGE_SIZE){
taskExecution.setExitMessage(taskExecution.getExitMessage().substring(0, MAX_EXIT_MESSAGE_SIZE - 1));
}
private void validateExitInformation(long executionId, Integer exitCode, Date endTime){
Assert.notNull(exitCode, "exitCode should not be null");
Assert.isTrue(exitCode >= 0, "exit code must be greater than or equal to zero");
Assert.notNull(endTime, "TaskExecution endTime cannot be null.");
}
private String trimExitMessage(String exitMessage){
String result = exitMessage;
if(exitMessage != null &&
exitMessage.length() > MAX_EXIT_MESSAGE_SIZE) {
result = exitMessage.substring(0, MAX_EXIT_MESSAGE_SIZE - 1);
}
return result;
}
}

View File

@@ -73,14 +73,14 @@ public class TaskLifecycleListenerTests {
@Test
public void testTaskCreate() {
context.refresh();
verifyTaskExecution(0, false, null, null);
verifyTaskExecution(0, false, 0, null);
}
@Test
public void testTaskCreateWithArgs() {
context.register(ArgsConfiguration.class);
context.refresh();
verifyTaskExecution(2, false, null, null);
verifyTaskExecution(2, false, 0, null);
}
@Test
@@ -130,7 +130,7 @@ public class TaskLifecycleListenerTests {
}
else {
assertNull(taskExecution.getEndTime());
assertNull(taskExecution.getExitCode());
assertTrue(taskExecution.getExitCode() == 0);
}
assertEquals("testTask", taskExecution.getTaskName());

View File

@@ -58,7 +58,8 @@ public class JdbcTaskExecutionDaoTests {
@DirtiesContext
public void saveTaskExecution() {
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
dao.saveTaskExecution(expectedTaskExecution);
expectedTaskExecution = dao.createTaskExecution(expectedTaskExecution.getTaskName(), expectedTaskExecution.getStartTime(),
expectedTaskExecution.getParameters());
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
TestDBUtils.getTaskExecutionFromDB(dataSource, expectedTaskExecution.getExecutionId()));
@@ -66,20 +67,25 @@ public class JdbcTaskExecutionDaoTests {
@Test
@DirtiesContext
public void updateTaskExecution() {
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
dao.saveTaskExecution(expectedTaskExecution);
dao.updateTaskExecution(expectedTaskExecution);
public void completeTaskExecution() {
TaskExecution expectedTaskExecution = TestVerifierUtils.endSampleTaskExecutionNoParam();
expectedTaskExecution = dao.createTaskExecution(expectedTaskExecution.getTaskName(),
expectedTaskExecution.getStartTime(), expectedTaskExecution.getParameters());
dao.completeTaskExecution(expectedTaskExecution.getExecutionId(),
expectedTaskExecution.getExitCode(), expectedTaskExecution.getEndTime(),
expectedTaskExecution.getExitMessage());
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
TestDBUtils.getTaskExecutionFromDB(dataSource, expectedTaskExecution.getExecutionId()));
}
@Test(expected = IllegalStateException.class)
@DirtiesContext
public void updateTaskExecutionWithNoCreate() {
public void completeTaskExecutionWithNoCreate() {
JdbcTaskExecutionDao dao = new JdbcTaskExecutionDao(dataSource);
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
dao.updateTaskExecution(expectedTaskExecution);
TaskExecution expectedTaskExecution = TestVerifierUtils.endSampleTaskExecutionNoParam();
dao.completeTaskExecution(expectedTaskExecution.getExecutionId(),
expectedTaskExecution.getExitCode(), expectedTaskExecution.getEndTime(),
expectedTaskExecution.getExitMessage());
}
}

View File

@@ -35,7 +35,8 @@ public class MapTaskExecutionDaoTests {
public void saveTaskExecution(){
MapTaskExecutionDao dao = new MapTaskExecutionDao();
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
dao.saveTaskExecution(expectedTaskExecution);
expectedTaskExecution = dao.createTaskExecution(expectedTaskExecution.getTaskName(),
expectedTaskExecution.getStartTime(), expectedTaskExecution.getParameters());
Map<Long, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
assertNotNull("taskExecutionMap must not be null", taskExecutionMap);
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
@@ -43,11 +44,14 @@ public class MapTaskExecutionDaoTests {
}
@Test
public void updateTaskExecution(){
public void completeTaskExecution(){
MapTaskExecutionDao dao = new MapTaskExecutionDao();
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
dao.saveTaskExecution(expectedTaskExecution);
dao.updateTaskExecution(expectedTaskExecution);
expectedTaskExecution = dao.createTaskExecution(expectedTaskExecution.getTaskName(),
expectedTaskExecution.getStartTime(), expectedTaskExecution.getParameters());
dao.completeTaskExecution(expectedTaskExecution.getExecutionId(),
expectedTaskExecution.getExitCode(), expectedTaskExecution.getEndTime(),
expectedTaskExecution.getExitMessage());
Map<Long, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
assertNotNull("taskExecutionMap must not be null", taskExecutionMap);
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,

View File

@@ -31,6 +31,7 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
@@ -41,7 +42,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
@@ -164,8 +164,8 @@ public class SimpleTaskExplorerTests {
}
for (; i < (COMPLETE_COUNT + TEST_COUNT); i++) {
TaskExecution expectedTaskExecution = new TaskExecution(i, 0, TASK_NAME, new Date(), null, null, new ArrayList<String>(0));
this.taskRepository.createTaskExecution(expectedTaskExecution);
TaskExecution expectedTaskExecution = this.taskRepository.createTaskExecution(
TASK_NAME, new Date(), new ArrayList<String>());
expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution);
}
Pageable pageable = new PageRequest(0, 10);
@@ -190,6 +190,7 @@ public class SimpleTaskExplorerTests {
final int TEST_COUNT = 5;
final int COMPLETE_COUNT = 7;
final String TASK_NAME = "FOOBAR";
Random randomGenerator = new Random();
Map<Long, TaskExecution> expectedResults = new HashMap<>();
//Store completed jobs
@@ -198,9 +199,8 @@ public class SimpleTaskExplorerTests {
}
for (int i = 0; i < TEST_COUNT; i++) {
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
expectedTaskExecution.setTaskName(TASK_NAME);
this.taskRepository.createTaskExecution(expectedTaskExecution);
TaskExecution expectedTaskExecution = this.taskRepository.createTaskExecution(
TASK_NAME, new Date(), new ArrayList<String>());
expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution);
}
@@ -307,7 +307,8 @@ public class SimpleTaskExplorerTests {
private TaskExecution createAndSaveTaskExecution(int i) {
TaskExecution taskExecution = TestVerifierUtils.createSampleTaskExecution(i);
this.taskRepository.createTaskExecution(taskExecution);
taskExecution = this.taskRepository.createTaskExecution(taskExecution.getTaskName(),
taskExecution.getStartTime(), taskExecution.getParameters());
return taskExecution;
}

View File

@@ -16,6 +16,9 @@
package org.springframework.cloud.task.repository.support;
import java.util.Date;
import java.util.UUID;
import javax.sql.DataSource;
import org.junit.Test;
@@ -74,30 +77,56 @@ public class SimpleTaskRepositoryJdbcTests {
@Test
@DirtiesContext
public void testUpdateTaskExecution() {
public void testCompleteTaskExecution() {
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
expectedTaskExecution = TaskExecutionCreator.updateTaskExecution(taskRepository,
expectedTaskExecution.getExecutionId());
TaskExecution actualTaskExecution = TestDBUtils.getTaskExecutionFromDB(
dataSource, expectedTaskExecution.getExecutionId());
expectedTaskExecution.setEndTime(new Date());
expectedTaskExecution.setExitCode(77);
expectedTaskExecution.setExitMessage(UUID.randomUUID().toString());
TaskExecution actualTaskExecution = TaskExecutionCreator.completeExecution(taskRepository, expectedTaskExecution);
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
}
@Test
@DirtiesContext
public void testCreateTaskExecutionNoParamMaxExitMessageSize(){
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
TaskExecution expectedTaskExecution = TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
expectedTaskExecution.setExitMessage(new String(new char[SimpleTaskRepository.MAX_EXIT_MESSAGE_SIZE+1]));
taskRepository.createTaskExecution(expectedTaskExecution);
expectedTaskExecution.setEndTime(new Date());
taskRepository.completeTaskExecution(expectedTaskExecution.getExecutionId(),
expectedTaskExecution.getExitCode(), new Date(),
expectedTaskExecution.getExitMessage());
}
@Test(expected=IllegalArgumentException.class)
@DirtiesContext
public void testCreateTaskExecutionNoParamMaxTaskName(){
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
expectedTaskExecution.setTaskName(new String(new char[SimpleTaskRepository.MAX_TASK_NAME_SIZE+1]));
taskRepository.createTaskExecution(expectedTaskExecution);
taskRepository.createTaskExecution(
new String(new char[SimpleTaskRepository.MAX_TASK_NAME_SIZE+1]),
new Date(), null);
}
@Test(expected=IllegalArgumentException.class)
@DirtiesContext
public void testCreateTaskExecutionNegativeException(){
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
expectedTaskExecution.setEndTime(new Date());
expectedTaskExecution.setExitCode(-1);
TaskExecution actualTaskExecution = TaskExecutionCreator.completeExecution(taskRepository, expectedTaskExecution);
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
}
@Test(expected=IllegalArgumentException.class)
@DirtiesContext
public void testCreateTaskExecutionNullEndTime(){
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
expectedTaskExecution.setExitCode(-1);
TaskExecutionCreator.completeExecution(taskRepository, expectedTaskExecution);
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.task.repository.support;
import java.util.Date;
import ch.qos.logback.core.Appender;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -49,14 +51,14 @@ public class SimpleTaskRepositoryLoggerTests {
}
@Test
public void testTaskUpdate() {
public void testTaskComplete() {
final Appender mockAppender = TestVerifierUtils.getMockAppender();
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
TaskExecutionCreator.updateTaskExecution(taskRepository,
expectedTaskExecution.getExecutionId());
expectedTaskExecution.setEndTime(new Date());
TaskExecutionCreator.completeExecution(taskRepository, expectedTaskExecution);
TestVerifierUtils.verifyLogEntryExists(mockAppender,
"Updating: TaskExecution{executionId="
"Updating: TaskExecution with executionId="
+ expectedTaskExecution.getExecutionId());
}

View File

@@ -16,12 +16,12 @@
package org.springframework.cloud.task.repository.support;
import static org.springframework.test.util.AssertionErrors.assertTrue;
import java.util.Date;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
@@ -31,6 +31,8 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import static org.springframework.test.util.AssertionErrors.assertTrue;
/**
* Tests for the SimpleTaskRepository that uses Map as a datastore.
* @author Glenn Renfro.
@@ -64,14 +66,13 @@ public class SimpleTaskRepositoryMapTests {
}
@Test
public void testUpdateTaskExecution() {
public void testCompleteTaskExecution() {
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
expectedTaskExecution = TaskExecutionCreator.updateTaskExecution(taskRepository,
expectedTaskExecution.getExecutionId());
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
getSingleTaskExecutionFromMapRepository(taskRepository,
expectedTaskExecution.getExecutionId()));
expectedTaskExecution.setEndTime(new Date());
TaskExecution actualTaskExecution = TaskExecutionCreator.completeExecution(taskRepository, expectedTaskExecution);
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
}
private TaskExecution getSingleTaskExecutionFromMapRepository(
@@ -83,6 +84,14 @@ public class SimpleTaskRepositoryMapTests {
return taskMap.get(taskExecutionId);
}
@Test(expected=IllegalArgumentException.class)
public void testCreateTaskExecutionNullEndTime(){
TaskExecution expectedTaskExecution =
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
expectedTaskExecution.setExitCode(-1);
TaskExecutionCreator.completeExecution(taskRepository, expectedTaskExecution);
}
@Configuration
public static class EmptyConfiguration{}
}

View File

@@ -38,7 +38,8 @@ public class TaskExecutionCreator {
*/
public static TaskExecution createAndStoreTaskExecutionNoParams(TaskRepository taskRepository) {
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
taskRepository.createTaskExecution(expectedTaskExecution);
expectedTaskExecution = taskRepository.createTaskExecution(expectedTaskExecution.getTaskName(),
expectedTaskExecution.getStartTime(), expectedTaskExecution.getParameters());
return expectedTaskExecution;
}
@@ -54,7 +55,8 @@ public class TaskExecutionCreator {
params.add(UUID.randomUUID().toString());
params.add(UUID.randomUUID().toString());
expectedTaskExecution.setParameters(params);
taskRepository.createTaskExecution(expectedTaskExecution);
expectedTaskExecution = taskRepository.createTaskExecution(expectedTaskExecution.getTaskName(),
expectedTaskExecution.getStartTime(), expectedTaskExecution.getParameters());
return expectedTaskExecution;
}
@@ -64,10 +66,10 @@ public class TaskExecutionCreator {
* @param taskRepository the taskRepository where the taskExecution should be updated.
* @return the taskExecution created.
*/
public static TaskExecution updateTaskExecution(TaskRepository taskRepository,
long taskExecutionId) {
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam(taskExecutionId);
taskRepository.update(expectedTaskExecution);
return expectedTaskExecution;
public static TaskExecution completeExecution(TaskRepository taskRepository,
TaskExecution expectedTaskExecution) {
return taskRepository.completeTaskExecution(expectedTaskExecution.getExecutionId(),
expectedTaskExecution.getExitCode(), expectedTaskExecution.getEndTime(),
expectedTaskExecution.getExitMessage());
}
}

View File

@@ -82,16 +82,14 @@ public class TestVerifierUtils {
*
* @return
*/
public static TaskExecution createSampleTaskExecutionNoParam(long executionId) {
public static TaskExecution createSampleTaskExecutionNoParam() {
Random randomGenerator = new Random();
int exitCode = randomGenerator.nextInt();
Date startTime = new Date();
Date endTime = new Date();
long executionId = randomGenerator.nextLong();
String taskName = UUID.randomUUID().toString();
String exitMessage = UUID.randomUUID().toString();
return new TaskExecution(executionId, exitCode, taskName,
startTime, endTime, exitMessage, new ArrayList<String>());
return new TaskExecution(executionId, 0, taskName,
startTime, null, null, new ArrayList<String>());
}
/**
@@ -99,7 +97,7 @@ public class TestVerifierUtils {
*
* @return
*/
public static TaskExecution createSampleTaskExecutionNoParam() {
public static TaskExecution endSampleTaskExecutionNoParam() {
Random randomGenerator = new Random();
int exitCode = randomGenerator.nextInt();
Date startTime = new Date();
@@ -119,38 +117,14 @@ public class TestVerifierUtils {
*/
public static TaskExecution createSampleTaskExecution(long executionId) {
Random randomGenerator = new Random();
int exitCode = randomGenerator.nextInt();
Date startTime = new Date();
Date endTime = new Date();
String taskName = UUID.randomUUID().toString();
String exitMessage = UUID.randomUUID().toString();
List<String> params = new ArrayList<>(PARAM_SIZE);
for (int i = 0 ; i < PARAM_SIZE ; i++){
params.add(UUID.randomUUID().toString());
}
return new TaskExecution(executionId, exitCode, taskName,
startTime, endTime, exitMessage, params);
}
/**
* Creates a fully populated TaskExecution for testing.
*
* @return
*/
public static TaskExecution createSampleTaskExecution() {
Random randomGenerator = new Random();
int exitCode = randomGenerator.nextInt();
long executionId = randomGenerator.nextLong();
Date startTime = new Date();
Date endTime = new Date();
String taskName = UUID.randomUUID().toString();
String exitMessage = UUID.randomUUID().toString();
List<String> params = new ArrayList<>(PARAM_SIZE);
for (int i = 0 ; i < PARAM_SIZE ; i++){
params.add(UUID.randomUUID().toString());
}
return new TaskExecution(executionId, exitCode, taskName,
startTime, endTime, exitMessage, params);
return new TaskExecution(executionId, null, taskName,
startTime, null, null, params);
}
/**

View File

@@ -16,8 +16,8 @@
package org.springframework.cloud.task.timestamp;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -41,7 +41,7 @@ public class TaskApplicationTests {
public void testTimeStampApp() throws Exception {
final String TEST_DATE_DOTS = ".......";
final String CREATE_TASK_MESSAGE = "Creating: TaskExecution{executionId=";
final String UPDATE_TASK_MESSAGE = "Updating: TaskExecution{executionId=";
final String UPDATE_TASK_MESSAGE = "Updating: TaskExecution with executionId=";
String[] args = { "--format=yyyy" + TEST_DATE_DOTS };
assertEquals(0, SpringApplication.exit(SpringApplication
@@ -62,7 +62,7 @@ public class TaskApplicationTests {
while (matcher.find()) {
count++;
}
assertEquals("The number of task titles did not match expected: ", 2, count);
assertEquals("The number of task titles did not match expected: ", 3, count);
}
}

View File

@@ -0,0 +1,18 @@
#
# Copyright 2016 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
logging.level.root=DEBUG
spring.application.name=Demo Timestamp Task