Changed Database tests to JUnit 5

Affects issues:
- Close #1110
This commit is contained in:
Rsl1122 2019-08-06 14:35:13 +03:00
parent 239f087f41
commit 846cce0eed
4 changed files with 452 additions and 434 deletions

View File

@ -16,12 +16,19 @@
*/ */
package com.djrapitops.plan.db; package com.djrapitops.plan.db;
import org.junit.BeforeClass; import com.djrapitops.plan.system.PlanSystem;
import org.junit.Test; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import utilities.DBPreparer;
import utilities.RandomData;
import utilities.mocks.PluginMockComponent;
import static org.junit.Assert.assertEquals; import java.nio.file.Path;
import java.util.UUID;
/** /**
* Test for the H2 database * Test for the H2 database
@ -29,21 +36,34 @@ import static org.junit.Assert.assertEquals;
* @author Rsl1122, Fuzzlemann * @author Rsl1122, Fuzzlemann
* @see SQLiteTest * @see SQLiteTest
*/ */
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(JUnitPlatform.class)
public class H2Test extends CommonDBTest { @ExtendWith(MockitoExtension.class)
public class H2Test implements DatabaseTest {
@BeforeClass private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
public static void setUpClass() throws Exception {
handleSetup("H2"); private static PlanSystem system;
private static Database database;
@BeforeAll
static void setupDatabase(@TempDir Path temp) throws Exception {
system = new PluginMockComponent(temp).getPlanSystem();
database = new DBPreparer(system, TEST_PORT_NUMBER).prepareH2()
.orElseThrow(IllegalStateException::new);
} }
@Test @Override
public void testH2GetConfigName() { public Database db() {
assertEquals("h2", db.getType().getConfigName()); return database;
} }
@Test @Override
public void testH2GetName() { public UUID serverUUID() {
assertEquals("H2", db.getType().getName()); return system.getServerInfo().getServerUUID();
}
@Override
public PlanSystem system() {
return system;
} }
} }

View File

