TaskExecution should use long id
* Replace String executionId with a long executionId * Add externalExecutionID that is a String resolves spring-cloud/spring-cloud-task#47
This commit is contained in:
committed by
Michael Minella
parent
1c3bb94bbd
commit
584cfb90f5
@@ -64,7 +64,7 @@ public class TaskLifecycleListenerTests {
|
||||
context.refresh();
|
||||
this.listener = context.getBean(TaskLifecycleListener.class);
|
||||
TestVerifierUtils.verifyLogEntryExists(mockAppender,
|
||||
"Creating: TaskExecution{executionId='" +
|
||||
"Creating: TaskExecution{executionId=" +
|
||||
listener.getTaskExecution().getExecutionId());
|
||||
assertEquals("Create should report that exit code is zero",
|
||||
0, listener.getTaskExecution().getExitCode());
|
||||
@@ -78,7 +78,7 @@ public class TaskLifecycleListenerTests {
|
||||
this.listener = context.getBean(TaskLifecycleListener.class);
|
||||
this.listener.onApplicationEvent(new ContextClosedEvent(context));
|
||||
TestVerifierUtils.verifyLogEntryExists(mockAppender,
|
||||
"Updating: TaskExecution{executionId='" +
|
||||
"Updating: TaskExecution{executionId=" +
|
||||
listener.getTaskExecution().getExecutionId());
|
||||
assertEquals("Update should report that exit code is zero",
|
||||
0, listener.getTaskExecution().getExitCode());
|
||||
@@ -91,7 +91,7 @@ public class TaskLifecycleListenerTests {
|
||||
this.listener = context.getBean(TaskLifecycleListener.class);
|
||||
listener.onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[]{}, context, new RuntimeException("This was expected")));
|
||||
TestVerifierUtils.verifyLogEntryExists(mockAppender,
|
||||
"Updating: TaskExecution{executionId='" +
|
||||
"Updating: TaskExecution{executionId=" +
|
||||
listener.getTaskExecution().getExecutionId());
|
||||
assertEquals("Update should report that exit code is one",
|
||||
1, listener.getTaskExecution().getExitCode());
|
||||
|
||||
@@ -18,9 +18,9 @@ package org.springframework.cloud.task.repository.dao;
|
||||
|
||||
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;
|
||||
@@ -28,7 +28,6 @@ import org.springframework.cloud.task.configuration.TestConfiguration;
|
||||
import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
import org.springframework.cloud.task.util.TestVerifierUtils;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -47,10 +46,17 @@ public class JdbcTaskExecutionDaoTests {
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
private JdbcTaskExecutionDao dao;
|
||||
|
||||
@Before
|
||||
public void setup(){
|
||||
dao = new JdbcTaskExecutionDao(dataSource);
|
||||
dao.setTaskIncrementer(TestDBUtils.getIncrementer(dataSource));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void saveTaskExecution() {
|
||||
JdbcTaskExecutionDao dao = new JdbcTaskExecutionDao(dataSource);
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
|
||||
@@ -58,20 +64,9 @@ public class JdbcTaskExecutionDaoTests {
|
||||
TestDBUtils.getTaskExecutionFromDB(dataSource, expectedTaskExecution.getExecutionId()));
|
||||
}
|
||||
|
||||
@Test(expected = DuplicateKeyException.class)
|
||||
@DirtiesContext
|
||||
public void duplicateSaveTaskExecution() {
|
||||
JdbcTaskExecutionDao dao = new JdbcTaskExecutionDao(dataSource);
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void updateTaskExecution() {
|
||||
JdbcTaskExecutionDao dao = new JdbcTaskExecutionDao(dataSource);
|
||||
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
dao.updateTaskExecution(expectedTaskExecution);
|
||||
|
||||
@@ -36,7 +36,7 @@ public class MapTaskExecutionDaoTests {
|
||||
MapTaskExecutionDao dao = new MapTaskExecutionDao();
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
Map<String, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
|
||||
Map<Long, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
|
||||
assertNotNull("taskExecutionMap must not be null", taskExecutionMap);
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
taskExecutionMap.get(expectedTaskExecution.getExecutionId()));
|
||||
@@ -48,7 +48,7 @@ public class MapTaskExecutionDaoTests {
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
dao.updateTaskExecution(expectedTaskExecution);
|
||||
Map<String, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
|
||||
Map<Long, TaskExecution> taskExecutionMap = dao.getTaskExecutions();
|
||||
assertNotNull("taskExecutionMap must not be null", taskExecutionMap);
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution,
|
||||
taskExecutionMap.get(expectedTaskExecution.getExecutionId()));
|
||||
|
||||
@@ -41,24 +41,24 @@ public class FindAllPagingQueryProviderTests {
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{"Oracle", "SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
{"Oracle", "SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE FROM "
|
||||
+ "(SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "(SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE, ROWNUM as "
|
||||
+ "TMP_ROW_NUM FROM (SELECT TASK_EXECUTION_ID, START_TIME, "
|
||||
+ "TMP_ROW_NUM FROM (SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, "
|
||||
+ "END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, "
|
||||
+ "STATUS_CODE FROM %PREFIX%EXECUTION ORDER BY START_TIME DESC, "
|
||||
+ "TASK_EXECUTION_ID DESC)) WHERE TMP_ROW_NUM >= 1 AND "
|
||||
+ "TMP_ROW_NUM < 11"},
|
||||
{"HSQL Database Engine","SELECT LIMIT 0 10 TASK_EXECUTION_ID, "
|
||||
{"HSQL Database Engine","SELECT LIMIT 0 10 TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, "
|
||||
+ "START_TIME, END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, "
|
||||
+ "LAST_UPDATED, STATUS_CODE FROM %PREFIX%EXECUTION ORDER BY "
|
||||
+ "START_TIME DESC, TASK_EXECUTION_ID DESC"},
|
||||
{"PostgreSQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, "
|
||||
{"PostgreSQL","SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, "
|
||||
+ "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE "
|
||||
+ "FROM %PREFIX%EXECUTION ORDER BY START_TIME DESC, "
|
||||
+ "TASK_EXECUTION_ID DESC LIMIT 10 OFFSET 0"},
|
||||
{"MySQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
{"MySQL","SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE FROM "
|
||||
+ "%PREFIX%EXECUTION ORDER BY START_TIME DESC, "
|
||||
+ "TASK_EXECUTION_ID DESC LIMIT 0, 10"}
|
||||
|
||||
@@ -42,27 +42,27 @@ public class WhereClausePagingQueryProviderTests {
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{"Oracle", "SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
{"Oracle", "SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE FROM "
|
||||
+ "(SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "(SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE, ROWNUM as "
|
||||
+ "TMP_ROW_NUM FROM (SELECT TASK_EXECUTION_ID, START_TIME, "
|
||||
+ "TMP_ROW_NUM FROM (SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, "
|
||||
+ "END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, "
|
||||
+ "STATUS_CODE FROM %PREFIX%EXECUTION "
|
||||
+ "WHERE TASK_EXECUTION_ID = '0000' ORDER BY START_TIME DESC, "
|
||||
+ "TASK_EXECUTION_ID DESC)) WHERE TMP_ROW_NUM >= 1 AND "
|
||||
+ "TMP_ROW_NUM < 11"},
|
||||
{"HSQL Database Engine","SELECT LIMIT 0 10 TASK_EXECUTION_ID, "
|
||||
+ "START_TIME, END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, "
|
||||
+ "TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, EXIT_CODE, EXIT_MESSAGE, "
|
||||
+ "LAST_UPDATED, STATUS_CODE FROM %PREFIX%EXECUTION "
|
||||
+ "WHERE TASK_EXECUTION_ID = '0000' ORDER BY "
|
||||
+ "START_TIME DESC, TASK_EXECUTION_ID DESC"},
|
||||
{"PostgreSQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, "
|
||||
{"PostgreSQL","SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, "
|
||||
+ "TASK_NAME, EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE "
|
||||
+ "FROM %PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = '0000' "
|
||||
+ "ORDER BY START_TIME DESC, "
|
||||
+ "TASK_EXECUTION_ID DESC LIMIT 10 OFFSET 0"},
|
||||
{"MySQL","SELECT TASK_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
{"MySQL","SELECT TASK_EXECUTION_ID, TASK_EXTERNAL_EXECUTION_ID, START_TIME, END_TIME, TASK_NAME, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE FROM "
|
||||
+ "%PREFIX%EXECUTION WHERE TASK_EXECUTION_ID = '0000' "
|
||||
+ "ORDER BY START_TIME DESC, "
|
||||
|
||||
@@ -16,20 +16,16 @@
|
||||
package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.task.repository.support.DatabaseType.HSQL;
|
||||
import static org.springframework.cloud.task.repository.support.DatabaseType.MYSQL;
|
||||
import static org.springframework.cloud.task.repository.support.DatabaseType.ORACLE;
|
||||
import static org.springframework.cloud.task.repository.support.DatabaseType.POSTGRES;
|
||||
import static org.springframework.cloud.task.repository.support.DatabaseType.fromProductName;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
|
||||
/**
|
||||
* Tests that the correct database names are selected from datasource metadata.
|
||||
@@ -56,41 +52,26 @@ public class DatabaseTypeTests {
|
||||
|
||||
@Test
|
||||
public void testFromMetaDataForHsql() throws Exception {
|
||||
DataSource ds = getMockDataSource("HSQL Database Engine");
|
||||
DataSource ds = TestDBUtils.getMockDataSource("HSQL Database Engine");
|
||||
assertEquals(HSQL, DatabaseType.fromMetaData(ds));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMetaDataForOracle() throws Exception {
|
||||
DataSource ds = getMockDataSource("Oracle");
|
||||
DataSource ds = TestDBUtils.getMockDataSource("Oracle");
|
||||
assertEquals(ORACLE, DatabaseType.fromMetaData(ds));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMetaDataForPostgres() throws Exception {
|
||||
DataSource ds = getMockDataSource("PostgreSQL");
|
||||
DataSource ds = TestDBUtils.getMockDataSource("PostgreSQL");
|
||||
assertEquals(POSTGRES, DatabaseType.fromMetaData(ds));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMetaDataForMySQL() throws Exception {
|
||||
DataSource ds = getMockDataSource("MySQL");
|
||||
DataSource ds = TestDBUtils.getMockDataSource("MySQL");
|
||||
assertEquals(MYSQL, DatabaseType.fromMetaData(ds));
|
||||
}
|
||||
|
||||
public DataSource getMockDataSource(String databaseProductName) throws Exception {
|
||||
DatabaseMetaData dmd = mock(DatabaseMetaData.class);
|
||||
DataSource ds = mock(DataSource.class);
|
||||
Connection con = mock(Connection.class);
|
||||
when(ds.getConnection()).thenReturn(con);
|
||||
when(con.getMetaData()).thenReturn(dmd);
|
||||
when(dmd.getDatabaseProductName()).thenReturn(databaseProductName);
|
||||
return ds;
|
||||
}
|
||||
|
||||
public DataSource getMockDataSource(Exception e) throws Exception {
|
||||
DataSource ds = mock(DataSource.class);
|
||||
when(ds.getConnection()).thenReturn(null);
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -50,7 +51,9 @@ import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfigurati
|
||||
import org.springframework.cloud.task.configuration.TestConfiguration;
|
||||
import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.TaskExecutionDao;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
import org.springframework.cloud.task.util.TestVerifierUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -71,6 +74,9 @@ public class SimpleTaskExplorerTests {
|
||||
@Autowired
|
||||
private TaskExplorer taskExplorer;
|
||||
|
||||
@Autowired(required = false)
|
||||
private DataSource dataSource;
|
||||
|
||||
private DaoType testType;
|
||||
|
||||
@Parameterized.Parameters
|
||||
@@ -91,6 +97,9 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
if (testType == DaoType.jdbc) {
|
||||
initializeJdbcExplorerTest();
|
||||
dao = new JdbcTaskExecutionDao(dataSource);
|
||||
((JdbcTaskExecutionDao)dao).
|
||||
setTaskIncrementer(TestDBUtils.getIncrementer(dataSource));
|
||||
}
|
||||
else {
|
||||
initializeMapExplorerTest();
|
||||
@@ -108,8 +117,8 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
@Test
|
||||
public void getTaskExecution() {
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
for (String taskExecutionId : expectedResults.keySet()) {
|
||||
Map<Long, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
for (Long taskExecutionId : expectedResults.keySet()) {
|
||||
TaskExecution actualTaskExecution =
|
||||
taskExplorer.getTaskExecution(taskExecutionId);
|
||||
assertNotNull(String.format(
|
||||
@@ -122,10 +131,10 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
@Test
|
||||
public void taskExecutionNotFound() {
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
Map< Long, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
|
||||
TaskExecution actualTaskExecution =
|
||||
taskExplorer.getTaskExecution("NO_EXECUTION_PRESENT");
|
||||
taskExplorer.getTaskExecution(-5);
|
||||
assertNull(String.format(
|
||||
"expected null for actualTaskExecution %s", testType),
|
||||
actualTaskExecution);
|
||||
@@ -133,8 +142,8 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
@Test
|
||||
public void getTaskCountByTaskName() {
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
for (Map.Entry<String, TaskExecution> entry : expectedResults.entrySet()) {
|
||||
Map<Long, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
for (Map.Entry<Long, TaskExecution> entry : expectedResults.entrySet()) {
|
||||
String taskName = entry.getValue().getTaskName();
|
||||
assertEquals(String.format(
|
||||
"task count for task name did not match expected result for testType %s",
|
||||
@@ -145,7 +154,7 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
@Test
|
||||
public void getTaskCount() {
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(33);
|
||||
Map<Long, TaskExecution> expectedResults = createSampleDataSet(33);
|
||||
assertEquals(String.format(
|
||||
"task count did not match expected result for test Type %s",
|
||||
testType),
|
||||
@@ -158,17 +167,15 @@ public class SimpleTaskExplorerTests {
|
||||
final int COMPLETE_COUNT = 5;
|
||||
final String TASK_NAME = "FOOBAR";
|
||||
|
||||
Map<String, TaskExecution> expectedResults = new HashMap<>();
|
||||
Map<Long, TaskExecution> expectedResults = new HashMap<>();
|
||||
//Store completed jobs
|
||||
for (int i = 0; i < COMPLETE_COUNT; i++) {
|
||||
createAndSaveTaskExecution();
|
||||
int i = 0;
|
||||
for (; i < COMPLETE_COUNT; i++) {
|
||||
createAndSaveTaskExecution(i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < TEST_COUNT; i++) {
|
||||
TaskExecution expectedTaskExecution = new TaskExecution();
|
||||
expectedTaskExecution.setStartTime(new Date());
|
||||
expectedTaskExecution.setExecutionId(UUID.randomUUID().toString());
|
||||
expectedTaskExecution.setTaskName(TASK_NAME);
|
||||
for (; i < (COMPLETE_COUNT + TEST_COUNT); i++) {
|
||||
TaskExecution expectedTaskExecution = new TaskExecution(i, 0, TASK_NAME, new Date(), null, null, null, new ArrayList<String>(0), null);
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution);
|
||||
}
|
||||
@@ -194,10 +201,10 @@ public class SimpleTaskExplorerTests {
|
||||
final int RESULT_SET_SIZE = 3;
|
||||
final String TASK_NAME = "FOOBAR";
|
||||
|
||||
Map<String, TaskExecution> expectedResults = new HashMap<>();
|
||||
Map<Long, TaskExecution> expectedResults = new HashMap<>();
|
||||
//Store completed jobs
|
||||
for (int i = 0; i < COMPLETE_COUNT; i++) {
|
||||
createAndSaveTaskExecution();
|
||||
createAndSaveTaskExecution(i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < TEST_COUNT; i++) {
|
||||
@@ -226,7 +233,7 @@ public class SimpleTaskExplorerTests {
|
||||
final int TEST_COUNT = 5;
|
||||
Set<String> expectedResults = new HashSet<>();
|
||||
for (int i = 0; i < TEST_COUNT; i++) {
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution();
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution(i);
|
||||
expectedResults.add(expectedTaskExecution.getTaskName());
|
||||
}
|
||||
List<String> actualTaskNames = taskExplorer.getTaskNames();
|
||||
@@ -261,9 +268,9 @@ public class SimpleTaskExplorerTests {
|
||||
}
|
||||
|
||||
private void verifyPageResults(Pageable pageable, int totalNumberOfExecs) {
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(totalNumberOfExecs);
|
||||
List<String> sortedExecIds = getSortedOfTaskExecIds(expectedResults);
|
||||
Iterator<String> expectedTaskExecutionIter = sortedExecIds.iterator();
|
||||
Map<Long, TaskExecution> expectedResults = createSampleDataSet(totalNumberOfExecs);
|
||||
List<Long> sortedExecIds = getSortedOfTaskExecIds(expectedResults);
|
||||
Iterator<Long> expectedTaskExecutionIter = sortedExecIds.iterator();
|
||||
//Verify pageable totals
|
||||
Page taskPage = taskExplorer.findAll(pageable);
|
||||
int pagesExpected = (int) Math.ceil(totalNumberOfExecs / ((double) pageable.getPageSize()));
|
||||
@@ -291,7 +298,7 @@ public class SimpleTaskExplorerTests {
|
||||
pageNumber), expectedPageSize, actualTaskExecutions.size());
|
||||
for (TaskExecution actualExecution : actualTaskExecutions) {
|
||||
assertEquals(String.format("Element on page %n did not match expected",
|
||||
pageNumber), expectedTaskExecutionIter.next(),
|
||||
pageNumber), (long)expectedTaskExecutionIter.next(),
|
||||
actualExecution.getExecutionId());
|
||||
TestVerifierUtils.verifyTaskExecution(
|
||||
expectedResults.get(actualExecution.getExecutionId()),
|
||||
@@ -306,8 +313,8 @@ public class SimpleTaskExplorerTests {
|
||||
assertEquals("Elements processed did not equal expected,", totalNumberOfExecs, elementCount);
|
||||
}
|
||||
|
||||
private TaskExecution createAndSaveTaskExecution() {
|
||||
TaskExecution taskExecution = TestVerifierUtils.createSampleTaskExecution();
|
||||
private TaskExecution createAndSaveTaskExecution(int i) {
|
||||
TaskExecution taskExecution = TestVerifierUtils.createSampleTaskExecution(i);
|
||||
dao.saveTaskExecution(taskExecution);
|
||||
return taskExecution;
|
||||
}
|
||||
@@ -333,18 +340,18 @@ public class SimpleTaskExplorerTests {
|
||||
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
|
||||
}
|
||||
|
||||
private Map<String, TaskExecution> createSampleDataSet(int count){
|
||||
Map<String, TaskExecution> expectedResults = new HashMap<>();
|
||||
private Map<Long, TaskExecution> createSampleDataSet(int count){
|
||||
Map<Long, TaskExecution> expectedResults = new HashMap<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution();
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution(i);
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution);
|
||||
}
|
||||
return expectedResults;
|
||||
}
|
||||
|
||||
private List<String> getSortedOfTaskExecIds(Map<String, TaskExecution> taskExecutionMap){
|
||||
List<String> sortedExecIds = new ArrayList<>(taskExecutionMap.size());
|
||||
private List<Long> getSortedOfTaskExecIds(Map<Long, TaskExecution> taskExecutionMap){
|
||||
List<Long> sortedExecIds = new ArrayList<>(taskExecutionMap.size());
|
||||
TreeSet sortedSet = getTreeSet();
|
||||
sortedSet.addAll(taskExecutionMap.values());
|
||||
Iterator <TaskExecution> iterator = sortedSet.descendingIterator();
|
||||
@@ -360,7 +367,7 @@ public class SimpleTaskExplorerTests {
|
||||
public int compare(TaskExecution e1, TaskExecution e2) {
|
||||
int result = e1.getStartTime().compareTo(e2.getStartTime());
|
||||
if (result == 0){
|
||||
result = e1.getExecutionId().compareTo(e2.getExecutionId());
|
||||
result = Long.valueOf(e1.getExecutionId()).compareTo(e2.getExecutionId());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -116,9 +116,10 @@ public class SimpleTaskRepositoryJdbcTests {
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCreateTaskExecutionNoParamMaxExecutionId(){
|
||||
public void testCreateTaskExecutionNoParamMaxExternalExecutionId(){
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
expectedTaskExecution.setExecutionId(new String(new char[SimpleTaskRepository.MAX_EXECUTION_ID_SIZE+1]));
|
||||
expectedTaskExecution.setExternalExecutionID(
|
||||
new String(new char[SimpleTaskRepository.MAX_EXTERNAL_EXECUTION_ID_SIZE+1]));
|
||||
taskRepository.createTaskExecution(expectedTaskExecution);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class SimpleTaskRepositoryLoggerTests {
|
||||
TaskExecution expectedTaskExecution =
|
||||
TaskExecutionCreator.createAndStoreTaskExecutionNoParams(taskRepository);
|
||||
TestVerifierUtils.verifyLogEntryExists(mockAppender,
|
||||
"Creating: TaskExecution{executionId='" + expectedTaskExecution.getExecutionId());
|
||||
"Creating: TaskExecution{executionId=" + expectedTaskExecution.getExecutionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -56,7 +56,7 @@ public class SimpleTaskRepositoryLoggerTests {
|
||||
TaskExecutionCreator.updateTaskExecution(taskRepository,
|
||||
expectedTaskExecution.getExecutionId());
|
||||
TestVerifierUtils.verifyLogEntryExists(mockAppender,
|
||||
"Updating: TaskExecution{executionId='"
|
||||
"Updating: TaskExecution{executionId="
|
||||
+ expectedTaskExecution.getExecutionId());
|
||||
}
|
||||
|
||||
|
||||
@@ -73,8 +73,8 @@ public class SimpleTaskRepositoryMapTests {
|
||||
}
|
||||
|
||||
private TaskExecution getSingleTaskExecutionFromMapRepository(
|
||||
TaskRepository repository, String taskExecutionId){
|
||||
Map<String, TaskExecution> taskMap = ((MapTaskExecutionDao)
|
||||
TaskRepository repository, long taskExecutionId){
|
||||
Map<Long, TaskExecution> taskMap = ((MapTaskExecutionDao)
|
||||
((SimpleTaskRepository)taskRepository).getTaskExecutionDao()).getTaskExecutions();
|
||||
assertTrue("taskExecutionId must be in MapTaskExecutionRepository",
|
||||
taskMap.containsKey(taskExecutionId));
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -26,6 +25,7 @@ import org.junit.Test;
|
||||
import org.springframework.cloud.task.repository.TaskRepository;
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
|
||||
/**
|
||||
* Tests that the TaskRepositoryFactoryBeans produce the correct repositories.
|
||||
@@ -36,8 +36,8 @@ import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
public class TaskRepositoryFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testJdbcTaskRepositoryFactoryBean() {
|
||||
DataSource dataSource = mock(DataSource.class);
|
||||
public void testJdbcTaskRepositoryFactoryBean() throws Exception{
|
||||
DataSource dataSource = TestDBUtils.getMockDataSource("HSQL Database Engine");
|
||||
JdbcTaskRepositoryFactoryBean factory = new JdbcTaskRepositoryFactoryBean(dataSource);
|
||||
TaskRepository repository = factory.getObject();
|
||||
assertThat(repository, instanceOf(SimpleTaskRepository.class));
|
||||
|
||||
@@ -65,9 +65,8 @@ public class TaskExecutionCreator {
|
||||
* @return the taskExecution created.
|
||||
*/
|
||||
public static TaskExecution updateTaskExecution(TaskRepository taskRepository,
|
||||
String taskExecutionId) {
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
expectedTaskExecution.setExecutionId(taskExecutionId);
|
||||
long taskExecutionId) {
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam(taskExecutionId);
|
||||
taskRepository.update(expectedTaskExecution);
|
||||
return expectedTaskExecution;
|
||||
}
|
||||
|
||||
@@ -32,12 +32,17 @@ import java.util.TreeMap;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.item.database.Order;
|
||||
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.database.PagingQueryProvider;
|
||||
import org.springframework.cloud.task.repository.database.support.SqlPagingQueryProviderFactoryBean;
|
||||
import org.springframework.cloud.task.repository.support.DatabaseType;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.support.MetaDataAccessException;
|
||||
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
||||
|
||||
/**
|
||||
* Provides a suite of tools that allow tests the ability to retrieve results from a
|
||||
@@ -49,13 +54,12 @@ public class TestDBUtils {
|
||||
|
||||
/**
|
||||
* Retrieves the TaskExecution from the datasource.
|
||||
*
|
||||
* @param dataSource The datasource from which to retrieve the taskExecution.
|
||||
* @param dataSource The datasource from which to retrieve the taskExecution.
|
||||
* @param taskExecutionId The id of the task to search.
|
||||
* @return taskExecution
|
||||
* @return taskExecution retrieved from the database.
|
||||
*/
|
||||
public static TaskExecution getTaskExecutionFromDB(DataSource dataSource,
|
||||
String taskExecutionId) {
|
||||
long taskExecutionId) {
|
||||
String sql = "SELECT * FROM TASK_EXECUTION WHERE "
|
||||
+ "TASK_EXECUTION_ID = '"
|
||||
+ taskExecutionId + "'";
|
||||
@@ -64,14 +68,15 @@ public class TestDBUtils {
|
||||
List<TaskExecution> rows = jdbcTemplate.query(sql, new RowMapper<TaskExecution>(){
|
||||
@Override
|
||||
public TaskExecution mapRow(ResultSet rs, int rownumber) throws SQLException {
|
||||
TaskExecution taskExecution=new TaskExecution();
|
||||
taskExecution.setExecutionId(rs.getString(1));
|
||||
taskExecution.setStartTime(rs.getTimestamp("START_TIME"));
|
||||
taskExecution.setEndTime(rs.getTimestamp("END_TIME"));
|
||||
taskExecution.setExitCode(rs.getInt("EXIT_CODE"));
|
||||
taskExecution.setExitMessage(rs.getString("EXIT_MESSAGE"));
|
||||
taskExecution.setStatusCode(rs.getString("STATUS_CODE"));
|
||||
taskExecution.setTaskName(rs.getString("TASK_NAME"));
|
||||
TaskExecution taskExecution=new TaskExecution(rs.getLong("TASK_EXECUTION_ID"),
|
||||
rs.getInt("EXIT_CODE"),
|
||||
rs.getString("TASK_NAME"),
|
||||
rs.getTimestamp("START_TIME"),
|
||||
rs.getTimestamp("END_TIME"),
|
||||
rs.getString("STATUS_CODE"),
|
||||
rs.getString("EXIT_MESSAGE"),
|
||||
new ArrayList<String>(0),
|
||||
rs.getString("TASK_EXTERNAL_EXECUTION_ID"));
|
||||
return taskExecution;
|
||||
}
|
||||
});
|
||||
@@ -124,6 +129,12 @@ public class TestDBUtils {
|
||||
return pagingQueryProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a mock DataSource for use in testing.
|
||||
* @param databaseProductName the name of the database type to mock.
|
||||
* @return a mock DataSource.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static DataSource getMockDataSource(String databaseProductName) throws Exception {
|
||||
DatabaseMetaData dmd = mock(DatabaseMetaData.class);
|
||||
DataSource ds = mock(DataSource.class);
|
||||
@@ -134,6 +145,25 @@ public class TestDBUtils {
|
||||
return ds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a incrementer for the DataSource.
|
||||
* @param dataSource the datasource that the incrementer will use to record current id.
|
||||
* @return a DataFieldMaxValueIncrementer object.
|
||||
*/
|
||||
public static DataFieldMaxValueIncrementer getIncrementer(DataSource dataSource){
|
||||
DataFieldMaxValueIncrementerFactory incrementerFactory =
|
||||
new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
|
||||
String databaseType = null;
|
||||
try {
|
||||
databaseType = DatabaseType.fromMetaData(dataSource).name();
|
||||
}
|
||||
catch (MetaDataAccessException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return incrementerFactory.getIncrementer(databaseType,
|
||||
"TASK_SEQ");
|
||||
}
|
||||
|
||||
private static void populateParamsToDB(DataSource dataSource, TaskExecution taskExecution) {
|
||||
String sql = "SELECT * FROM TASK_EXECUTION_PARAMS WHERE TASK_EXECUTION_ID = '"
|
||||
+ taskExecution.getExecutionId() + "'";
|
||||
|
||||
@@ -82,19 +82,63 @@ public class TestVerifierUtils {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static TaskExecution createSampleTaskExecutionNoParam() {
|
||||
public static TaskExecution createSampleTaskExecutionNoParam(long executionId) {
|
||||
Random randomGenerator = new Random();
|
||||
int exitCode = randomGenerator.nextInt();
|
||||
Date startTime = new Date();
|
||||
Date endTime = new Date();
|
||||
String executionId = UUID.randomUUID().toString();
|
||||
String externalTaskExecutionID = UUID.randomUUID().toString();
|
||||
String taskName = UUID.randomUUID().toString();
|
||||
String exitMessage = UUID.randomUUID().toString();
|
||||
String statusCode = UUID.randomUUID().toString().substring(0, 9);
|
||||
|
||||
return new TaskExecution(executionId, exitCode, taskName,
|
||||
startTime, endTime, statusCode,
|
||||
exitMessage, new ArrayList<String>());
|
||||
exitMessage, new ArrayList<String>(), externalTaskExecutionID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a fully populated TaskExecution (except params) for testing.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
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 externalTaskExecutionID = UUID.randomUUID().toString();
|
||||
String taskName = UUID.randomUUID().toString();
|
||||
String exitMessage = UUID.randomUUID().toString();
|
||||
String statusCode = UUID.randomUUID().toString().substring(0, 9);
|
||||
|
||||
return new TaskExecution(executionId, exitCode, taskName,
|
||||
startTime, endTime, statusCode,
|
||||
exitMessage, new ArrayList<String>(), externalTaskExecutionID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a fully populated TaskExecution for testing.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static TaskExecution createSampleTaskExecution(long executionId) {
|
||||
Random randomGenerator = new Random();
|
||||
int exitCode = randomGenerator.nextInt();
|
||||
Date startTime = new Date();
|
||||
Date endTime = new Date();
|
||||
String externalExecutionId = UUID.randomUUID().toString();
|
||||
String taskName = UUID.randomUUID().toString();
|
||||
String exitMessage = UUID.randomUUID().toString();
|
||||
String statusCode = UUID.randomUUID().toString().substring(0, 9);
|
||||
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, statusCode,
|
||||
exitMessage, params, externalExecutionId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,9 +149,10 @@ public class TestVerifierUtils {
|
||||
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 executionId = UUID.randomUUID().toString();
|
||||
String externalExecutionId = UUID.randomUUID().toString();
|
||||
String taskName = UUID.randomUUID().toString();
|
||||
String exitMessage = UUID.randomUUID().toString();
|
||||
String statusCode = UUID.randomUUID().toString().substring(0, 9);
|
||||
@@ -117,7 +162,7 @@ public class TestVerifierUtils {
|
||||
}
|
||||
return new TaskExecution(executionId, exitCode, taskName,
|
||||
startTime, endTime, statusCode,
|
||||
exitMessage, params);
|
||||
exitMessage, params, externalExecutionId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,6 +175,9 @@ public class TestVerifierUtils {
|
||||
TaskExecution actualTaskExecution) {
|
||||
assertEquals("taskExecutionId must be equal", expectedTaskExecution.getExecutionId(),
|
||||
actualTaskExecution.getExecutionId());
|
||||
assertEquals("taskExternalExecutionId must be equal",
|
||||
expectedTaskExecution.getExternalExecutionID(),
|
||||
actualTaskExecution.getExternalExecutionID());
|
||||
assertEquals("startTime must be equal",
|
||||
expectedTaskExecution.getStartTime(),
|
||||
actualTaskExecution.getStartTime());
|
||||
|
||||
Reference in New Issue
Block a user