TSK-1735: replaced taskanaUnitTest.properties with testcontainers

This commit is contained in:
Mustapha Zorgati 2021-09-20 21:33:47 +02:00
parent fb4234b6f1
commit 9da7304bca
10 changed files with 116 additions and 110 deletions

View File

@ -274,15 +274,13 @@ jobs:
with:
name: ${{ env.ARTIFACTS_TASKANA_JARS_NAME }}
path: ${{ env.ARTIFACTS_TASKANA_JARS_PATH }}
- name: Prepare database
run: docker-databases/prepare_db.sh ${{ matrix.database }}
- name: Generate JavaDoc for Rest Documentation
if: matrix.module == 'taskana-simplehistory-rest-spring'
run: ./mvnw -B validate -pl :taskana-rest-spring
- name: Test
run: ./mvnw -B verify -pl :${{matrix.module}} -Dcheckstyle.skip
env:
db.type: ${{ matrix.database }}
DB: ${{ matrix.database }}
- name: Upload JaCoCo Report
if: matrix.database == 'H2'
uses: actions/upload-artifact@v2

View File

@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All in taskana-core DB2" type="JUnit" factoryName="JUnit" folderName="lib">
<module name="taskana-core" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<envs>
<env name="DB" value="DB2" />
</envs>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,15 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All in taskana-core POSTGRES" type="JUnit" factoryName="JUnit" folderName="lib">
<module name="taskana-core" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
<envs>
<env name="DB" value="POSTGRES" />
</envs>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,18 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run ArchitectureTest" type="JUnit" factoryName="JUnit" folderName="lib">
<module name="taskana-core" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="pro.taskana.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="acceptance" />
<option name="MAIN_CLASS_NAME" value="acceptance.ArchitectureTest" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
</component>

View File

@ -106,6 +106,28 @@
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>db2</artifactId>
<version>${version.testcontainers}</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>${version.testcontainers}</version>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- TEST DEPENDENCIES -->
<dependency>

View File

@ -1,4 +1,4 @@
package testapi.util;
package pro.taskana.common.test;
import static java.time.temporal.ChronoUnit.SECONDS;

View File

