Skip to content

Commit

Permalink
Update BOLT integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Aug 21, 2017
1 parent c747f51 commit 69fce53
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 42 deletions.
Expand Up @@ -167,7 +167,7 @@ public AnyValue[] fields()


public CypherAdapterRecord reset( QueryResult.Record cypherRecord ) throws BoltIOException 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; return this;
} }
} }
Expand Down
Expand Up @@ -30,11 +30,13 @@
import org.neo4j.bolt.v1.runtime.BoltConnectionFatality; import org.neo4j.bolt.v1.runtime.BoltConnectionFatality;
import org.neo4j.bolt.v1.runtime.BoltStateMachine; import org.neo4j.bolt.v1.runtime.BoltStateMachine;
import org.neo4j.bolt.v1.runtime.StatementProcessor; import org.neo4j.bolt.v1.runtime.StatementProcessor;
import org.neo4j.bolt.v1.runtime.spi.Record;
import org.neo4j.function.ThrowingAction; import org.neo4j.function.ThrowingAction;
import org.neo4j.function.ThrowingBiConsumer; import org.neo4j.function.ThrowingBiConsumer;
import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.values.AnyValue; 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 java.lang.String.format;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -80,14 +82,15 @@ public boolean matches( final Object item )
{ {
final RecordedBoltResponse response = (RecordedBoltResponse) item; final RecordedBoltResponse response = (RecordedBoltResponse) item;
return response.message() == SUCCESS && return response.message() == SUCCESS &&
response.hasMetadata( key ) && response.hasMetadata( key ) &&
response.metadata( key ).equals( value ); response.metadata( key ).equals( value );
} }


@Override @Override
public void describeTo( Description description ) 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() ) );
} }
}; };
} }
Expand All @@ -96,13 +99,16 @@ public static Matcher<RecordedBoltResponse> succeededWithRecord( final Object...
{ {
return new BaseMatcher<RecordedBoltResponse>() return new BaseMatcher<RecordedBoltResponse>()
{ {
private AnyValue[] anyValues = Arrays.stream( values ).map( AnyValues::of ).toArray( AnyValue[]::new );

@Override @Override
public boolean matches( final Object item ) public boolean matches( final Object item )
{ {

final RecordedBoltResponse response = (RecordedBoltResponse) item; final RecordedBoltResponse response = (RecordedBoltResponse) item;
Record[] records = response.records(); QueryResult.Record[] records = response.records();
return response.message() == SUCCESS && return response.message() == SUCCESS &&
Arrays.equals( records[0].fields(), values ); Arrays.equals( records[0].fields(), anyValues );
} }


@Override @Override
Expand All @@ -122,14 +128,15 @@ public boolean matches( final Object item )
{ {
final RecordedBoltResponse response = (RecordedBoltResponse) item; final RecordedBoltResponse response = (RecordedBoltResponse) item;
return response.message() == SUCCESS && return response.message() == SUCCESS &&
response.hasMetadata( key ) && response.hasMetadata( key ) &&
pattern.matcher( response.metadata( key ).toString() ).matches(); pattern.matcher( ((TextValue) response.metadata( key )).stringValue() ).matches();
} }


@Override @Override
public void describeTo( Description description ) 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() ) );
} }
}; };
} }
Expand Down Expand Up @@ -162,14 +169,15 @@ public boolean matches( final Object item )
{ {
final RecordedBoltResponse response = (RecordedBoltResponse) item; final RecordedBoltResponse response = (RecordedBoltResponse) item;
return response.message() == FAILURE && return response.message() == FAILURE &&
response.hasMetadata( "code" ) && response.hasMetadata( "code" ) &&
response.metadata( "code" ).equals( stringValue( status.code().serialize() ) ); response.metadata( "code" ).equals( stringValue( status.code().serialize() ) );
} }


@Override @Override
public void describeTo( Description description ) 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() ) );
} }
}; };
} }
Expand Down Expand Up @@ -294,7 +302,7 @@ public static void verifyKillsConnection( ThrowingAction<BoltConnectionFatality>
} }


