Skip to content

Commit

Permalink
Changes requested by code review have been applied.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cppwfs committed Nov 28, 2023
1 parent a0aa8a0 commit 2f494a0
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,19 +28,23 @@
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());
dataSource.setUsername(dbContainer.getUsername());
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");

Expand All @@ -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<String> getResourceFiles(String path) throws IOException {
List<String> fileNames = new ArrayList<>();

Expand All @@ -61,11 +72,30 @@ private List<String> getResourceFiles(String path) throws IOException {
fileNames.add(fileName);
}
}
fileNames.sort(new SchemaComparator());
return fileNames;
}

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<String> {

@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("-")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -138,15 +125,15 @@ 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);
assertThat(this.jdbcSearchableJobExecutionDao.countJobExecutions(BatchStatus.COMPLETED)).isEqualTo(5);
}

@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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit 2f494a0

Please sign in to comment.