Add missing properties to commands.yml on start and reload

This commit is contained in:
ljacqu 2017-05-21 14:21:40 +02:00
parent 5c6af0330e
commit 5ca9112c12
3 changed files with 57 additions and 3 deletions

View File

@ -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<String> 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);
}
/**

View File

@ -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));
}
}

View File

@ -25,6 +25,7 @@ onSessionLogin:
welcome:
command: 'msg %p Session login!'
executor: CONSOLE
onUnregister: {}
onLogout:
announce:
command: 'broadcast %p (%ip) logged out'