private MapValue mapValue( Object... kv ) { assert kv.length % 2 == 0; String[] keys = new String[kv.length / 2]; AnyValue[] values = new AnyValue[kv.length / 2]; for ( int i = 0; i < kv.length; i += 2 ) { keys[i / 2] = (String) kv[i]; values[i / 2] = (AnyValue) kv[i + 1]; } return map( keys, values ); } }
private MapValue props( Object... keyValue ) { String[] keys = new String[keyValue.length / 2]; AnyValue[] values = new AnyValue[keyValue.length / 2]; for ( int i = 0; i < keyValue.length; i++ ) { if ( i % 2 == 0 ) { keys[i / 2] = (String) keyValue[i]; } else { values[i / 2] = (AnyValue) keyValue[i]; } } return VirtualValues.map( keys, values ); } }
private static MapValue singletonMap( String key, Object value ) { return VirtualValues.map( new String[]{key}, new AnyValue[]{ValueUtils.of( value )} ); } }
public static VirtualValue map( Object... keyOrVal ) { assert keyOrVal.length % 2 == 0; String[] keys = new String[keyOrVal.length / 2]; AnyValue[] values = new AnyValue[keyOrVal.length / 2]; for ( int i = 0; i < keyOrVal.length; i += 2 ) { keys[i / 2] = (String) keyOrVal[i]; values[i / 2] = toAnyValue( keyOrVal[i + 1] ); } return VirtualValues.map( keys, values ); }
private MapValue singletonMap( String key, Object value ) { return VirtualValues.map( new String[]{key}, new AnyValue[]{ValueUtils.of( value )} ); }
@Test public void shouldHandleMapsWithLists() { // Given MapValue map = map( new String[]{"foo", "bar"}, new AnyValue[]{longValue( 42L ), list( stringValue( "baz" ) )} ); // When map.writeTo( converter ); // Then Map<?,?> value = (Map<?,?>) converter.value(); assertThat( value.get( "foo" ), equalTo( 42L ) ); assertThat( value.get( "bar" ), equalTo( singletonList( "baz" ) ) ); assertThat( value.size(), equalTo( 2 ) ); } }
@Test public void shouldSerializeNode() throws Throwable { NodeValue nodeValue = nodeValue( 12L, stringArray( "User", "Banana" ), map( new String[]{"name", "age"}, new AnyValue[]{stringValue( "Bob" ), intValue( 14 )} ) ); assertThat( serialized( nodeValue ), equalTo( "B1 71 91 B3 4E 0C 92 84 55 73 65 72 86 42 61 6E" + lineSeparator() + "61 6E 61 A2 84 6E 61 6D 65 83 42 6F 62 83 61 67" + lineSeparator() + "65 0E" ) ); }
@Test public void shouldSerializeNode() throws Throwable { NodeValue nodeValue = nodeValue( 12L, stringArray( "User", "Banana" ), VirtualValues .map( new String[]{"name", "age"}, new AnyValue[]{stringValue( "Bob" ), intValue( 14 )} ) ); assertThat( serialized( nodeValue ), equalTo( "B1 71 91 B3 4E 0C 92 84 55 73 65 72 86 42 61 6E" + lineSeparator() + "61 6E 61 A2 84 6E 61 6D 65 83 42 6F 62 83 61 67" + lineSeparator() + "65 0E" ) ); }
@Test public void shouldHandleListWithMaps() { // Given ListValue list = list( longValue( 42L ), map( new String[]{"foo", "bar"}, new AnyValue[]{longValue( 42L ), stringValue( "baz" )} ) ); // When list.writeTo( converter ); // Then List<?> converted = (List<?>) converter.value(); assertThat( converted.get( 0 ), equalTo( 42L ) ); assertThat( converted.get( 1 ), equalTo( MapUtil.map( "foo", 42L, "bar", "baz" ) ) ); }
@Test public void shouldWriteSuccessMessage() throws Exception { PackOutput output = mock( PackOutput.class ); Neo4jPack.Packer packer = mock( Neo4jPack.Packer.class ); BoltResponseMessageWriterV1 writer = newWriter( output, packer ); MapValue metadata = map( new String[]{"a", "b", "c"}, new AnyValue[]{intValue( 1 ), stringValue( "2" ), date( 2010, 02, 02 )} ); writer.write( new SuccessMessage( metadata ) ); InOrder inOrder = inOrder( output, packer ); inOrder.verify( output ).beginMessage(); inOrder.verify( packer ).pack( metadata ); inOrder.verify( output ).messageSucceeded(); }
private void testUnpackableStructParametersWithKnownType( Neo4jPack packerForSerialization, AnyValue parameterValue, String expectedMessage ) throws Exception { String statement = "RETURN $x"; MapValue parameters = VirtualValues.map( new String[]{"x"}, new AnyValue[]{parameterValue } ); BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerForSerialization, new RunMessage( statement, parameters ) ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).handleExternalFailure( eq( Neo4jError.from( Status.Statement.TypeError, expectedMessage ) ), any() ); }
@Test public void shouldHandleMaps() { // Given MapValue map = map( new String[]{"foo", "bar"}, new AnyValue[]{longValue( 42L ), stringValue( "baz" )} ); // When map.writeTo( converter ); // Then assertThat( converter.value(), equalTo( MapUtil.map( "foo", 42L, "bar", "baz" ) ) ); }
@Test void shouldDecodeBeginMessage() throws Exception { BeginMessage originalMessage = new BeginMessage( VirtualValues.map( new String[]{"tx_metadata", "tx_timeout"}, new AnyValue[]{EMPTY, longValue( 10000 )} ) ); assertOriginalMessageEqualsToDecoded( originalMessage, decoder ); } }
@Test void shouldDecodeRunMessage() throws Exception { testMessageDecoding( new RunMessage( "RETURN $answer", map( new String[]{"answer"}, new AnyValue[]{stringValue( "42" )} ) ) ); }
@Test public void shouldSerializeRelationship() throws Throwable { RelationshipValue rel = relationshipValue( 12L, nodeValue( 1L, stringArray(), VirtualValues.EMPTY_MAP ), nodeValue( 2L, stringArray(), VirtualValues.EMPTY_MAP ), stringValue( "KNOWS" ), VirtualValues.map( new String[]{"name", "age"}, new AnyValue[]{stringValue( "Bob" ), intValue( 14 )} ) ); assertThat( serialized( rel ), equalTo( "B1 71 91 B5 52 0C 01 02 85 4B 4E 4F 57 53 A2 84" + lineSeparator() + "6E 61 6D 65 83 42 6F 62 83 61 67 65 0E" ) ); }
@Test public void shouldSerializeRelationship() throws Throwable { RelationshipValue rel = relationshipValue( 12L, nodeValue( 1L, stringArray(), VirtualValues.EMPTY_MAP ), nodeValue( 2L, stringArray(), VirtualValues.EMPTY_MAP ), stringValue( "KNOWS" ), map( new String[]{"name", "age"}, new AnyValue[]{stringValue( "Bob" ), intValue( 14 )} ) ); assertThat( serialized( rel ), equalTo( "B1 71 91 B5 52 0C 01 02 85 4B 4E 4F 57 53 A2 84" + lineSeparator() + "6E 61 6D 65 83 42 6F 62 83 61 67 65 0E" ) ); }
private <T extends AnyValue> void testSendingAndReceivingOfBoltV2Value( T value ) throws Exception { negotiateBoltV2(); connection.send( util.chunk( new RunMessage( "RETURN $value", map( new String[]{"value"}, new AnyValue[]{value} ) ), PullAllMessage.INSTANCE ) ); assertThat( connection, util.eventuallyReceives( msgSuccess(), msgRecord( eqRecord( equalTo( value ) ) ), msgSuccess() ) ); }
private <T extends AnyValue> void testSendingOfBoltV2Value( T value ) throws Exception { negotiateBoltV2(); connection.send( util.chunk( new RunMessage( "CREATE (n:Node {value: $value}) RETURN 42", map( new String[]{"value"}, new AnyValue[]{value} ) ), PullAllMessage.INSTANCE ) ); assertThat( connection, util.eventuallyReceives( msgSuccess(), msgRecord( eqRecord( equalTo( longValue( 42 ) ) ) ), msgSuccess() ) ); }
@Test public void shouldNotTouchValuesThatDoNotNeedConversion() { // Given ListValue nodeList = VirtualValues.list( nodeProxyValue, directNodeValue ); ListValue relationshipList = VirtualValues.list( relationshipProxyValue, directRelationshipValue ); MapValue nodeMap = VirtualValues.map( new String[]{"a", "b"}, new AnyValue[]{nodeProxyValue, directNodeValue} ); MapValue relationshipMap = VirtualValues.map( new String[]{"a", "b"}, new AnyValue[]{relationshipProxyValue, directRelationshipValue} ); // Verify verifyDoesNotTouchValue( nodeProxyValue ); verifyDoesNotTouchValue( relationshipProxyValue ); verifyDoesNotTouchValue( directNodeValue ); verifyDoesNotTouchValue( directRelationshipValue ); verifyDoesNotTouchValue( nodeList ); verifyDoesNotTouchValue( relationshipList ); verifyDoesNotTouchValue( nodeMap ); verifyDoesNotTouchValue( relationshipMap ); // This is not an exhaustive test since the other cases are very uninteresting... verifyDoesNotTouchValue( Values.booleanValue( false ) ); verifyDoesNotTouchValue( Values.stringValue( "Hello" ) ); verifyDoesNotTouchValue( Values.longValue( 42L ) ); // ... }
@Test void shouldDecodeAckFailure() throws Exception { Neo4jPackV1 neo4jPack = new Neo4jPackV1(); RunMessage originalMessage = new RunMessage( "UNWIND range(1, 10) AS x RETURN x, $y", map( new String[]{"y"}, new AnyValue[]{longValue( 42 )} ) ); PackedInputArray innput = new PackedInputArray( serialize( neo4jPack, originalMessage ) ); Unpacker unpacker = neo4jPack.newUnpacker( innput ); // these two steps are executed before decoding in order to select a correct decoder unpacker.unpackStructHeader(); unpacker.unpackStructSignature(); RequestMessage deserializedMessage = decoder.decode( unpacker ); assertEquals( originalMessage, deserializedMessage ); } }