@ -18,20 +18,27 @@ package com.djrapitops.plan.db;
import com.djrapitops.plan.data.container.GeoInfo; import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.db.access.queries.ServerAggregateQueries; import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.access.transactions.events.PlayerRegisterTransaction; import com.djrapitops.plan.db.access.transactions.events.PlayerRegisterTransaction;
import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.settings.paths.DatabaseSettings; import org.junit.jupiter.api.Assumptions;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import utilities.CIProperties; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.junit.jupiter.MockitoExtension;
import utilities.DBPreparer;
import utilities.RandomData;
import utilities.mocks.PluginMockComponent;
import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assume.assumeTrue;
/** /**
* Tests for {@link MySQLDB}. * Tests for {@link MySQLDB}.
@ -41,37 +48,40 @@ import static org.junit.Assume.assumeTrue;
* *
* @author Rsl1122 * @author Rsl1122
*/ */
public class MySQLTest extends CommonDBTest { @RunWith(JUnitPlatform.class)
@ExtendWith(MockitoExtension.class)
class MySQLTest implements DatabaseTest {
@BeforeClass private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
public static void setUpDatabase() throws Exception {
boolean isCI = Boolean.parseBoolean(System.getenv(CIProperties.IS_CI_SERVICE));
assumeTrue(isCI);
PlanConfig config = component.getPlanSystem().getConfigSystem().getConfig(); private static PlanSystem system;
config.set(DatabaseSettings.MYSQL_DATABASE, "Plan"); private static Database database;
config.set(DatabaseSettings.MYSQL_USER, "travis");
config.set(DatabaseSettings.MYSQL_PASS, "");
config.set(DatabaseSettings.MYSQL_HOST, "127.0.0.1");
config.set(DatabaseSettings.TYPE, "MySQL");
handleSetup("MySQL"); @BeforeAll
clearDatabase(); static void setupDatabase(@TempDir Path temp) throws Exception {
system = new PluginMockComponent(temp).getPlanSystem();
Optional<Database> mysql = new DBPreparer(system, TEST_PORT_NUMBER).prepareMySQL();
Assumptions.assumeTrue(mysql.isPresent());
database = mysql.get();
} }
private static void clearDatabase() { @Override
db.executeTransaction(new Transaction() { public Database db() {
@Override return database;
protected void performOperations() { }
execute("DROP DATABASE Plan");
execute("CREATE DATABASE Plan"); @Override
execute("USE Plan"); public UUID serverUUID() {
} return system.getServerInfo().getServerUUID();
}); }
@Override
public PlanSystem system() {
return system;
} }
@Test @Test
public void networkGeolocationsAreCountedAppropriately() { void networkGeolocationsAreCountedAppropriately() {
UUID firstUuid = UUID.randomUUID(); UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID(); UUID secondUuid = UUID.randomUUID();
UUID thirdUuid = UUID.randomUUID(); UUID thirdUuid = UUID.randomUUID();
@ -79,9 +89,9 @@ public class MySQLTest extends CommonDBTest {
UUID fifthUuid = UUID.randomUUID(); UUID fifthUuid = UUID.randomUUID();
UUID sixthUuid = UUID.randomUUID(); UUID sixthUuid = UUID.randomUUID();
db.executeTransaction(new PlayerRegisterTransaction(firstUuid, () -> 0L, "")); database.executeTransaction(new PlayerRegisterTransaction(firstUuid, () -> 0L, ""));
db.executeTransaction(new PlayerRegisterTransaction(secondUuid, () -> 0L, "")); database.executeTransaction(new PlayerRegisterTransaction(secondUuid, () -> 0L, ""));
db.executeTransaction(new PlayerRegisterTransaction(thirdUuid, () -> 0L, "")); database.executeTransaction(new PlayerRegisterTransaction(thirdUuid, () -> 0L, ""));
saveGeoInfo(firstUuid, new GeoInfo("-", "Norway", 0)); saveGeoInfo(firstUuid, new GeoInfo("-", "Norway", 0));
saveGeoInfo(firstUuid, new GeoInfo("-", "Finland", 5)); saveGeoInfo(firstUuid, new GeoInfo("-", "Finland", 5));
@ -91,7 +101,7 @@ public class MySQLTest extends CommonDBTest {
saveGeoInfo(fifthUuid, new GeoInfo("-", "Not Known", 0)); saveGeoInfo(fifthUuid, new GeoInfo("-", "Not Known", 0));
saveGeoInfo(sixthUuid, new GeoInfo("-", "Local Machine", 0)); saveGeoInfo(sixthUuid, new GeoInfo("-", "Local Machine", 0));
Map<String, Integer> got = db.query(ServerAggregateQueries.networkGeolocationCounts()); Map<String, Integer> got = database.query(ServerAggregateQueries.networkGeolocationCounts());
Map<String, Integer> expected = new HashMap<>(); Map<String, Integer> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland. // first user has a more recent connection from Finland so their country should be counted as Finland.

View File

@ -21,74 +21,95 @@ import com.djrapitops.plan.db.access.queries.ServerAggregateQueries;
import com.djrapitops.plan.db.access.queries.objects.ServerQueries; import com.djrapitops.plan.db.access.queries.objects.ServerQueries;
import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction; import com.djrapitops.plan.db.access.transactions.StoreServerInformationTransaction;
import com.djrapitops.plan.db.access.transactions.events.PlayerRegisterTransaction; import com.djrapitops.plan.db.access.transactions.events.PlayerRegisterTransaction;
import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import org.junit.BeforeClass; import org.junit.jupiter.api.BeforeAll;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import utilities.DBPreparer;
import utilities.OptionalAssert; import utilities.OptionalAssert;
import utilities.RandomData;
import utilities.mocks.PluginMockComponent;
import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.*;
/** /**
* @author Rsl1122 * @author Rsl1122
*/ */
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(JUnitPlatform.class)
public class SQLiteTest extends CommonDBTest { @ExtendWith(MockitoExtension.class)
public class SQLiteTest implements DatabaseTest {
@BeforeClass private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);
public static void setUpClass() throws Exception {
handleSetup("SQLite"); private static PlanSystem system;
private static Database database;
@BeforeAll
static void setupDatabase(@TempDir Path temp) throws Exception {
system = new PluginMockComponent(temp).getPlanSystem();
database = new DBPreparer(system, TEST_PORT_NUMBER).prepareSQLite()
.orElseThrow(IllegalStateException::new);
}
@Override
public Database db() {
return database;
}
@Override
public UUID serverUUID() {
return system.getServerInfo().getServerUUID();
}
@Override
public PlanSystem system() {
return system;
} }
@Test @Test
public void testSQLiteGetConfigName() { void testServerTableBungeeSave() {
assertEquals("sqlite", db.getType().getConfigName()); Optional<Server> bungeeInfo = database.query(ServerQueries.fetchProxyServerInformation());
}
@Test
public void testSQLiteGetName() {
assertEquals("SQLite", db.getType().getName());
}
@Test
public void testServerTableBungeeSave() {
Optional<Server> bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation());
assertFalse(bungeeInfo.isPresent()); assertFalse(bungeeInfo.isPresent());
UUID bungeeUUID = UUID.randomUUID(); UUID bungeeUUID = UUID.randomUUID();
Server bungeeCord = new Server(-1, bungeeUUID, "BungeeCord", "Random:1234", 20); Server bungeeCord = new Server(-1, bungeeUUID, "BungeeCord", "Random:1234", 20);
db.executeTransaction(new StoreServerInformationTransaction(bungeeCord)); database.executeTransaction(new StoreServerInformationTransaction(bungeeCord));
commitTest(); commitTest();
bungeeCord.setId(2); bungeeCord.setId(2);
bungeeInfo = db.query(ServerQueries.fetchProxyServerInformation()); bungeeInfo = database.query(ServerQueries.fetchProxyServerInformation());
assertTrue(bungeeInfo.isPresent()); assertTrue(bungeeInfo.isPresent());
assertEquals(bungeeCord, bungeeInfo.get()); assertEquals(bungeeCord, bungeeInfo.get());
Optional<Server> found = db.query(ServerQueries.fetchServerMatchingIdentifier(bungeeUUID)); Optional<Server> found = database.query(ServerQueries.fetchServerMatchingIdentifier(bungeeUUID));
OptionalAssert.equals(2, found.map(Server::getId)); OptionalAssert.equals(2, found.map(Server::getId));
} }
@Test @Test
public void testServerTableBungee() { void testServerTableBungee() {
testServerTableBungeeSave(); testServerTableBungeeSave();
Map<UUID, Server> serverInformation = db.query(ServerQueries.fetchPlanServerInformation()); Map<UUID, Server> serverInformation = database.query(ServerQueries.fetchPlanServerInformation());
assertEquals(1, serverInformation.values().stream().filter(Server::isNotProxy).count()); assertEquals(1, serverInformation.values().stream().filter(Server::isNotProxy).count());
assertEquals(1, serverInformation.values().stream().filter(Server::isProxy).count()); assertEquals(1, serverInformation.values().stream().filter(Server::isProxy).count());
} }
@Test @Test
public void networkGeolocationsAreCountedAppropriately() { void networkGeolocationsAreCountedAppropriately() {
UUID firstUuid = UUID.randomUUID(); UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID(); UUID secondUuid = UUID.randomUUID();
UUID thirdUuid = UUID.randomUUID(); UUID thirdUuid = UUID.randomUUID();
@ -96,9 +117,9 @@ public class SQLiteTest extends CommonDBTest {
UUID fifthUuid = UUID.randomUUID(); UUID fifthUuid = UUID.randomUUID();
UUID sixthUuid = UUID.randomUUID(); UUID sixthUuid = UUID.randomUUID();
db.executeTransaction(new PlayerRegisterTransaction(firstUuid, () -> 0L, "")); database.executeTransaction(new PlayerRegisterTransaction(firstUuid, () -> 0L, ""));
db.executeTransaction(new PlayerRegisterTransaction(secondUuid, () -> 0L, "")); database.executeTransaction(new PlayerRegisterTransaction(secondUuid, () -> 0L, ""));
db.executeTransaction(new PlayerRegisterTransaction(thirdUuid, () -> 0L, "")); database.executeTransaction(new PlayerRegisterTransaction(thirdUuid, () -> 0L, ""));
saveGeoInfo(firstUuid, new GeoInfo("-", "Norway", 0)); saveGeoInfo(firstUuid, new GeoInfo("-", "Norway", 0));
saveGeoInfo(firstUuid, new GeoInfo("-", "Finland", 5)); saveGeoInfo(firstUuid, new GeoInfo("-", "Finland", 5));
@ -108,7 +129,7 @@ public class SQLiteTest extends CommonDBTest {
saveGeoInfo(fifthUuid, new GeoInfo("-", "Not Known", 0)); saveGeoInfo(fifthUuid, new GeoInfo("-", "Not Known", 0));
saveGeoInfo(sixthUuid, new GeoInfo("-", "Local Machine", 0)); saveGeoInfo(sixthUuid, new GeoInfo("-", "Local Machine", 0));
Map<String, Integer> got = db.query(ServerAggregateQueries.networkGeolocationCounts()); Map<String, Integer> got = database.query(ServerAggregateQueries.networkGeolocationCounts());
Map<String, Integer> expected = new HashMap<>(); Map<String, Integer> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland. // first user has a more recent connection from Finland so their country should be counted as Finland.