From eb413c0f8561f2f22372083cd57bf99835965342 Mon Sep 17 00:00:00 2001 From: Dmitriy Tverdiakov <11927660+injectives@users.noreply.github.com> Date: Fri, 26 Feb 2021 15:09:18 +0000 Subject: [PATCH] Updating tests according to the server-side changes (#834) --- .../integration/async/AsyncSessionIT.java | 148 +++++++++--------- .../integration/async/AsyncTransactionIT.java | 18 +-- .../integration/reactive/RxTransactionIT.java | 8 +- .../neo4j/driver/internal/util/Matchers.java | 11 +- .../driver/stress/BlockingWrongQuery.java | 2 +- .../driver/stress/BlockingWrongQueryInTx.java | 2 +- .../stress/BlockingWrongQueryWithRetries.java | 2 +- 7 files changed, 98 insertions(+), 93 deletions(-) diff --git a/driver/src/test/java/org/neo4j/driver/integration/async/AsyncSessionIT.java b/driver/src/test/java/org/neo4j/driver/integration/async/AsyncSessionIT.java index 00cc1e35f3..034b61fbd4 100644 --- a/driver/src/test/java/org/neo4j/driver/integration/async/AsyncSessionIT.java +++ b/driver/src/test/java/org/neo4j/driver/integration/async/AsyncSessionIT.java @@ -154,7 +154,7 @@ void shouldFailForIncorrectQuery() ResultCursor cursor = await( session.runAsync( "RETURN" ) ); Exception e = assertThrows( Exception.class, () -> await( cursor.nextAsync() ) ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); } @Test @@ -446,9 +446,9 @@ void shouldFailForEachWhenActionFails() IOException e = assertThrows( IOException.class, () -> await( cursor.forEachAsync( record -> - { - throw new CompletionException( error ); - } ) ) ); + { + throw new CompletionException( error ); + } ) ) ); assertEquals( error, e ); } @@ -462,7 +462,7 @@ void shouldConvertToListWithEmptyCursor() void shouldConvertToListWithNonEmptyCursor() { testList( "UNWIND range(1, 100, 10) AS x RETURN x", - Arrays.asList( 1L, 11L, 21L, 31L, 41L, 51L, 61L, 71L, 81L, 91L ) ); + Arrays.asList( 1L, 11L, 21L, 31L, 41L, 51L, 61L, 71L, 81L, 91L ) ); } @Test @@ -489,9 +489,9 @@ void shouldFailWhenListTransformationFunctionFails() RuntimeException e = assertThrows( RuntimeException.class, () -> await( cursor.listAsync( record -> - { - throw error; - } ) ) ); + { + throw error; + } ) ) ); assertEquals( error, e ); } @@ -604,8 +604,8 @@ public CompletionStage execute( AsyncTransaction tx ) throw new SessionExpiredException( "Oh!" ); } return tx.runAsync( "UNWIND range(1, 10) AS x RETURN count(x)" ) - .thenCompose( ResultCursor::singleAsync ) - .thenApply( record -> record.get( 0 ).asInt() ); + .thenCompose( ResultCursor::singleAsync ) + .thenApply( record -> record.get( 0 ).asInt() ); } } ); @@ -629,8 +629,8 @@ public CompletionStage execute( AsyncTransaction tx ) throw new ServiceUnavailableException( "Oh!" ); } return tx.runAsync( "CREATE (n1:TestNode), (n2:TestNode) RETURN 2" ) - .thenCompose( ResultCursor::singleAsync ) - .thenApply( record -> record.get( 0 ).asInt() ); + .thenCompose( ResultCursor::singleAsync ) + .thenApply( record -> record.get( 0 ).asInt() ); } } ); @@ -651,16 +651,16 @@ void shouldExecuteReadTransactionUntilSuccessWhenWorkFails() public CompletionStage execute( AsyncTransaction tx ) { return tx.runAsync( "RETURN 42" ) - .thenCompose( ResultCursor::singleAsync ) - .thenApply( record -> record.get( 0 ).asInt() ) - .thenCompose( result -> - { - if ( failures.getAndIncrement() < maxFailures ) - { - return failedFuture( new TransientException( "A", "B" ) ); - } - return completedFuture( result ); - } ); + .thenCompose( ResultCursor::singleAsync ) + .thenApply( record -> record.get( 0 ).asInt() ) + .thenCompose( result -> + { + if ( failures.getAndIncrement() < maxFailures ) + { + return failedFuture( new TransientException( "A", "B" ) ); + } + return completedFuture( result ); + } ); } } ); @@ -680,16 +680,16 @@ void shouldExecuteWriteTransactionUntilSuccessWhenWorkFails() public CompletionStage execute( AsyncTransaction tx ) { return tx.runAsync( "CREATE (:MyNode) RETURN 'Hello'" ) - .thenCompose( ResultCursor::singleAsync ) - .thenApply( record -> record.get( 0 ).asString() ) - .thenCompose( result -> - { - if ( failures.getAndIncrement() < maxFailures ) - { - return failedFuture( new ServiceUnavailableException( "Hi" ) ); - } - return completedFuture( result ); - } ); + .thenCompose( ResultCursor::singleAsync ) + .thenApply( record -> record.get( 0 ).asString() ) + .thenCompose( result -> + { + if ( failures.getAndIncrement() < maxFailures ) + { + return failedFuture( new ServiceUnavailableException( "Hi" ) ); + } + return completedFuture( result ); + } ); } } ); @@ -779,7 +779,7 @@ void shouldPropagateFailureInCloseFromPreviousRun() void shouldCloseCleanlyAfterFailure() { CompletionStage runWithOpenTx = session.beginTransactionAsync() - .thenCompose( tx -> session.runAsync( "RETURN 1" ) ); + .thenCompose( tx -> session.runAsync( "RETURN 1" ) ); ClientException e = assertThrows( ClientException.class, () -> await( runWithOpenTx ) ); assertThat( e.getMessage(), startsWith( "Queries cannot be run directly on a session with an open transaction" ) ); @@ -791,9 +791,9 @@ void shouldCloseCleanlyAfterFailure() void shouldPropagateFailureFromFirstIllegalQuery() { CompletionStage allQueries = session.runAsync( "CREATE (:Node1)" ) - .thenCompose( ignore -> session.runAsync( "CREATE (:Node2)" ) ) - .thenCompose( ignore -> session.runAsync( "RETURN invalid" ) ) - .thenCompose( ignore -> session.runAsync( "CREATE (:Node3)" ) ); + .thenCompose( ignore -> session.runAsync( "CREATE (:Node2)" ) ) + .thenCompose( ignore -> session.runAsync( "RETURN invalid" ) ) + .thenCompose( ignore -> session.runAsync( "CREATE (:Node3)" ) ); ClientException e = assertThrows( ClientException.class, () -> await( allQueries ) ); assertThat( e, is( syntaxError( "Variable `invalid` not defined" ) ) ); @@ -820,31 +820,31 @@ private Future>> runNestedQueries( ResultCursor inp return resultFuture; } - private void runNestedQueries(ResultCursor inputCursor, List> stages, - CompletableFuture>> resultFuture ) + private void runNestedQueries( ResultCursor inputCursor, List> stages, + CompletableFuture>> resultFuture ) { final CompletionStage recordResponse = inputCursor.nextAsync(); stages.add( recordResponse ); recordResponse.whenComplete( ( record, error ) -> - { - if ( error != null ) - { - resultFuture.completeExceptionally( error ); - } - else if ( record != null ) - { - runNestedQuery( inputCursor, record, stages, resultFuture ); - } - else - { - resultFuture.complete( stages ); - } - } ); - } - - private void runNestedQuery(ResultCursor inputCursor, Record record, - List> stages, CompletableFuture>> resultFuture ) + { + if ( error != null ) + { + resultFuture.completeExceptionally( error ); + } + else if ( record != null ) + { + runNestedQuery( inputCursor, record, stages, resultFuture ); + } + else + { + resultFuture.complete( stages ); + } + } ); + } + + private void runNestedQuery( ResultCursor inputCursor, Record record, + List> stages, CompletableFuture>> resultFuture ) { Node node = record.get( 0 ).asNode(); long id = node.get( "id" ).asLong(); @@ -852,27 +852,27 @@ private void runNestedQuery(ResultCursor inputCursor, Record record, CompletionStage response = session.runAsync( "MATCH (p:Person {id: $id}) SET p.age = $age RETURN p", - parameters( "id", id, "age", age ) ); + parameters( "id", id, "age", age ) ); response.whenComplete( ( cursor, error ) -> - { - if ( error != null ) - { - resultFuture.completeExceptionally( Futures.completionExceptionCause( error ) ); - } - else - { - stages.add( cursor.nextAsync() ); - runNestedQueries( inputCursor, stages, resultFuture ); - } - } ); + { + if ( error != null ) + { + resultFuture.completeExceptionally( Futures.completionExceptionCause( error ) ); + } + else + { + stages.add( cursor.nextAsync() ); + runNestedQueries( inputCursor, stages, resultFuture ); + } + } ); } private long countNodesByLabel( String label ) { CompletionStage countStage = session.runAsync( "MATCH (n:" + label + ") RETURN count(n)" ) - .thenCompose( ResultCursor::singleAsync ) - .thenApply( record -> record.get( 0 ).asLong() ); + .thenCompose( ResultCursor::singleAsync ) + .thenApply( record -> record.get( 0 ).asLong() ); return await( countStage ); } @@ -960,13 +960,13 @@ public CompletionStage execute( AsyncTransaction tx ) CompletableFuture resultFuture = new CompletableFuture<>(); tx.runAsync( query ).whenComplete( ( cursor, error ) -> - processQueryResult( cursor, Futures.completionExceptionCause( error ), resultFuture ) ); + processQueryResult( cursor, Futures.completionExceptionCause( error ), resultFuture ) ); return resultFuture; } - private void processQueryResult(ResultCursor cursor, Throwable error, - CompletableFuture resultFuture ) + private void processQueryResult( ResultCursor cursor, Throwable error, + CompletableFuture resultFuture ) { if ( error != null ) { @@ -975,7 +975,7 @@ private void processQueryResult(ResultCursor cursor, Throwable error, } cursor.nextAsync().whenComplete( ( record, fetchError ) -> - processFetchResult( record, Futures.completionExceptionCause( fetchError ), resultFuture ) ); + processFetchResult( record, Futures.completionExceptionCause( fetchError ), resultFuture ) ); } private void processFetchResult( Record record, Throwable error, CompletableFuture resultFuture ) diff --git a/driver/src/test/java/org/neo4j/driver/integration/async/AsyncTransactionIT.java b/driver/src/test/java/org/neo4j/driver/integration/async/AsyncTransactionIT.java index 6d7dee22ba..b710cdf87a 100644 --- a/driver/src/test/java/org/neo4j/driver/integration/async/AsyncTransactionIT.java +++ b/driver/src/test/java/org/neo4j/driver/integration/async/AsyncTransactionIT.java @@ -33,6 +33,7 @@ import java.util.concurrent.CompletionStage; import java.util.concurrent.atomic.AtomicInteger; +import org.neo4j.driver.Bookmark; import org.neo4j.driver.Query; import org.neo4j.driver.Record; import org.neo4j.driver.Value; @@ -41,11 +42,10 @@ import org.neo4j.driver.async.ResultCursor; import org.neo4j.driver.exceptions.ClientException; import org.neo4j.driver.exceptions.NoSuchRecordException; -import org.neo4j.driver.exceptions.ServiceUnavailableException; -import org.neo4j.driver.Bookmark; import org.neo4j.driver.exceptions.ResultConsumedException; -import org.neo4j.driver.summary.ResultSummary; +import org.neo4j.driver.exceptions.ServiceUnavailableException; import org.neo4j.driver.summary.QueryType; +import org.neo4j.driver.summary.ResultSummary; import org.neo4j.driver.types.Node; import org.neo4j.driver.util.DatabaseExtension; import org.neo4j.driver.util.ParallelizableIT; @@ -63,8 +63,8 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.neo4j.driver.Values.parameters; import static org.neo4j.driver.SessionConfig.builder; +import static org.neo4j.driver.Values.parameters; import static org.neo4j.driver.internal.InternalBookmark.parse; import static org.neo4j.driver.internal.util.Iterables.single; import static org.neo4j.driver.internal.util.Matchers.containsResultAvailableAfterAndResultConsumedAfter; @@ -222,7 +222,7 @@ void shouldFailToCommitAfterSingleWrongQuery() ResultCursor cursor = await( tx.runAsync( "RETURN" ) ); Exception e = assertThrows( Exception.class, () -> await( cursor.consumeAsync() ) ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); assertThrows( ClientException.class, () -> await( tx.commitAsync() ) ); } @@ -235,7 +235,7 @@ void shouldAllowRollbackAfterSingleWrongQuery() ResultCursor cursor = await( tx.runAsync( "RETURN" ) ); Exception e = assertThrows( Exception.class, () -> await( cursor.nextAsync() ) ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); assertThat( await( tx.rollbackAsync() ), is( nullValue() ) ); } @@ -257,7 +257,7 @@ void shouldFailToCommitAfterCoupleCorrectAndSingleWrongQuery() ResultCursor cursor3 = await( tx.runAsync( "RETURN" ) ); Exception e = assertThrows( Exception.class, () -> await( cursor3.consumeAsync() ) ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); assertThrows( ClientException.class, () -> await( tx.commitAsync() ) ); } @@ -280,7 +280,7 @@ void shouldAllowRollbackAfterCoupleCorrectAndSingleWrongQuery() ResultCursor cursor3 = await( tx.runAsync( "RETURN" ) ); Exception e = assertThrows( Exception.class, () -> await( cursor3.consumeAsync() ) ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); assertThat( await( tx.rollbackAsync() ), is( nullValue() ) ); } @@ -292,7 +292,7 @@ void shouldNotAllowNewQueriesAfterAnIncorrectQuery() ResultCursor cursor = await( tx.runAsync( "RETURN" ) ); Exception e1 = assertThrows( Exception.class, () -> await( cursor.nextAsync() ) ); - assertThat( e1, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e1, is( syntaxError() ) ); ClientException e2 = assertThrows( ClientException.class, () -> tx.runAsync( "CREATE ()" ) ); assertThat( e2.getMessage(), startsWith( "Cannot run more queries in this transaction" ) ); diff --git a/driver/src/test/java/org/neo4j/driver/integration/reactive/RxTransactionIT.java b/driver/src/test/java/org/neo4j/driver/integration/reactive/RxTransactionIT.java index 2c21eb7586..dab0451a32 100644 --- a/driver/src/test/java/org/neo4j/driver/integration/reactive/RxTransactionIT.java +++ b/driver/src/test/java/org/neo4j/driver/integration/reactive/RxTransactionIT.java @@ -38,17 +38,17 @@ import java.util.stream.Stream; import org.neo4j.driver.Bookmark; -import org.neo4j.driver.Record; import org.neo4j.driver.Query; +import org.neo4j.driver.Record; import org.neo4j.driver.Value; import org.neo4j.driver.exceptions.ClientException; import org.neo4j.driver.exceptions.ServiceUnavailableException; import org.neo4j.driver.internal.util.EnabledOnNeo4jWith; -import org.neo4j.driver.reactive.RxSession; import org.neo4j.driver.reactive.RxResult; +import org.neo4j.driver.reactive.RxSession; import org.neo4j.driver.reactive.RxTransaction; -import org.neo4j.driver.summary.ResultSummary; import org.neo4j.driver.summary.QueryType; +import org.neo4j.driver.summary.ResultSummary; import org.neo4j.driver.types.Node; import org.neo4j.driver.util.DatabaseExtension; import org.neo4j.driver.util.ParallelizableIT; @@ -899,7 +899,7 @@ private static void assertFailToRunWrongQuery(RxTransaction tx ) { RxResult result = tx.run( "RETURN" ); Exception e = assertThrows( Exception.class, () -> await( result.records() ) ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); } private void assertCanRunReturnOne( RxTransaction tx ) diff --git a/driver/src/test/java/org/neo4j/driver/internal/util/Matchers.java b/driver/src/test/java/org/neo4j/driver/internal/util/Matchers.java index 39368e1722..75e85df9e4 100644 --- a/driver/src/test/java/org/neo4j/driver/internal/util/Matchers.java +++ b/driver/src/test/java/org/neo4j/driver/internal/util/Matchers.java @@ -25,6 +25,8 @@ import java.util.Objects; import java.util.concurrent.TimeUnit; +import org.neo4j.driver.Driver; +import org.neo4j.driver.exceptions.ClientException; import org.neo4j.driver.internal.BoltServerAddress; import org.neo4j.driver.internal.DirectConnectionProvider; import org.neo4j.driver.internal.InternalDriver; @@ -34,8 +36,6 @@ import org.neo4j.driver.internal.cluster.RoutingTable; import org.neo4j.driver.internal.cluster.loadbalancing.LoadBalancer; import org.neo4j.driver.internal.spi.ConnectionProvider; -import org.neo4j.driver.Driver; -import org.neo4j.driver.exceptions.ClientException; import org.neo4j.driver.summary.ResultSummary; public final class Matchers @@ -200,6 +200,11 @@ public void describeTo( Description description ) }; } + public static Matcher syntaxError() + { + return syntaxError( null ); + } + public static Matcher syntaxError( String messagePrefix ) { return new TypeSafeMatcher() @@ -211,7 +216,7 @@ protected boolean matchesSafely( Throwable error ) { ClientException clientError = (ClientException) error; return clientError.code().contains( "SyntaxError" ) && - clientError.getMessage().startsWith( messagePrefix ); + (messagePrefix == null || clientError.getMessage().startsWith( messagePrefix )); } return false; } diff --git a/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQuery.java b/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQuery.java index 5fa623519f..d3f304e295 100644 --- a/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQuery.java +++ b/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQuery.java @@ -40,7 +40,7 @@ public void execute( C context ) try ( Session session = newSession( AccessMode.READ, context ) ) { Exception e = assertThrows( Exception.class, () -> session.run( "RETURN" ).consume() ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); } } } diff --git a/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryInTx.java b/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryInTx.java index 4cd4d7530f..00f1f0f677 100644 --- a/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryInTx.java +++ b/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryInTx.java @@ -43,7 +43,7 @@ public void execute( C context ) try ( Transaction tx = beginTransaction( session, context ) ) { Exception e = assertThrows( Exception.class, () -> tx.run( "RETURN" ).consume() ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); } } } diff --git a/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryWithRetries.java b/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryWithRetries.java index fff632b292..1e218216a5 100644 --- a/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryWithRetries.java +++ b/driver/src/test/java/org/neo4j/driver/stress/BlockingWrongQueryWithRetries.java @@ -40,7 +40,7 @@ public void execute( C context ) try ( Session session = newSession( AccessMode.READ, context ) ) { Exception e = assertThrows( Exception.class, () -> session.readTransaction( tx -> tx.run( "RETURN" ).consume() ) ); - assertThat( e, is( syntaxError( "Unexpected end of input" ) ) ); + assertThat( e, is( syntaxError() ) ); } } }