From 19f85afea3858fe854c20b67700dea9351a2c2f5 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Tue, 5 Mar 2019 16:47:55 +0200 Subject: [PATCH] [#948] Fixed Server JSON export --- .../store/containers/PerServerContainer.java | 5 +- .../containers/AllPlayerContainersQuery.java | 5 +- .../info/request/AnalysisExportTest.java | 110 ++++++++++++++++++ 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/system/info/request/AnalysisExportTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/data/store/containers/PerServerContainer.java b/Plan/common/src/main/java/com/djrapitops/plan/data/store/containers/PerServerContainer.java index 537ee8876..77ddb615d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/data/store/containers/PerServerContainer.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/data/store/containers/PerServerContainer.java @@ -60,11 +60,12 @@ public class PerServerContainer extends HashMap { container.putSupplier(PerServerKeys.WORLD_TIMES, () -> SessionsMutator.forContainer(container).toTotalWorldTimes()); container.putSupplier(PerServerKeys.PLAYER_DEATHS, () -> SessionsMutator.forContainer(container).toPlayerDeathList()); container.putSupplier(PerServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList()); - container.putSupplier(PerServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(PerServerKeys.PLAYER_KILLS).size()); + container.putSupplier(PerServerKeys.PLAYER_KILL_COUNT, () -> container.getValue(PerServerKeys.PLAYER_KILLS).map(Collection::size).orElse(0)); container.putSupplier(PerServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount()); container.putSupplier(PerServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount()); + container.putSupplier(PerServerKeys.PLAYER_DEATH_COUNT, () -> SessionsMutator.forContainer(container).toPlayerDeathCount()); container.putSupplier(PerServerKeys.MOB_DEATH_COUNT, () -> - container.getUnsafe(PerServerKeys.DEATH_COUNT) - container.getUnsafe(PerServerKeys.PLAYER_DEATH_COUNT) + container.getValue(PerServerKeys.DEATH_COUNT).orElse(0) - container.getValue(PerServerKeys.PLAYER_DEATH_COUNT).orElse(0) ); } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java index 137f94bdb..a03cc51dc 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/queries/containers/AllPlayerContainersQuery.java @@ -96,11 +96,12 @@ public class AllPlayerContainersQuery implements Query> { container.putSupplier(PerServerKeys.WORLD_TIMES, () -> SessionsMutator.forContainer(container).toTotalWorldTimes()); container.putSupplier(PerServerKeys.PLAYER_DEATHS, () -> SessionsMutator.forContainer(container).toPlayerDeathList()); container.putSupplier(PerServerKeys.PLAYER_KILLS, () -> SessionsMutator.forContainer(container).toPlayerKillList()); - container.putSupplier(PerServerKeys.PLAYER_KILL_COUNT, () -> container.getUnsafe(PerServerKeys.PLAYER_KILLS).size()); + container.putSupplier(PerServerKeys.PLAYER_KILL_COUNT, () -> container.getValue(PerServerKeys.PLAYER_KILLS).map(Collection::size).orElse(0)); container.putSupplier(PerServerKeys.MOB_KILL_COUNT, () -> SessionsMutator.forContainer(container).toMobKillCount()); container.putSupplier(PerServerKeys.DEATH_COUNT, () -> SessionsMutator.forContainer(container).toDeathCount()); + container.putSupplier(PerServerKeys.PLAYER_DEATH_COUNT, () -> SessionsMutator.forContainer(container).toPlayerDeathCount()); container.putSupplier(PerServerKeys.MOB_DEATH_COUNT, () -> - container.getUnsafe(PerServerKeys.DEATH_COUNT) - container.getUnsafe(PerServerKeys.PLAYER_DEATH_COUNT) + container.getValue(PerServerKeys.DEATH_COUNT).orElse(0) - container.getValue(PerServerKeys.PLAYER_DEATH_COUNT).orElse(0) ); perServerContainer.put(serverUUID, container); perServerContainers.put(playerUUID, perServerContainer); diff --git a/Plan/common/src/test/java/com/djrapitops/plan/system/info/request/AnalysisExportTest.java b/Plan/common/src/test/java/com/djrapitops/plan/system/info/request/AnalysisExportTest.java new file mode 100644 index 000000000..44e8bfcb9 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/system/info/request/AnalysisExportTest.java @@ -0,0 +1,110 @@ +/* + * 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.system.info.request; + +import com.djrapitops.plan.api.exceptions.connection.WebException; +import com.djrapitops.plan.data.container.Session; +import com.djrapitops.plan.db.Database; +import com.djrapitops.plan.db.access.transactions.events.PlayerRegisterTransaction; +import com.djrapitops.plan.db.access.transactions.events.SessionEndTransaction; +import com.djrapitops.plan.db.access.transactions.events.WorldNameStoreTransaction; +import com.djrapitops.plan.system.PlanSystem; +import com.djrapitops.plan.system.settings.config.PlanConfig; +import com.djrapitops.plan.system.settings.paths.ExportSettings; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import utilities.TestConstants; +import utilities.dagger.DaggerPlanPluginComponent; +import utilities.dagger.PlanPluginComponent; +import utilities.mocks.PlanPluginMocker; + +import java.io.File; +import java.nio.file.Path; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Test against Server JSON Export errors. + * + * @author Rsl1122 + */ +class AnalysisExportTest { + + private PlanSystem system; + + @BeforeEach + void setupPlanSystem(@TempDir Path dir) throws Exception { + PlanPluginComponent component = DaggerPlanPluginComponent.builder().plan(PlanPluginMocker.setUp() + .withDataFolder(dir.toFile()).withLogging() + .withResourceFetchingFromJar() + .getPlanMock() + ).build(); + + system = component.system(); + PlanConfig config = system.getConfigSystem().getConfig(); + config.set(ExportSettings.JSON_EXPORT_PATH, "Test"); + config.set(ExportSettings.SERVER_JSON, true); + system.enable(); + + Database database = system.getDatabaseSystem().getDatabase(); + storeSomeData(database); + } + + private void storeSomeData(Database database) { + UUID uuid = TestConstants.PLAYER_ONE_UUID; + database.executeTransaction(new PlayerRegisterTransaction(uuid, () -> 1000L, "name")); + Session session = new Session(uuid, TestConstants.SERVER_UUID, 1000L, "world", "SURVIVAL"); + session.endSession(11000L); + database.executeTransaction(new WorldNameStoreTransaction(TestConstants.SERVER_UUID, "world")); + database.executeTransaction(new SessionEndTransaction(session)); + } + + @AfterEach + void tearDownSystem() { + system.disable(); + } + + @Test + void serverJSONIsExported() throws WebException { + system.getInfoSystem().generateAnalysisPage(TestConstants.SERVER_UUID); + + File exportFolder = system.getPlanFiles().getFileFromPluginFolder("Test"); + File[] folders = exportFolder.listFiles(); + assertNotNull(folders); + + boolean found = false; + for (File folder : folders) { + if (folder.isFile()) { + continue; + } + if (folder.getName().equals("server")) { + for (File file : folder.listFiles()) { + if (file.getName().contains("Test.json")) { + found = true; + } + } + } + } + + assertTrue(found); + } + +} \ No newline at end of file