Fix sonar detected bug and implement database tests

This commit is contained in:
Aurora Lahtela 2024-03-10 10:09:16 +02:00
parent 7014822edb
commit 5691a30988
8 changed files with 110 additions and 4 deletions

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.delivery.domain.datatransfer;
import com.djrapitops.plan.utilities.dev.Untrusted;
import java.util.Objects;
import java.util.UUID;
/**
@ -56,4 +57,26 @@ public class AllowlistBounce {
return lastTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AllowlistBounce bounce = (AllowlistBounce) o;
return getCount() == bounce.getCount() && getLastTime() == bounce.getLastTime() && Objects.equals(getPlayerUUID(), bounce.getPlayerUUID()) && Objects.equals(getPlayerName(), bounce.getPlayerName());
}
@Override
public int hashCode() {
return Objects.hash(getPlayerUUID(), getPlayerName(), getCount(), getLastTime());
}
@Override
public String toString() {
return "AllowlistBounce{" +
"playerUUID=" + playerUUID +
", playerName='" + playerName + '\'' +
", count=" + count +
", lastTime=" + lastTime +
'}';
}
}

View File

@ -111,7 +111,7 @@ public class AllowlistJSONResolver extends JSONResolver {
return jsonResolverService.resolve(timestamp, DataID.PLAYER_ALLOWLIST_BOUNCES, serverUUID,
theUUID -> Map.of(
"allowlist_bounces", database.query(AllowlistQueries.getBounces(serverUUID)),
"last_seen_by_uuid", database.query(SessionQueries.getLastSeen(serverUUID))
"last_seen_by_uuid", database.query(SessionQueries.lastSeen(serverUUID))
)
);
}

View File

@ -1015,7 +1015,7 @@ public class SessionQueries {
};
}
public static Query<Map<UUID, Long>> getLastSeen(ServerUUID serverUUID) {
public static Query<Map<UUID, Long>> lastSeen(ServerUUID serverUUID) {
String sql = SELECT + UsersTable.USER_UUID + ", MAX(" + SessionsTable.SESSION_END + ") as last_seen" +
FROM + SessionsTable.TABLE_NAME + " s" +
INNER_JOIN + UsersTable.TABLE_NAME + " u ON u." + UsersTable.ID + "=s." + SessionsTable.USER_ID +

View File

@ -43,6 +43,7 @@ public class RemoveEverythingTransaction extends Patch {
clearTable(WorldTimesTable.TABLE_NAME);
clearTable(SessionsTable.TABLE_NAME);
clearTable(JoinAddressTable.TABLE_NAME);
clearTable(AllowlistBounceTable.TABLE_NAME);
clearTable(WorldTable.TABLE_NAME);
clearTable(PingTable.TABLE_NAME);
clearTable(UserInfoTable.TABLE_NAME);

View File

@ -28,6 +28,7 @@ import com.djrapitops.plan.storage.database.transactions.patches.BadJoinAddressD
public interface DatabaseTestAggregate extends
ActivityIndexQueriesTest,
AllowlistQueriesTest,
DatabaseBackupTest,
ExtensionsDatabaseTest,
GeolocationQueriesTest,

View File

@ -0,0 +1,63 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.storage.database.queries;
import com.djrapitops.plan.delivery.domain.datatransfer.AllowlistBounce;
import com.djrapitops.plan.storage.database.DatabaseTestPreparer;
import com.djrapitops.plan.storage.database.queries.objects.AllowlistQueries;
import com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction;
import com.djrapitops.plan.storage.database.transactions.events.StoreAllowlistBounceTransaction;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import utilities.TestConstants;
import java.util.List;
import java.util.concurrent.ExecutionException;
import static org.junit.jupiter.api.Assertions.assertEquals;
public interface AllowlistQueriesTest extends DatabaseTestPreparer {
@Test
@DisplayName("plan_allowlist_bounce is empty")
default void allowListTableIsEmpty() {
List<AllowlistBounce> expected = List.of();
List<AllowlistBounce> result = db().query(AllowlistQueries.getBounces(serverUUID()));
assertEquals(expected, result);
}
@Test
@DisplayName("plan_allowlist_bounce is cleared by RemoveEverythingTransaction")
default void allowListTableIsEmptyAfterClear() throws ExecutionException, InterruptedException {
allowListBounceIsStored();
db().executeTransaction(new RemoveEverythingTransaction());
allowListTableIsEmpty();
}
@Test
@DisplayName("Allowlist bounce is stored")
default void allowListBounceIsStored() throws ExecutionException, InterruptedException {
AllowlistBounce bounce = new AllowlistBounce(TestConstants.PLAYER_ONE_UUID, TestConstants.PLAYER_ONE_NAME, 1, System.currentTimeMillis());
db().executeTransaction(new StoreAllowlistBounceTransaction(bounce.getPlayerUUID(), bounce.getPlayerName(), serverUUID(), bounce.getLastTime()))
.get();
List<AllowlistBounce> expected = List.of(bounce);
List<AllowlistBounce> result = db().query(AllowlistQueries.getBounces(serverUUID()));
assertEquals(expected, result);
}
}

View File

@ -37,6 +37,7 @@ import com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythi
import com.djrapitops.plan.storage.database.transactions.events.*;
import com.djrapitops.plan.utilities.java.Maps;
import net.playeranalytics.plugin.scheduling.TimeAmount;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import utilities.RandomData;
@ -485,4 +486,21 @@ public interface SessionQueriesTest extends DatabaseTestPreparer {
Map<String, Long> results = db().query(SessionQueries.playtimePerServer(Long.MIN_VALUE, Long.MAX_VALUE));
assertEquals(expected, results);
}
@Test
@DisplayName("Last seen query by server uuid groups last seen by player")
default void lastSeenByServerIsGroupedByPlayer() {
prepareForSessionSave();
List<FinishedSession> player1Sessions = RandomData.randomSessions(serverUUID(), worlds, playerUUID, player2UUID);
List<FinishedSession> player2Sessions = RandomData.randomSessions(serverUUID(), worlds, player2UUID, playerUUID);
player1Sessions.forEach(session -> db().executeTransaction(new StoreSessionTransaction(session)));
player2Sessions.forEach(session -> db().executeTransaction(new StoreSessionTransaction(session)));
long lastSeenP1 = new SessionsMutator(player1Sessions).toLastSeen();
long lastSeenP2 = new SessionsMutator(player2Sessions).toLastSeen();
Map<UUID, Long> expected = Map.of(playerUUID, lastSeenP1, player2UUID, lastSeenP2);
Map<UUID, Long> result = db().query(SessionQueries.lastSeen(serverUUID()));
assertEquals(expected, result);
}
}

View File

@ -57,12 +57,12 @@ const AllowlistBounceTable = ({bounces, lastSeen}) => {
order: [[1, "desc"]]
}
if (!preferencesLoaded) return <></>;
const rowKeyFunction = useCallback((row, column) => {
return row.player + "-" + (column ? JSON.stringify(column.data) : '');
}, []);
if (!preferencesLoaded) return <></>;
return (
<DataTablesTable id={"allowlist-bounce-table"} options={options} colorClass={"bg-orange"}
rowKeyFunction={rowKeyFunction}/>