mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-21 05:50:18 +08:00
Wrote tests for some commands
This commit is contained in:
parent
c22da1bf76
commit
2eba115f6f
@ -18,46 +18,60 @@ package com.djrapitops.plan.commands;
|
||||
|
||||
import com.djrapitops.plan.PlanSystem;
|
||||
import com.djrapitops.plan.commands.use.*;
|
||||
import com.djrapitops.plan.identification.Server;
|
||||
import com.djrapitops.plan.identification.ServerUUID;
|
||||
import com.djrapitops.plan.settings.Permissions;
|
||||
import com.djrapitops.plan.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.settings.config.paths.WebserverSettings;
|
||||
import com.djrapitops.plan.settings.locale.lang.CommandLang;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.database.transactions.StoreServerInformationTransaction;
|
||||
import com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction;
|
||||
import com.djrapitops.plan.storage.file.PlanFiles;
|
||||
import extension.FullSystemExtension;
|
||||
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 org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.mockito.Mockito;
|
||||
import utilities.mocks.PluginMockComponent;
|
||||
|
||||
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.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(FullSystemExtension.class)
|
||||
class PlanCommandTest {
|
||||
|
||||
PlanCommand underTest;
|
||||
PlanSystem system;
|
||||
|
||||
@BeforeEach
|
||||
void preparePlanCommand(@TempDir Path tempDir) throws Exception {
|
||||
PluginMockComponent mockComponent = new PluginMockComponent(tempDir);
|
||||
system = mockComponent.getPlanSystem();
|
||||
void preparePlanCommand(PlanConfig config, PlanCommand command, PlanSystem system) {
|
||||
config.set(WebserverSettings.DISABLED, true);
|
||||
system.enable();
|
||||
underTest = mockComponent.getComponent().planCommand();
|
||||
underTest = command;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSystem() {
|
||||
void tearDownSystem(PlanSystem system) {
|
||||
if (system != null) system.disable();
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildingHasNoBuilderErrors() {
|
||||
assertNotNull(underTest.build());
|
||||
void buildingHasNoBuilderErrors(PlanCommand command) {
|
||||
assertNotNull(command.build());
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "Command not executed without permission: /plan {0}")
|
||||
@ -113,12 +127,13 @@ class PlanCommandTest {
|
||||
CMDSender sender = Mockito.mock(CMDSender.class);
|
||||
|
||||
// Sending messages
|
||||
ConsoleMessageBuilder messageBuilder = new ConsoleMessageBuilder(System.out::println);
|
||||
ConsoleMessageBuilder messageBuilder = Mockito.spy(new ConsoleMessageBuilder(System.out::println));
|
||||
ConsoleChatFormatter chatFormatter = new ConsoleChatFormatter();
|
||||
lenient().when(sender.buildMessage()).thenReturn(messageBuilder);
|
||||
lenient().when(sender.getFormatter()).thenReturn(chatFormatter);
|
||||
|
||||
// Permissions
|
||||
lenient().when(sender.supportsChatEvents()).thenReturn(true);
|
||||
lenient().when(sender.hasAllPermissionsFor(any())).thenCallRealMethod();
|
||||
lenient().when(sender.isMissingPermissionsFor(any())).thenCallRealMethod();
|
||||
lenient().when(sender.hasPermission((Permissions) any())).thenCallRealMethod();
|
||||
@ -126,7 +141,76 @@ class PlanCommandTest {
|
||||
when(sender.hasPermission(permission)).thenReturn(true);
|
||||
}
|
||||
|
||||
lenient().when(sender.getUUID()).thenReturn(Optional.of(UUID.randomUUID()));
|
||||
|
||||
return sender;
|
||||
}
|
||||
|
||||
@Test
|
||||
void serverCommandSendsLink() {
|
||||
CMDSender sender = runCommand("server", "plan.server");
|
||||
|
||||
verify(sender.buildMessage(), times(1)).link(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void networkCommandSendsLink(Database database) {
|
||||
try {
|
||||
Server server = new Server(ServerUUID.randomUUID(), "Serve", "", "");
|
||||
server.setProxy(true);
|
||||
database.executeTransaction(new StoreServerInformationTransaction(server));
|
||||
|
||||
CMDSender sender = runCommand("network", "plan.network");
|
||||
|
||||
verify(sender.buildMessage(), times(1)).link(anyString());
|
||||
} finally {
|
||||
database.executeTransaction(new RemoveEverythingTransaction());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void playerSelfCommandSendsLink() {
|
||||
CMDSender sender = runCommand("player", "plan.player.self");
|
||||
|
||||
verify(sender.buildMessage(), times(1)).link(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void playerOtherCommandSendsLink() {
|
||||
CMDSender sender = runCommand("player Test", "plan.player.self", "plan.player.other");
|
||||
|
||||
verify(sender.buildMessage(), times(1)).link(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonSelfCommandSendsLink() {
|
||||
CMDSender sender = runCommand("json", "plan.json.self");
|
||||
|
||||
verify(sender.buildMessage(), times(1)).link(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonOtherCommandSendsLink() {
|
||||
CMDSender sender = runCommand("json Test", "plan.json.self", "plan.json.other");
|
||||
|
||||
verify(sender.buildMessage(), times(1)).link(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void backupCommandCreatesBackup(PlanFiles files) throws IOException {
|
||||
runCommand("db backup SQLite", "plan.data.base", "plan.data.backup");
|
||||
|
||||
Path dataDirectory = files.getDataDirectory();
|
||||
try (Stream<Path> list = Files.list(dataDirectory)) {
|
||||
String foundBackupFile = list
|
||||
.map(Path::toFile)
|
||||
.map(File::getName)
|
||||
.filter(name -> name.endsWith(".db") && !name.startsWith("database"))
|
||||
.findFirst()
|
||||
.orElseThrow(AssertionError::new);
|
||||
|
||||
System.out.println("Found: " + foundBackupFile);
|
||||
assertTrue(foundBackupFile.contains("backup"));
|
||||
}
|
||||
}
|
||||
}
|
@ -17,6 +17,7 @@
|
||||
package extension;
|
||||
|
||||
import com.djrapitops.plan.PlanSystem;
|
||||
import com.djrapitops.plan.commands.PlanCommand;
|
||||
import com.djrapitops.plan.delivery.export.Exporter;
|
||||
import com.djrapitops.plan.identification.ServerUUID;
|
||||
import com.djrapitops.plan.settings.config.PlanConfig;
|
||||
@ -83,6 +84,7 @@ public class FullSystemExtension implements ParameterResolver, BeforeAllCallback
|
||||
PlanConfig.class.equals(type) ||
|
||||
ServerUUID.class.equals(type) ||
|
||||
PlanPluginComponent.class.equals(type) ||
|
||||
PlanCommand.class.equals(type) ||
|
||||
Database.class.equals(type) ||
|
||||
Exporter.class.equals(type);
|
||||
}
|
||||
@ -109,6 +111,13 @@ public class FullSystemExtension implements ParameterResolver, BeforeAllCallback
|
||||
throw new ParameterResolutionException("Error getting " + type.getName(), e);
|
||||
}
|
||||
}
|
||||
if (PlanCommand.class.equals(type)) {
|
||||
try {
|
||||
return component.getComponent().planCommand();
|
||||
} catch (Exception e) {
|
||||
throw new ParameterResolutionException("Error getting " + type.getName(), e);
|
||||
}
|
||||
}
|
||||
if (Database.class.equals(type)) {
|
||||
return planSystem.getDatabaseSystem().getDatabase();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user