Skip to content

Commit

Permalink
Add unit test for constructor, reformat test according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Federico Herrera committed Nov 21, 2023
1 parent 4acbcfc commit f2e8877
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ch/jalu/configme/properties/MapProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MapProperty<V> extends BaseProperty<Map<String, V>> {
private final PropertyType<V> 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
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/ch/jalu/configme/properties/MapPropertyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -111,6 +113,20 @@ void shouldKeepOrderInExportValue() {
assertThat(((Map<Integer, String>) exportValue).keySet(), contains("first", "second", "third", "fourth"));
}

@Test
void shouldUseEmptyMapAsDefaultValue() {
// given
MapProperty<Integer> property = new MapProperty<>("test", NumberType.INTEGER);

//when
Map<String, Integer> actualDefaultValue = property.getDefaultValue();
String actualPath = property.getPath();

// then
assertThat(actualDefaultValue, anEmptyMap());
assertThat(actualPath, equalTo("test"));
}

private static Map<String, String> createSampleMap() {
Map<String, String> map = new HashMap<>();
map.put("test", "keks");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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<String, InnerProperties> 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<String> actualShapeProperty = configurationData.getValue(SampleConfig.SHAPE);
Map<String, InnerProperties> actualMapProperty = configurationData.getValue(SampleConfig.MAP_PROPERTY);
//when
PropertyValue<Map<String, InnerProperties>> 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<List<String>> SHAPE = newListProperty("shape");

public static final Property<Map<String, InnerProperties>> MAP_PROPERTY = new MapProperty<>(
"map",
"",
BeanPropertyType.of(InnerProperties.class)
);

Expand Down

0 comments on commit f2e8877

Please # to comment.