Skip to content

Commit

Permalink
#191 Add test for map property using bean type on root path
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Apr 19, 2021
1 parent ca5b9e7 commit 78a5b01
Showing 1 changed file with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package ch.jalu.configme.beanmapper;

import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.SettingsManagerBuilder;
import ch.jalu.configme.TestUtils;
import ch.jalu.configme.properties.MapProperty;
import ch.jalu.configme.properties.types.BeanPropertyType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;

import static ch.jalu.configme.properties.PropertyInitializer.mapProperty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;

/**
* Tests that a map property can be used in the root with a bean type as values.
*
* @see <a href="https://github.com/AuthMe/ConfigMe/issues/191">Issue #191</a>
*/
class BeanMapOnRootLevelTest {

@TempDir
Path tempDir;

@Test
void shouldLoadMap() throws IOException {
// given
String yaml = "basic:\n"
+ "\n name: \"hello\"\n"
+ "\n lore:\n"
+ "\n - \"world\""
+ "\n - \"moon\"";
Path tempFile = TestUtils.createTemporaryFile(tempDir);
Files.write(tempFile, yaml.getBytes());

// when
SettingsManager settingsManager = SettingsManagerBuilder.withYamlFile(tempFile)
.configurationData(TestSettingsHolder.class)
.create();

// then
Map<String, Info> result = settingsManager.getProperty(TestSettingsHolder.INFO);
assertThat(result.keySet(), contains("basic"));
assertThat(result.get("basic").getName(), equalTo("hello"));
assertThat(result.get("basic").getLore(), contains("world", "moon"));
}

public static final class TestSettingsHolder implements SettingsHolder {

public static final MapProperty<Info> INFO = mapProperty(BeanPropertyType.of(Info.class))
.path("")
.defaultEntry("default", new Info("default", "def"))
.build();

private TestSettingsHolder() {
}
}

public static final class Info {

private String name;
private List<String> lore;

public Info(String name, String... lore) {
}

public Info() {
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<String> getLore() {
return lore;
}

public void setLore(List<String> lore) {
this.lore = lore;
}
}
}

0 comments on commit 78a5b01

Please sign in to comment.