Skip to content

Commit 1047741

Browse files
committed
Improve DisposableAsyncResultCursor tests
1 parent 6dd837f commit 1047741

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/NetworkSession.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public CompletionStage<ResultCursor> runAsync( Query query, TransactionConfig co
8282
buildResultCursorFactory( query, config ).thenCompose( ResultCursorFactory::asyncResult );
8383

8484
resultCursorStage = newResultCursorStage.exceptionally( error -> null );
85-
return newResultCursorStage.thenCompose( AsyncResultCursor::mapSuccessfulRunCompletionAsync ).thenApply( cursor -> cursor );
85+
return newResultCursorStage.thenCompose( AsyncResultCursor::mapSuccessfulRunCompletionAsync ).thenApply( cursor -> cursor ); // convert the return type
8686
}
8787

8888
public CompletionStage<RxResultCursor> runRx(Query query, TransactionConfig config )

driver/src/main/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class DisposableAsyncResultCursor implements AsyncResultCursor
3636
private final AsyncResultCursor delegate;
3737
private boolean isDisposed;
3838

39-
public DisposableAsyncResultCursor(AsyncResultCursor delegate )
39+
public DisposableAsyncResultCursor( AsyncResultCursor delegate )
4040
{
4141
this.delegate = delegate;
4242
}
@@ -122,6 +122,6 @@ boolean isDisposed()
122122
@Override
123123
public CompletableFuture<AsyncResultCursor> mapSuccessfulRunCompletionAsync()
124124
{
125-
return this.delegate.mapSuccessfulRunCompletionAsync().thenApply( ignored -> this.delegate );
125+
return this.delegate.mapSuccessfulRunCompletionAsync().thenApply( ignored -> this );
126126
}
127127
}

driver/src/test/java/org/neo4j/driver/internal/cursor/DisposableAsyncResultCursorTest.java

+59-25
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,52 @@
1818
*/
1919
package org.neo4j.driver.internal.cursor;
2020

21+
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
2223

24+
import java.util.concurrent.CompletableFuture;
25+
2326
import org.neo4j.driver.internal.util.Futures;
2427

2528
import static org.junit.jupiter.api.Assertions.assertFalse;
29+
import static org.junit.jupiter.api.Assertions.assertSame;
30+
import static org.junit.jupiter.api.Assertions.assertThrows;
2631
import static org.junit.jupiter.api.Assertions.assertTrue;
2732
import static org.mockito.ArgumentMatchers.any;
33+
import static org.mockito.BDDMockito.given;
34+
import static org.mockito.BDDMockito.then;
2835
import static org.mockito.Mockito.mock;
2936
import static org.mockito.Mockito.when;
3037
import static org.neo4j.driver.util.TestUtil.await;
3138

