mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-06 15:44:49 +08:00
Added tests for JSONStorage
This commit is contained in:
parent
46bf6a12ab
commit
6e340073a2
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.delivery.domain.mutators;
|
||||
|
||||
import com.djrapitops.plan.settings.locale.Locale;
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<File> 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<String> lines = Files.lines(file.toPath())) {
|
||||
List<String> expected = Collections.singletonList(stored.json);
|
||||
List<String> 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<String> lines = Files.lines(file.toPath())) {
|
||||
List<String> expected = Collections.singletonList(stored.json);
|
||||
List<String> 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<String> lines = Files.lines(file.toPath())) {
|
||||
List<String> expected = Collections.singletonList(stored.json);
|
||||
List<String> 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<String> lines = Files.lines(file.toPath())) {
|
||||
List<String> expected = Collections.singletonList(stored.json);
|
||||
List<String> 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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user