SCT-41 Introduces findAll for task explorer
resolves spring-cloud/spring-cloud-task#41
This commit is contained in:
committed by
Michael Minella
parent
f30cbf36bf
commit
0e90778f20
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.task.repository.database.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class FindAllPagingQueryProviderTests {
|
||||
|
||||
private String databaseProductName;
|
||||
private String expectedQuery;
|
||||
private Pageable pageable = new PageRequest(0, 10);
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{"Oracle", "SELECT TASK_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, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE, ROWNUM as "
|
||||
+ "TMP_ROW_NUM FROM (SELECT TASK_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, "
|
||||
+ "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, "
|
||||
+ "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, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE FROM "
|
||||
+ "%PREFIX%EXECUTION ORDER BY START_TIME DESC, "
|
||||
+ "TASK_EXECUTION_ID DESC LIMIT 0, 10"}
|
||||
});
|
||||
}
|
||||
|
||||
public FindAllPagingQueryProviderTests(String databaseProductName, String expectedQuery) {
|
||||
this.databaseProductName = databaseProductName;
|
||||
this.expectedQuery = expectedQuery;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneratedQuery() throws Exception{
|
||||
String actualQuery = TestDBUtils.getPagingQueryProvider(databaseProductName).getPageQuery(pageable);
|
||||
assertEquals(String.format(
|
||||
"the generated query for %s, was not the expected query",
|
||||
databaseProductName), expectedQuery, actualQuery);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.task.repository.database.support;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
public class InvalidPagingQueryProviderTests {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testInvalidDatabase() throws Exception{
|
||||
Pageable pageable = new PageRequest(0, 10);
|
||||
String actualQuery = TestDBUtils.getPagingQueryProvider("Invalid").getPageQuery(pageable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.task.repository.database.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.database.Order;
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.database.PagingQueryProvider;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
public class SqlPagingQueryProviderFactoryBeanTests {
|
||||
|
||||
private SqlPagingQueryProviderFactoryBean factoryBean;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception{
|
||||
factoryBean = new SqlPagingQueryProviderFactoryBean();
|
||||
factoryBean.setDataSource(TestDBUtils.getMockDataSource("MySQL"));
|
||||
factoryBean.setDatabaseType("Oracle");
|
||||
factoryBean.setSelectClause(JdbcTaskExecutionDao.SELECT_CLAUSE);
|
||||
factoryBean.setFromClause(JdbcTaskExecutionDao.FROM_CLAUSE);
|
||||
Map<String, Order> orderMap = new TreeMap<>();
|
||||
orderMap.put("START_TIME", Order.DESCENDING);
|
||||
orderMap.put("TASK_EXECUTION_ID", Order.DESCENDING);
|
||||
factoryBean.setSortKeys(orderMap);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDatabaseType() throws Exception{
|
||||
PagingQueryProvider pagingQueryProvider = factoryBean.getObject();
|
||||
assertThat(pagingQueryProvider, instanceOf(OraclePagingQueryProvider.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSingleton(){
|
||||
assertTrue(factoryBean.isSingleton());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.task.repository.database.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.springframework.cloud.task.repository.database.PagingQueryProvider;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class WhereClausePagingQueryProviderTests {
|
||||
|
||||
private String databaseProductName;
|
||||
private String expectedQuery;
|
||||
private Pageable pageable = new PageRequest(0, 10);
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object[]> data() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{"Oracle", "SELECT TASK_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, "
|
||||
+ "EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED, STATUS_CODE, ROWNUM as "
|
||||
+ "TMP_ROW_NUM FROM (SELECT TASK_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, "
|
||||
+ "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, "
|
||||
+ "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, "
|
||||
+ "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 0, 10"}
|
||||
});
|
||||
}
|
||||
|
||||
public WhereClausePagingQueryProviderTests(String databaseProductName, String expectedQuery) {
|
||||
this.databaseProductName = databaseProductName;
|
||||
this.expectedQuery = expectedQuery;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeneratedQuery() throws Exception{
|
||||
PagingQueryProvider pagingQueryProvider =
|
||||
TestDBUtils.getPagingQueryProvider(databaseProductName,
|
||||
"TASK_EXECUTION_ID = '0000'");
|
||||
String actualQuery = pagingQueryProvider.getPageQuery(pageable);
|
||||
assertEquals(String.format(
|
||||
"the generated query for %s, was not the expected query",
|
||||
databaseProductName), expectedQuery, actualQuery);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ 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;
|
||||
@@ -45,6 +46,7 @@ public class DatabaseTypeTests {
|
||||
assertEquals(HSQL, fromProductName("HSQL Database Engine"));
|
||||
assertEquals(ORACLE, fromProductName("Oracle"));
|
||||
assertEquals(POSTGRES, fromProductName("PostgreSQL"));
|
||||
assertEquals(MYSQL, fromProductName("MySQL"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -70,6 +72,12 @@ public class DatabaseTypeTests {
|
||||
assertEquals(POSTGRES, DatabaseType.fromMetaData(ds));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMetaDataForMySQL() throws Exception {
|
||||
DataSource ds = getMockDataSource("MySQL");
|
||||
assertEquals(MYSQL, DatabaseType.fromMetaData(ds));
|
||||
}
|
||||
|
||||
public DataSource getMockDataSource(String databaseProductName) throws Exception {
|
||||
DatabaseMetaData dmd = mock(DatabaseMetaData.class);
|
||||
DataSource ds = mock(DataSource.class);
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 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.
|
||||
*/
|
||||
package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
|
||||
public class NoOpTaskExplorerTests {
|
||||
|
||||
private TaskExplorer taskExplorer;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
taskExplorer = new NoOpTaskExplorer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTaskExecution() throws Exception {
|
||||
assertNull(taskExplorer.getTaskExecution("abc"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindRunningTaskExecutions() throws Exception {
|
||||
assertEquals(taskExplorer.findRunningTaskExecutions("foo").size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTaskNames() throws Exception {
|
||||
assertEquals(taskExplorer.getTaskNames().size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTaskExecutionCount() throws Exception {
|
||||
assertEquals(taskExplorer.getTaskExecutionCount("foo"), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTaskExecutionsByName() throws Exception {
|
||||
assertEquals(taskExplorer.getTaskExecutionsByName("foo", 0, 100).size(), 0);
|
||||
}
|
||||
}
|
||||
@@ -16,19 +16,23 @@
|
||||
|
||||
package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -40,7 +44,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;
|
||||
@@ -52,6 +55,9 @@ import org.springframework.cloud.task.repository.dao.TaskExecutionDao;
|
||||
import org.springframework.cloud.task.util.TestVerifierUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
@@ -77,23 +83,24 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
@Parameterized.Parameters
|
||||
public static Collection<Object> data() {
|
||||
return Arrays.asList(new Object[] {
|
||||
DaoType.jdbc , DaoType.map });
|
||||
return Arrays.asList(new Object[]{
|
||||
DaoType.jdbc, DaoType.map });
|
||||
}
|
||||
|
||||
public SimpleTaskExplorerTests(DaoType testType) {
|
||||
this.testType = testType;
|
||||
}
|
||||
|
||||
@Rule
|
||||
public ExpectedException expected = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void testDefaultContext() throws Exception {
|
||||
|
||||
if(testType == DaoType.jdbc){
|
||||
if (testType == DaoType.jdbc) {
|
||||
initializeJdbcExplorerTest();
|
||||
}
|
||||
else{
|
||||
else {
|
||||
initializeMapExplorerTest();
|
||||
}
|
||||
|
||||
@@ -109,13 +116,7 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
@Test
|
||||
public void getTaskExecution() {
|
||||
final int TEST_COUNT = 5;
|
||||
Map<String, TaskExecution> expectedResults = new HashMap<>();
|
||||
for (int i = 0; i < TEST_COUNT; i++) {
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution();
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution);
|
||||
}
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
for (String taskExecutionId : expectedResults.keySet()) {
|
||||
TaskExecution actualTaskExecution =
|
||||
taskExplorer.getTaskExecution(taskExecutionId);
|
||||
@@ -129,39 +130,36 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
@Test
|
||||
public void taskExecutionNotFound() {
|
||||
final int TEST_COUNT = 5;
|
||||
Map<String, TaskExecution> expectedResults = new HashMap<>();
|
||||
for (int i = 0; i < TEST_COUNT; i++) {
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution();
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution);
|
||||
}
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
|
||||
TaskExecution actualTaskExecution =
|
||||
taskExplorer.getTaskExecution("NO_EXECUTION_PRESENT");
|
||||
assertNull(String.format(
|
||||
"expected null for actualTaskExecution %s", testType),
|
||||
actualTaskExecution);
|
||||
assertNull(String.format(
|
||||
"expected null for actualTaskExecution %s", testType),
|
||||
actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTaskCountByTaskName() {
|
||||
final int TEST_COUNT = 5;
|
||||
Map<String, TaskExecution> expectedResults = new HashMap<>();
|
||||
for (int i = 0; i < TEST_COUNT; i++) {
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution();
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution);
|
||||
}
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(5);
|
||||
for (Map.Entry<String, 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",
|
||||
testType),
|
||||
1, taskExplorer.getTaskExecutionCount(taskName));
|
||||
1, taskExplorer.getTaskExecutionCountByTaskName(taskName));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTaskCount() {
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(33);
|
||||
assertEquals(String.format(
|
||||
"task count did not match expected result for test Type %s",
|
||||
testType),
|
||||
33, taskExplorer.getTaskExecutionCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findRunningTasks() {
|
||||
final int TEST_COUNT = 2;
|
||||
@@ -227,7 +225,7 @@ public class SimpleTaskExplorerTests {
|
||||
expectedResults.containsKey(result.getExecutionId()));
|
||||
assertEquals(
|
||||
String.format("taskName for taskExecution is incorrect for testType %s",
|
||||
testType), TASK_NAME, result.getTaskName());
|
||||
testType), TASK_NAME, result.getTaskName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,13 +244,83 @@ public class SimpleTaskExplorerTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllExecutionsOffBoundry() {
|
||||
Pageable pageable = new PageRequest(0, 10);
|
||||
verifyPageResults(pageable, 103);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllExecutionsOffBoundryByOne() {
|
||||
Pageable pageable = new PageRequest(0, 10);
|
||||
verifyPageResults(pageable, 101);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllExecutionsOnBoundry() {
|
||||
Pageable pageable = new PageRequest(0, 10);
|
||||
verifyPageResults(pageable, 100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findAllExecutionsNoResult() {
|
||||
Pageable pageable = new PageRequest(0, 10);
|
||||
verifyPageResults(pageable, 0);
|
||||
}
|
||||
|
||||
private void verifyPageResults(Pageable pageable, int totalNumberOfExecs) {
|
||||
Map<String, TaskExecution> expectedResults = createSampleDataSet(totalNumberOfExecs);
|
||||
List<String> sortedExecIds = getSortedOfTaskExecIds(expectedResults);
|
||||
Iterator<String> expectedTaskExecutionIter = sortedExecIds.iterator();
|
||||
//Verify pageable totals
|
||||
Page taskPage = taskExplorer.findAll(pageable);
|
||||
int pagesExpected = (int) Math.ceil(totalNumberOfExecs / ((double) pageable.getPageSize()));
|
||||
assertEquals("actual page count return was not the expected total",
|
||||
pagesExpected,
|
||||
taskPage.getTotalPages());
|
||||
assertEquals("actual element count was not the expected count", totalNumberOfExecs,
|
||||
taskPage.getTotalElements());
|
||||
|
||||
//Verify pagination
|
||||
Pageable actualPageable = new PageRequest(0, pageable.getPageSize());
|
||||
boolean hasMorePages = taskPage.hasContent();
|
||||
int pageNumber = 0;
|
||||
int elementCount = 0;
|
||||
while (hasMorePages) {
|
||||
taskPage = taskExplorer.findAll(actualPageable);
|
||||
hasMorePages = taskPage.hasNext();
|
||||
List<TaskExecution> actualTaskExecutions = taskPage.getContent();
|
||||
int expectedPageSize = pageable.getPageSize();
|
||||
if (!hasMorePages && pageable.getPageSize() != actualTaskExecutions.size()) {
|
||||
expectedPageSize = totalNumberOfExecs % pageable.getPageSize();
|
||||
}
|
||||
assertEquals(
|
||||
String.format("Element count on page did not match on the %n page",
|
||||
pageNumber), expectedPageSize, actualTaskExecutions.size());
|
||||
for (TaskExecution actualExecution : actualTaskExecutions) {
|
||||
assertEquals(String.format("Element on page %n did not match expected",
|
||||
pageNumber), expectedTaskExecutionIter.next(),
|
||||
actualExecution.getExecutionId());
|
||||
TestVerifierUtils.verifyTaskExecution(
|
||||
expectedResults.get(actualExecution.getExecutionId()),
|
||||
actualExecution);
|
||||
elementCount++;
|
||||
}
|
||||
actualPageable = taskPage.nextPageable();
|
||||
pageNumber++;
|
||||
}
|
||||
//Verify actual totals
|
||||
assertEquals("Pages processed did not equal expected", pagesExpected, pageNumber);
|
||||
assertEquals("Elements processed did not equal expected,", totalNumberOfExecs, elementCount);
|
||||
}
|
||||
|
||||
private TaskExecution createAndSaveTaskExecution() {
|
||||
TaskExecution taskExecution = TestVerifierUtils.createSampleTaskExecution();
|
||||
dao.saveTaskExecution(taskExecution);
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
private void initializeJdbcExplorerTest(){
|
||||
private void initializeJdbcExplorerTest() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(TestConfiguration.class,
|
||||
EmbeddedDataSourceConfiguration.class,
|
||||
@@ -273,6 +341,39 @@ public class SimpleTaskExplorerTests {
|
||||
AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
|
||||
}
|
||||
|
||||
private Map<String, TaskExecution> createSampleDataSet(int count){
|
||||
Map<String, TaskExecution> expectedResults = new HashMap<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
TaskExecution expectedTaskExecution = createAndSaveTaskExecution();
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(),
|
||||
expectedTaskExecution);
|
||||
}
|
||||
return expectedResults;
|
||||
}
|
||||
|
||||
private List<String> getSortedOfTaskExecIds(Map<String, TaskExecution> taskExecutionMap){
|
||||
List<String> sortedExecIds = new ArrayList<>(taskExecutionMap.size());
|
||||
TreeSet sortedSet = getTreeSet();
|
||||
sortedSet.addAll(taskExecutionMap.values());
|
||||
Iterator <TaskExecution> iterator = sortedSet.descendingIterator();
|
||||
while(iterator.hasNext()){
|
||||
sortedExecIds.add(iterator.next().getExecutionId());
|
||||
}
|
||||
return sortedExecIds;
|
||||
}
|
||||
|
||||
private TreeSet getTreeSet(){
|
||||
return new TreeSet<TaskExecution>(new Comparator<TaskExecution>() {
|
||||
@Override
|
||||
public int compare(TaskExecution e1, TaskExecution e2) {
|
||||
int result = e1.getStartTime().compareTo(e2.getStartTime());
|
||||
if (result == 0){
|
||||
result = e1.getExecutionId().compareTo(e2.getExecutionId());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private enum DaoType{jdbc, map}
|
||||
}
|
||||
|
||||
@@ -93,5 +93,33 @@ public class SimpleTaskRepositoryJdbcTests {
|
||||
dataSource, expectedTaskExecution.getExecutionId());
|
||||
TestVerifierUtils.verifyTaskExecution(expectedTaskExecution, actualTaskExecution);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTaskExecutionNoParamMaxExitMessageSize(){
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
expectedTaskExecution.setExitMessage(new String(new char[SimpleTaskRepository.MAX_EXIT_MESSAGE_SIZE+1]));
|
||||
taskRepository.createTaskExecution(expectedTaskExecution);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCreateTaskExecutionNoParamMaxTaskName(){
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
expectedTaskExecution.setTaskName(new String(new char[SimpleTaskRepository.MAX_TASK_NAME_SIZE+1]));
|
||||
taskRepository.createTaskExecution(expectedTaskExecution);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCreateTaskExecutionNoParamMaxStatus(){
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
expectedTaskExecution.setStatusCode(new String(new char[SimpleTaskRepository.MAX_STATUS_CODE_SIZE+1]));
|
||||
taskRepository.createTaskExecution(expectedTaskExecution);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCreateTaskExecutionNoParamMaxExecutionId(){
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
expectedTaskExecution.setExecutionId(new String(new char[SimpleTaskRepository.MAX_EXECUTION_ID_SIZE+1]));
|
||||
taskRepository.createTaskExecution(expectedTaskExecution);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,16 +17,25 @@
|
||||
package org.springframework.cloud.task.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.item.database.Order;
|
||||
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.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
@@ -39,10 +48,10 @@ import org.springframework.jdbc.core.RowMapper;
|
||||
public class TestDBUtils {
|
||||
|
||||
/**
|
||||
* Retrieves the TaskExecution from the datasource
|
||||
* Retrieves the TaskExecution from the datasource.
|
||||
*
|
||||
* @param dataSource The datasource from which to retrieve the taskExecution
|
||||
* @param taskExecutionId The id of the task to search .
|
||||
* @param dataSource The datasource from which to retrieve the taskExecution.
|
||||
* @param taskExecutionId The id of the task to search.
|
||||
* @return taskExecution
|
||||
*/
|
||||
public static TaskExecution getTaskExecutionFromDB(DataSource dataSource,
|
||||
@@ -73,6 +82,58 @@ public class TestDBUtils {
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a pagingQueryProvider specific database type with a findAll.
|
||||
* @param databaseProductName of the database.
|
||||
* @return a PagingQueryPovider that will return all the requested information.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static PagingQueryProvider getPagingQueryProvider(String databaseProductName) throws Exception{
|
||||
return getPagingQueryProvider(databaseProductName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a pagingQueryProvider specific database type with a query containing a where clause.
|
||||
* @param databaseProductName of the database.
|
||||
* @param whereClause to be applied to the query.
|
||||
* @return a PagingQueryProvider that will return the requested information.
|
||||
* @throws Exception
|
||||
*/
|
||||
public static PagingQueryProvider getPagingQueryProvider(String databaseProductName,
|
||||
String whereClause) throws Exception{
|
||||
DataSource dataSource = getMockDataSource(databaseProductName);
|
||||
Map<String, Order> orderMap = new TreeMap<>();
|
||||
orderMap.put("START_TIME", Order.DESCENDING);
|
||||
orderMap.put("TASK_EXECUTION_ID", Order.DESCENDING);
|
||||
SqlPagingQueryProviderFactoryBean factoryBean = new SqlPagingQueryProviderFactoryBean();
|
||||
factoryBean.setSelectClause(JdbcTaskExecutionDao.SELECT_CLAUSE);
|
||||
factoryBean.setFromClause(JdbcTaskExecutionDao.FROM_CLAUSE);
|
||||
if(whereClause != null){
|
||||
factoryBean.setWhereClause(whereClause);
|
||||
}
|
||||
factoryBean.setSortKeys(orderMap);
|
||||
factoryBean.setDataSource(dataSource);
|
||||
PagingQueryProvider pagingQueryProvider = null;
|
||||
try {
|
||||
pagingQueryProvider = factoryBean.getObject();
|
||||
pagingQueryProvider.init(dataSource);
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return pagingQueryProvider;
|
||||
}
|
||||
|
||||
public static 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;
|
||||
}
|
||||
|
||||
private static void populateParamsToDB(DataSource dataSource, TaskExecution taskExecution) {
|
||||
String sql = "SELECT * FROM TASK_EXECUTION_PARAMS WHERE TASK_EXECUTION_ID = '"
|
||||
+ taskExecution.getExecutionId() + "'";
|
||||
|
||||
Reference in New Issue
Block a user