From f2e8877a580d52bfb5de603c2f391f89bbe3930d Mon Sep 17 00:00:00 2001 From: Federico Herrera Date: Tue, 21 Nov 2023 12:54:29 -0300 Subject: [PATCH] Add unit test for constructor, reformat test according to feedback --- .../jalu/configme/properties/MapProperty.java | 2 +- .../configme/properties/MapPropertyTest.java | 16 ++++++++++ .../YamlFileResourceRootMapPropertyTest.java | 30 +++++++++---------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/main/java/ch/jalu/configme/properties/MapProperty.java b/src/main/java/ch/jalu/configme/properties/MapProperty.java index e9460d56..cbf44f7c 100644 --- a/src/main/java/ch/jalu/configme/properties/MapProperty.java +++ b/src/main/java/ch/jalu/configme/properties/MapProperty.java @@ -22,7 +22,7 @@ public class MapProperty extends BaseProperty> { private final PropertyType valueType; /** - * Constructor. Build a {@link MapProperty} with empty default values. + * Constructor. Builds a {@link MapProperty} with an empty map as default value. * * @param path the path of the property * @param valueType the property type of the values diff --git a/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java b/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java index 806fde84..a243c274 100644 --- a/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java +++ b/src/test/java/ch/jalu/configme/properties/MapPropertyTest.java @@ -3,6 +3,7 @@ import ch.jalu.configme.TestUtils; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.convertresult.PropertyValue; +import ch.jalu.configme.properties.types.NumberType; import ch.jalu.configme.properties.types.PropertyType; import ch.jalu.configme.properties.types.StringType; import ch.jalu.configme.resource.PropertyReader; @@ -27,6 +28,7 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.collection.IsMapWithSize.anEmptyMap; import static org.mockito.BDDMockito.given; /** @@ -111,6 +113,20 @@ void shouldKeepOrderInExportValue() { assertThat(((Map) exportValue).keySet(), contains("first", "second", "third", "fourth")); } + @Test + void shouldUseEmptyMapAsDefaultValue() { + // given + MapProperty property = new MapProperty<>("test", NumberType.INTEGER); + + //when + Map actualDefaultValue = property.getDefaultValue(); + String actualPath = property.getPath(); + + // then + assertThat(actualDefaultValue, anEmptyMap()); + assertThat(actualPath, equalTo("test")); + } + private static Map createSampleMap() { Map map = new HashMap<>(); map.put("test", "keks"); diff --git a/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java b/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java index 09ddd985..7567e9b9 100644 --- a/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java +++ b/src/test/java/ch/jalu/configme/resource/YamlFileResourceRootMapPropertyTest.java @@ -6,6 +6,7 @@ import ch.jalu.configme.configurationdata.ConfigurationDataBuilder; import ch.jalu.configme.properties.MapProperty; import ch.jalu.configme.properties.Property; +import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.properties.types.BeanPropertyType; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -17,10 +18,9 @@ import java.util.List; import java.util.Map; -import static ch.jalu.configme.properties.PropertyInitializer.newListProperty; -import static java.util.Collections.singletonList; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; class YamlFileResourceRootMapPropertyTest { @@ -29,34 +29,32 @@ class YamlFileResourceRootMapPropertyTest { @Test void shouldWriteAndReadFile() { - Path yamlFile = TestUtils.createTemporaryFile(temporaryFolder); - - YamlFileResource resource = new YamlFileResource(yamlFile); + //given + Path tempFolder = TestUtils.createTemporaryFile(temporaryFolder); + YamlFileResource resource = new YamlFileResource(tempFolder); ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration(SampleConfig.class); - configurationData.setValue(SampleConfig.SHAPE, singletonList("foo")); - Map mapProperty = new HashMap<>(); mapProperty.put("props", new InnerProperties()); - mapProperty.get("props").setProps(Arrays.asList("bar", "baz")); + mapProperty.get("props").setProps(Arrays.asList("foo", "bar")); configurationData.setValue(SampleConfig.MAP_PROPERTY, mapProperty); - resource.exportProperties(configurationData); - List actualShapeProperty = configurationData.getValue(SampleConfig.SHAPE); - Map actualMapProperty = configurationData.getValue(SampleConfig.MAP_PROPERTY); + //when + PropertyValue> actualPropertyValue = + SampleConfig.MAP_PROPERTY.determineValue(resource.createReader()); - assertThat(actualShapeProperty, equalTo(singletonList("foo"))); - assertThat(actualMapProperty, equalTo(mapProperty)); + //then + assertThat(actualPropertyValue.isValidInResource(), equalTo(true)); + assertThat(actualPropertyValue.getValue().keySet(), contains("props")); + assertThat(actualPropertyValue.getValue().get("props").getProps(), contains("foo", "bar")); } public static final class SampleConfig implements SettingsHolder { - public static final Property> SHAPE = newListProperty("shape"); - public static final Property> MAP_PROPERTY = new MapProperty<>( - "map", + "", BeanPropertyType.of(InnerProperties.class) );