diff --git a/driver/src/main/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursor.java b/driver/src/main/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursor.java index f3e177eca1..a013e5612a 100644 --- a/driver/src/main/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursor.java +++ b/driver/src/main/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursor.java @@ -36,7 +36,7 @@ public class DisposableAsyncResultCursor implements AsyncResultCursor private final AsyncResultCursor delegate; private boolean isDisposed; - public DisposableAsyncResultCursor(AsyncResultCursor delegate ) + public DisposableAsyncResultCursor( AsyncResultCursor delegate ) { this.delegate = delegate; } @@ -122,6 +122,6 @@ boolean isDisposed() @Override public CompletableFuture mapSuccessfulRunCompletionAsync() { - return this.delegate.mapSuccessfulRunCompletionAsync().thenApply( ignored -> this.delegate ); + return this.delegate.mapSuccessfulRunCompletionAsync().thenApply( ignored -> this ); } } diff --git a/driver/src/test/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursorTest.java b/driver/src/test/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursorTest.java index e6f49e812d..dce0ba1481 100644 --- a/driver/src/test/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursorTest.java +++ b/driver/src/test/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursorTest.java @@ -18,25 +18,52 @@ */ package org.neo4j.driver.internal.cursor; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.concurrent.CompletableFuture; + import org.neo4j.driver.internal.util.Futures; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.neo4j.driver.util.TestUtil.await; class DisposableAsyncResultCursorTest { - @Test - void summaryShouldDisposeCursor() throws Throwable + DisposableAsyncResultCursor cursor; + + AsyncResultCursor delegate; + + @BeforeEach + void beforeEach() { - // Given - DisposableAsyncResultCursor cursor = newCursor(); + delegate = mock( AsyncResultCursor.class ); + when( delegate.consumeAsync() ).thenReturn( Futures.completedWithNull() ); + when( delegate.discardAllFailureAsync() ).thenReturn( Futures.completedWithNull() ); + when( delegate.peekAsync() ).thenReturn( Futures.completedWithNull() ); + when( delegate.nextAsync() ).thenReturn( Futures.completedWithNull() ); + when( delegate.singleAsync() ).thenReturn( Futures.completedWithNull() ); + when( delegate.forEachAsync( any() ) ).thenReturn( Futures.completedWithNull() ); + when( delegate.listAsync() ).thenReturn( Futures.completedWithNull() ); + when( delegate.listAsync( any() ) ).thenReturn( Futures.completedWithNull() ); + when( delegate.pullAllFailureAsync() ).thenReturn( Futures.completedWithNull() ); + when( delegate.mapSuccessfulRunCompletionAsync() ).thenReturn( CompletableFuture.completedFuture( delegate ) ); + + cursor = new DisposableAsyncResultCursor( delegate ); + } + + @Test + void summaryShouldDisposeCursor() + { // When await( cursor.consumeAsync() ); @@ -45,11 +72,8 @@ void summaryShouldDisposeCursor() throws Throwable } @Test - void consumeShouldDisposeCursor() throws Throwable + void consumeShouldDisposeCursor() { - // Given - DisposableAsyncResultCursor cursor = newCursor(); - // When await( cursor.discardAllFailureAsync() ); @@ -58,17 +82,16 @@ void consumeShouldDisposeCursor() throws Throwable } @Test - void shouldNotDisposeCursor() throws Throwable + void shouldNotDisposeCursor() { - // Given - DisposableAsyncResultCursor cursor = newCursor(); - // When cursor.keys(); await( cursor.peekAsync() ); await( cursor.nextAsync() ); await( cursor.singleAsync() ); - await( cursor.forEachAsync( record -> {} ) ); + await( cursor.forEachAsync( record -> + { + } ) ); await( cursor.listAsync() ); await( cursor.listAsync( record -> record ) ); await( cursor.pullAllFailureAsync() ); @@ -77,18 +100,29 @@ void shouldNotDisposeCursor() throws Throwable assertFalse( cursor.isDisposed() ); } - private static DisposableAsyncResultCursor newCursor() + @Test + void shouldReturnItselfOnMapSuccessfulRunCompletionAsync() { - AsyncResultCursor delegate = mock( AsyncResultCursor.class ); - when( delegate.consumeAsync() ).thenReturn( Futures.completedWithNull() ); - when( delegate.discardAllFailureAsync() ).thenReturn( Futures.completedWithNull() ); - when( delegate.peekAsync() ).thenReturn( Futures.completedWithNull() ); - when( delegate.nextAsync() ).thenReturn( Futures.completedWithNull() ); - when( delegate.singleAsync() ).thenReturn( Futures.completedWithNull() ); - when( delegate.forEachAsync( any() ) ).thenReturn( Futures.completedWithNull() ); - when( delegate.listAsync() ).thenReturn( Futures.completedWithNull() ); - when( delegate.listAsync( any() ) ).thenReturn( Futures.completedWithNull() ); - when( delegate.pullAllFailureAsync() ).thenReturn( Futures.completedWithNull() ); - return new DisposableAsyncResultCursor( delegate ); + // When + AsyncResultCursor actual = await( cursor.mapSuccessfulRunCompletionAsync() ); + + // Then + then( delegate ).should().mapSuccessfulRunCompletionAsync(); + assertSame( cursor, actual ); + } + + @Test + void shouldFailOnMapSuccessfulRunCompletionAsyncFailure() + { + // Given + Throwable error = mock( Throwable.class ); + given( delegate.mapSuccessfulRunCompletionAsync() ).willReturn( Futures.failedFuture( error ) ); + + // When + Throwable actual = assertThrows( Throwable.class, () -> await( cursor.mapSuccessfulRunCompletionAsync() ) ); + + // Then + then( delegate ).should().mapSuccessfulRunCompletionAsync(); + assertSame( error, actual ); } }