From 9e44000d2139fe8fbad5c220eb73649db84991e9 Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Wed, 14 Dec 2022 19:33:44 +0200 Subject: [PATCH] Fix setting boolean parameters dynamically Affects issues: - Fixed #2784 --- .../queries/QueryParameterSetter.java | 2 + ...xtensionQueryResultTableDataQueryTest.java | 83 +++++++++++++++++++ .../database/DatabaseTestAggregate.java | 4 +- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionQueryResultTableDataQueryTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java index cd1393230..6ace2ec9c 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/database/queries/QueryParameterSetter.java @@ -51,6 +51,8 @@ public class QueryParameterSetter { private static void setParameter(PreparedStatement statement, int index, Object parameter) throws SQLException { if (parameter == null) { statement.setNull(index, Types.VARCHAR); + } else if (parameter instanceof Boolean) { + statement.setBoolean(index, (Boolean) parameter); } else if (parameter instanceof Integer) { statement.setInt(index, (Integer) parameter); } else if (parameter instanceof Long) { diff --git a/Plan/common/src/test/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionQueryResultTableDataQueryTest.java b/Plan/common/src/test/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionQueryResultTableDataQueryTest.java new file mode 100644 index 000000000..4a2679e41 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/extension/implementation/storage/queries/ExtensionQueryResultTableDataQueryTest.java @@ -0,0 +1,83 @@ +/* + * 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 com.djrapitops.plan.extension.implementation.storage.queries; + +import com.djrapitops.plan.extension.CallEvents; +import com.djrapitops.plan.extension.ExtensionSvc; +import com.djrapitops.plan.extension.implementation.results.ExtensionTabData; +import com.djrapitops.plan.gathering.domain.DataMap; +import com.djrapitops.plan.gathering.domain.FinishedSession; +import com.djrapitops.plan.identification.ServerUUID; +import com.djrapitops.plan.storage.database.Database; +import com.djrapitops.plan.storage.database.DatabaseTestPreparer; +import com.djrapitops.plan.storage.database.queries.ExtensionsDatabaseTest; +import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries; +import com.djrapitops.plan.storage.database.transactions.events.PlayerRegisterTransaction; +import com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction; +import com.djrapitops.plan.storage.database.transactions.events.StoreWorldNameTransaction; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import utilities.RandomData; +import utilities.TestConstants; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public interface ExtensionQueryResultTableDataQueryTest extends DatabaseTestPreparer { + + @BeforeEach + default void unregisterExtension() { + ExtensionSvc extensionService = extensionService(); + extensionService.unregister(new ExtensionsDatabaseTest.PlayerExtension()); + } + + @Test + @DisplayName("Query result table query for extension data gets results") + default void extensionQueryResultTableQueryGetsResults() { + storeDataForTest(); + + Integer userIdOne = db().query(UserIdentifierQueries.fetchUserId(TestConstants.PLAYER_ONE_UUID)) + .orElseThrow(AssertionError::new); + Integer userIdTwo = db().query(UserIdentifierQueries.fetchUserId(TestConstants.PLAYER_TWO_UUID)) + .orElseThrow(AssertionError::new); + Map result = db().query(new ExtensionQueryResultTableDataQuery(serverUUID(), List.of(userIdOne, userIdTwo))); + assertEquals(Set.of(TestConstants.PLAYER_ONE_UUID, TestConstants.PLAYER_TWO_UUID), result.keySet()); + } + + private void storeDataForTest() { + ExtensionSvc extensionService = extensionService(); + extensionService.register(new ExtensionsDatabaseTest.PlayerExtension()); + + Database database = db(); + ServerUUID serverUUID = serverUUID(); + UUID uuid = TestConstants.PLAYER_ONE_UUID; + database.executeTransaction(new PlayerRegisterTransaction(uuid, RandomData::randomTime, TestConstants.PLAYER_ONE_NAME)); + database.executeTransaction(new PlayerRegisterTransaction(TestConstants.PLAYER_TWO_UUID, RandomData::randomTime, TestConstants.PLAYER_TWO_NAME)); + FinishedSession session = new FinishedSession(uuid, serverUUID, 1000L, 11000L, 500L, new DataMap()); + database.executeTransaction(new StoreWorldNameTransaction(serverUUID, "world")); + database.executeTransaction(new StoreSessionTransaction(session)); + + extensionService.updatePlayerValues(uuid, TestConstants.PLAYER_ONE_NAME, CallEvents.MANUAL); + extensionService.updatePlayerValues(TestConstants.PLAYER_TWO_UUID, TestConstants.PLAYER_TWO_NAME, CallEvents.MANUAL); + } + +} \ No newline at end of file diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTestAggregate.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTestAggregate.java index fcbd63488..bf07e4dfa 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTestAggregate.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/DatabaseTestAggregate.java @@ -16,6 +16,7 @@ */ package com.djrapitops.plan.storage.database; +import com.djrapitops.plan.extension.implementation.storage.queries.ExtensionQueryResultTableDataQueryTest; import com.djrapitops.plan.storage.database.queries.*; import com.djrapitops.plan.storage.database.queries.analysis.TopListQueriesTest; import com.djrapitops.plan.storage.database.transactions.commands.ChangeUserUUIDTransactionTest; @@ -37,6 +38,7 @@ public interface DatabaseTestAggregate extends FilterQueryTest, JoinAddressQueriesTest, ChangeUserUUIDTransactionTest, - CombineUserTransactionTest { + CombineUserTransactionTest, + ExtensionQueryResultTableDataQueryTest { /* Collects all query tests together so its easier to implement database tests */ }