From 6e340073a2cd9d132c41061279a754d427db2774 Mon Sep 17 00:00:00 2001
From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com>
Date: Sat, 30 Jan 2021 20:13:04 +0200
Subject: [PATCH] Added tests for JSONStorage
---
.../plan/storage/json/JSONStorage.java | 14 ++
.../domain/mutators/ActivityIndexTest.java | 16 ++
.../plan/storage/json/JSONStorageTest.java | 166 ++++++++++++++++++
3 files changed, 196 insertions(+)
create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/storage/json/JSONStorageTest.java
diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/json/JSONStorage.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/json/JSONStorage.java
index a8d91ac7d..9bda04511 100644
--- a/Plan/common/src/main/java/com/djrapitops/plan/storage/json/JSONStorage.java
+++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/json/JSONStorage.java
@@ -18,6 +18,7 @@ package com.djrapitops.plan.storage.json;
import com.google.gson.Gson;
+import java.util.Objects;
import java.util.Optional;
/**
@@ -57,5 +58,18 @@ public interface JSONStorage {
this.json = json;
this.timestamp = timestamp;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ StoredJSON that = (StoredJSON) o;
+ return timestamp == that.timestamp && Objects.equals(json, that.json);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(json, timestamp);
+ }
}
}
diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/domain/mutators/ActivityIndexTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/domain/mutators/ActivityIndexTest.java
index 0ec175d81..d503149cf 100644
--- a/Plan/common/src/test/java/com/djrapitops/plan/delivery/domain/mutators/ActivityIndexTest.java
+++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/domain/mutators/ActivityIndexTest.java
@@ -1,3 +1,19 @@
+/*
+ * 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.delivery.domain.mutators;
import com.djrapitops.plan.settings.locale.Locale;
diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/json/JSONStorageTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/json/JSONStorageTest.java
new file mode 100644
index 000000000..2d0cec4f7
--- /dev/null
+++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/json/JSONStorageTest.java
@@ -0,0 +1,166 @@
+/*
+ * 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.storage.json;
+
+import com.djrapitops.plan.storage.file.PlanFiles;
+import com.djrapitops.plugin.logging.console.TestPluginLogger;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.mockito.Mockito;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.Mockito.when;
+
+class JSONStorageTest {
+
+ private JSONStorage UNDER_TEST;
+ private Path tempDir;
+
+ @BeforeEach
+ void setUp(@TempDir Path tempDir) {
+ PlanFiles files = Mockito.mock(PlanFiles.class);
+ this.tempDir = tempDir;
+ when(files.getJSONStorageDirectory()).thenReturn(this.tempDir);
+
+ UNDER_TEST = new JSONFileStorage(files, new TestPluginLogger());
+ }
+
+ private Optional findTheFile() {
+ File[] files = tempDir.toFile().listFiles();
+ if (files != null) {
+ for (File file : files) {
+ return Optional.of(file);
+ }
+ }
+ return Optional.empty();
+ }
+
+ @Test
+ void stringDataIsStored() throws IOException {
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", "data");
+
+ File file = findTheFile().orElseThrow(AssertionError::new);
+ assertEquals("Identifier-" + stored.timestamp + ".json", file.getName());
+ try (Stream lines = Files.lines(file.toPath())) {
+ List expected = Collections.singletonList(stored.json);
+ List result = lines.collect(Collectors.toList());
+ assertEquals(expected, result);
+ }
+ }
+
+ @Test
+ void serializedDataIsStored() throws IOException {
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"));
+
+ File file = findTheFile().orElseThrow(AssertionError::new);
+ assertEquals("Identifier-" + stored.timestamp + ".json", file.getName());
+ try (Stream lines = Files.lines(file.toPath())) {
+ List expected = Collections.singletonList(stored.json);
+ List result = lines.collect(Collectors.toList());
+ assertEquals(expected, result);
+ }
+ }
+
+ @Test
+ void stringDataIsStoredWithTimestamp() throws IOException {
+ long timestamp = System.currentTimeMillis();
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", "data", timestamp);
+
+ File file = findTheFile().orElseThrow(AssertionError::new);
+ assertEquals(timestamp, stored.timestamp);
+ assertEquals("Identifier-" + timestamp + ".json", file.getName());
+ try (Stream lines = Files.lines(file.toPath())) {
+ List expected = Collections.singletonList(stored.json);
+ List result = lines.collect(Collectors.toList());
+ assertEquals(expected, result);
+ }
+ }
+
+ @Test
+ void serializedDataIsStoredWithTimestamp() throws IOException {
+ long timestamp = System.currentTimeMillis();
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"), timestamp);
+
+ File file = findTheFile().orElseThrow(AssertionError::new);
+ assertEquals(timestamp, stored.timestamp);
+ assertEquals("Identifier-" + timestamp + ".json", file.getName());
+ try (Stream lines = Files.lines(file.toPath())) {
+ List expected = Collections.singletonList(stored.json);
+ List result = lines.collect(Collectors.toList());
+ assertEquals(expected, result);
+ }
+ }
+
+ @Test
+ void anythingStartingWithIsFetched() throws IOException {
+ assertFalse(UNDER_TEST.fetchJSON("Identifier").isPresent());
+ stringDataIsStoredWithTimestamp();
+ JSONStorage.StoredJSON found = UNDER_TEST.fetchJSON("Identifier").orElseThrow(AssertionError::new);
+ assertEquals("data", found.json);
+ }
+
+ @Test
+ void storedWithExactDateIsFetched() {
+ long timestamp = System.currentTimeMillis();
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"), timestamp);
+ JSONStorage.StoredJSON found = UNDER_TEST.fetchExactJson("Identifier", timestamp).orElseThrow(AssertionError::new);
+ assertEquals(stored, found);
+ }
+
+ @Test
+ void storedWithLaterDateIsFetched() {
+ long timestamp = System.currentTimeMillis();
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"), timestamp);
+ JSONStorage.StoredJSON found = UNDER_TEST.fetchJsonMadeAfter("Identifier", timestamp - TimeUnit.DAYS.toMillis(1L)).orElseThrow(AssertionError::new);
+ assertEquals(stored, found);
+ }
+
+ @Test
+ void storedWithLaterDateIsNotFetched() {
+ long timestamp = System.currentTimeMillis();
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"), timestamp);
+ assertFalse(UNDER_TEST.fetchJsonMadeAfter("Identifier", timestamp + TimeUnit.DAYS.toMillis(1L)).isPresent());
+ }
+
+ @Test
+ void storedWithEarlierDateIsFetched() {
+ long timestamp = System.currentTimeMillis();
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"), timestamp);
+ JSONStorage.StoredJSON found = UNDER_TEST.fetchJsonMadeBefore("Identifier", timestamp + TimeUnit.DAYS.toMillis(1L)).orElseThrow(AssertionError::new);
+ assertEquals(stored, found);
+ }
+
+ @Test
+ void storedWithEarlierDateIsNotFetched() {
+ long timestamp = System.currentTimeMillis();
+ JSONStorage.StoredJSON stored = UNDER_TEST.storeJson("Identifier", Collections.singletonList("data"), timestamp);
+ assertFalse(UNDER_TEST.fetchJsonMadeBefore("Identifier", timestamp - TimeUnit.DAYS.toMillis(1L)).isPresent());
+ }
+}
\ No newline at end of file