11package ch .jalu .configme .properties ;
22
33import ch .jalu .configme .properties .convertresult .PropertyValue ;
4+ import ch .jalu .configme .properties .types .BooleanType ;
5+ import ch .jalu .configme .properties .types .EnumPropertyType ;
6+ import ch .jalu .configme .properties .types .NumberType ;
7+ import ch .jalu .configme .properties .types .PropertyType ;
8+ import ch .jalu .configme .properties .types .StringType ;
49import ch .jalu .configme .resource .PropertyReader ;
510import ch .jalu .configme .samples .TestEnum ;
611import org .junit .jupiter .api .Test ;
914import org .mockito .junit .jupiter .MockitoExtension ;
1015
1116import java .util .Optional ;
17+ import java .util .concurrent .TimeUnit ;
1218
1319import static ch .jalu .configme .TestUtils .isErrorValueOf ;
1420import static ch .jalu .configme .TestUtils .isValidValueOf ;
1521import static java .util .Optional .of ;
1622import static org .hamcrest .MatcherAssert .assertThat ;
1723import static org .hamcrest .Matchers .equalTo ;
24+ import static org .hamcrest .Matchers .nullValue ;
1825import static org .mockito .BDDMockito .given ;
19- import static org .mockito .Mockito .doReturn ;
20- import static org .mockito .Mockito .only ;
21- import static org .mockito .Mockito .spy ;
22- import static org .mockito .Mockito .verify ;
26+ import static org .mockito .Mockito .mock ;
2327
2428/**
2529 * Test for {@link OptionalProperty}.
@@ -33,9 +37,9 @@ class OptionalPropertyTest {
3337 @ Test
3438 void shouldReturnPresentValues () {
3539 // given
36- OptionalProperty <Boolean > booleanProp = new OptionalProperty <>(new BooleanProperty ( "bool.path.test" , false ) );
37- OptionalProperty <Integer > intProp = new OptionalProperty <>(new IntegerProperty ( "int.path.test" , 0 ) );
38- OptionalProperty <TestEnum > enumProp = new OptionalProperty <>(new EnumProperty <>( "enum.path.test" , TestEnum . class , TestEnum .SECOND ));
40+ OptionalProperty <Boolean > booleanProp = new OptionalProperty <>("bool.path.test" , BooleanType . BOOLEAN );
41+ OptionalProperty <Integer > intProp = new OptionalProperty <>("int.path.test" , NumberType . INTEGER );
42+ OptionalProperty <TestEnum > enumProp = new OptionalProperty <>("enum.path.test" , new EnumPropertyType <>( TestEnum .class ));
3943
4044 given (reader .getObject ("bool.path.test" )).willReturn (true );
4145 given (reader .getObject ("int.path.test" )).willReturn (27 );
@@ -55,9 +59,9 @@ void shouldReturnPresentValues() {
5559 @ Test
5660 void shouldReturnEmptyOptional () {
5761 // given
58- OptionalProperty <Boolean > booleanProp = new OptionalProperty <>(new BooleanProperty ( "bool.path.wrong" , false ) );
59- OptionalProperty <Integer > intProp = new OptionalProperty <>(new IntegerProperty ( "int.path.wrong" , 0 ) );
60- OptionalProperty <TestEnum > enumProp = new OptionalProperty <>(new EnumProperty <>( "enum.path.wrong" , TestEnum . class , TestEnum .SECOND ));
62+ OptionalProperty <Boolean > booleanProp = new OptionalProperty <>("bool.path.wrong" , BooleanType . BOOLEAN );
63+ OptionalProperty <Integer > intProp = new OptionalProperty <>("int.path.wrong" , NumberType . INTEGER );
64+ OptionalProperty <TestEnum > enumProp = new OptionalProperty <>("enum.path.wrong" , new EnumPropertyType <>( TestEnum .class ));
6165
6266 // when
6367 PropertyValue <Optional <Boolean >> boolResult = booleanProp .determineValue (reader );
@@ -73,7 +77,7 @@ void shouldReturnEmptyOptional() {
7377 @ Test
7478 void shouldAllowToDefineDefaultValue () {
7579 // given
76- OptionalProperty <Integer > integerProp = new OptionalProperty <>(new IntegerProperty ( " path" , 0 ) , 42 );
80+ OptionalProperty <Integer > integerProp = new OptionalProperty <>("int. path.wrong " , NumberType . INTEGER , 42 );
7781
7882 // when
7983 Optional <Integer > defaultValue = integerProp .getDefaultValue ();
@@ -85,48 +89,67 @@ void shouldAllowToDefineDefaultValue() {
8589 @ Test
8690 void shouldReturnValueWithInvalidFlagIfReturnedFromReader () {
8791 // given
88- StringProperty baseProperty = spy (new StringProperty ("the.path" , "DEFAULT" ));
89- doReturn (PropertyValue .withValueRequiringRewrite ("this should be discarded" )).when (baseProperty ).determineValue (reader );
90- given (reader .contains ("the.path" )).willReturn (true );
91- OptionalProperty <String > optionalProperty = new OptionalProperty <>(baseProperty );
92+ given (reader .getObject ("the.path" )).willReturn (400 );
93+ OptionalProperty <Byte > optionalProperty = new OptionalProperty <>("the.path" , NumberType .BYTE );
9294
9395 // when
94- PropertyValue <Optional <String >> value = optionalProperty .determineValue (reader );
96+ PropertyValue <Optional <Byte >> value = optionalProperty .determineValue (reader );
9597
9698 // then
97- assertThat (value , isErrorValueOf (Optional .empty ( )));
99+ assertThat (value , isErrorValueOf (Optional .of ( Byte . MAX_VALUE )));
98100 }
99101
100102 @ Test
101- void shouldDelegateToBasePropertyAndHaveEmptyOptionalAsDefault () {
103+ void shouldValidateWithBasePropertyNullSafe () {
102104 // given
103- StringProperty baseProperty = new StringProperty ("some.path" , "Def" );
104- OptionalProperty <String > property = new OptionalProperty <>(baseProperty );
105+ OptionalProperty <String > property = new OptionalProperty <>("path" , StringType .STRING );
105106
106107 // when
107- Optional <String > defaultValue = property .getDefaultValue ();
108- String path = property .getPath ();
108+ boolean isEmptyValid = property .isValidValue (Optional .empty ());
109+ boolean isValueValid = property .isValidValue (Optional .of ("foo" ));
110+ boolean isNullValid = property .isValidValue (null );
109111
110112 // then
111- assertThat (defaultValue , equalTo (Optional .empty ()));
112- assertThat (path , equalTo ("some.path" ));
113+ assertThat (isEmptyValid , equalTo (true ));
114+ assertThat (isValueValid , equalTo (true ));
115+ assertThat (isNullValid , equalTo (false ));
113116 }
114117
115118 @ Test
116- void shouldValidateWithBasePropertyNullSafe () {
119+ void shouldReturnNullAsExportValue () {
117120 // given
118- StringProperty baseProperty = spy (new StringProperty ("some.path" , "Def" ));
119- OptionalProperty <String > property = new OptionalProperty <>(baseProperty );
121+ OptionalProperty <Integer > property = new OptionalProperty <>("int.path" , NumberType .INTEGER );
120122
121123 // when
122- boolean isEmptyValid = property .isValidValue (Optional .empty ());
123- boolean isValueValid = property .isValidValue (Optional .of ("foo" ));
124- boolean isNullValid = property .isValidValue (null );
124+ Object exportValue = property .toExportValue (Optional .empty ());
125125
126126 // then
127- assertThat (isEmptyValid , equalTo (true ));
128- assertThat (isValueValid , equalTo (true ));
129- assertThat (isNullValid , equalTo (false ));
130- verify (baseProperty , only ()).isValidValue ("foo" );
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" ));
131154 }
132155}
0 commit comments