public static void verifyOneResponse( BoltStateMachine.State initialState, public static void verifyOneResponse( BoltStateMachine.State initialState,
ThrowingBiConsumer<BoltStateMachine, BoltResponseRecorder, BoltConnectionFatality> transition ) ThrowingBiConsumer<BoltStateMachine,BoltResponseRecorder,BoltConnectionFatality> transition )
throws AuthenticationException, BoltConnectionFatality throws AuthenticationException, BoltConnectionFatality
{ {
BoltStateMachine machine = newMachine( initialState ); BoltStateMachine machine = newMachine( initialState );
Expand Down
Expand Up @@ -25,7 +25,6 @@
import java.util.Map; import java.util.Map;


import org.neo4j.bolt.v1.messaging.BoltResponseMessage; import org.neo4j.bolt.v1.messaging.BoltResponseMessage;
import org.neo4j.bolt.v1.runtime.spi.Record;
import org.neo4j.values.AnyValue; import org.neo4j.values.AnyValue;
import org.neo4j.values.result.QueryResult; import org.neo4j.values.result.QueryResult;


Expand Down Expand Up @@ -82,9 +81,9 @@ public void assertRecord( int index, Object... values )
assertArrayEquals( records.get( index ).fields(), 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 ); return records.toArray( recordArray );
} }
} }
Expand Up @@ -180,7 +180,7 @@ public static Matcher<ResponseMessage> msgSuccess( final Map<String,Object> meta
protected boolean matchesSafely( ResponseMessage t ) protected boolean matchesSafely( ResponseMessage t )
{ {
assertThat( t, instanceOf( SuccessMessage.class ) ); assertThat( t, instanceOf( SuccessMessage.class ) );
assertThat( ((SuccessMessage) t).meta(), equalTo( metadata ) ); assertThat( toRawMap( ((SuccessMessage) t).meta() ), equalTo( metadata ) );
return true; return true;
} }


Expand All @@ -200,7 +200,8 @@ public static Matcher<ResponseMessage> msgSuccess( final Matcher<Map<? extends S
protected boolean matchesSafely( ResponseMessage t ) protected boolean matchesSafely( ResponseMessage t )
{ {
assertThat( t, instanceOf( SuccessMessage.class ) ); assertThat( t, instanceOf( SuccessMessage.class ) );
assertThat( toRawMap( ((SuccessMessage) t).meta() ), matcher ); Map<String,Object> actual = toRawMap( ((SuccessMessage) t).meta() );
assertThat( actual, matcher );
return true; return true;
} }


Expand Down
Expand Up @@ -38,9 +38,10 @@
import org.neo4j.bolt.v1.runtime.BoltStateMachine; import org.neo4j.bolt.v1.runtime.BoltStateMachine;
import org.neo4j.bolt.v1.runtime.Neo4jError; import org.neo4j.bolt.v1.runtime.Neo4jError;
import org.neo4j.bolt.v1.runtime.spi.BoltResult; 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.kernel.api.exceptions.Status;
import org.neo4j.values.AnyValue; 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 java.util.Collections.emptyMap;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
Expand All @@ -55,6 +56,8 @@
import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.IGNORED; import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.IGNORED;
import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.SUCCESS; import static org.neo4j.bolt.v1.messaging.BoltResponseMessage.SUCCESS;
import static org.neo4j.helpers.collection.MapUtil.map; 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" ) @SuppressWarnings( "unchecked" )
public class BoltConnectionIT public class BoltConnectionIT
Expand Down Expand Up @@ -156,7 +159,7 @@ public void shouldExecuteStatement() throws Throwable
machine.pullAll( recorder ); machine.pullAll( recorder );


// Then // Then
recorder.nextResponse().assertRecord( 0, "k" ); recorder.nextResponse().assertRecord( 0, stringValue( "k" ) );
//assertThat( pulling.next(), streamContaining( StreamMatchers.eqRecord( equalTo( "k" ) ) ) ); //assertThat( pulling.next(), streamContaining( StreamMatchers.eqRecord( equalTo( "k" ) ) ) );
} }


Expand Down Expand Up @@ -445,7 +448,7 @@ public void shouldBeAbleToCleanlyRunMultipleSessionsInSingleThread() throws Thro


// When I issue a statement in a separate session // When I issue a statement in a separate session
Object[] stream = runAndPull( secondMachine, "CREATE (a:Person) RETURN id(a)" ); 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 // And when I roll back that first session transaction
runAndPull( firstMachine, "ROLLBACK" ); runAndPull( firstMachine, "ROLLBACK" );
Expand Down Expand Up @@ -484,9 +487,9 @@ public void shouldSupportUsingPeriodicCommitInSession() throws Exception
assertThat( result.length, equalTo( 1 ) ); assertThat( result.length, equalTo( 1 ) );
Record record = (Record) result[0]; Record record = (Record) result[0];


Object[] fields = record.fields(); AnyValue[] fields = record.fields();
assertThat( fields.length, equalTo( 1) ); assertThat( fields.length, equalTo( 1) );
assertThat( fields[0], equalTo( 150L ) ); assertThat( fields[0], equalTo( longValue( 150L )) );


