diff --git a/Plan/common/src/test/java/utilities/CIProperties.java b/Plan/common/src/test/java/utilities/CIProperties.java index 8fc15935e..61b5dbb8e 100644 --- a/Plan/common/src/test/java/utilities/CIProperties.java +++ b/Plan/common/src/test/java/utilities/CIProperties.java @@ -23,7 +23,13 @@ package utilities; */ public class CIProperties { + @Deprecated public static final String IS_CI_SERVICE = "TRAVIS"; + public static final String MYSQL_DATABASE = "MYSQL_DB"; + // Notice to CI configurer: This user should only have local access. + // (To prevent outside connections with CI user) + public static final String MYSQL_USER = "MYSQL_USER"; + public static final String MYSQL_PASS = "MYSQL_PASS"; public static final String CHROME_DRIVER = "CHROMEDRIVER"; private CIProperties() { diff --git a/Plan/common/src/test/java/utilities/DBPreparer.java b/Plan/common/src/test/java/utilities/DBPreparer.java new file mode 100644 index 000000000..dd32cb9b7 --- /dev/null +++ b/Plan/common/src/test/java/utilities/DBPreparer.java @@ -0,0 +1,101 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package utilities; + +import com.djrapitops.plan.api.exceptions.EnableException; +import com.djrapitops.plan.db.DBType; +import com.djrapitops.plan.db.Database; +import com.djrapitops.plan.db.SQLDB; +import com.djrapitops.plan.db.access.transactions.Transaction; +import com.djrapitops.plan.system.PlanSystem; +import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.settings.config.PlanConfig; +import com.djrapitops.plan.system.settings.paths.DatabaseSettings; +import com.djrapitops.plan.system.settings.paths.WebserverSettings; +import com.djrapitops.plugin.utilities.Format; +import com.djrapitops.plugin.utilities.Verify; +import com.google.common.util.concurrent.MoreExecutors; + +import java.util.Optional; + +public class DBPreparer { + + private final PlanSystem system; + private int testPortNumber; + + public DBPreparer(PlanSystem system, int testPortNumber) { + this.system = system; + this.testPortNumber = testPortNumber; + } + + public Optional prepareSQLite() throws EnableException { + String dbName = DBType.SQLITE.getName(); + return Optional.of(prepareDBByName(dbName)); + } + + public Optional prepareH2() throws EnableException { + String dbName = DBType.H2.getName(); + return Optional.of(prepareDBByName(dbName)); + } + + private SQLDB prepareDBByName(String dbName) throws EnableException { + PlanConfig config = system.getConfigSystem().getConfig(); + config.set(WebserverSettings.PORT, testPortNumber); + config.set(DatabaseSettings.TYPE, dbName); + system.enable(); + + DBSystem dbSystem = system.getDatabaseSystem(); + SQLDB db = (SQLDB) dbSystem.getActiveDatabaseByName(dbName); + db.setTransactionExecutorServiceProvider(MoreExecutors::newDirectExecutorService); + db.init(); + return db; + } + + public Optional prepareMySQL() throws EnableException { + String database = System.getenv(CIProperties.MYSQL_DATABASE); + String user = System.getenv(CIProperties.MYSQL_USER); + String pass = System.getenv(CIProperties.MYSQL_PASS); + if (Verify.containsNull(database, user, pass)) { + return Optional.empty(); + } + + // Attempt to Prevent SQL Injection with Environment variable. + String formattedDatabase = new Format(database) + .removeSymbols() + .toString(); + + String dbName = DBType.MYSQL.getName(); + + PlanConfig config = system.getConfigSystem().getConfig(); + config.set(DatabaseSettings.MYSQL_DATABASE, formattedDatabase); + config.set(DatabaseSettings.MYSQL_USER, user); + config.set(DatabaseSettings.MYSQL_PASS, pass); + config.set(DatabaseSettings.MYSQL_HOST, "127.0.0.1"); + config.set(DatabaseSettings.TYPE, dbName); + + SQLDB mysql = prepareDBByName(dbName); + mysql.executeTransaction(new Transaction() { + @Override + protected void performOperations() { + execute("DROP DATABASE " + formattedDatabase); + execute("CREATE DATABASE " + formattedDatabase); + execute("USE " + formattedDatabase); + } + }); + return Optional.of(mysql); + } +}