@ -1,14 +1,12 @@
package pro.taskana.common.test.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Optional;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.JdbcDatabaseContainer;
import pro.taskana.common.internal.configuration.DB;
import pro.taskana.common.test.DockerContainerCreator;
/**
* The DataSourceGenerator provides the proper {@linkplain DataSource} for all Integration tests.
@ -23,22 +21,22 @@ import org.slf4j.LoggerFactory;
*/
public final class DataSourceGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceGenerator.class);
private static final DataSource DATA_SOURCE;
private static final String SCHEMA_NAME;
private static final String DEFAULT_SCHEMA_NAME = "TASKANA";
private static final int POOL_TIME_TO_WAIT = 50;
static {
String propertiesFileName = System.getProperty("user.home") + "/taskanaUnitTest.properties";
File f = new File(propertiesFileName);
if (f.exists() && !f.isDirectory()) {
DATA_SOURCE = createDataSourceFromProperties(propertiesFileName);
SCHEMA_NAME = getSchemaNameFromPropertiesObject(propertiesFileName);
DB db = retrieveDatabaseFromEnv();
Optional<JdbcDatabaseContainer<?>> dockerContainer =
DockerContainerCreator.createDockerContainer(db);
SCHEMA_NAME = determineSchemaName(db);
if (dockerContainer.isPresent()) {
dockerContainer.get().start();
DATA_SOURCE = DockerContainerCreator.createDataSource(dockerContainer.get());
} else {
DATA_SOURCE = createDataSourceForH2();
SCHEMA_NAME = DEFAULT_SCHEMA_NAME;
}
}
@ -52,84 +50,19 @@ public final class DataSourceGenerator {
return SCHEMA_NAME;
}
private static DataSource createDataSourceFromProperties(String propertiesFileName) {
DataSource ds;
try (InputStream input = new FileInputStream(propertiesFileName)) {
Properties prop = new Properties();
prop.load(input);
boolean propertiesFileIsComplete = true;
String warningMessage = "";
String jdbcDriver = prop.getProperty("jdbcDriver");
if (jdbcDriver == null || jdbcDriver.length() == 0) {
propertiesFileIsComplete = false;
warningMessage += ", jdbcDriver property missing";
}
String jdbcUrl = prop.getProperty("jdbcUrl");
if (jdbcUrl == null || jdbcUrl.length() == 0) {
propertiesFileIsComplete = false;
warningMessage += ", jdbcUrl property missing";
}
String dbUserName = prop.getProperty("dbUserName");
if (dbUserName == null || dbUserName.length() == 0) {
propertiesFileIsComplete = false;
warningMessage += ", dbUserName property missing";
}
String dbPassword = prop.getProperty("dbPassword");
if (dbPassword == null || dbPassword.length() == 0) {
propertiesFileIsComplete = false;
warningMessage += ", dbPassword property missing";
}
if (propertiesFileIsComplete) {
ds =
new PooledDataSource(
Thread.currentThread().getContextClassLoader(),
jdbcDriver,
jdbcUrl,
dbUserName,
dbPassword);
((PooledDataSource) ds)
.forceCloseAll(); // otherwise, the MyBatis pool is not initialized correctly
} else {
LOGGER.warn("propertiesFile {} is incomplete {}", propertiesFileName, warningMessage);
LOGGER.warn("Using default Datasource for Test");
ds = createDataSourceForH2();
}
} catch (IOException e) {
LOGGER.warn("createDataSourceFromProperties caught Exception ", e);
LOGGER.warn("Using default Datasource for Test");
ds = createDataSourceForH2();
}
return ds;
private static String determineSchemaName(DB db) {
return db == DB.POSTGRES ? "taskana" : "TASKANA";
}
private static String getSchemaNameFromPropertiesObject(String propertiesFileName) {
String schemaName = DEFAULT_SCHEMA_NAME;
try (InputStream input = new FileInputStream(propertiesFileName)) {
Properties prop = new Properties();
prop.load(input);
boolean propertiesFileIsComplete = true;
String warningMessage = "";
schemaName = prop.getProperty("schemaName");
if (schemaName == null || schemaName.length() == 0) {
propertiesFileIsComplete = false;
warningMessage += ", schemaName property missing";
}
if (!propertiesFileIsComplete) {
LOGGER.warn("propertiesFile {} is incomplete {}", propertiesFileName, warningMessage);
LOGGER.warn("Using default Datasource for Test");
schemaName = DEFAULT_SCHEMA_NAME;
}
} catch (IOException e) {
LOGGER.warn("getSchemaNameFromPropertiesObject caught Exception ", e);
LOGGER.warn("Using default schemaName for Test");
private static DB retrieveDatabaseFromEnv() {
String property = System.getenv("DB");
DB db;
try {
db = DB.valueOf(property);
} catch (Exception ex) {
db = DB.H2;
}
return schemaName;
return db;
}
private static DataSource createDataSourceForH2() {

View File

@ -111,7 +111,7 @@
<version>${version.archunit}</version>
<scope>test</scope>
</dependency>
<dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>db2</artifactId>
<version>${version.testcontainers}</version>

View File

@ -28,8 +28,10 @@ import pro.taskana.task.api.models.TaskSummary;
import pro.taskana.task.internal.builder.TaskBuilder;
import pro.taskana.task.internal.jobs.helper.TaskUpdatePriorityWorker;
import pro.taskana.task.internal.models.TaskImpl;
import pro.taskana.workbasket.api.WorkbasketPermission;
import pro.taskana.workbasket.api.WorkbasketService;
import pro.taskana.workbasket.api.models.WorkbasketSummary;
import pro.taskana.workbasket.internal.builder.WorkbasketAccessItemBuilder;
@TaskanaIntegrationTest
class TaskUpdatePriorityWorkerAccTest {
@ -42,18 +44,29 @@ class TaskUpdatePriorityWorkerAccTest {
TaskSummary task1;
TaskSummary task2;
Task completedTask;
ClassificationSummary classificationSummary;
WorkbasketSummary workbasketSummary;
@WithAccessId(user = "admin")
@BeforeAll
void setUp(ClassificationService classificationService, WorkbasketService workbasketService)
throws Exception {
ClassificationSummary classificationSummary =
classificationSummary =
DefaultTestEntities.defaultTestClassification()
.buildAndStore(classificationService)
.asSummary();
WorkbasketSummary workbasketSummary =
workbasketSummary =
DefaultTestEntities.defaultTestWorkbasket().buildAndStore(workbasketService).asSummary();
// Currently, we have a bug: TSK-1736
// Because of that we need at least one WorkbasketAccessItem with the correct permissions.
// Otherwise, the DB2 will not work.
WorkbasketAccessItemBuilder.newWorkbasketAccessItem()
.workbasketId(workbasketSummary.getId())
.accessId("whatever")
.permission(WorkbasketPermission.READ)
.buildAndStore(workbasketService);
TaskBuilder taskBuilder =
TaskBuilder.newTask()
.classificationSummary(classificationSummary)
@ -127,16 +140,8 @@ class TaskUpdatePriorityWorkerAccTest {
@Test
@WithAccessId(user = "admin")
void should_executeBatch(
WorkbasketService workbasketService, ClassificationService classificationService)
throws Exception {
void should_executeBatch() throws Exception {
// given
ClassificationSummary classificationSummary =
DefaultTestEntities.defaultTestClassification()
.buildAndStore(classificationService)
.asSummary();
WorkbasketSummary workbasketSummary =
DefaultTestEntities.defaultTestWorkbasket().buildAndStore(workbasketService).asSummary();
Task oldTask =
TaskBuilder.newTask()
.classificationSummary(classificationSummary)

View File

@ -1,7 +1,8 @@
package testapi.extensions;
import static org.junit.platform.commons.support.AnnotationSupport.isAnnotated;
import static testapi.util.DockerContainerCreator.createDockerContainer;
import static pro.taskana.common.test.DockerContainerCreator.createDataSource;
import static pro.taskana.common.test.DockerContainerCreator.createDockerContainer;
import static testapi.util.ExtensionCommunicator.getClassLevelStore;
import static testapi.util.ExtensionCommunicator.isTopLevelClass;
@ -21,7 +22,6 @@ import org.testcontainers.containers.JdbcDatabaseContainer;
import testapi.CleanTaskanaContext;
import testapi.TaskanaEngineConfigurationModifier;
import testapi.WithServiceProvider;
import testapi.util.DockerContainerCreator;
import pro.taskana.common.internal.configuration.DB;
@ -47,7 +47,7 @@ public class TestContainerExtension implements AfterAllCallback, InvocationInter
.ifPresentOrElse(
container -> {
container.start();
store.put(STORE_DATA_SOURCE, DockerContainerCreator.createDataSource(container));
store.put(STORE_DATA_SOURCE, createDataSource(container));
store.put(STORE_CONTAINER, container);
},
() -> store.put(STORE_DATA_SOURCE, createDataSourceForH2()));
@ -87,7 +87,7 @@ public class TestContainerExtension implements AfterAllCallback, InvocationInter
}
private static DB retrieveDatabaseFromEnv() {
String property = System.getenv("db.type");
String property = System.getenv("DB");
DB db;
try {
db = DB.valueOf(property);