From 2f494a0015de1ead2debfc3ebc3eb9f2d3a2eb4b Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Mon, 27 Nov 2023 17:56:16 -0500 Subject: [PATCH] Changes requested by code review have been applied. Tests no long have test in the name but more of a TDD title Set the access to be package level for all tests and variables Database changes removed Compararator added so that schema scripts are called in the proper order Rebased --- .../resources/schemas/mariadb/V6-dataflow.sql | 38 ++++++++++++++-- .../server/batch/AbstractDaoTests.java | 34 ++++++++++++++- ...actJdbcJobSearchableExecutionDaoTests.java | 43 +++++++------------ ...ractJdbcJobSearchableInstanceDaoTests.java | 14 +++--- ...JobSearchableExecutionMariadbDaoTests.java | 2 +- ...obSearchableExecutionPostgresDaoTests.java | 2 +- ...cJobSearchableInstanceMariadbDaoTests.java | 2 +- ...JobSearchableInstancePostgresDaoTests.java | 2 +- 8 files changed, 91 insertions(+), 46 deletions(-) diff --git a/spring-cloud-dataflow-server-core/src/main/resources/schemas/mariadb/V6-dataflow.sql b/spring-cloud-dataflow-server-core/src/main/resources/schemas/mariadb/V6-dataflow.sql index 2e1c4b5c42..671962da3b 100644 --- a/spring-cloud-dataflow-server-core/src/main/resources/schemas/mariadb/V6-dataflow.sql +++ b/spring-cloud-dataflow-server-core/src/main/resources/schemas/mariadb/V6-dataflow.sql @@ -141,6 +141,38 @@ CREATE TABLE BOOT3_BATCH_JOB_EXECUTION_CONTEXT ); -CREATE SEQUENCE BOOT3_BATCH_STEP_EXECUTION_SEQ START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775806 INCREMENT BY 1 NOCACHE NOCYCLE; -CREATE SEQUENCE BOOT3_BATCH_JOB_EXECUTION_SEQ START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775806 INCREMENT BY 1 NOCACHE NOCYCLE; -CREATE SEQUENCE BOOT3_BATCH_JOB_SEQ START WITH 1 MINVALUE 1 MAXVALUE 9223372036854775806 INCREMENT BY 1 NOCACHE NOCYCLE; +CREATE TABLE BOOT3_BATCH_STEP_EXECUTION_SEQ +( + ID BIGINT NOT NULL, + UNIQUE_KEY CHAR(1) NOT NULL, + constraint UNIQUE_KEY_UN unique (UNIQUE_KEY) +); + +INSERT INTO BOOT3_BATCH_STEP_EXECUTION_SEQ (ID, UNIQUE_KEY) +select * +from (select 0 as ID, '0' as UNIQUE_KEY) as tmp +where not exists(select * from BOOT3_BATCH_STEP_EXECUTION_SEQ); + +CREATE TABLE BOOT3_BATCH_JOB_EXECUTION_SEQ +( + ID BIGINT NOT NULL, + UNIQUE_KEY CHAR(1) NOT NULL, + constraint UNIQUE_KEY_UN unique (UNIQUE_KEY) +); + +INSERT INTO BOOT3_BATCH_JOB_EXECUTION_SEQ (ID, UNIQUE_KEY) +select * +from (select 0 as ID, '0' as UNIQUE_KEY) as tmp +where not exists(select * from BOOT3_BATCH_JOB_EXECUTION_SEQ); + +CREATE TABLE BOOT3_BATCH_JOB_SEQ +( + ID BIGINT NOT NULL, + UNIQUE_KEY CHAR(1) NOT NULL, + constraint UNIQUE_KEY_UN unique (UNIQUE_KEY) +); + +INSERT INTO BOOT3_BATCH_JOB_SEQ (ID, UNIQUE_KEY) +select * +from (select 0 as ID, '0' as UNIQUE_KEY) as tmp +where not exists(select * from BOOT3_BATCH_JOB_SEQ); \ No newline at end of file diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractDaoTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractDaoTests.java index d8e401b9a1..282d7e47bf 100644 --- a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractDaoTests.java +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractDaoTests.java @@ -16,6 +16,7 @@ package org.springframework.cloud.dataflow.server.batch; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.testcontainers.containers.JdbcDatabaseContainer; import org.testcontainers.ext.ScriptUtils; @@ -27,11 +28,15 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; public class AbstractDaoTests { - protected DataSource createDataSourceForContainer(JdbcDatabaseContainer dbContainer) { + DataSource dataSource; + JdbcTemplate jdbcTemplate; + + DataSource createDataSourceForContainer(JdbcDatabaseContainer dbContainer) { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(dbContainer.getDriverClassName()); dataSource.setUrl(dbContainer.getJdbcUrl()); @@ -39,7 +44,7 @@ protected DataSource createDataSourceForContainer(JdbcDatabaseContainer dbContai dataSource.setPassword(dbContainer.getPassword()); return dataSource; } - protected void createDataFlowSchema(JdbcDatabaseContainer dbContainer, String schemaName) throws IOException { + void createDataFlowSchema(JdbcDatabaseContainer dbContainer, String schemaName) throws IOException { JdbcDatabaseDelegate containerDelegate = new JdbcDatabaseDelegate(dbContainer, ""); ScriptUtils.runInitScript(containerDelegate, "schemas/drop-table-schema-" + schemaName + ".sql"); @@ -49,6 +54,12 @@ protected void createDataFlowSchema(JdbcDatabaseContainer dbContainer, String sc }); } + void prepareForTest(JdbcDatabaseContainer dbContainer, String schemaName) throws IOException { + this.dataSource = createDataSourceForContainer(dbContainer); + this.jdbcTemplate = new JdbcTemplate(this.dataSource); + createDataFlowSchema(dbContainer, schemaName); + } + private List getResourceFiles(String path) throws IOException { List fileNames = new ArrayList<>(); @@ -61,6 +72,7 @@ private List getResourceFiles(String path) throws IOException { fileNames.add(fileName); } } + fileNames.sort(new SchemaComparator()); return fileNames; } @@ -68,4 +80,22 @@ private InputStream getResourceFileAsStream(String resourceFile) { InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceFile); return stream == null ? getClass().getResourceAsStream(resourceFile) : stream; } + + private static class SchemaComparator implements Comparator { + + @Override + public int compare(String o1, String o2) { + int result = 0; + if (getVersion(o1) > getVersion(o2)) { + result = 1; + } else if (getVersion(o1) < getVersion(o2)) { + result = -1; + } + return result; + } + + private int getVersion(String fileName) { + return Integer.valueOf(fileName.substring(1, fileName.indexOf("-"))); + } + } } diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableExecutionDaoTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableExecutionDaoTests.java index 121fb965a6..a85f405e32 100644 --- a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableExecutionDaoTests.java +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableExecutionDaoTests.java @@ -28,12 +28,9 @@ import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory; import org.springframework.cloud.dataflow.core.database.support.DatabaseType; import org.springframework.cloud.dataflow.core.database.support.MultiSchemaIncrementerFactory; -import org.springframework.cloud.task.batch.listener.support.JdbcTaskBatchDao; -import org.springframework.cloud.task.repository.TaskRepository; import org.springframework.jdbc.core.JdbcTemplate; import org.testcontainers.containers.JdbcDatabaseContainer; -import javax.sql.DataSource; import java.util.ArrayList; import java.util.List; @@ -43,29 +40,19 @@ public abstract class AbstractJdbcJobSearchableExecutionDaoTests extends Abstrac private static final String BASE_JOB_INSTANCE_NAME = "JOB_INST_"; - protected JdbcSearchableJobExecutionDao jdbcSearchableJobExecutionDao; + JdbcSearchableJobExecutionDao jdbcSearchableJobExecutionDao; - protected JdbcSearchableJobInstanceDao jdbcSearchableJobInstanceDao; + JdbcSearchableJobInstanceDao jdbcSearchableJobInstanceDao; - protected DataSource dataSource; + DataFieldMaxValueIncrementerFactory incrementerFactory; - protected DataFieldMaxValueIncrementerFactory incrementerFactory; + JdbcJobExecutionDao jobExecutionDao; - protected JdbcJobExecutionDao jobExecutionDao; + JdbcStepExecutionDao stepExecutionDao; - protected JdbcStepExecutionDao stepExecutionDao; - - protected JdbcTemplate jdbcTemplate; - - protected TaskRepository taskRepository; - - protected JdbcTaskBatchDao jdbcTaskBatchDao; - - public void setupSearchableExecutionDaoTest(JdbcDatabaseContainer dbContainer, String schemaName, + void setupSearchableExecutionDaoTest(JdbcDatabaseContainer dbContainer, String schemaName, DatabaseType databaseType) throws Exception { - this.dataSource = createDataSourceForContainer(dbContainer); - this.jdbcTemplate = new JdbcTemplate(this.dataSource); - createDataFlowSchema(dbContainer, schemaName); + prepareForTest(dbContainer, schemaName); this.jdbcSearchableJobExecutionDao = new JdbcSearchableJobExecutionDao(); this.jdbcSearchableJobExecutionDao.setDataSource(this.dataSource); @@ -89,7 +76,7 @@ public void setupSearchableExecutionDaoTest(JdbcDatabaseContainer dbContainer, S } @Test - public void testJobExecutionCount() { + void retrieveJobExecutionCountBeforeAndAfterJobExecution() { assertThat(this.jdbcSearchableJobExecutionDao.countJobExecutions()).isEqualTo(0); JobInstance jobInstance = jdbcSearchableJobInstanceDao.createJobInstance(BASE_JOB_INSTANCE_NAME, new JobParameters()); @@ -98,7 +85,7 @@ public void testJobExecutionCount() { } @Test - public void testGetCompletedJobExecutionsByType() { + void retrieveJobExecutionsByTypeAfterJobExeuction() { String suffix = "_BY_NAME"; assertThat(this.jdbcSearchableJobExecutionDao.getJobExecutions(BASE_JOB_INSTANCE_NAME + suffix, BatchStatus.COMPLETED, 0, 5).size()).isEqualTo(0); @@ -112,7 +99,7 @@ public void testGetCompletedJobExecutionsByType() { } @Test - public void testGetJobExecutions() { + void retrieveJobExecutionCountWithoutFilter() { String suffix = "_BY_NAME"; String suffixFailed = suffix + "_FAILED"; assertThat(this.jdbcSearchableJobExecutionDao.getJobExecutions(BASE_JOB_INSTANCE_NAME + suffix, @@ -128,7 +115,7 @@ public void testGetJobExecutions() { } @Test - public void testJobExecutionCountByName() { + void retrieveJobExecutionCountFilteredByName() { String suffix = "COUNT_BY_NAME"; assertThat(this.jdbcSearchableJobExecutionDao.countJobExecutions(BASE_JOB_INSTANCE_NAME + suffix)) .isEqualTo(0); @@ -138,7 +125,7 @@ public void testJobExecutionCountByName() { } @Test - public void testJobExecutionCountByStatus() { + void retrieveJobExecutionCountFilteredByStatus() { String suffix = "_COUNT_BY_NAME"; assertThat(this.jdbcSearchableJobExecutionDao.countJobExecutions(BatchStatus.COMPLETED)).isEqualTo(0); createJobExecutions(BASE_JOB_INSTANCE_NAME + suffix, 5); @@ -146,7 +133,7 @@ public void testJobExecutionCountByStatus() { } @Test - public void testJobExecutionCountByNameAndStatus() { + void retrieveJobExecutionCountFilteredNameAndStatus() { String suffix = "_COUNT_BY_NAME_STATUS"; assertThat(this.jdbcSearchableJobExecutionDao.countJobExecutions(BASE_JOB_INSTANCE_NAME + suffix, BatchStatus.COMPLETED)).isEqualTo(0); @@ -156,7 +143,7 @@ public void testJobExecutionCountByNameAndStatus() { } @Test - public void testJobExecutionsWithStepCount() { + void retrieveJobExecutionWithStepCount() { String suffix = "_JOB_EXECUTIONS_WITH_STEP_COUNT"; createJobExecutions(BASE_JOB_INSTANCE_NAME + suffix, 5); @@ -168,7 +155,7 @@ public void testJobExecutionsWithStepCount() { } @Test - public void testJobExecutionsWithStepCountByJobInstance() { + void retrieveJobExecutionWithStepCountFilteredJobInstance() { String suffix = "_JOB_EXECUTIONS_WITH_STEP_COUNT_BY_JOB_INSTANCE"; createJobExecutions(BASE_JOB_INSTANCE_NAME + suffix, 5); diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableInstanceDaoTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableInstanceDaoTests.java index 1681179ba9..28cf2f91aa 100644 --- a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableInstanceDaoTests.java +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/AbstractJdbcJobSearchableInstanceDaoTests.java @@ -18,7 +18,6 @@ import org.junit.jupiter.api.Test; import org.springframework.batch.core.JobParameters; -import org.springframework.jdbc.core.JdbcTemplate; import org.testcontainers.containers.JdbcDatabaseContainer; import javax.sql.DataSource; @@ -28,20 +27,17 @@ public abstract class AbstractJdbcJobSearchableInstanceDaoTests extends AbstractDaoTests { private static final String BASE_JOB_INST_NAME = "JOB_INST_"; - private DataSource dataSource; + JdbcSearchableJobInstanceDao jdbcSearchableJobInstanceDao; - public JdbcSearchableJobInstanceDao jdbcSearchableJobInstanceDao; - - public void setupSearchableExecutionDaoTest(JdbcDatabaseContainer dbContainer, String schemaName) throws Exception { - this.dataSource = createDataSourceForContainer(dbContainer); - createDataFlowSchema(dbContainer, schemaName); + void setupSearchableExecutionDaoTest(JdbcDatabaseContainer dbContainer, String schemaName) throws Exception { + prepareForTest(dbContainer, schemaName); jdbcSearchableJobInstanceDao = new JdbcSearchableJobInstanceDao(); - jdbcSearchableJobInstanceDao.setJdbcTemplate(new JdbcTemplate(this.dataSource)); + jdbcSearchableJobInstanceDao.setJdbcTemplate(this.jdbcTemplate); jdbcSearchableJobInstanceDao.afterPropertiesSet(); } @Test - public void testCountJobInstances() { + void retrieveJobInstanceCountWithoutFilter() { assertThat(jdbcSearchableJobInstanceDao.countJobInstances(BASE_JOB_INST_NAME)).isEqualTo(0); jdbcSearchableJobInstanceDao.createJobInstance(BASE_JOB_INST_NAME, new JobParameters()); assertThat(jdbcSearchableJobInstanceDao.countJobInstances(BASE_JOB_INST_NAME)).isEqualTo(1); diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionMariadbDaoTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionMariadbDaoTests.java index 7752371225..fac4bc03e1 100644 --- a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionMariadbDaoTests.java +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionMariadbDaoTests.java @@ -30,7 +30,7 @@ public class JdbcJobSearchableExecutionMariadbDaoTests extends AbstractJdbcJobSe private static final JdbcDatabaseContainer dbContainer = new MariaDBContainer("mariadb:10.9.3"); @BeforeEach - public void setup() throws Exception { + void setup() throws Exception { setupSearchableExecutionDaoTest(dbContainer, "mariadb", DatabaseType.MYSQL); } } diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionPostgresDaoTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionPostgresDaoTests.java index babb459735..0ae916eed4 100644 --- a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionPostgresDaoTests.java +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableExecutionPostgresDaoTests.java @@ -30,7 +30,7 @@ public class JdbcJobSearchableExecutionPostgresDaoTests extends AbstractJdbcJobS private static final JdbcDatabaseContainer dbContainer = new PostgreSQLContainer("postgres:11.1"); @BeforeEach - public void setup() throws Exception { + void setup() throws Exception { setupSearchableExecutionDaoTest(dbContainer, "postgresql", DatabaseType.POSTGRES); } } diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstanceMariadbDaoTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstanceMariadbDaoTests.java index e207539c28..96a779c065 100644 --- a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstanceMariadbDaoTests.java +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstanceMariadbDaoTests.java @@ -29,7 +29,7 @@ public class JdbcJobSearchableInstanceMariadbDaoTests extends AbstractJdbcJobSea private static final JdbcDatabaseContainer mariaDBContainer = new MariaDBContainer<>("mariadb:10.9.3"); @BeforeEach - public void setup() throws Exception{ + void setup() throws Exception{ setupSearchableExecutionDaoTest(mariaDBContainer, "mariadb"); } } diff --git a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstancePostgresDaoTests.java b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstancePostgresDaoTests.java index 5a4ca35b4b..713d7fbc44 100644 --- a/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstancePostgresDaoTests.java +++ b/spring-cloud-dataflow-server-core/src/test/java/org/springframework/cloud/dataflow/server/batch/JdbcJobSearchableInstancePostgresDaoTests.java @@ -29,7 +29,7 @@ public class JdbcJobSearchableInstancePostgresDaoTests extends AbstractJdbcJobSe private static final JdbcDatabaseContainer mariaDBContainer = new PostgreSQLContainer<>("postgres:11.1"); @BeforeEach - public void setup() throws Exception{ + void setup() throws Exception{ setupSearchableExecutionDaoTest(mariaDBContainer, "postgresql"); } }