#411 Improve command migration handling, write tests
This commit is contained in:
@@ -15,7 +15,6 @@ import org.mockito.runners.MockitoJUnitRunner;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -53,7 +52,7 @@ public class ForceLoginCommandTest {
|
||||
|
||||
// then
|
||||
verify(bukkitService).getPlayerExact(playerName);
|
||||
verify(sender).sendMessage(argThat(equalTo("Player needs to be online!")));
|
||||
verify(sender).sendMessage("Player needs to be online!");
|
||||
verifyZeroInteractions(management);
|
||||
}
|
||||
|
||||
@@ -69,7 +68,7 @@ public class ForceLoginCommandTest {
|
||||
|
||||
// then
|
||||
verify(bukkitService).getPlayerExact(playerName);
|
||||
verify(sender).sendMessage(argThat(equalTo("Player needs to be online!")));
|
||||
verify(sender).sendMessage("Player needs to be online!");
|
||||
verifyZeroInteractions(management);
|
||||
}
|
||||
|
||||
|
||||
@@ -160,7 +160,7 @@ public class MessagesIntegrationTest {
|
||||
messages.send(sender, key);
|
||||
|
||||
// then
|
||||
verify(sender).sendMessage(argThat(equalTo("Use /captcha THE_CAPTCHA to solve the captcha")));
|
||||
verify(sender).sendMessage("Use /captcha THE_CAPTCHA to solve the captcha");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -83,6 +83,24 @@ public class SettingsTest {
|
||||
assertThat(result, equalTo(emailMessage));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadRecoveryCodeMessage() throws IOException {
|
||||
// given
|
||||
String emailMessage = "Your recovery code is %code.";
|
||||
File emailFile = new File(testPluginFolder, "recovery_code_email.html");
|
||||
createFile(emailFile);
|
||||
Files.write(emailFile.toPath(), emailMessage.getBytes());
|
||||
|
||||
PropertyResource resource = mock(PropertyResource.class);
|
||||
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
|
||||
|
||||
// when
|
||||
String result = settings.getRecoveryCodeEmailMessage();
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(emailMessage));
|
||||
}
|
||||
|
||||
private static void createFile(File file) {
|
||||
try {
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
@@ -19,7 +19,7 @@ final class CommandConfigTestHelper {
|
||||
* @param executor the expected executor
|
||||
* @return the matcher
|
||||
*/
|
||||
public static Matcher<Command> isCommand(String cmd, Executor executor) {
|
||||
static Matcher<Command> isCommand(String cmd, Executor executor) {
|
||||
return new TypeSafeMatcher<Command>() {
|
||||
@Override
|
||||
protected boolean matchesSafely(Command item) {
|
||||
|
||||
@@ -4,26 +4,33 @@ import com.google.common.io.Files;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import fr.xephi.authme.settings.SettingsMigrationService;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import static fr.xephi.authme.settings.commandconfig.CommandConfigTestHelper.isCommand;
|
||||
import static java.lang.String.format;
|
||||
import static org.hamcrest.Matchers.anEmptyMap;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.only;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
/**
|
||||
* Test for {@link CommandManager}.
|
||||
@@ -33,16 +40,18 @@ public class CommandManagerTest {
|
||||
|
||||
private static final String TEST_FILES_FOLDER = "/fr/xephi/authme/settings/commandconfig/";
|
||||
|
||||
private CommandManager manager;
|
||||
@InjectMocks
|
||||
private CommandsMigrater commandsMigrater;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private CommandManager manager;
|
||||
@Mock
|
||||
private BukkitService bukkitService;
|
||||
@Mock
|
||||
private Settings settings;
|
||||
@Mock
|
||||
private CommandsMigrater commandsMigrater;
|
||||
private SettingsMigrationService settingsMigrationService;
|
||||
|
||||
private File testFolder;
|
||||
|
||||
@Before
|
||||
@@ -75,10 +84,9 @@ public class CommandManagerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // TODO #411: Implement tested behavior
|
||||
public void shouldLoadIncompleteFile() {
|
||||
// given
|
||||
File configFile = copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.incomplete.yml");
|
||||
|
||||
// when
|
||||
initManager();
|
||||
@@ -90,23 +98,74 @@ public class CommandManagerTest {
|
||||
isCommand("msg %p Welcome back", Executor.CONSOLE),
|
||||
isCommand("list", Executor.PLAYER)));
|
||||
assertThat(commandConfig.getOnRegister(), anEmptyMap());
|
||||
}
|
||||
|
||||
// verify that we have rewritten the file with the proper sections
|
||||
FileConfiguration configuration = YamlConfiguration.loadConfiguration(configFile);
|
||||
assertThat(configuration.contains("onRegister"), equalTo(true));
|
||||
assertThat(configuration.contains("doesNotExist"), equalTo(false));
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnJoin() {
|
||||
// given
|
||||
String name = "Bobby1";
|
||||
|
||||
// when
|
||||
testCommandExecution(name, CommandManager::runCommandsOnJoin);
|
||||
|
||||
// then
|
||||
verify(bukkitService, only()).dispatchConsoleCommand(format("broadcast %s has joined", name));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnRegister() {
|
||||
// given
|
||||
String name = "luis";
|
||||
|
||||
// when
|
||||
testCommandExecution(name, CommandManager::runCommandsOnRegister);
|
||||
|
||||
// then
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("me I just registered"));
|
||||
verify(bukkitService).dispatchConsoleCommand(format("log %s registered", name));
|
||||
verifyNoMoreInteractions(bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecuteCommandsOnLogin() {
|
||||
// given
|
||||
String name = "plaYer01";
|
||||
|
||||
// when
|
||||
testCommandExecution(name, CommandManager::runCommandsOnLogin);
|
||||
|
||||
// then
|
||||
verify(bukkitService).dispatchConsoleCommand(format("msg %s Welcome back", name));
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("motd"));
|
||||
verify(bukkitService).dispatchCommand(any(Player.class), eq("list"));
|
||||
verifyNoMoreInteractions(bukkitService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveHiddenConstructorInSettingsHolderClass() {
|
||||
// given / when / then
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(CommandSettingsHolder.class);
|
||||
}
|
||||
|
||||
|
||||
private void testCommandExecution(String playerName, BiConsumer<CommandManager, Player> testMethod) {
|
||||
copyJarFileAsCommandsYml(TEST_FILES_FOLDER + "commands.complete.yml");
|
||||
initManager();
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(playerName);
|
||||
|
||||
testMethod.accept(manager, player);
|
||||
}
|
||||
|
||||
private void initManager() {
|
||||
manager = new CommandManager(testFolder, bukkitService, commandsMigrater, settings);
|
||||
manager = new CommandManager(testFolder, bukkitService, commandsMigrater);
|
||||
}
|
||||
|
||||
private File copyJarFileAsCommandsYml(String path) {
|
||||
private void copyJarFileAsCommandsYml(String path) {
|
||||
File source = TestHelper.getJarFile(path);
|
||||
File destination = new File(testFolder, "commands.yml");
|
||||
try {
|
||||
Files.copy(source, destination);
|
||||
return destination;
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package fr.xephi.authme.settings.commandconfig;
|
||||
|
||||
import com.github.authme.configme.knownproperties.ConfigurationDataBuilder;
|
||||
import com.github.authme.configme.resource.PropertyResource;
|
||||
import com.github.authme.configme.resource.YamlFileResource;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.SettingsMigrationService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests that commands.yml is well-formed.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class CommandYmlConsistencyTest {
|
||||
|
||||
@InjectMocks
|
||||
private CommandsMigrater commandsMigrater;
|
||||
|
||||
@Mock
|
||||
private SettingsMigrationService settingsMigrationService;
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Before
|
||||
public void setUpSettingsMigrationService() {
|
||||
given(settingsMigrationService.getOnLoginCommands()).willReturn(Collections.emptyList());
|
||||
given(settingsMigrationService.getOnLoginConsoleCommands()).willReturn(Collections.emptyList());
|
||||
given(settingsMigrationService.getOnRegisterCommands()).willReturn(Collections.emptyList());
|
||||
given(settingsMigrationService.getOnRegisterConsoleCommands()).willReturn(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadWithNoMigrations() {
|
||||
// given
|
||||
File commandFile = TestHelper.getJarFile("/commands.yml");
|
||||
PropertyResource resource = new YamlFileResource(commandFile);
|
||||
|
||||
// when
|
||||
boolean result = commandsMigrater.checkAndMigrate(
|
||||
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,19 @@
|
||||
package fr.xephi.authme.settings.commandconfig;
|
||||
|
||||
import com.github.authme.configme.knownproperties.ConfigurationDataBuilder;
|
||||
import com.github.authme.configme.resource.PropertyResource;
|
||||
import com.github.authme.configme.resource.YamlFileResource;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.settings.SettingsMigrationService;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -108,4 +113,19 @@ public class CommandsMigraterTest {
|
||||
isCommand("log %p registered", Executor.CONSOLE),
|
||||
isCommand("whois %p", Executor.CONSOLE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // TODO ConfigMe/#29: Create PropertyResource#getKeys
|
||||
public void shouldRewriteForEmptyFile() {
|
||||
// given
|
||||
File commandFile = TestHelper.getJarFile("/fr/xephi/authme/settings/commandconfig/commands.empty.yml");
|
||||
PropertyResource resource = new YamlFileResource(commandFile);
|
||||
|
||||
// when
|
||||
boolean result = commandsMigrater.checkAndMigrate(
|
||||
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties());
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package fr.xephi.authme.settings.properties;
|
||||
|
||||
import com.github.authme.configme.knownproperties.ConfigurationData;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.closeTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link AuthMeSettingsRetriever}.
|
||||
*/
|
||||
public class AuthMeSettingsRetrieverTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturnAllProperties() {
|
||||
// given / when
|
||||
ConfigurationData configurationData = AuthMeSettingsRetriever.buildConfigurationData();
|
||||
|
||||
// then
|
||||
// Note ljacqu 20161123: Check that the number of properties returned corresponds to what we expect with
|
||||
// an error margin of 10: this prevents us from having to adjust the test every time the config is changed.
|
||||
// If this test fails, replace the first argument in closeTo() with the new number of properties
|
||||
assertThat((double) configurationData.getProperties().size(),
|
||||
closeTo(150, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveHiddenConstructor() {
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(AuthMeSettingsRetriever.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user