Skip to content

Commit

Permalink
Allow tests to use different database systems, such as MariaDB
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Nov 16, 2024
1 parent 5f662cb commit 9fc17d3
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
6 changes: 6 additions & 0 deletions FROST-Server.Tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
<version>1.20.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
*/
package de.fraunhofer.iosb.ilt.statests;

import static de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.ConnectionUtils.TAG_DB_DRIVER;
import static de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.ConnectionUtils.TAG_DB_PASSWRD;
import static de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.ConnectionUtils.TAG_DB_URL;
import static de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.utils.ConnectionUtils.TAG_DB_USERNAME;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.PREFIX_PERSISTENCE;
import static de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings.PREFIX_PLUGINS;
import static de.fraunhofer.iosb.ilt.frostserver.settings.PersistenceSettings.TAG_AUTO_UPDATE_DATABASE;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.fail;

Expand All @@ -31,6 +37,7 @@
import de.fraunhofer.iosb.ilt.frostserver.settings.BusSettings;
import de.fraunhofer.iosb.ilt.frostserver.settings.CoreSettings;
import de.fraunhofer.iosb.ilt.frostserver.settings.MqttSettings;
import de.fraunhofer.iosb.ilt.frostserver.settings.PersistenceSettings;
import de.fraunhofer.iosb.ilt.frostserver.util.Constants;
import de.fraunhofer.iosb.ilt.statests.c01sensingcore.Capability1CoreOnlyTests10;
import de.fraunhofer.iosb.ilt.statests.c01sensingcore.Capability1CoreOnlyTests11;
Expand Down Expand Up @@ -196,16 +203,19 @@ public class TestSuite {
public static final String KEY_DB_NAME = "dbName";

public static final String VAL_PERSISTENCE_MANAGER = "de.fraunhofer.iosb.ilt.frostserver.persistence.pgjooq.PostgresPersistenceManager";
public static final String VAL_ID_TYPE_DEFAULT = Constants.VALUE_ID_TYPE_UUID;
public static final String VAL_ID_TYPE_DEFAULT = Constants.VALUE_ID_TYPE_LONG;
public static final String VAL_ID_TYPE_LOCATIONS = Constants.VALUE_ID_TYPE_UUID;
public static final String VAL_ID_TYPE_OBSERVATIONS = Constants.VALUE_ID_TYPE_LONG;
public static final String VAL_ID_TYPE_OBSERVEDPROPERTIES = Constants.VALUE_ID_TYPE_LONG;
public static final String VAL_ID_TYPE_HIST_LOCATIONS = Constants.VALUE_ID_TYPE_LONG;
public static final String VAL_PG_DB = "sensorthings";
public static final String VAL_PG_USER = "sensorthings";
public static final String VAL_PG_PASS = "ChangeMe";

private static final String DATABASE_CONNECT_URL_BASE = "jdbc:tc:postgis:14-3.2:///";
private static final String DATABASE_CONNECT_URL_POSTFIX = "?TC_DAEMON=true&TC_INITSCRIPT=file:src/test/resources/pgInit.sql";
private static final String DB_PG_CONNECT_URL_BASE = "jdbc:tc:postgis:14-3.2:///";
private static final String DB_PG_CONNECT_URL_POSTFIX = "?TC_DAEMON=true&TC_INITSCRIPT=file:src/test/resources/pgInit.sql";
private static final String DB_MARIADB_CONNECT_URL_BASE = "jdbc:tc:mariadb:11.5.2:///";
private static final String DB_MARIADB_CONNECT_URL_POSTFIX = "?TC_DAEMON=true";

private static TestSuite instance;

Expand Down Expand Up @@ -361,16 +371,18 @@ private void startHttpServer(int key, Map<String, String> parameters) {
handler.setInitParameter(CoreSettings.TAG_TEMP_PATH, System.getProperty("java.io.tmpdir"));

handler.setInitParameter(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_DEFAULT, VAL_ID_TYPE_DEFAULT);
handler.setInitParameter(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_LOCATION, VAL_ID_TYPE_LOCATIONS);
handler.setInitParameter(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_OBSERVATION, VAL_ID_TYPE_OBSERVATIONS);
handler.setInitParameter(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_OBS_PROPERTY, VAL_ID_TYPE_OBSERVEDPROPERTIES);
handler.setInitParameter(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_HIST_LOCATION, VAL_ID_TYPE_HIST_LOCATIONS);

handler.setInitParameter("persistence.persistenceManagerImplementationClass", VAL_PERSISTENCE_MANAGER);
handler.setInitParameter("persistence.autoUpdateDatabase", "true");
handler.setInitParameter("persistence.db.driver", "org.postgresql.Driver");
handler.setInitParameter("persistence.db.url", createDbUrl(parameters.get(KEY_DB_NAME)));
handler.setInitParameter("persistence.db.username", VAL_PG_USER);
handler.setInitParameter("persistence.db.password", VAL_PG_PASS);
String dbDriver = paramsMap.computeIfAbsent(PREFIX_PERSISTENCE + TAG_DB_DRIVER, t -> "org.postgresql.Driver");
handler.setInitParameter(PREFIX_PERSISTENCE + PersistenceSettings.TAG_IMPLEMENTATION_CLASS, VAL_PERSISTENCE_MANAGER);
handler.setInitParameter(PREFIX_PERSISTENCE + TAG_AUTO_UPDATE_DATABASE, "true");
handler.setInitParameter(PREFIX_PERSISTENCE + TAG_DB_DRIVER, dbDriver);
handler.setInitParameter(PREFIX_PERSISTENCE + TAG_DB_URL, createDbUrl(dbDriver, parameters.get(KEY_DB_NAME)));
handler.setInitParameter(PREFIX_PERSISTENCE + TAG_DB_USERNAME, VAL_PG_USER);
handler.setInitParameter(PREFIX_PERSISTENCE + TAG_DB_PASSWRD, VAL_PG_PASS);

handler.setInitParameter("bus." + BusSettings.TAG_IMPLEMENTATION_CLASS, "de.fraunhofer.iosb.ilt.frostserver.messagebus.MqttMessageBus");
handler.setInitParameter("bus." + MqttMessageBus.TAG_MQTT_BROKER, "tcp://" + mqttBus.getHost() + ":" + mqttBus.getFirstMappedPort());
Expand Down Expand Up @@ -399,8 +411,12 @@ private void startHttpServer(int key, Map<String, String> parameters) {
serverSetting.initExtensionsAndTypes();
}

public static String createDbUrl(String dbName) {
return DATABASE_CONNECT_URL_BASE + dbName + DATABASE_CONNECT_URL_POSTFIX;
public static String createDbUrl(String type, String dbName) {
if (type.toLowerCase().contains("mariadb")) {
return DB_MARIADB_CONNECT_URL_BASE + dbName + DB_MARIADB_CONNECT_URL_POSTFIX;
} else {
return DB_PG_CONNECT_URL_BASE + dbName + DB_PG_CONNECT_URL_POSTFIX;
}
}

private void startMqttServer(int key, Map<String, String> parameters) throws IOException {
Expand Down Expand Up @@ -429,15 +445,18 @@ private void startMqttServer(int key, Map<String, String> parameters) throws IOE
properties.put("mqtt.WebsocketPort", "" + mqttWsPort);

properties.put(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_DEFAULT, VAL_ID_TYPE_DEFAULT);
properties.put(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_LOCATION, VAL_ID_TYPE_LOCATIONS);
properties.put(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_OBSERVATION, VAL_ID_TYPE_OBSERVATIONS);
properties.put(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_OBS_PROPERTY, VAL_ID_TYPE_OBSERVEDPROPERTIES);
properties.put(PREFIX_PLUGINS + CoreModelSettings.TAG_ID_TYPE_HIST_LOCATION, VAL_ID_TYPE_HIST_LOCATIONS);

properties.put("persistence.persistenceManagerImplementationClass", VAL_PERSISTENCE_MANAGER);
properties.put("persistence.db.driver", "org.postgresql.Driver");
properties.put("persistence.db.url", createDbUrl(parameters.get(KEY_DB_NAME)));
properties.put("persistence.db.username", VAL_PG_USER);
properties.put("persistence.db.password", VAL_PG_PASS);
String dbDriver = parameters.getOrDefault(PREFIX_PERSISTENCE + TAG_DB_DRIVER, "org.postgresql.Driver");
properties.put(PREFIX_PERSISTENCE + PersistenceSettings.TAG_IMPLEMENTATION_CLASS, VAL_PERSISTENCE_MANAGER);
properties.put(PREFIX_PERSISTENCE + TAG_AUTO_UPDATE_DATABASE, "true");
properties.put(PREFIX_PERSISTENCE + TAG_DB_DRIVER, dbDriver);
properties.put(PREFIX_PERSISTENCE + TAG_DB_URL, createDbUrl(dbDriver, parameters.get(KEY_DB_NAME)));
properties.put(PREFIX_PERSISTENCE + TAG_DB_USERNAME, VAL_PG_USER);
properties.put(PREFIX_PERSISTENCE + TAG_DB_PASSWRD, VAL_PG_PASS);
properties.put("bus." + BusSettings.TAG_IMPLEMENTATION_CLASS, "de.fraunhofer.iosb.ilt.frostserver.messagebus.MqttMessageBus");
properties.put("bus." + MqttMessageBus.TAG_MQTT_BROKER, "tcp://" + mqttBus.getHost() + ":" + mqttBus.getFirstMappedPort());
properties.putAll(parameters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public abstract class BasicAuthAnonReadTests extends AbstractAuthTests {
SERVER_PROPERTIES.put("auth.allowAnonymousRead", "true");
SERVER_PROPERTIES.put("auth.autoUpdateDatabase", "true");
final String dbName = "basicauth";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbName));
SERVER_PROPERTIES.put("auth.db.driver", "org.postgresql.Driver");
final String dbDriver = "org.postgresql.Driver";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbDriver, dbName));
SERVER_PROPERTIES.put("auth.db.driver", dbDriver);
SERVER_PROPERTIES.put("auth.db.username", TestSuite.VAL_PG_USER);
SERVER_PROPERTIES.put("auth.db.password", TestSuite.VAL_PG_PASS);
SERVER_PROPERTIES.put(KEY_DB_NAME, dbName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public abstract class BasicAuthCryptPwTests extends AbstractAuthTests {
SERVER_PROPERTIES.put("auth.autoUpdateDatabase", "true");
SERVER_PROPERTIES.put("auth.plainTextPassword", "false");
final String dbName = "hashauth";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbName));
SERVER_PROPERTIES.put("auth.db.driver", "org.postgresql.Driver");
final String dbDriver = "org.postgresql.Driver";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbDriver, dbName));
SERVER_PROPERTIES.put("auth.db.driver", dbDriver);
SERVER_PROPERTIES.put("auth.db.username", TestSuite.VAL_PG_USER);
SERVER_PROPERTIES.put("auth.db.password", TestSuite.VAL_PG_PASS);
SERVER_PROPERTIES.put(KEY_DB_NAME, dbName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ public abstract class BasicAuthTests extends AbstractAuthTests {
SERVER_PROPERTIES.put("auth.allowAnonymousRead", "false");
SERVER_PROPERTIES.put("auth.autoUpdateDatabase", "true");
final String dbName = "basicauth";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbName));
SERVER_PROPERTIES.put("auth.db.driver", "org.postgresql.Driver");
final String dbDriver = "org.postgresql.Driver";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbDriver, dbName));
SERVER_PROPERTIES.put("auth.db.driver", dbDriver);
SERVER_PROPERTIES.put("auth.db.username", TestSuite.VAL_PG_USER);
SERVER_PROPERTIES.put("auth.db.password", TestSuite.VAL_PG_PASS);
SERVER_PROPERTIES.put(KEY_DB_NAME, dbName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public class FineGrainedAuthTestsBasic extends FineGrainedAuthTests {
SERVER_PROPERTIES.put("auth.allowAnonymousRead", "false");
SERVER_PROPERTIES.put("auth.autoUpdateDatabase", "true");
final String dbName = "fineGrainedAuthBasic";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbName));
SERVER_PROPERTIES.put("auth.db.driver", "org.postgresql.Driver");
final String dbDriver = "org.postgresql.Driver";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbDriver, dbName));
SERVER_PROPERTIES.put("auth.db.driver", dbDriver);
SERVER_PROPERTIES.put("auth.db.username", TestSuite.VAL_PG_USER);
SERVER_PROPERTIES.put("auth.db.password", TestSuite.VAL_PG_PASS);
SERVER_PROPERTIES.put("auth.plainTextPassword", "false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public class FineGrainedAuthTestsKeycloak extends FineGrainedAuthTests {
static {
FineGrainedAuthTests.addCommonProperties(SERVER_PROPERTIES);
final String dbName = "fineGrainedAuthKeycloak";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbName));
SERVER_PROPERTIES.put("auth.db.driver", "org.postgresql.Driver");
final String dbDriver = "org.postgresql.Driver";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbDriver, dbName));
SERVER_PROPERTIES.put("auth.db.driver", dbDriver);
SERVER_PROPERTIES.put("auth.db.username", TestSuite.VAL_PG_USER);
SERVER_PROPERTIES.put("auth.db.password", TestSuite.VAL_PG_PASS);
SERVER_PROPERTIES.put("auth.plainTextPassword", "false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ private static String resourceUrl(String path, String name) {

static {
final String dbName = "keycloakauth";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbName));
SERVER_PROPERTIES.put("auth.db.driver", "org.postgresql.Driver");
final String dbDriver = "org.postgresql.Driver";
SERVER_PROPERTIES.put("auth.db.url", TestSuite.createDbUrl(dbDriver, dbName));
SERVER_PROPERTIES.put("auth.db.driver", dbDriver);
SERVER_PROPERTIES.put("auth.db.username", TestSuite.VAL_PG_USER);
SERVER_PROPERTIES.put("auth.db.password", TestSuite.VAL_PG_PASS);
SERVER_PROPERTIES.put(KEY_DB_NAME, dbName);
Expand Down

0 comments on commit 9fc17d3

Please sign in to comment.