Wrote dynamic tests for placeholders

Affects issues:
- Close #2076
This commit is contained in:
Aurora Lahtela 2022-05-28 09:25:15 +03:00
parent 253cfd251b
commit 38b67272a6
4 changed files with 184 additions and 5 deletions

View File

@ -121,9 +121,11 @@ public final class PlanPlaceholders {
return Objects.toString(staticLoader.apply(arguments));
}
UUID playerUUID = arguments.get(0).flatMap(this::getPlayerUUIDForIdentifier).orElse(uuid);
UUID playerUUID = arguments.get(0)
.flatMap(this::getPlayerUUIDForIdentifier)
.orElse(uuid);
PlayerContainer player;
if (uuid != null) {
if (playerUUID != null) {
player = dbSystem.getDatabase().query(ContainerFetchQueries.fetchPlayerContainer(playerUUID));
} else {
player = null;
@ -144,4 +146,21 @@ public final class PlanPlaceholders {
public interface PlayerPlaceholderLoader extends BiFunction<PlayerContainer, List<String>, Serializable> {}
public interface StaticPlaceholderLoader extends Function<Arguments, Serializable> {}
public List<String> getRegisteredServerPlaceholders() {
List<String> placeholders = new ArrayList<>();
placeholders.addAll(staticPlaceholders.keySet());
placeholders.addAll(rawHandlers.keySet());
Collections.sort(placeholders);
return placeholders;
}
public List<String> getRegisteredPlayerPlaceholders() {
List<String> placeholders = new ArrayList<>(playerPlaceholders.keySet());
Collections.sort(placeholders);
return placeholders;
}
}

View File

@ -32,6 +32,7 @@ import com.djrapitops.plan.storage.database.queries.objects.*;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.NavigableMap;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@ -137,8 +138,10 @@ public class SessionPlaceHolders implements Placeholders {
placeholders.registerStatic("sessions_unique_players_day",
parameters -> database.query(PlayerCountQueries.uniquePlayerCount(dayAgo(), now(), getServerUUID(parameters))));
placeholders.registerStatic("sessions_unique_players_today",
parameters -> database.query(PlayerCountQueries.uniquePlayerCounts(dayAgo(), now(), config.getTimeZone().getOffset(now()), getServerUUID(parameters)))
.lastEntry().getValue());
parameters -> {
NavigableMap<Long, Integer> playerCounts = database.query(PlayerCountQueries.uniquePlayerCounts(dayAgo(), now(), config.getTimeZone().getOffset(now()), getServerUUID(parameters)));
return playerCounts.isEmpty() ? 0 : playerCounts.lastEntry().getValue();
});
placeholders.registerStatic("sessions_unique_players_week",
parameters -> database.query(PlayerCountQueries.uniquePlayerCount(weekAgo(), now(), getServerUUID(parameters))));
placeholders.registerStatic("sessions_unique_players_month",
@ -217,7 +220,9 @@ public class SessionPlaceHolders implements Placeholders {
}
private ServerUUID getServerUUID(Arguments parameters) {
return parameters.get(0).flatMap(this::getServerUUIDForServerIdentifier).orElseGet(serverInfo::getServerUUID);
return parameters.get(0)
.flatMap(this::getServerUUIDForServerIdentifier)
.orElseGet(serverInfo::getServerUUID);
}
private Optional<ServerUUID> getServerUUIDForServerIdentifier(String serverIdentifier) {

View File

@ -0,0 +1,152 @@
package com.djrapitops.plan.placeholder;
import com.djrapitops.plan.PlanSystem;
import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.transactions.events.PlayerServerRegisterTransaction;
import com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction;
import com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.io.TempDir;
import utilities.RandomData;
import utilities.TestConstants;
import utilities.dagger.PlanPluginComponent;
import utilities.mocks.PluginMockComponent;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.*;
class PlanPlaceholdersTest {
private static PlanPluginComponent component;
private static PlanPlaceholders underTest;
private static ServerUUID serverUUID;
private static UUID playerUUID;
@BeforeAll
static void prepareSystem(@TempDir Path tempDir) throws Exception {
component = new PluginMockComponent(tempDir).getComponent();
component.system().enable();
serverUUID = component.system().getServerInfo().getServerUUID();
underTest = component.placeholders();
playerUUID = UUID.randomUUID();
storeSomeData();
}
private static void storeSomeData() {
Database database = component.system().getDatabaseSystem().getDatabase();
database.executeTransaction(new PlayerServerRegisterTransaction(
playerUUID,
System::currentTimeMillis,
RandomData.randomString(5),
serverUUID,
() -> RandomData.randomString(5)
));
database.executeTransaction(new PlayerServerRegisterTransaction(
TestConstants.PLAYER_TWO_UUID,
System::currentTimeMillis,
TestConstants.PLAYER_TWO_NAME,
serverUUID,
() -> RandomData.randomString(5)
));
String worldName = RandomData.randomString(10);
database.executeTransaction(new WorldNameStoreTransaction(serverUUID, worldName));
database.executeTransaction(new StoreSessionTransaction(RandomData.randomSession(serverUUID, new String[]{worldName}, playerUUID, TestConstants.PLAYER_TWO_UUID)));
}
@AfterAll
static void clearSystem() {
if (component != null) {
PlanSystem system = component.system();
if (system != null) {
system.disable();
}
}
}
@TestFactory
@DisplayName("Server placeholders return something")
Collection<DynamicTest> testServerPlaceholders() {
return underTest.getRegisteredServerPlaceholders().stream()
.map(placeholder -> DynamicTest.dynamicTest("'" + placeholder + "' returns something", () -> {
String result = underTest.onPlaceholderRequest(UUID.randomUUID(), placeholder, Collections.emptyList());
System.out.println("Placeholder '" + placeholder + "' was replaced with: '" + result + "'");
assertNotNull(result);
assertNotEquals(placeholder, result);
}))
.collect(Collectors.toList());
}
@TestFactory
@DisplayName("Server placeholders return something on console")
Collection<DynamicTest> testServerPlaceholdersOnConsole() {
return underTest.getRegisteredServerPlaceholders().stream()
.map(placeholder -> DynamicTest.dynamicTest("'" + placeholder + "' returns something", () -> {
String result = underTest.onPlaceholderRequest(null, placeholder, Collections.emptyList());
System.out.println("Placeholder '" + placeholder + "' was replaced with: '" + result + "'");
assertNotNull(result);
assertNotEquals(placeholder, result);
}))
.collect(Collectors.toList());
}
@TestFactory
@DisplayName("Server placeholders return something for another server")
Collection<DynamicTest> testServerPlaceholdersWithParameter() {
return underTest.getRegisteredServerPlaceholders().stream()
.map(placeholder -> DynamicTest.dynamicTest("'" + placeholder + ":<server>' returns something", () -> {
String result = underTest.onPlaceholderRequest(UUID.randomUUID(), placeholder, List.of(serverUUID.toString()));
System.out.println("Placeholder '" + placeholder + ":" + serverUUID.toString() + "' was replaced with: '" + result + "'");
assertNotNull(result);
assertNotEquals(placeholder, result);
}))
.collect(Collectors.toList());
}
@TestFactory
@DisplayName("Player placeholders return something")
Collection<DynamicTest> testPlayerPlaceholders() {
return underTest.getRegisteredPlayerPlaceholders().stream()
.map(placeholder -> DynamicTest.dynamicTest("'" + placeholder + "' returns something", () -> {
String result = underTest.onPlaceholderRequest(playerUUID, placeholder, Collections.emptyList());
System.out.println("Placeholder '" + placeholder + "' was replaced with: '" + result + "'");
assertNotNull(result);
assertNotEquals(placeholder, result);
}))
.collect(Collectors.toList());
}
@TestFactory
@DisplayName("Player placeholders return nothing on Console")
Collection<DynamicTest> testPlayerPlaceholdersOnConsole() {
return underTest.getRegisteredPlayerPlaceholders().stream()
.map(placeholder -> DynamicTest.dynamicTest("'" + placeholder + "' returns something", () -> {
String result = underTest.onPlaceholderRequest(null, placeholder, Collections.emptyList());
System.out.println("Placeholder '" + placeholder + "' was replaced with: '" + result + "'");
assertNull(result);
}))
.collect(Collectors.toList());
}
@TestFactory
@DisplayName("Player placeholders return something on Console for other player")
Collection<DynamicTest> testPlayerPlaceholdersOnConsoleForOtherPlayer() {
return underTest.getRegisteredPlayerPlaceholders().stream()
.map(placeholder -> DynamicTest.dynamicTest("'" + placeholder + "' returns something", () -> {
String result = underTest.onPlaceholderRequest(null, placeholder, List.of(playerUUID.toString()));
System.out.println("Placeholder '" + placeholder + "' was replaced with: '" + result + "'");
assertNotNull(result);
assertNotEquals(placeholder, result);
}))
.collect(Collectors.toList());
}
}

View File

@ -22,6 +22,7 @@ import com.djrapitops.plan.commands.PlanCommand;
import com.djrapitops.plan.modules.FiltersModule;
import com.djrapitops.plan.modules.PlaceholderModule;
import com.djrapitops.plan.modules.PlatformAbstractionLayerModule;
import com.djrapitops.plan.placeholder.PlanPlaceholders;
import com.djrapitops.plan.utilities.logging.PluginErrorLogger;
import dagger.BindsInstance;
import dagger.Component;
@ -56,6 +57,8 @@ public interface PlanPluginComponent {
PluginErrorLogger pluginErrorLogger();
PlanPlaceholders placeholders();
@Component.Builder
interface Builder {
@BindsInstance