3239
class DisposableAsyncResultCursorTest
3340
{
34-
@Test
35-
void summaryShouldDisposeCursor() throws Throwable
41+
DisposableAsyncResultCursor cursor;
42+
43+
AsyncResultCursor delegate;
44+
45+
@BeforeEach
46+
void beforeEach()
3647
{
37-
// Given
38-
DisposableAsyncResultCursor cursor = newCursor();
48+
delegate = mock( AsyncResultCursor.class );
3949

50+
when( delegate.consumeAsync() ).thenReturn( Futures.completedWithNull() );
51+
when( delegate.discardAllFailureAsync() ).thenReturn( Futures.completedWithNull() );
52+
when( delegate.peekAsync() ).thenReturn( Futures.completedWithNull() );
53+
when( delegate.nextAsync() ).thenReturn( Futures.completedWithNull() );
54+
when( delegate.singleAsync() ).thenReturn( Futures.completedWithNull() );
55+
when( delegate.forEachAsync( any() ) ).thenReturn( Futures.completedWithNull() );
56+
when( delegate.listAsync() ).thenReturn( Futures.completedWithNull() );
57+
when( delegate.listAsync( any() ) ).thenReturn( Futures.completedWithNull() );
58+
when( delegate.pullAllFailureAsync() ).thenReturn( Futures.completedWithNull() );
59+
when( delegate.mapSuccessfulRunCompletionAsync() ).thenReturn( CompletableFuture.completedFuture( delegate ) );
60+
61+
cursor = new DisposableAsyncResultCursor( delegate );
62+
}
63+
64+
@Test
65+
void summaryShouldDisposeCursor()
66+
{
4067
// When
4168
await( cursor.consumeAsync() );
4269

@@ -45,11 +72,8 @@ void summaryShouldDisposeCursor() throws Throwable
4572
}
4673

4774
@Test
48-
void consumeShouldDisposeCursor() throws Throwable
75+
void consumeShouldDisposeCursor()
4976
{
50-
// Given
51-
DisposableAsyncResultCursor cursor = newCursor();
52-
5377
// When
5478
await( cursor.discardAllFailureAsync() );
5579

@@ -58,17 +82,16 @@ void consumeShouldDisposeCursor() throws Throwable
5882
}
5983

6084
@Test
61-
void shouldNotDisposeCursor() throws Throwable
85+
void shouldNotDisposeCursor()
6286
{
63-
// Given
64-
DisposableAsyncResultCursor cursor = newCursor();
65-
6687
// When
6788
cursor.keys();
6889
await( cursor.peekAsync() );
6990
await( cursor.nextAsync() );
7091
await( cursor.singleAsync() );
71-
await( cursor.forEachAsync( record -> {} ) );
92+
await( cursor.forEachAsync( record ->
93+
{
94+
} ) );
7295
await( cursor.listAsync() );
7396
await( cursor.listAsync( record -> record ) );
7497
await( cursor.pullAllFailureAsync() );
@@ -77,18 +100,29 @@ void shouldNotDisposeCursor() throws Throwable
77100
assertFalse( cursor.isDisposed() );
78101
}
79102

80-
private static DisposableAsyncResultCursor newCursor()
103+
@Test
104+
void shouldReturnItselfOnMapSuccessfulRunCompletionAsync()
81105
{
82-
AsyncResultCursor delegate = mock( AsyncResultCursor.class );
83-
when( delegate.consumeAsync() ).thenReturn( Futures.completedWithNull() );
84-
when( delegate.discardAllFailureAsync() ).thenReturn( Futures.completedWithNull() );
85-
when( delegate.peekAsync() ).thenReturn( Futures.completedWithNull() );
86-
when( delegate.nextAsync() ).thenReturn( Futures.completedWithNull() );
87-
when( delegate.singleAsync() ).thenReturn( Futures.completedWithNull() );
88-
when( delegate.forEachAsync( any() ) ).thenReturn( Futures.completedWithNull() );
89-
when( delegate.listAsync() ).thenReturn( Futures.completedWithNull() );
90-
when( delegate.listAsync( any() ) ).thenReturn( Futures.completedWithNull() );
91-
when( delegate.pullAllFailureAsync() ).thenReturn( Futures.completedWithNull() );
92-
return new DisposableAsyncResultCursor( delegate );
106+
// When
107+
AsyncResultCursor actual = await( cursor.mapSuccessfulRunCompletionAsync() );
108+
109+
// Then
110+
then( delegate ).should().mapSuccessfulRunCompletionAsync();
111+
assertSame( cursor, actual );
112+
}
113+
114+
@Test
115+
void shouldFailOnMapSuccessfulRunCompletionAsyncFailure()
116+
{
117+
// Given
118+
Throwable error = mock( Throwable.class );
119+
given( delegate.mapSuccessfulRunCompletionAsync() ).willReturn( Futures.failedFuture( error ) );
120+
121+
// When
122+
Throwable actual = assertThrows( Throwable.class, () -> await( cursor.mapSuccessfulRunCompletionAsync() ) );
123+
124+
// Then
125+
then( delegate ).should().mapSuccessfulRunCompletionAsync();
126+
assertSame( error, actual );
93127
}
94128
}

0 commit comments

Comments
 (0)