From 69fce5366923bc34da40fbd05e26b9655e2e0c7b Mon Sep 17 00:00:00 2001 From: Pontus Melke Date: Sun, 6 Aug 2017 23:01:06 +0200 Subject: [PATCH] Update BOLT integration tests --- .../bolt/v1/runtime/CypherAdapterStream.java | 2 +- .../org/neo4j/bolt/testing/BoltMatchers.java | 34 ++++++++++------- .../bolt/testing/RecordedBoltResponse.java | 5 +-- .../v1/messaging/util/MessageMatchers.java | 5 ++- .../runtime/integration/BoltConnectionIT.java | 19 ++++++---- .../integration/AuthenticationIT.java | 7 ++++ .../integration/TransportSessionIT.java | 38 +++++++++++-------- 7 files changed, 68 insertions(+), 42 deletions(-) diff --git a/community/bolt/src/main/java/org/neo4j/bolt/v1/runtime/CypherAdapterStream.java b/community/bolt/src/main/java/org/neo4j/bolt/v1/runtime/CypherAdapterStream.java index f5cbb3699d322..4398bb0c87e36 100644 --- a/community/bolt/src/main/java/org/neo4j/bolt/v1/runtime/CypherAdapterStream.java +++ b/community/bolt/src/main/java/org/neo4j/bolt/v1/runtime/CypherAdapterStream.java @@ -167,7 +167,7 @@ public AnyValue[] fields() public CypherAdapterRecord reset( QueryResult.Record cypherRecord ) throws BoltIOException { - System.arraycopy( this.fields, 0, cypherRecord.fields(), 0, this.fields.length ); + System.arraycopy( cypherRecord.fields(), 0, this.fields, 0, this.fields.length ); return this; } } diff --git a/community/bolt/src/test/java/org/neo4j/bolt/testing/BoltMatchers.java b/community/bolt/src/test/java/org/neo4j/bolt/testing/BoltMatchers.java index bbcfcb382abcf..e45c932c7f7f2 100644 --- a/community/bolt/src/test/java/org/neo4j/bolt/testing/BoltMatchers.java +++ b/community/bolt/src/test/java/org/neo4j/bolt/testing/BoltMatchers.java @@ -30,11 +30,13 @@ import org.neo4j.bolt.v1.runtime.BoltConnectionFatality; import org.neo4j.bolt.v1.runtime.BoltStateMachine; import org.neo4j.bolt.v1.runtime.StatementProcessor; -import org.neo4j.bolt.v1.runtime.spi.Record; import org.neo4j.function.ThrowingAction; import org.neo4j.function.ThrowingBiConsumer; import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.values.AnyValue; +import org.neo4j.values.AnyValues; +import org.neo4j.values.result.QueryResult; +import org.neo4j.values.storable.TextValue; import static java.lang.String.format; import static org.junit.Assert.assertEquals; @@ -80,14 +82,15 @@ public boolean matches( final Object item ) { final RecordedBoltResponse response = (RecordedBoltResponse) item; return response.message() == SUCCESS && - response.hasMetadata( key ) && - response.metadata( key ).equals( value ); + response.hasMetadata( key ) && + response.metadata( key ).equals( value ); } @Override public void describeTo( Description description ) { - description.appendValue( SUCCESS ).appendText( format( " with metadata %s = %s", key, value.toString() ) ); + description.appendValue( SUCCESS ) + .appendText( format( " with metadata %s = %s", key, value.toString() ) ); } }; } @@ -96,13 +99,16 @@ public static Matcher succeededWithRecord( final Object... { return new BaseMatcher() { + private AnyValue[] anyValues = Arrays.stream( values ).map( AnyValues::of ).toArray( AnyValue[]::new ); + @Override public boolean matches( final Object item ) { + final RecordedBoltResponse response = (RecordedBoltResponse) item; - Record[] records = response.records(); + QueryResult.Record[] records = response.records(); return response.message() == SUCCESS && - Arrays.equals( records[0].fields(), values ); + Arrays.equals( records[0].fields(), anyValues ); } @Override @@ -122,14 +128,15 @@ public boolean matches( final Object item ) { final RecordedBoltResponse response = (RecordedBoltResponse) item; return response.message() == SUCCESS && - response.hasMetadata( key ) && - pattern.matcher( response.metadata( key ).toString() ).matches(); + response.hasMetadata( key ) && + pattern.matcher( ((TextValue) response.metadata( key )).stringValue() ).matches(); } @Override public void describeTo( Description description ) { - description.appendValue( SUCCESS ).appendText( format( " with metadata %s ~ %s", key, pattern.toString() ) ); + description.appendValue( SUCCESS ) + .appendText( format( " with metadata %s ~ %s", key, pattern.toString() ) ); } }; } @@ -162,14 +169,15 @@ public boolean matches( final Object item ) { final RecordedBoltResponse response = (RecordedBoltResponse) item; return response.message() == FAILURE && - response.hasMetadata( "code" ) && - response.metadata( "code" ).equals( stringValue( status.code().serialize() ) ); + response.hasMetadata( "code" ) && + response.metadata( "code" ).equals( stringValue( status.code().serialize() ) ); } @Override public void describeTo( Description description ) { - description.appendValue( FAILURE ).appendText( format( " with status code %s", status.code().serialize() ) ); + description.appendValue( FAILURE ) + .appendText( format( " with status code %s", status.code().serialize() ) ); } }; } @@ -294,7 +302,7 @@ public static void verifyKillsConnection( ThrowingAction } public static void verifyOneResponse( BoltStateMachine.State initialState, - ThrowingBiConsumer transition ) + ThrowingBiConsumer transition ) throws AuthenticationException, BoltConnectionFatality { BoltStateMachine machine = newMachine( initialState ); diff --git a/community/bolt/src/test/java/org/neo4j/bolt/testing/RecordedBoltResponse.java b/community/bolt/src/test/java/org/neo4j/bolt/testing/RecordedBoltResponse.java index 831e138ad2dbc..8af88716e3754 100644 --- a/community/bolt/src/test/java/org/neo4j/bolt/testing/RecordedBoltResponse.java +++ b/community/bolt/src/test/java/org/neo4j/bolt/testing/RecordedBoltResponse.java @@ -25,7 +25,6 @@ import java.util.Map; import org.neo4j.bolt.v1.messaging.BoltResponseMessage; -import org.neo4j.bolt.v1.runtime.spi.Record; import org.neo4j.values.AnyValue; import org.neo4j.values.result.QueryResult; @@ -82,9 +81,9 @@ public void assertRecord( int index, Object... values ) assertArrayEquals( records.get( index ).fields(), values ); } - public Record[] records() + public QueryResult.Record[] records() { - Record[] recordArray = new Record[records.size()]; + QueryResult.Record[] recordArray = new QueryResult.Record[records.size()]; return records.toArray( recordArray ); } } diff --git a/community/bolt/src/test/java/org/neo4j/bolt/v1/messaging/util/MessageMatchers.java b/community/bolt/src/test/java/org/neo4j/bolt/v1/messaging/util/MessageMatchers.java index 501e300bbb273..50d62d0688935 100644 --- a/community/bolt/src/test/java/org/neo4j/bolt/v1/messaging/util/MessageMatchers.java +++ b/community/bolt/src/test/java/org/neo4j/bolt/v1/messaging/util/MessageMatchers.java @@ -180,7 +180,7 @@ public static Matcher msgSuccess( final Map meta protected boolean matchesSafely( ResponseMessage t ) { assertThat( t, instanceOf( SuccessMessage.class ) ); - assertThat( ((SuccessMessage) t).meta(), equalTo( metadata ) ); + assertThat( toRawMap( ((SuccessMessage) t).meta() ), equalTo( metadata ) ); return true; } @@ -200,7 +200,8 @@ public static Matcher msgSuccess( final Matcher actual = toRawMap( ((SuccessMessage) t).meta() ); + assertThat( actual, matcher ); return true; } diff --git a/community/bolt/src/test/java/org/neo4j/bolt/v1/runtime/integration/BoltConnectionIT.java b/community/bolt/src/test/java/org/neo4j/bolt/v1/runtime/integration/BoltConnectionIT.java index b32b0de5efac7..cdd994b9c5cdd 100644 --- a/community/bolt/src/test/java/org/neo4j/bolt/v1/runtime/integration/BoltConnectionIT.java +++ b/community/bolt/src/test/java/org/neo4j/bolt/v1/runtime/integration/BoltConnectionIT.java @@ -38,9 +38,10 @@ import org.neo4j.bolt.v1.runtime.BoltStateMachine; import org.neo4j.bolt.v1.runtime.Neo4jError; import org.neo4j.bolt.v1.runtime.spi.BoltResult; -import org.neo4j.bolt.v1.runtime.spi.Record; import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.values.AnyValue; +import org.neo4j.values.result.QueryResult.Record; +import org.neo4j.values.storable.LongValue; import static java.util.Collections.emptyMap; import static org.hamcrest.CoreMatchers.equalTo; @@ -55,6 +56,8 @@ import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.IGNORED; import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.SUCCESS; import static org.neo4j.helpers.collection.MapUtil.map; +import static org.neo4j.values.storable.Values.longValue; +import static org.neo4j.values.storable.Values.stringValue; @SuppressWarnings( "unchecked" ) public class BoltConnectionIT @@ -156,7 +159,7 @@ public void shouldExecuteStatement() throws Throwable machine.pullAll( recorder ); // Then - recorder.nextResponse().assertRecord( 0, "k" ); + recorder.nextResponse().assertRecord( 0, stringValue( "k" ) ); //assertThat( pulling.next(), streamContaining( StreamMatchers.eqRecord( equalTo( "k" ) ) ) ); } @@ -445,7 +448,7 @@ public void shouldBeAbleToCleanlyRunMultipleSessionsInSingleThread() throws Thro // When I issue a statement in a separate session Object[] stream = runAndPull( secondMachine, "CREATE (a:Person) RETURN id(a)" ); - long id = (long) ((Record) stream[0]).fields()[0]; + long id = ((LongValue) ((Record) stream[0]).fields()[0]).value(); // And when I roll back that first session transaction runAndPull( firstMachine, "ROLLBACK" ); @@ -484,9 +487,9 @@ public void shouldSupportUsingPeriodicCommitInSession() throws Exception assertThat( result.length, equalTo( 1 ) ); Record record = (Record) result[0]; - Object[] fields = record.fields(); + AnyValue[] fields = record.fields(); assertThat( fields.length, equalTo( 1) ); - assertThat( fields[0], equalTo( 150L ) ); + assertThat( fields[0], equalTo( longValue( 150L )) ); /* * 7 tokens have been created for @@ -608,7 +611,7 @@ public void shouldAllowNewTransactionAfterFailure() throws Throwable Object[] stream = runAndPull( machine, "RETURN 1" ); // Then - assertThat( ((Record) stream[0]).fields()[0], equalTo( 1L ) ); + assertThat( ((Record) stream[0]).fields()[0], equalTo( longValue( 1L )) ); } private String createLocalIrisData( BoltStateMachine machine ) throws Exception @@ -628,12 +631,12 @@ private Object[] runAndPull( BoltStateMachine machine, String statement ) throws return runAndPull( machine, statement, EMPTY_PARAMS, SUCCESS ); } - private Object[] runAndPull( BoltStateMachine machine, String statement, Map params ) throws Exception + private Record[] runAndPull( BoltStateMachine machine, String statement, Map params ) throws Exception { return runAndPull( machine, statement, params, SUCCESS ); } - private Object[] runAndPull( BoltStateMachine machine, String statement, Map params, + private Record[] runAndPull( BoltStateMachine machine, String statement, Map params, BoltResponseMessage expectedResponse ) throws Exception { BoltResponseRecorder recorder = new BoltResponseRecorder(); diff --git a/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/AuthenticationIT.java b/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/AuthenticationIT.java index 40663ada70000..337e9a9667538 100644 --- a/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/AuthenticationIT.java +++ b/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/AuthenticationIT.java @@ -55,6 +55,7 @@ import org.neo4j.kernel.internal.Version; import org.neo4j.test.TestGraphDatabaseFactory; import org.neo4j.test.rule.fs.EphemeralFileSystemRule; +import org.neo4j.values.AnyValue; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; @@ -67,6 +68,7 @@ import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.msgSuccess; import static org.neo4j.bolt.v1.transport.integration.TransportTestUtil.eventuallyDisconnects; import static org.neo4j.bolt.v1.transport.integration.TransportTestUtil.eventuallyReceives; +import static org.neo4j.helpers.collection.MapUtil.genericMap; import static org.neo4j.helpers.collection.MapUtil.map; @RunWith( Parameterized.class ) @@ -564,4 +566,9 @@ public boolean gotSpecialMessage() return specialMessage != null; } } + + public static Map mapValue( Object... objects ) + { + return genericMap( objects ); + } } diff --git a/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/TransportSessionIT.java b/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/TransportSessionIT.java index 862c14cb27190..d28659ad5dfa9 100644 --- a/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/TransportSessionIT.java +++ b/community/bolt/src/test/java/org/neo4j/bolt/v1/transport/integration/TransportSessionIT.java @@ -41,6 +41,8 @@ import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.helpers.HostnamePort; import org.neo4j.kernel.api.exceptions.Status; +import org.neo4j.values.storable.Values; +import org.neo4j.values.virtual.VirtualValues; import static java.util.Arrays.asList; import static java.util.Collections.emptyMap; @@ -64,6 +66,10 @@ import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.msgSuccess; import static org.neo4j.bolt.v1.runtime.spi.StreamMatchers.eqRecord; import static org.neo4j.bolt.v1.transport.integration.TransportTestUtil.eventuallyReceives; +import static org.neo4j.values.storable.Values.NO_VALUE; +import static org.neo4j.values.storable.Values.longValue; +import static org.neo4j.values.storable.Values.stringValue; +import static org.neo4j.values.virtual.VirtualValues.list; @SuppressWarnings( "unchecked" ) @RunWith( Parameterized.class ) @@ -143,9 +149,9 @@ public void shouldRunSimpleStatement() throws Throwable msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( asList( "a", "a_squared" ) ) ), hasKey( "result_available_after" ) ) ), - msgRecord( eqRecord( equalTo( 1L ), equalTo( 1L ) ) ), - msgRecord( eqRecord( equalTo( 2L ), equalTo( 4L ) ) ), - msgRecord( eqRecord( equalTo( 3L ), equalTo( 9L ) ) ), + msgRecord( eqRecord( equalTo( longValue( 1L ) ), equalTo( longValue( 1L ) ) ) ), + msgRecord( eqRecord( equalTo( longValue( 2L ) ), equalTo( longValue( 4L ) ) ) ), + msgRecord( eqRecord( equalTo( longValue( 3L ) ), equalTo( longValue( 9L ) ) ) ), msgSuccess( allOf( hasEntry( is( "type" ), equalTo( "r" ) ), hasKey( "result_consumed_after" ) ) ) ) ); } @@ -198,7 +204,7 @@ public void shouldBeAbleToRunQueryAfterAckFailure() throws Throwable assertThat( client, eventuallyReceives( msgSuccess(), msgSuccess(), - msgRecord( eqRecord( equalTo( 1L ) ) ), + msgRecord( eqRecord( equalTo( longValue( 1L ) ) ) ), msgSuccess() ) ); } @@ -218,7 +224,7 @@ public void shouldRunProcedure() throws Throwable msgSuccess(), msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "age" ) ) ), hasKey( "result_available_after" ) ) ), - msgRecord( eqRecord( equalTo( 2L ) ) ), + msgRecord( eqRecord( equalTo( longValue( 2L ) ) ) ), msgSuccess() ) ); // When @@ -230,7 +236,7 @@ public void shouldRunProcedure() throws Throwable assertThat( client, eventuallyReceives( msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "label" ) ) ), hasKey( "result_available_after" ) ) ), - msgRecord( eqRecord( Matchers.equalTo( "Test" ) ) ), + msgRecord( eqRecord( Matchers.equalTo( stringValue( "Test" ) ) ) ), msgSuccess() ) ); } @@ -324,7 +330,7 @@ public void shouldNotLeakStatsToNextStatement() throws Throwable // Then assertThat( client, eventuallyReceives( msgSuccess(), - msgRecord( eqRecord( equalTo( 1L ) ) ), + msgRecord( eqRecord( equalTo( longValue( 1L ) ) ) ), msgSuccess( allOf( hasEntry( is( "type" ), equalTo( "r" ) ), hasKey( "result_consumed_after" ) ) ) ) ); } @@ -373,7 +379,7 @@ public void shouldFailNicelyOnPoints() throws Throwable msgSuccess(), msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "p" ) ) ), hasKey( "result_available_after" ) ) ), - msgRecord( eqRecord( nullValue() ) ), + msgRecord( eqRecord( equalTo( NO_VALUE ) ) ), msgFailure( Status.Request.Invalid, "Point is not yet supported as a return type in Bolt" ) ) ); } @@ -393,8 +399,8 @@ public void shouldFailNicelyOnNullKeysInMap() throws Throwable //Given HashMap params = new HashMap<>(); HashMap inner = new HashMap<>(); - inner.put(null, 42L); - inner.put("foo", 1337L); + inner.put( null, 42L ); + inner.put( "foo", 1337L ); params.put( "p", inner ); // When @@ -409,8 +415,9 @@ public void shouldFailNicelyOnNullKeysInMap() throws Throwable assertThat( client, eventuallyReceives( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyReceives( msgSuccess(), - msgFailure( Status.Request.Invalid, "Value `null` is not supported as key in maps, must be a non-nullable string."), - msgIgnored())); + msgFailure( Status.Request.Invalid, + "Value `null` is not supported as key in maps, must be a non-nullable string." ), + msgIgnored() ) ); client.send( TransportTestUtil.chunk( ackFailure(), run( "RETURN 1" ), pullAll() ) ); @@ -418,7 +425,7 @@ public void shouldFailNicelyOnNullKeysInMap() throws Throwable assertThat( client, eventuallyReceives( msgSuccess(), msgSuccess(), - msgRecord( eqRecord( equalTo( 1L ) ) ), + msgRecord( eqRecord( equalTo( longValue( 1L ) ) ) ), msgSuccess() ) ); } @@ -437,7 +444,8 @@ public void shouldFailNicelyWhenDroppingUnknownIndex() throws Throwable assertThat( client, eventuallyReceives( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyReceives( msgSuccess(), - msgFailure( Status.Schema.IndexDropFailed, "Unable to drop index on :Movie12345(id): No such INDEX ON :Movie12345(id)."), - msgIgnored()) ); + msgFailure( Status.Schema.IndexDropFailed, + "Unable to drop index on :Movie12345(id): No such INDEX ON :Movie12345(id)." ), + msgIgnored() ) ); } }