Skip to content

Commit 84f8967

Browse files
committed
#448 Add tests for the export value of Optional properties
1 parent d78751d commit 84f8967

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/test/java/ch/jalu/configme/properties/OptionalPropertyTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import ch.jalu.configme.properties.types.BooleanType;
55
import ch.jalu.configme.properties.types.EnumPropertyType;
66
import ch.jalu.configme.properties.types.NumberType;
7+
import ch.jalu.configme.properties.types.PropertyType;
78
import ch.jalu.configme.properties.types.StringType;
89
import ch.jalu.configme.resource.PropertyReader;
910
import ch.jalu.configme.samples.TestEnum;
@@ -13,13 +14,16 @@
1314
import org.mockito.junit.jupiter.MockitoExtension;
1415

1516
import java.util.Optional;
17+
import java.util.concurrent.TimeUnit;
1618

1719
import static ch.jalu.configme.TestUtils.isErrorValueOf;
1820
import static ch.jalu.configme.TestUtils.isValidValueOf;
1921
import static java.util.Optional.of;
2022
import static org.hamcrest.MatcherAssert.assertThat;
2123
import static org.hamcrest.Matchers.equalTo;
24+
import static org.hamcrest.Matchers.nullValue;
2225
import static org.mockito.BDDMockito.given;
26+
import static org.mockito.Mockito.mock;
2327

2428
/**
2529
* Test for {@link OptionalProperty}.
@@ -110,4 +114,42 @@ void shouldValidateWithBasePropertyNullSafe() {
110114
assertThat(isValueValid, equalTo(true));
111115
assertThat(isNullValid, equalTo(false));
112116
}
117+
118+
@Test
119+
void shouldReturnNullAsExportValue() {
120+
// given
121+
OptionalProperty<Integer> property = new OptionalProperty<>("int.path", NumberType.INTEGER);
122+
123+
// when
124+
Object exportValue = property.toExportValue(Optional.empty());
125+
126+
// then
127+
assertThat(exportValue, nullValue());
128+
}
129+
130+
@Test
131+
void shouldReturnNullIfValuePropertyTypeReturnsNull() {
132+
// given
133+
PropertyType<String> valuePropertyType = mock(PropertyType.class);
134+
given(valuePropertyType.toExportValue("demo")).willReturn(null);
135+
OptionalProperty<String> optionalProperty = new OptionalProperty<>("int.path", valuePropertyType);
136+
137+
// when
138+
Object exportValue = optionalProperty.toExportValue(Optional.of("demo"));
139+
140+
// then
141+
assertThat(exportValue, nullValue());
142+
}
143+
144+
@Test
145+
void shouldConstructExportValue() {
146+
// given
147+
OptionalProperty<TimeUnit> optionalProperty = new OptionalProperty<>("duration.unit", EnumPropertyType.of(TimeUnit.class));
148+
149+
// when
150+
Object exportValue = optionalProperty.toExportValue(Optional.of(TimeUnit.HOURS));
151+
152+
// then
153+
assertThat(exportValue, equalTo("HOURS"));
154+
}
113155
}

0 commit comments

Comments
 (0)