|
4 | 4 | import ch.jalu.configme.properties.types.BooleanType; |
5 | 5 | import ch.jalu.configme.properties.types.EnumPropertyType; |
6 | 6 | import ch.jalu.configme.properties.types.NumberType; |
| 7 | +import ch.jalu.configme.properties.types.PropertyType; |
7 | 8 | import ch.jalu.configme.properties.types.StringType; |
8 | 9 | import ch.jalu.configme.resource.PropertyReader; |
9 | 10 | import ch.jalu.configme.samples.TestEnum; |
|
13 | 14 | import org.mockito.junit.jupiter.MockitoExtension; |
14 | 15 |
|
15 | 16 | import java.util.Optional; |
| 17 | +import java.util.concurrent.TimeUnit; |
16 | 18 |
|
17 | 19 | import static ch.jalu.configme.TestUtils.isErrorValueOf; |
18 | 20 | import static ch.jalu.configme.TestUtils.isValidValueOf; |
19 | 21 | import static java.util.Optional.of; |
20 | 22 | import static org.hamcrest.MatcherAssert.assertThat; |
21 | 23 | import static org.hamcrest.Matchers.equalTo; |
| 24 | +import static org.hamcrest.Matchers.nullValue; |
22 | 25 | import static org.mockito.BDDMockito.given; |
| 26 | +import static org.mockito.Mockito.mock; |
23 | 27 |
|
24 | 28 | /** |
25 | 29 | * Test for {@link OptionalProperty}. |
@@ -110,4 +114,42 @@ void shouldValidateWithBasePropertyNullSafe() { |
110 | 114 | assertThat(isValueValid, equalTo(true)); |
111 | 115 | assertThat(isNullValid, equalTo(false)); |
112 | 116 | } |
| 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 | + } |
113 | 155 | } |
0 commit comments