/* /*
* 7 tokens have been created for * 7 tokens have been created for
Expand Down Expand Up @@ -608,7 +611,7 @@ public void shouldAllowNewTransactionAfterFailure() throws Throwable
Object[] stream = runAndPull( machine, "RETURN 1" ); Object[] stream = runAndPull( machine, "RETURN 1" );


// Then // 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 private String createLocalIrisData( BoltStateMachine machine ) throws Exception
Expand All @@ -628,12 +631,12 @@ private Object[] runAndPull( BoltStateMachine machine, String statement ) throws
return runAndPull( machine, statement, EMPTY_PARAMS, SUCCESS ); return runAndPull( machine, statement, EMPTY_PARAMS, SUCCESS );
} }


private Object[] runAndPull( BoltStateMachine machine, String statement, Map<String, Object> params ) throws Exception private Record[] runAndPull( BoltStateMachine machine, String statement, Map<String, Object> params ) throws Exception
{ {
return runAndPull( machine, statement, params, SUCCESS ); return runAndPull( machine, statement, params, SUCCESS );
} }


private Object[] runAndPull( BoltStateMachine machine, String statement, Map<String, Object> params, private Record[] runAndPull( BoltStateMachine machine, String statement, Map<String, Object> params,
BoltResponseMessage expectedResponse ) throws Exception BoltResponseMessage expectedResponse ) throws Exception
{ {
BoltResponseRecorder recorder = new BoltResponseRecorder(); BoltResponseRecorder recorder = new BoltResponseRecorder();
Expand Down
Expand Up @@ -55,6 +55,7 @@
import org.neo4j.kernel.internal.Version; import org.neo4j.kernel.internal.Version;
import org.neo4j.test.TestGraphDatabaseFactory; import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule; import org.neo4j.test.rule.fs.EphemeralFileSystemRule;
import org.neo4j.values.AnyValue;


import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
Expand All @@ -67,6 +68,7 @@
import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.msgSuccess; 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.eventuallyDisconnects;
import static org.neo4j.bolt.v1.transport.integration.TransportTestUtil.eventuallyReceives; 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; import static org.neo4j.helpers.collection.MapUtil.map;


@RunWith( Parameterized.class ) @RunWith( Parameterized.class )
Expand Down Expand Up @@ -564,4 +566,9 @@ public boolean gotSpecialMessage()
return specialMessage != null; return specialMessage != null;
} }
} }

public static Map<String,AnyValue> mapValue( Object... objects )
{
return genericMap( objects );
}
} }
Expand Up @@ -41,6 +41,8 @@
import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.HostnamePort; import org.neo4j.helpers.HostnamePort;
import org.neo4j.kernel.api.exceptions.Status; 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.Arrays.asList;
import static java.util.Collections.emptyMap; import static java.util.Collections.emptyMap;
Expand All @@ -64,6 +66,10 @@
import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.msgSuccess; 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.runtime.spi.StreamMatchers.eqRecord;
import static org.neo4j.bolt.v1.transport.integration.TransportTestUtil.eventuallyReceives; 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" ) @SuppressWarnings( "unchecked" )
@RunWith( Parameterized.class ) @RunWith( Parameterized.class )
Expand Down Expand Up @@ -143,9 +149,9 @@ public void shouldRunSimpleStatement() throws Throwable
msgSuccess( msgSuccess(
allOf( hasEntry( is( "fields" ), equalTo( asList( "a", "a_squared" ) ) ), allOf( hasEntry( is( "fields" ), equalTo( asList( "a", "a_squared" ) ) ),
hasKey( "result_available_after" ) ) ), hasKey( "result_available_after" ) ) ),
msgRecord( eqRecord( equalTo( 1L ), equalTo( 1L ) ) ), msgRecord( eqRecord( equalTo( longValue( 1L ) ), equalTo( longValue( 1L ) ) ) ),
msgRecord( eqRecord( equalTo( 2L ), equalTo( 4L ) ) ), msgRecord( eqRecord( equalTo( longValue( 2L ) ), equalTo( longValue( 4L ) ) ) ),
msgRecord( eqRecord( equalTo( 3L ), equalTo( 9L ) ) ), msgRecord( eqRecord( equalTo( longValue( 3L ) ), equalTo( longValue( 9L ) ) ) ),
msgSuccess( allOf( hasEntry( is( "type" ), equalTo( "r" ) ), msgSuccess( allOf( hasEntry( is( "type" ), equalTo( "r" ) ),
hasKey( "result_consumed_after" ) ) ) ) ); hasKey( "result_consumed_after" ) ) ) ) );
} }
Expand Down Expand Up @@ -198,7 +204,7 @@ public void shouldBeAbleToRunQueryAfterAckFailure() throws Throwable
assertThat( client, eventuallyReceives( assertThat( client, eventuallyReceives(
msgSuccess(), msgSuccess(),
msgSuccess(), msgSuccess(),
msgRecord( eqRecord( equalTo( 1L ) ) ), msgRecord( eqRecord( equalTo( longValue( 1L ) ) ) ),
msgSuccess() ) ); msgSuccess() ) );
} }


Expand All @@ -218,7 +224,7 @@ public void shouldRunProcedure() throws Throwable
msgSuccess(), msgSuccess(),
msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "age" ) ) ), msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "age" ) ) ),
hasKey( "result_available_after" ) ) ), hasKey( "result_available_after" ) ) ),
msgRecord( eqRecord( equalTo( 2L ) ) ), msgRecord( eqRecord( equalTo( longValue( 2L ) ) ) ),
msgSuccess() ) ); msgSuccess() ) );


// When // When
Expand All @@ -230,7 +236,7 @@ public void shouldRunProcedure() throws Throwable
assertThat( client, eventuallyReceives( assertThat( client, eventuallyReceives(
msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "label" ) ) ), msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "label" ) ) ),
hasKey( "result_available_after" ) ) ), hasKey( "result_available_after" ) ) ),
msgRecord( eqRecord( Matchers.equalTo( "Test" ) ) ), msgRecord( eqRecord( Matchers.equalTo( stringValue( "Test" ) ) ) ),
msgSuccess() msgSuccess()
) ); ) );
} }
Expand Down Expand Up @@ -324,7 +330,7 @@ public void shouldNotLeakStatsToNextStatement() throws Throwable
// Then // Then
assertThat( client, eventuallyReceives( assertThat( client, eventuallyReceives(
msgSuccess(), msgSuccess(),
msgRecord( eqRecord( equalTo( 1L ) ) ), msgRecord( eqRecord( equalTo( longValue( 1L ) ) ) ),
msgSuccess( allOf( hasEntry( is( "type" ), equalTo( "r" ) ), msgSuccess( allOf( hasEntry( is( "type" ), equalTo( "r" ) ),
hasKey( "result_consumed_after" ) ) ) ) ); hasKey( "result_consumed_after" ) ) ) ) );
} }
Expand Down Expand Up @@ -373,7 +379,7 @@ public void shouldFailNicelyOnPoints() throws Throwable
msgSuccess(), msgSuccess(),
msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "p" ) ) ), msgSuccess( allOf( hasEntry( is( "fields" ), equalTo( singletonList( "p" ) ) ),
hasKey( "result_available_after" ) ) ), 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" ) ) ); msgFailure( Status.Request.Invalid, "Point is not yet supported as a return type in Bolt" ) ) );
} }


Expand All @@ -393,8 +399,8 @@ public void shouldFailNicelyOnNullKeysInMap() throws Throwable
//Given //Given
HashMap<String,Object> params = new HashMap<>(); HashMap<String,Object> params = new HashMap<>();
HashMap<String,Object> inner = new HashMap<>(); HashMap<String,Object> inner = new HashMap<>();
inner.put(null, 42L); inner.put( null, 42L );
inner.put("foo", 1337L); inner.put( "foo", 1337L );
params.put( "p", inner ); params.put( "p", inner );


// When // When
Expand All @@ -409,16 +415,17 @@ public void shouldFailNicelyOnNullKeysInMap() throws Throwable
assertThat( client, eventuallyReceives( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyReceives( new byte[]{0, 0, 0, 1} ) );
assertThat( client, eventuallyReceives( assertThat( client, eventuallyReceives(
msgSuccess(), msgSuccess(),
msgFailure( Status.Request.Invalid, "Value `null` is not supported as key in maps, must be a non-nullable string."), msgFailure( Status.Request.Invalid,
msgIgnored())); "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() ) ); client.send( TransportTestUtil.chunk( ackFailure(), run( "RETURN 1" ), pullAll() ) );


// Then // Then
assertThat( client, eventuallyReceives( assertThat( client, eventuallyReceives(
msgSuccess(), msgSuccess(),
msgSuccess(), msgSuccess(),
msgRecord( eqRecord( equalTo( 1L ) ) ), msgRecord( eqRecord( equalTo( longValue( 1L ) ) ) ),
msgSuccess() ) ); msgSuccess() ) );
} }


Expand All @@ -437,7 +444,8 @@ public void shouldFailNicelyWhenDroppingUnknownIndex() throws Throwable
assertThat( client, eventuallyReceives( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyReceives( new byte[]{0, 0, 0, 1} ) );
assertThat( client, eventuallyReceives( assertThat( client, eventuallyReceives(
msgSuccess(), msgSuccess(),
msgFailure( Status.Schema.IndexDropFailed, "Unable to drop index on :Movie12345(id): No such INDEX ON :Movie12345(id)."), msgFailure( Status.Schema.IndexDropFailed,
msgIgnored()) ); "Unable to drop index on :Movie12345(id): No such INDEX ON :Movie12345(id)." ),
msgIgnored() ) );
} }
} }

0 comments on commit 69fce53

Please sign in to comment.