Split Geolocation related queries to own test

This commit is contained in:
Risto Lahtela 2020-03-11 16:57:51 +02:00
parent 952cac1fbf
commit b49baa1275
9 changed files with 164 additions and 167 deletions

View File

@ -67,4 +67,14 @@ public class Ping extends DateObj<Double> {
public int hashCode() {
return Objects.hash(serverUUID, average, min, max);
}
@Override
public String toString() {
return "Ping{" +
"serverUUID=" + serverUUID +
", average=" + average +
", min=" + min +
", max=" + max +
'}';
}
}

View File

@ -80,19 +80,13 @@ public interface DatabaseTest extends DatabaseTestPreparer {
@Test
default void testTPSSaving() {
Random r = new Random();
List<TPS> expected = new ArrayList<>();
for (int i = 0; i < RandomData.randomInt(1, 5); i++) {
expected.add(new TPS(r.nextLong(), r.nextDouble(), r.nextInt(100000000), r.nextDouble(), r.nextLong(), r.nextInt(), r.nextInt(), r.nextLong()));
}
List<TPS> expected = RandomData.randomTPS();
for (TPS tps : expected) {
execute(DataStoreQueries.storeTPS(serverUUID(), tps));
}
commitTest();
forcePersistenceCheck();
assertEquals(expected, db().query(TPSQueries.fetchTPSDataOfServer(serverUUID())));
}
@ -106,20 +100,6 @@ public interface DatabaseTest extends DatabaseTestPreparer {
db().executeTransaction(new PlayerRegisterTransaction(player2UUID, () -> 123456789L, "Test"));
}
@Test
default void geoInformationIsStored() {
saveUserOne();
long time = System.currentTimeMillis();
GeoInfo expected = new GeoInfo("TestLocation", time);
saveGeoInfo(playerUUID, expected);
commitTest();
List<GeoInfo> result = db().query(GeoInfoQueries.fetchAllGeoInformation()).get(playerUUID);
assertEquals(Collections.singletonList(expected), result);
}
@Test
default void testNicknamesTable() {
saveUserOne();
@ -127,7 +107,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
Nickname expected = new Nickname("TestNickname", System.currentTimeMillis(), serverUUID());
db().executeTransaction(new NicknameStoreTransaction(playerUUID, expected, (uuid, name) -> false /* Not cached */));
db().executeTransaction(new NicknameStoreTransaction(playerUUID, expected, (uuid, name) -> true /* Cached */));
commitTest();
forcePersistenceCheck();
List<Nickname> nicknames = db().query(NicknameQueries.fetchNicknameDataOfPlayer(playerUUID));
assertEquals(1, nicknames.size());
@ -138,7 +118,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
default void webUserIsRegistered() {
WebUser expected = new WebUser(TestConstants.PLAYER_ONE_NAME, "RandomGarbageBlah", 0);
db().executeTransaction(new RegisterWebUserTransaction(expected));
commitTest();
forcePersistenceCheck();
Optional<WebUser> found = db().query(WebUserQueries.fetchWebUser(TestConstants.PLAYER_ONE_NAME));
assertTrue(found.isPresent());
@ -163,7 +143,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
String[] expected = {"Test", "Test2", "Test3"};
saveWorlds(expected);
commitTest();
forcePersistenceCheck();
Collection<String> result = db().query(LargeFetchQueries.fetchAllWorldNames()).get(serverUUID());
assertEquals(new HashSet<>(Arrays.asList(expected)), result);
@ -223,7 +203,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
execute(DataStoreQueries.storeSession(session));
commitTest();
forcePersistenceCheck();
Map<UUID, List<Session>> sessions = db().query(SessionQueries.fetchSessionsOfPlayer(playerUUID));
assertTrue(sessions.containsKey(serverUUID()));
@ -250,7 +230,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
execute(DataStoreQueries.storeSession(session));
commitTest();
forcePersistenceCheck();
Map<UUID, List<Session>> sessions = db().query(SessionQueries.fetchSessionsOfPlayer(playerUUID));
List<Session> savedSessions = sessions.get(serverUUID());
@ -317,7 +297,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
// Updates the name
db().executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> 0, "NewName"));
commitTest();
forcePersistenceCheck();
assertFalse(db().query(UserIdentifierQueries.fetchPlayerUUIDOf(TestConstants.PLAYER_ONE_NAME)).isPresent());
@ -334,7 +314,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
for (int i = 0; i < random + 1; i++) {
db().executeTransaction(new KickStoreTransaction(playerUUID));
}
commitTest();
forcePersistenceCheck();
OptionalAssert.equals(random + 2, db().query(BaseUserQueries.fetchBaseUserOfPlayer(playerUUID)).map(BaseUser::getTimesKicked));
}
@ -447,7 +427,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
session.setPlayerKills(createKills());
execute(DataStoreQueries.storeSession(session));
commitTest();
forcePersistenceCheck();
Map<UUID, List<Session>> sessions = db().query(SessionQueries.fetchSessionsOfServer(serverUUID()));
@ -521,7 +501,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
session.setPlayerKills(expected);
execute(DataStoreQueries.storeSession(session));
commitTest();
forcePersistenceCheck();
Map<UUID, List<Session>> sessions = db().query(SessionQueries.fetchSessionsOfPlayer(playerUUID));
List<Session> savedSessions = sessions.get(serverUUID());
@ -986,81 +966,6 @@ public interface DatabaseTest extends DatabaseTestPreparer {
assertEquals(expected, result);
}
@Test
default void serverGeolocationsAreCountedAppropriately() {
UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID();
UUID thirdUuid = UUID.randomUUID();
UUID fourthUuid = UUID.randomUUID();
UUID fifthUuid = UUID.randomUUID();
UUID sixthUuid = UUID.randomUUID();
Database database = db();
database.executeTransaction(new PlayerServerRegisterTransaction(firstUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(secondUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(thirdUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(fourthUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(fifthUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(sixthUuid, () -> 0L, "", serverUUID()));
saveGeoInfo(firstUuid, new GeoInfo("Norway", 0));
saveGeoInfo(firstUuid, new GeoInfo("Finland", 5));
saveGeoInfo(secondUuid, new GeoInfo("Sweden", 0));
saveGeoInfo(thirdUuid, new GeoInfo("Denmark", 0));
saveGeoInfo(fourthUuid, new GeoInfo("Denmark", 0));
saveGeoInfo(fifthUuid, new GeoInfo("Not Known", 0));
saveGeoInfo(sixthUuid, new GeoInfo("Local Machine", 0));
Map<String, Integer> got = database.query(GeoInfoQueries.serverGeolocationCounts(serverUUID()));
Map<String, Integer> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland.
expected.put("Finland", 1);
expected.put("Sweden", 1);
expected.put("Not Known", 1);
expected.put("Local Machine", 1);
expected.put("Denmark", 2);
assertEquals(expected, got);
}
@Test
default void networkGeolocationsAreCountedAppropriately() {
UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID();
UUID thirdUuid = UUID.randomUUID();
UUID fourthUuid = UUID.randomUUID();
UUID fifthUuid = UUID.randomUUID();
UUID sixthUuid = UUID.randomUUID();
Database db = db();
db.executeTransaction(new PlayerRegisterTransaction(firstUuid, () -> 0L, ""));
db.executeTransaction(new PlayerRegisterTransaction(secondUuid, () -> 0L, ""));
db.executeTransaction(new PlayerRegisterTransaction(thirdUuid, () -> 0L, ""));
saveGeoInfo(firstUuid, new GeoInfo("Norway", 0));
saveGeoInfo(firstUuid, new GeoInfo("Finland", 5));
saveGeoInfo(secondUuid, new GeoInfo("Sweden", 0));
saveGeoInfo(thirdUuid, new GeoInfo("Denmark", 0));
saveGeoInfo(fourthUuid, new GeoInfo("Denmark", 0));
saveGeoInfo(fifthUuid, new GeoInfo("Not Known", 0));
saveGeoInfo(sixthUuid, new GeoInfo("Local Machine", 0));
Map<String, Integer> got = db.query(GeoInfoQueries.networkGeolocationCounts());
Map<String, Integer> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland.
expected.put("Finland", 1);
expected.put("Sweden", 1);
expected.put("Not Known", 1);
expected.put("Local Machine", 1);
expected.put("Denmark", 2);
assertEquals(expected, got);
}
@Test
default void bungeeInformationIsStored() {
Optional<Server> bungeeInfo = db().query(ServerQueries.fetchProxyServerInformation());
@ -1070,7 +975,7 @@ public interface DatabaseTest extends DatabaseTestPreparer {
Server bungeeCord = new Server(-1, bungeeUUID, "BungeeCord", "Random:1234", 20);
db().executeTransaction(new StoreServerInformationTransaction(bungeeCord));
commitTest();
forcePersistenceCheck();
bungeeCord.setId(2);
@ -1092,55 +997,6 @@ public interface DatabaseTest extends DatabaseTestPreparer {
assertEquals(1, serverInformation.values().stream().filter(Server::isProxy).count());
}
@Test
default void pingIsGroupedByGeolocationAppropriately() {
UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID();
UUID thirdUuid = UUID.randomUUID();
UUID fourthUuid = UUID.randomUUID();
UUID fifthUuid = UUID.randomUUID();
UUID sixthUuid = UUID.randomUUID();
Database database = db();
database.executeTransaction(new PlayerServerRegisterTransaction(firstUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(secondUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(thirdUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(fourthUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(fifthUuid, () -> 0L, "", serverUUID()));
database.executeTransaction(new PlayerServerRegisterTransaction(sixthUuid, () -> 0L, "", serverUUID()));
saveGeoInfo(firstUuid, new GeoInfo("Norway", 0));
saveGeoInfo(firstUuid, new GeoInfo("Finland", 5));
saveGeoInfo(secondUuid, new GeoInfo("Sweden", 0));
saveGeoInfo(thirdUuid, new GeoInfo("Denmark", 0));
saveGeoInfo(fourthUuid, new GeoInfo("Denmark", 0));
saveGeoInfo(fifthUuid, new GeoInfo("Not Known", 0));
saveGeoInfo(sixthUuid, new GeoInfo("Local Machine", 0));
long time = System.currentTimeMillis();
List<DateObj<Integer>> ping = Collections.singletonList(new DateObj<>(time, 5));
database.executeTransaction(new PingStoreTransaction(firstUuid, serverUUID(), ping));
database.executeTransaction(new PingStoreTransaction(secondUuid, serverUUID(), ping));
database.executeTransaction(new PingStoreTransaction(thirdUuid, serverUUID(), ping));
database.executeTransaction(new PingStoreTransaction(fourthUuid, serverUUID(), ping));
database.executeTransaction(new PingStoreTransaction(fifthUuid, serverUUID(), ping));
database.executeTransaction(new PingStoreTransaction(sixthUuid, serverUUID(), ping));
Map<String, Ping> got = database.query(PingQueries.fetchPingDataOfServerByGeolocation(serverUUID()));
Map<String, Ping> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland.
Ping expectedPing = new Ping(time, serverUUID(), 5, 5, 5);
expected.put("Finland", expectedPing);
expected.put("Sweden", expectedPing);
expected.put("Not Known", expectedPing);
expected.put("Local Machine", expectedPing);
expected.put("Denmark", expectedPing);
assertEquals(expected, got);
}
@Test
default void registerDateIsMinimized() {
executeTransactions(

View File

@ -76,7 +76,7 @@ public interface DatabaseTestPreparer {
});
}
default void commitTest() {
default void forcePersistenceCheck() {
db().close();
db().init();
}

View File

@ -17,7 +17,8 @@
package com.djrapitops.plan.storage.database;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.storage.database.queries.analysis.ActivityIndexQueriesTest;
import com.djrapitops.plan.storage.database.queries.ActivityIndexQueriesTest;
import com.djrapitops.plan.storage.database.queries.GeolocationQueriesTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
@ -40,7 +41,8 @@ import java.util.UUID;
@ExtendWith(MockitoExtension.class)
public class H2Test implements DatabaseTest,
ExtensionsDatabaseTest,
ActivityIndexQueriesTest {
ActivityIndexQueriesTest,
GeolocationQueriesTest {
private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);

View File

@ -17,7 +17,8 @@
package com.djrapitops.plan.storage.database;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.storage.database.queries.analysis.ActivityIndexQueriesTest;
import com.djrapitops.plan.storage.database.queries.ActivityIndexQueriesTest;
import com.djrapitops.plan.storage.database.queries.GeolocationQueriesTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
@ -35,7 +36,7 @@ import java.util.UUID;
/**
* Tests for MySQL database.
* <p>
* These settings assume CI environment with MySQL service running.
* The setup assumes CI environment with MySQL service running.
* 'MYSQL_DB' database should be created before the test.
*
* @author Rsl1122
@ -46,7 +47,8 @@ import java.util.UUID;
@ExtendWith(MockitoExtension.class)
class MySQLTest implements DatabaseTest,
ExtensionsDatabaseTest,
ActivityIndexQueriesTest {
ActivityIndexQueriesTest,
GeolocationQueriesTest {
private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);

View File

@ -17,7 +17,8 @@
package com.djrapitops.plan.storage.database;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.storage.database.queries.analysis.ActivityIndexQueriesTest;
import com.djrapitops.plan.storage.database.queries.ActivityIndexQueriesTest;
import com.djrapitops.plan.storage.database.queries.GeolocationQueriesTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
@ -40,7 +41,8 @@ import java.util.UUID;
@ExtendWith(MockitoExtension.class)
public class SQLiteTest implements DatabaseTest,
ExtensionsDatabaseTest,
ActivityIndexQueriesTest {
ActivityIndexQueriesTest,
GeolocationQueriesTest {
private static final int TEST_PORT_NUMBER = RandomData.randomInt(9005, 9500);

View File

@ -1,10 +1,10 @@
package com.djrapitops.plan.storage.database.queries.analysis;
package com.djrapitops.plan.storage.database.queries;
import com.djrapitops.plan.delivery.domain.TablePlayer;
import com.djrapitops.plan.delivery.domain.mutators.ActivityIndex;
import com.djrapitops.plan.gathering.domain.Session;
import com.djrapitops.plan.storage.database.DatabaseTestPreparer;
import com.djrapitops.plan.storage.database.queries.DataStoreQueries;
import com.djrapitops.plan.storage.database.queries.analysis.ActivityIndexQueries;
import com.djrapitops.plan.storage.database.queries.objects.NetworkTablePlayersQuery;
import com.djrapitops.plan.storage.database.queries.objects.ServerTablePlayersQuery;
import com.djrapitops.plan.storage.database.queries.objects.SessionQueries;

View File

@ -0,0 +1,125 @@
package com.djrapitops.plan.storage.database.queries;
import com.djrapitops.plan.delivery.domain.DateObj;
import com.djrapitops.plan.gathering.domain.GeoInfo;
import com.djrapitops.plan.gathering.domain.Ping;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.DatabaseTestPreparer;
import com.djrapitops.plan.storage.database.queries.objects.GeoInfoQueries;
import com.djrapitops.plan.storage.database.queries.objects.PingQueries;
import com.djrapitops.plan.storage.database.transactions.events.GeoInfoStoreTransaction;
import com.djrapitops.plan.storage.database.transactions.events.PingStoreTransaction;
import com.djrapitops.plan.storage.database.transactions.events.PlayerServerRegisterTransaction;
import org.junit.jupiter.api.Test;
import utilities.RandomData;
import utilities.TestConstants;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public interface GeolocationQueriesTest extends DatabaseTestPreparer {
@Test
default void geoInformationIsStored() {
db().executeTransaction(new PlayerServerRegisterTransaction(playerUUID, () -> 1000L, TestConstants.PLAYER_ONE_NAME, serverUUID()));
List<GeoInfo> expected = RandomData.randomGeoInfo();
for (GeoInfo geoInfo : expected) {
save(playerUUID, geoInfo);
}
forcePersistenceCheck();
List<GeoInfo> result = db().query(GeoInfoQueries.fetchAllGeoInformation()).get(playerUUID);
assertEquals(expected, result);
}
default void save(UUID uuid, GeoInfo info) {
db().executeTransaction(new GeoInfoStoreTransaction(uuid, info));
}
@Test
default void serverGeolocationsAreCountedAppropriately() {
storeSpecificGeolocations();
Map<String, Integer> got = db().query(GeoInfoQueries.serverGeolocationCounts(serverUUID()));
Map<String, Integer> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland.
expected.put("Finland", 1);
expected.put("Sweden", 1);
expected.put("Not Known", 1);
expected.put("Local Machine", 1);
expected.put("Denmark", 2);
assertEquals(expected, got);
}
@Test
default void networkGeolocationsAreCountedAppropriately() {
storeSpecificGeolocations();
Map<String, Integer> got = db().query(GeoInfoQueries.networkGeolocationCounts());
Map<String, Integer> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland.
expected.put("Finland", 1);
expected.put("Sweden", 1);
expected.put("Not Known", 1);
expected.put("Local Machine", 1);
expected.put("Denmark", 2);
assertEquals(expected, got);
}
default UUID[] storeSpecificGeolocations() {
UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID();
UUID thirdUuid = UUID.randomUUID();
UUID fourthUuid = UUID.randomUUID();
UUID fifthUuid = UUID.randomUUID();
UUID sixthUuid = UUID.randomUUID();
UUID[] uuids = {firstUuid, secondUuid, thirdUuid, fourthUuid, fifthUuid, sixthUuid};
Database db = db();
for (UUID uuid : uuids) {
db.executeTransaction(new PlayerServerRegisterTransaction(uuid, () -> 0L, "", serverUUID()));
}
save(firstUuid, new GeoInfo("Norway", 0));
save(firstUuid, new GeoInfo("Finland", 5));
save(secondUuid, new GeoInfo("Sweden", 0));
save(thirdUuid, new GeoInfo("Denmark", 0));
save(fourthUuid, new GeoInfo("Denmark", 0));
save(fifthUuid, new GeoInfo("Not Known", 0));
save(sixthUuid, new GeoInfo("Local Machine", 0));
return uuids;
}
@Test
default void pingIsGroupedByGeolocationAppropriately() {
UUID[] uuids = storeSpecificGeolocations();
Database db = db();
long time = System.currentTimeMillis();
List<DateObj<Integer>> ping = Collections.singletonList(new DateObj<>(time, 5));
for (UUID uuid : uuids) {
db.executeTransaction(new PingStoreTransaction(uuid, serverUUID(), ping));
}
Map<String, Ping> got = db.query(PingQueries.fetchPingDataOfServerByGeolocation(serverUUID()));
Map<String, Ping> expected = new HashMap<>();
// first user has a more recent connection from Finland so their country should be counted as Finland.
Ping expectedPing = new Ping(time, serverUUID(), 5, 5, 5);
expected.put("Finland", expectedPing);
expected.put("Sweden", expectedPing);
expected.put("Not Known", expectedPing);
expected.put("Local Machine", expectedPing);
expected.put("Denmark", expectedPing);
assertEquals(expected, got);
}
}

View File

@ -56,7 +56,7 @@ public class RandomData {
public static List<TPS> randomTPS() {
List<TPS> test = new ArrayList<>();
for (int i = 0; i < 20; i++) {
for (int i = 0; i < randomInt(5, 100); i++) {
int randInt = r.nextInt();
long randLong = Math.abs(r.nextLong());
test.add(new TPS(randLong, randLong, randInt, randLong, randLong, randInt, randInt, randLong));