Skip to content

Commit

Permalink
Improved DisposableAsyncResultCursor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
injectives committed Jun 8, 2021
1 parent 6dd837f commit 9e8e6ad
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
Expand Up @@ -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;
}
Expand Down Expand Up @@ -122,6 +122,6 @@ boolean isDisposed()
@Override
public CompletableFuture<AsyncResultCursor> mapSuccessfulRunCompletionAsync()
{
return this.delegate.mapSuccessfulRunCompletionAsync().thenApply( ignored -> this.delegate );
return this.delegate.mapSuccessfulRunCompletionAsync().thenApply( ignored -> this );
}
}
Expand Up @@ -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() );

Expand All @@ -45,11 +72,8 @@ void summaryShouldDisposeCursor() throws Throwable
}

@Test
void consumeShouldDisposeCursor() throws Throwable
void consumeShouldDisposeCursor()
{
// Given
DisposableAsyncResultCursor cursor = newCursor();

// When
await( cursor.discardAllFailureAsync() );

Expand All @@ -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() );
Expand All @@ -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 );
}
}

0 comments on commit 9e8e6ad

Please sign in to comment.