2020
2121import org .junit .jupiter .api .Test ;
2222
23+ import java .time .LocalDate ;
2324import java .util .Arrays ;
2425import java .util .Collection ;
2526import java .util .Collections ;
3132import org .neo4j .driver .internal .util .Extract ;
3233import org .neo4j .driver .internal .util .Iterables ;
3334import org .neo4j .driver .v1 .Value ;
34- import org .neo4j .driver .v1 .util . Function ;
35+ import org .neo4j .driver .v1 .exceptions . ClientException ;
3536import org .neo4j .driver .v1 .util .Pair ;
3637
3738import static java .util .Arrays .asList ;
39+ import static java .util .Collections .emptyMap ;
3840import static java .util .Collections .singletonList ;
41+ import static java .util .Collections .singletonMap ;
3942import static org .hamcrest .CoreMatchers .equalTo ;
4043import static org .hamcrest .Matchers .contains ;
4144import static org .hamcrest .Matchers .containsInAnyOrder ;
4245import static org .hamcrest .Matchers .empty ;
4346import static org .hamcrest .junit .MatcherAssert .assertThat ;
47+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4448import static org .junit .jupiter .api .Assertions .assertFalse ;
4549import static org .junit .jupiter .api .Assertions .assertThrows ;
4650import static org .neo4j .driver .v1 .Values .value ;
@@ -77,7 +81,7 @@ void extractMultipleShouldNotBeModifiable()
7781 @ Test
7882 void testMapOverList ()
7983 {
80- List <Integer > mapped = Extract .list ( new Value []{value ( 42 ), value ( 43 )}, integerExtractor () );
84+ List <Integer > mapped = Extract .list ( new Value []{value ( 42 ), value ( 43 )}, Value :: asInt );
8185
8286 assertThat ( mapped , equalTo ( Arrays .asList ( 42 , 43 ) ) );
8387 }
@@ -91,7 +95,7 @@ void testMapValues()
9195 map .put ( "k2" , value ( 42 ) );
9296
9397 // WHEN
94- Map <String ,Integer > mappedMap = Extract .map ( map , integerExtractor () );
98+ Map <String ,Integer > mappedMap = Extract .map ( map , Value :: asInt );
9599
96100 // THEN
97101 Collection <Integer > values = mappedMap .values ();
@@ -108,7 +112,7 @@ void testShouldPreserveMapOrderMapValues()
108112 map .put ( "k1" , value ( 42 ) );
109113
110114 // WHEN
111- Map <String ,Integer > mappedMap = Extract .map ( map , integerExtractor () );
115+ Map <String ,Integer > mappedMap = Extract .map ( map , Value :: asInt );
112116
113117 // THEN
114118 Collection <Integer > values = mappedMap .values ();
@@ -126,7 +130,7 @@ void testProperties()
126130 InternalNode node = new InternalNode ( 42L , Collections .singletonList ( "L" ), props );
127131
128132 // WHEN
129- Iterable <Pair <String , Integer >> properties = Extract .properties ( node , integerExtractor () );
133+ Iterable <Pair <String ,Integer >> properties = Extract .properties ( node , Value :: asInt );
130134
131135 // THEN
132136 Iterator <Pair <String , Integer >> iterator = properties .iterator ();
@@ -141,24 +145,43 @@ void testFields()
141145 // GIVEN
142146 InternalRecord record = new InternalRecord ( Arrays .asList ( "k1" ), new Value []{value ( 42 )} );
143147 // WHEN
144- List <Pair <String , Integer >> fields = Extract .fields ( record , integerExtractor () );
148+ List <Pair <String ,Integer >> fields = Extract .fields ( record , Value :: asInt );
145149
146150
147151 // THEN
148152 assertThat ( fields , equalTo ( Collections .singletonList ( InternalPair .of ( "k1" , 42 ) ) ) );
149153 }
150154
151- private Function <Value ,Integer > integerExtractor ()
155+ @ Test
156+ void shouldExtractMapOfValuesFromNullOrEmptyMap ()
157+ {
158+ assertEquals ( emptyMap (), Extract .mapOfValues ( null ) );
159+ assertEquals ( emptyMap (), Extract .mapOfValues ( emptyMap () ) );
160+ }
161+
162+ @ Test
163+ void shouldExtractMapOfValues ()
152164 {
153- return new Function <Value ,Integer >()
154- {
155-
156- @ Override
157- public Integer apply ( Value value )
158- {
159- return value .asInt ();
160- }
161- };
165+ Map <String ,Object > map = new HashMap <>();
166+ map .put ( "key1" , "value1" );
167+ map .put ( "key2" , 42L );
168+ map .put ( "key3" , LocalDate .now () );
169+ map .put ( "key4" , new byte []{1 , 2 , 3 } );
170+
171+ Map <String ,Value > mapOfValues = Extract .mapOfValues ( map );
172+
173+ assertEquals ( 4 , map .size () );
174+ assertEquals ( value ( "value1" ), mapOfValues .get ( "key1" ) );
175+ assertEquals ( value ( 42L ), mapOfValues .get ( "key2" ) );
176+ assertEquals ( value ( LocalDate .now () ), mapOfValues .get ( "key3" ) );
177+ assertEquals ( value ( new byte []{1 , 2 , 3 } ), mapOfValues .get ( "key4" ) );
162178 }
163179
180+ @ Test
181+ void shouldFailToExtractMapOfValuesFromUnsupportedValues ()
182+ {
183+ assertThrows ( ClientException .class , () -> Extract .mapOfValues ( singletonMap ( "key" , new InternalNode ( 1 ) ) ) );
184+ assertThrows ( ClientException .class , () -> Extract .mapOfValues ( singletonMap ( "key" , new InternalRelationship ( 1 , 1 , 1 , "HI" ) ) ) );
185+ assertThrows ( ClientException .class , () -> Extract .mapOfValues ( singletonMap ( "key" , new InternalPath ( new InternalNode ( 1 ) ) ) ) );
186+ }
164187}
0 commit comments