18
18
*/
19
19
package org .neo4j .driver .internal .cursor ;
20
20
21
+ import org .junit .jupiter .api .BeforeEach ;
21
22
import org .junit .jupiter .api .Test ;
22
23
24
+ import java .util .concurrent .CompletableFuture ;
25
+
23
26
import org .neo4j .driver .internal .util .Futures ;
24
27
25
28
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 ;
26
31
import static org .junit .jupiter .api .Assertions .assertTrue ;
27
32
import static org .mockito .ArgumentMatchers .any ;
33
+ import static org .mockito .BDDMockito .given ;
34
+ import static org .mockito .BDDMockito .then ;
28
35
import static org .mockito .Mockito .mock ;
29
36
import static org .mockito .Mockito .when ;
30
37
import static org .neo4j .driver .util .TestUtil .await ;
31
38
32
39
class DisposableAsyncResultCursorTest
33
40
{
34
- @ Test
35
- void summaryShouldDisposeCursor () throws Throwable
41
+ DisposableAsyncResultCursor cursor ;
42
+
43
+ AsyncResultCursor delegate ;
44
+
45
+ @ BeforeEach
46
+ void beforeEach ()
36
47
{
37
- // Given
38
- DisposableAsyncResultCursor cursor = newCursor ();
48
+ delegate = mock ( AsyncResultCursor .class );
39
49
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
+ {
40
67
// When
41
68
await ( cursor .consumeAsync () );
42
69
@@ -45,11 +72,8 @@ void summaryShouldDisposeCursor() throws Throwable
45
72
}
46
73
47
74
@ Test
48
- void consumeShouldDisposeCursor () throws Throwable
75
+ void consumeShouldDisposeCursor ()
49
76
{
50
- // Given
51
- DisposableAsyncResultCursor cursor = newCursor ();
52
-
53
77
// When
54
78
await ( cursor .discardAllFailureAsync () );
55
79
@@ -58,17 +82,16 @@ void consumeShouldDisposeCursor() throws Throwable
58
82
}
59
83
60
84
@ Test
61
- void shouldNotDisposeCursor () throws Throwable
85
+ void shouldNotDisposeCursor ()
62
86
{
63
- // Given
64
- DisposableAsyncResultCursor cursor = newCursor ();
65
-
66
87
// When
67
88
cursor .keys ();
68
89
await ( cursor .peekAsync () );
69
90
await ( cursor .nextAsync () );
70
91
await ( cursor .singleAsync () );
71
- await ( cursor .forEachAsync ( record -> {} ) );
92
+ await ( cursor .forEachAsync ( record ->
93
+ {
94
+ } ) );
72
95
await ( cursor .listAsync () );
73
96
await ( cursor .listAsync ( record -> record ) );
74
97
await ( cursor .pullAllFailureAsync () );
@@ -77,18 +100,29 @@ void shouldNotDisposeCursor() throws Throwable
77
100
assertFalse ( cursor .isDisposed () );
78
101
}
79
102
80
- private static DisposableAsyncResultCursor newCursor ()
103
+ @ Test
104
+ void shouldReturnItselfOnMapSuccessfulRunCompletionAsync ()
81
105
{
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 );
93
127
}
94
128
}
0 commit comments