From 5ca9112c1232038fd662c54c3be10fc34e931b55 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 21 May 2017 14:21:40 +0200 Subject: [PATCH] Add missing properties to commands.yml on start and reload --- .../CommandMigrationService.java | 9 +++- .../CommandMigrationServiceTest.java | 50 ++++++++++++++++++- .../commandconfig/commands.complete.yml | 1 + 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java index 1a945692f..e1d1085a6 100644 --- a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java +++ b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java @@ -4,6 +4,7 @@ import ch.jalu.configme.migration.MigrationService; import ch.jalu.configme.properties.Property; import ch.jalu.configme.resource.PropertyResource; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.settings.SettingsMigrationService; import fr.xephi.authme.util.RandomStringUtils; @@ -19,6 +20,11 @@ import java.util.stream.Collectors; */ class CommandMigrationService implements MigrationService { + /** List of all properties in {@link CommandConfig}. */ + @VisibleForTesting + static final List COMMAND_CONFIG_PROPERTIES = ImmutableList.of( + "onJoin", "onLogin", "onSessionLogin", "onRegister", "onUnregister", "onLogout"); + @Inject private SettingsMigrationService settingsMigrationService; @@ -38,8 +44,7 @@ class CommandMigrationService implements MigrationService { } private boolean isFileEmpty(PropertyResource resource) { - Object root = resource.getObject(""); - return (root instanceof Map) && ((Map) root).isEmpty(); + return COMMAND_CONFIG_PROPERTIES.stream().anyMatch(property -> resource.getObject(property) == null); } /** diff --git a/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java b/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java index be7b55b6c..21f4d0479 100644 --- a/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java +++ b/src/test/java/fr/xephi/authme/settings/commandconfig/CommandMigrationServiceTest.java @@ -1,5 +1,7 @@ package fr.xephi.authme.settings.commandconfig; +import ch.jalu.configme.beanmapper.BeanDescriptionFactory; +import ch.jalu.configme.beanmapper.BeanPropertyDescription; import ch.jalu.configme.configurationdata.ConfigurationDataBuilder; import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.YamlFileResource; @@ -24,6 +26,7 @@ import static fr.xephi.authme.settings.commandconfig.CommandConfigTestHelper.isC import static java.util.Collections.emptyList; import static org.hamcrest.Matchers.anEmptyMap; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.sameInstance; import static org.junit.Assert.assertThat; @@ -116,7 +119,7 @@ public class CommandMigrationServiceTest { @Test public void shouldRewriteForEmptyFile() { // given - File commandFile = TestHelper.getJarFile("/fr/xephi/authme/settings/commandconfig/commands.empty.yml"); + File commandFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "settings/commandconfig/commands.empty.yml"); PropertyResource resource = new YamlFileResource(commandFile); // when @@ -126,4 +129,49 @@ public class CommandMigrationServiceTest { // then assertThat(result, equalTo(true)); } + + @Test + public void shouldRewriteIncompleteFile() { + // given + File commandFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "settings/commandconfig/commands.incomplete.yml"); + PropertyResource resource = new YamlFileResource(commandFile); + + // when + boolean result = commandMigrationService.checkAndMigrate( + resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties()); + + // then + assertThat(result, equalTo(true)); + } + + @Test + public void shouldNotChangeCompleteFile() { + // given + File commandFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "settings/commandconfig/commands.complete.yml"); + PropertyResource resource = new YamlFileResource(commandFile); + + // when + boolean result = commandMigrationService.checkAndMigrate( + resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties()); + + // then + assertThat(result, equalTo(false)); + } + + /** + * Checks that {@link CommandMigrationService#COMMAND_CONFIG_PROPERTIES} contains all properties defined in the + * {@link CommandConfig} class. It is used to ensure that the commands.yml file is complete. + */ + @Test + public void shouldHaveAllPropertiesFromCommandConfig() { + // given + String[] properties = new BeanDescriptionFactory() + .collectWritableFields(CommandConfig.class) + .stream() + .map(BeanPropertyDescription::getName) + .toArray(String[]::new); + + // when / then + assertThat(CommandMigrationService.COMMAND_CONFIG_PROPERTIES, containsInAnyOrder(properties)); + } } diff --git a/src/test/resources/fr/xephi/authme/settings/commandconfig/commands.complete.yml b/src/test/resources/fr/xephi/authme/settings/commandconfig/commands.complete.yml index 4414ac129..57154c7fa 100644 --- a/src/test/resources/fr/xephi/authme/settings/commandconfig/commands.complete.yml +++ b/src/test/resources/fr/xephi/authme/settings/commandconfig/commands.complete.yml @@ -25,6 +25,7 @@ onSessionLogin: welcome: command: 'msg %p Session login!' executor: CONSOLE +onUnregister: {} onLogout: announce: command: 'broadcast %p (%ip) logged out'