diff --git a/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseState.java b/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseState.java index 348e0e3..1f79e13 100644 --- a/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseState.java +++ b/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseState.java @@ -1,11 +1,21 @@ package com.example.framework; +/** + * Defines a state of the database, which is defined by a set of SQL scripts. + */ public class DatabaseState { private final String stateName; private final String[] sqlscripts; + /** + * Constructor. + * + * @param stateName unique name of this database state. + * @param sqlscripts paths to SQL scripts within the classpath. These scripts will be executed to put the + * database into the database state described by this object. + */ public DatabaseState(String stateName, String... sqlscripts) { this.stateName = stateName; this.sqlscripts = sqlscripts; diff --git a/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStateHolder.java b/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStateHolder.java index 4fe63e4..a28cb37 100644 --- a/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStateHolder.java +++ b/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStateHolder.java @@ -1,13 +1,30 @@ package com.example.framework; +/** + * Holds the current database state. + */ public class DatabaseStateHolder { private static String currentDatabaseState; + /** + * Sets the database to the state with the specified name. + *

+ * WARNING: the database state is not thread safe. If there are multiple threads accessing + * the database in different states at the same time, apocalypse will come! + * + * @param databaseStateName the name of the {@link DatabaseState} to put the database in. + */ public static void setCurrentDatabaseState(String databaseStateName) { currentDatabaseState = databaseStateName; } + /** + * Returns the name of the current {@link DatabaseState}. + *

+ * WARNING: the database state is not thread safe. If there are multiple threads accessing + * the database in different states at the same time, apocalypse will come! + */ public static String getCurrentDatabaseState() { return currentDatabaseState; } diff --git a/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStatesInitializer.java b/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStatesInitializer.java index 13418c6..b34ae4e 100644 --- a/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStatesInitializer.java +++ b/pact-spring-data-rest-provider/src/test/java/com/example/framework/DatabaseStatesInitializer.java @@ -7,17 +7,34 @@ import javax.annotation.PostConstruct; import javax.sql.DataSource; import java.util.List; +/** + * Initializes a {@link DataSource} to a specified List of {@link DatabaseState}s. It is assumed that the {@link DataSource} + * can switch between several states. + */ public class DatabaseStatesInitializer { private final DataSource dataSource; private final List databaseStates; + /** + * Constructor. + * + * @param dataSource the {@link DataSource} to execute SQL scripts against. + * @param databaseStates the {@link DatabaseState}s to create within the {@link DataSource}. + */ public DatabaseStatesInitializer(DataSource dataSource, List databaseStates) { this.dataSource = dataSource; this.databaseStates = databaseStates; } + /** + * Executes SQL scripts to initialize the {@link DataSource} with several states. + *

+ * For each {@link DatabaseState}, the {@link DatabaseStateHolder} will be called to set the {@link DataSource} + * into that state. Then, the SQL scripts of that {@link DatabaseState} are executed against the {@link DataSource} + * to initialize that state. + */ @PostConstruct public void initialize() { for (DatabaseState databaseState : this.databaseStates) { diff --git a/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactDatabaseStatesAutoConfiguration.java b/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactDatabaseStatesAutoConfiguration.java index 922c12a..967fa7e 100644 --- a/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactDatabaseStatesAutoConfiguration.java +++ b/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactDatabaseStatesAutoConfiguration.java @@ -12,6 +12,22 @@ import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; +/** + *

+ * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration AutoConfiguration} that is activated when + * the pact-jvm-provider-junit module is in the classpath. + *

+ *

+ * This configuration provides a {@link DataSource} which allows to switch between multiple database states. + * Each database state is defined by a name and a set of SQL scripts which set the database into the desired state. + * The database states are configured via properties: + *

+ *   pact.databaseStates.<NAME>=/path/to/script1.sql,/path/to/script2.sql,...
+ * 
+ * The NAME of the databaseState can be used with {@link DatabaseStateHolder#setCurrentDatabaseState(String)} + * to set the {@link DataSource} into that state. + *

+ */ @Configuration @ConditionalOnClass(PactRunner.class) @EnableConfigurationProperties(PactProperties.class) diff --git a/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactProperties.java b/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactProperties.java index 8b8a552..688a026 100644 --- a/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactProperties.java +++ b/pact-spring-data-rest-provider/src/test/java/com/example/framework/PactProperties.java @@ -6,6 +6,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +/** + * Loads the properties "pact.databaseStates.<NAME>" into the Spring environment. + */ @ConfigurationProperties("pact") public class PactProperties { @@ -24,7 +27,7 @@ public class PactProperties { // keys and moves the actual key into the value, separated by a comma. Thus, we have all entries duplicated // and have to remove the entries with numeric keys. - if(!stateName.matches("^[0-9]+$")) { + if (!stateName.matches("^[0-9]+$")) { String sqlScriptsString = entry.getValue(); String[] sqlScripts = sqlScriptsString.split(","); databaseStatesList.add(new DatabaseState(stateName, sqlScripts)); diff --git a/pact-spring-data-rest-provider/src/test/java/com/example/framework/SpringBootStarterBuilder.java b/pact-spring-data-rest-provider/src/test/java/com/example/framework/SpringBootStarterBuilder.java deleted file mode 100644 index f1ae0d2..0000000 --- a/pact-spring-data-rest-provider/src/test/java/com/example/framework/SpringBootStarterBuilder.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.example.framework; - -import java.util.ArrayList; -import java.util.List; - -public class SpringBootStarterBuilder { - - private Class applicationClass; - - private List args = new ArrayList<>(); - - private List databaseStates = new ArrayList<>(); - - public SpringBootStarterBuilder withApplicationClass(Class clazz) { - this.applicationClass = clazz; - return this; - } - - public SpringBootStarterBuilder withArgument(String argument) { - this.args.add(argument); - return this; - } - - public SpringBootStarterBuilder withDatabaseState(String stateName, String... sqlScripts) { - this.databaseStates.add(new DatabaseState(stateName, sqlScripts)); - return this; - } - - public SpringBootStarter build() { - return new SpringBootStarter(this.applicationClass, this.databaseStates, args); - } - - -}