|
18 | 18 | */ |
19 | 19 | package org.neo4j.driver.v1.integration; |
20 | 20 |
|
| 21 | +import org.junit.jupiter.api.Test; |
21 | 22 | import org.junit.jupiter.api.extension.RegisterExtension; |
22 | 23 | import org.junit.jupiter.params.ParameterizedTest; |
23 | 24 | import org.junit.jupiter.params.provider.Arguments; |
24 | 25 | import org.junit.jupiter.params.provider.MethodSource; |
25 | 26 |
|
| 27 | +import java.util.Arrays; |
26 | 28 | import java.util.Collections; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.stream.Collectors; |
27 | 33 | import java.util.stream.Stream; |
28 | 34 |
|
29 | 35 | import org.neo4j.driver.internal.value.ListValue; |
30 | 36 | import org.neo4j.driver.internal.value.MapValue; |
| 37 | +import org.neo4j.driver.internal.value.NullValue; |
| 38 | +import org.neo4j.driver.internal.value.StringValue; |
31 | 39 | import org.neo4j.driver.v1.StatementResult; |
32 | 40 | import org.neo4j.driver.v1.Value; |
33 | 41 | import org.neo4j.driver.v1.Values; |
|
36 | 44 |
|
37 | 45 | import static org.hamcrest.CoreMatchers.equalTo; |
38 | 46 | import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
39 | 48 | import static org.neo4j.driver.v1.Values.parameters; |
40 | 49 |
|
41 | 50 | @ParallelizableIT |
@@ -70,4 +79,175 @@ void shouldHandleType( String statement, Value expectedValue ) |
70 | 79 | // Then |
71 | 80 | assertThat( cursor.single().get( "v" ), equalTo( expectedValue ) ); |
72 | 81 | } |
| 82 | + |
| 83 | + static Stream<Arguments> collectionItems() |
| 84 | + { |
| 85 | + return Stream.of( |
| 86 | + Arguments.of( Values.value( (Object) null ) ), |
| 87 | + Arguments.of( Values.value( 1L ) ), |
| 88 | + Arguments.of( Values.value( 1.1d ) ), |
| 89 | + Arguments.of( Values.value( "hello" ) ), |
| 90 | + Arguments.of( Values.value( true ) ) |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + @ParameterizedTest |
| 95 | + @MethodSource( "collectionItems" ) |
| 96 | + void shouldEchoVeryLongMap( Value collectionItem ) |
| 97 | + { |
| 98 | + // Given |
| 99 | + Map<String, Value> input = new HashMap<>(); |
| 100 | + for ( int i = 0; i < 1000; i ++ ) |
| 101 | + { |
| 102 | + input.put( String.valueOf( i ), collectionItem ); |
| 103 | + } |
| 104 | + MapValue mapValue = new MapValue( input ); |
| 105 | + |
| 106 | + // When & Then |
| 107 | + verifyCanEncodeAndDecode( mapValue ); |
| 108 | + } |
| 109 | + |
| 110 | + @ParameterizedTest |
| 111 | + @MethodSource( "collectionItems" ) |
| 112 | + void shouldEchoVeryLongList( Value collectionItem ) |
| 113 | + { |
| 114 | + // Given |
| 115 | + Value[] input = new Value[1000]; |
| 116 | + for ( int i = 0; i < 1000; i ++ ) |
| 117 | + { |
| 118 | + input[i] = collectionItem; |
| 119 | + } |
| 120 | + ListValue listValue = new ListValue( input ); |
| 121 | + |
| 122 | + // When & Then |
| 123 | + verifyCanEncodeAndDecode( listValue ); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void shouldEchoVeryLongString() |
| 129 | + { |
| 130 | + // Given |
| 131 | + char[] chars = new char[10000]; |
| 132 | + Arrays.fill( chars, '*' ); |
| 133 | + String longText = new String( chars ); |
| 134 | + StringValue input = new StringValue( longText ); |
| 135 | + |
| 136 | + // When & Then |
| 137 | + verifyCanEncodeAndDecode( input ); |
| 138 | + } |
| 139 | + |
| 140 | + static Stream<Arguments> scalarTypes() |
| 141 | + { |
| 142 | + return Stream.of( |
| 143 | + Arguments.of( Values.value( (Object) null ) ), |
| 144 | + Arguments.of( Values.value( true ) ), |
| 145 | + Arguments.of( Values.value( false ) ), |
| 146 | + Arguments.of( Values.value( 1L ) ), |
| 147 | + Arguments.of( Values.value( -17L ) ), |
| 148 | + Arguments.of( Values.value( -129L ) ), |
| 149 | + Arguments.of( Values.value( 129L ) ), |
| 150 | + Arguments.of( Values.value( 2147483647L ) ), |
| 151 | + Arguments.of( Values.value( -2147483648L ) ), |
| 152 | + Arguments.of( Values.value( -2147483648L ) ), |
| 153 | + Arguments.of( Values.value( 9223372036854775807L ) ), |
| 154 | + Arguments.of( Values.value( -9223372036854775808L ) ), |
| 155 | + Arguments.of( Values.value( 1.7976931348623157E+308d ) ), |
| 156 | + Arguments.of( Values.value( 2.2250738585072014e-308d ) ), |
| 157 | + Arguments.of( Values.value( 0.0d ) ), |
| 158 | + Arguments.of( Values.value( 1.1d ) ), |
| 159 | + Arguments.of( Values.value( "1" ) ), |
| 160 | + Arguments.of( Values.value( "-17∂ßå®" ) ), |
| 161 | + Arguments.of( Values.value( "String" ) ), |
| 162 | + Arguments.of( Values.value( "" ) ) |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + @ParameterizedTest |
| 167 | + @MethodSource( "scalarTypes" ) |
| 168 | + void shouldEchoScalarTypes( Value input ) |
| 169 | + { |
| 170 | + // When & Then |
| 171 | + verifyCanEncodeAndDecode( input ); |
| 172 | + } |
| 173 | + |
| 174 | + static Stream<Arguments> listToTest() |
| 175 | + { |
| 176 | + return Stream.of( |
| 177 | + Arguments.of( Values.value( 1, 2, 3, 4 ) ), |
| 178 | + Arguments.of( Values.value( true, false ) ), |
| 179 | + Arguments.of( Values.value( 1.1, 2.2, 3.3 ) ), |
| 180 | + Arguments.of( Values.value( "a", "b", "c", "˚C" ) ), |
| 181 | + Arguments.of( Values.value( NullValue.NULL, NullValue.NULL ) ), |
| 182 | + Arguments.of( Values.value( Values.values( NullValue.NULL, true, "-17∂ßå®", 1.7976931348623157E+308d, -9223372036854775808d ) ) ), |
| 183 | + Arguments.of( new ListValue( parameters( "a", 1, "b", true, "c", 1.1, "d", "˚C", "e", null ) ) ) |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + @ParameterizedTest |
| 188 | + @MethodSource( "listToTest" ) |
| 189 | + void shouldEchoList( Value input ) |
| 190 | + { |
| 191 | + // When & Then |
| 192 | + assertTrue( input instanceof ListValue ); |
| 193 | + verifyCanEncodeAndDecode( input ); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void shouldEchoNestedList() throws Throwable |
| 198 | + { |
| 199 | + Value input = Values.value( toValueStream( listToTest() ) ); |
| 200 | + |
| 201 | + // When & Then |
| 202 | + assertTrue( input instanceof ListValue ); |
| 203 | + verifyCanEncodeAndDecode( input ); |
| 204 | + } |
| 205 | + |
| 206 | + static Stream<Arguments> mapToTest() |
| 207 | + { |
| 208 | + return Stream.of( Arguments.of( parameters( "a", 1, "b", 2, "c", 3, "d", 4 ) ), |
| 209 | + Arguments.of( parameters( "a", true, "b", false ) ), |
| 210 | + Arguments.of( parameters( "a", 1.1, "b", 2.2, "c", 3.3 ) ), |
| 211 | + Arguments.of( parameters( "b", "a", "c", "b", "d", "c", "e", "˚C" ) ), |
| 212 | + Arguments.of( parameters( "a", null ) ), |
| 213 | + Arguments.of( parameters( "a", 1, "b", true, "c", 1.1, "d", "˚C", "e", null ) ) ); |
| 214 | + } |
| 215 | + |
| 216 | + @ParameterizedTest |
| 217 | + @MethodSource( "mapToTest" ) |
| 218 | + void shouldEchoMap( Value input ) |
| 219 | + { |
| 220 | + assertTrue( input instanceof MapValue ); |
| 221 | + // When & Then |
| 222 | + verifyCanEncodeAndDecode( input ); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + void shouldEchoNestedMap() throws Throwable |
| 227 | + { |
| 228 | + MapValue input = new MapValue( |
| 229 | + toValueStream( mapToTest() ) |
| 230 | + .collect( Collectors.toMap( Object::toString, Function.identity() ) ) ); |
| 231 | + |
| 232 | + // When & Then |
| 233 | + verifyCanEncodeAndDecode( input ); |
| 234 | + } |
| 235 | + |
| 236 | + private Stream<Value> toValueStream( Stream<Arguments> arguments ) |
| 237 | + { |
| 238 | + return arguments.map( arg -> { |
| 239 | + Object obj = arg.get()[0]; |
| 240 | + assertTrue( obj instanceof Value ); |
| 241 | + return (Value) obj; |
| 242 | + } ); |
| 243 | + } |
| 244 | + |
| 245 | + private void verifyCanEncodeAndDecode( Value input ) |
| 246 | + { |
| 247 | + // When |
| 248 | + StatementResult cursor = session.run( "RETURN {x} as y", parameters( "x", input ) ); |
| 249 | + |
| 250 | + // Then |
| 251 | + assertThat( cursor.single().get( "y" ), equalTo( input ) ); |
| 252 | + } |
73 | 253 | } |
0 commit comments