|
38 | 38 | import org.neo4j.driver.v1.exceptions.ServiceUnavailableException; |
39 | 39 | import org.neo4j.driver.v1.summary.ResultSummary; |
40 | 40 | import org.neo4j.driver.v1.summary.StatementType; |
| 41 | +import org.neo4j.driver.v1.util.Function; |
| 42 | +import org.neo4j.driver.v1.util.Functions; |
41 | 43 |
|
42 | 44 | import static java.util.Arrays.asList; |
43 | 45 | import static java.util.Collections.emptyList; |
|
54 | 56 | import static org.mockito.Mockito.verify; |
55 | 57 | import static org.mockito.Mockito.when; |
56 | 58 | import static org.neo4j.driver.internal.util.Futures.completedWithNull; |
| 59 | +import static org.neo4j.driver.internal.util.Futures.failedFuture; |
57 | 60 | import static org.neo4j.driver.v1.Values.value; |
58 | 61 | import static org.neo4j.driver.v1.Values.values; |
59 | 62 | import static org.neo4j.driver.v1.util.TestUtil.await; |
@@ -354,159 +357,102 @@ public void shouldFailForEachWhenGivenActionThrows() |
354 | 357 | } |
355 | 358 |
|
356 | 359 | @Test |
357 | | - public void shouldListAsyncWhenResultContainsMultipleRecords() |
| 360 | + public void shouldReturnFailureWhenExists() |
358 | 361 | { |
359 | 362 | PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
360 | 363 |
|
361 | | - Record record1 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 1, 1, 1 ) ); |
362 | | - Record record2 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 2, 2, 2 ) ); |
363 | | - Record record3 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 3, 3, 3 ) ); |
364 | | - Record record4 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 4, 4, 4 ) ); |
365 | | - when( pullAllHandler.nextAsync() ).thenReturn( completedFuture( record1 ) ) |
366 | | - .thenReturn( completedFuture( record2 ) ).thenReturn( completedFuture( record3 ) ) |
367 | | - .thenReturn( completedFuture( record4 ) ).thenReturn( completedWithNull() ); |
| 364 | + ServiceUnavailableException error = new ServiceUnavailableException( "Hi" ); |
| 365 | + when( pullAllHandler.failureAsync() ).thenReturn( completedFuture( error ) ); |
368 | 366 |
|
369 | 367 | InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
370 | 368 |
|
371 | | - assertEquals( asList( record1, record2, record3, record4 ), await( cursor.listAsync() ) ); |
| 369 | + assertEquals( error, await( cursor.failureAsync() ) ); |
372 | 370 | } |
373 | 371 |
|
374 | 372 | @Test |
375 | | - public void shouldListAsyncWhenResultContainsOneRecords() |
| 373 | + public void shouldReturnNullFailureWhenDoesNotExist() |
376 | 374 | { |
377 | 375 | PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
378 | | - |
379 | | - Record record = new InternalRecord( asList( "key1", "key2", "key3" ), values( 1, 1, 1 ) ); |
380 | | - when( pullAllHandler.nextAsync() ).thenReturn( completedFuture( record ) ) |
381 | | - .thenReturn( completedWithNull() ); |
| 376 | + when( pullAllHandler.failureAsync() ).thenReturn( completedWithNull() ); |
382 | 377 |
|
383 | 378 | InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
384 | 379 |
|
385 | | - assertEquals( singletonList( record ), await( cursor.listAsync() ) ); |
| 380 | + assertNull( await( cursor.failureAsync() ) ); |
386 | 381 | } |
387 | 382 |
|
388 | 383 | @Test |
389 | | - public void shouldListAsyncWhenResultContainsNoRecords() |
| 384 | + public void shouldListAsyncWithoutMapFunction() |
390 | 385 | { |
391 | 386 | PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
392 | | - when( pullAllHandler.nextAsync() ).thenReturn( completedWithNull() ); |
393 | 387 |
|
394 | | - InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
395 | | - |
396 | | - assertEquals( 0, await( cursor.listAsync() ).size() ); |
397 | | - } |
398 | | - |
399 | | - @Test |
400 | | - public void shouldListAsyncWithFunctionWhenResultContainsMultipleRecords() |
401 | | - { |
402 | | - PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
| 388 | + Record record1 = new InternalRecord( asList( "key1", "key2" ), values( 1, 1 ) ); |
| 389 | + Record record2 = new InternalRecord( asList( "key1", "key2" ), values( 2, 2 ) ); |
| 390 | + List<Record> records = asList( record1, record2 ); |
403 | 391 |
|
404 | | - Record record1 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 1, 11, 111 ) ); |
405 | | - Record record2 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 2, 22, 222 ) ); |
406 | | - Record record3 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 3, 33, 333 ) ); |
407 | | - Record record4 = new InternalRecord( asList( "key1", "key2", "key3" ), values( 4, 44, 444 ) ); |
408 | | - when( pullAllHandler.nextAsync() ).thenReturn( completedFuture( record1 ) ) |
409 | | - .thenReturn( completedFuture( record2 ) ).thenReturn( completedFuture( record3 ) ) |
410 | | - .thenReturn( completedFuture( record4 ) ).thenReturn( completedWithNull() ); |
| 392 | + when( pullAllHandler.listAsync( Functions.identity() ) ).thenReturn( completedFuture( records ) ); |
411 | 393 |
|
412 | 394 | InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
413 | 395 |
|
414 | | - List<Integer> values = await( cursor.listAsync( record -> record.get( "key2" ).asInt() ) ); |
415 | | - assertEquals( asList( 11, 22, 33, 44 ), values ); |
| 396 | + assertEquals( records, await( cursor.listAsync() ) ); |
| 397 | + verify( pullAllHandler ).listAsync( Functions.identity() ); |
416 | 398 | } |
417 | 399 |
|
418 | 400 | @Test |
419 | | - public void shouldListAsyncWithFunctionWhenResultContainsOneRecords() |
| 401 | + public void shouldListAsyncWithMapFunction() |
420 | 402 | { |
| 403 | + Function<Record,String> mapFunction = record -> record.get( 0 ).asString(); |
421 | 404 | PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
422 | 405 |
|
423 | | - Record singleRecord = new InternalRecord( asList( "key1", "key2", "key3" ), values( 1, 11, 111 ) ); |
424 | | - when( pullAllHandler.nextAsync() ).thenReturn( completedFuture( singleRecord ) ) |
425 | | - .thenReturn( completedWithNull() ); |
| 406 | + List<String> values = asList( "a", "b", "c", "d", "e" ); |
| 407 | + when( pullAllHandler.listAsync( mapFunction ) ).thenReturn( completedFuture( values ) ); |
426 | 408 |
|
427 | 409 | InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
428 | 410 |
|
429 | | - List<Long> values = await( cursor.listAsync( record -> record.get( "key3" ).asLong() ) ); |
430 | | - assertEquals( singletonList( 111L ), values ); |
| 411 | + assertEquals( values, await( cursor.listAsync( mapFunction ) ) ); |
| 412 | + verify( pullAllHandler ).listAsync( mapFunction ); |
431 | 413 | } |
432 | 414 |
|
433 | 415 | @Test |
434 | | - public void shouldListAsyncWithFunctionWhenResultContainsNoRecords() |
| 416 | + public void shouldPropagateFailureFromListAsyncWithoutMapFunction() |
435 | 417 | { |
436 | 418 | PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
437 | | - when( pullAllHandler.nextAsync() ).thenReturn( completedWithNull() ); |
| 419 | + RuntimeException error = new RuntimeException( "Hi" ); |
| 420 | + when( pullAllHandler.listAsync( Functions.identity() ) ).thenReturn( failedFuture( error ) ); |
438 | 421 |
|
439 | 422 | InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
440 | 423 |
|
441 | | - List<String> values = await( cursor.listAsync( record -> record.get( "key42" ).asString() ) ); |
442 | | - assertEquals( 0, values.size() ); |
443 | | - } |
444 | | - |
445 | | - @Test |
446 | | - public void shouldFailListAsyncWhenGivenFunctionThrows() |
447 | | - { |
448 | | - PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
449 | | - |
450 | | - Record record1 = new InternalRecord( asList( "key1", "key2" ), values( 1, 1 ) ); |
451 | | - Record record2 = new InternalRecord( asList( "key1", "key2" ), values( 2, 2 ) ); |
452 | | - Record record3 = new InternalRecord( asList( "key1", "key2" ), values( 3, 3 ) ); |
453 | | - when( pullAllHandler.nextAsync() ).thenReturn( completedFuture( record1 ) ) |
454 | | - .thenReturn( completedFuture( record2 ) ).thenReturn( completedFuture( record3 ) ) |
455 | | - .thenReturn( completedWithNull() ); |
456 | | - |
457 | | - InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
458 | | - |
459 | | - AtomicInteger recordsProcessed = new AtomicInteger(); |
460 | | - RuntimeException error = new RuntimeException( "Hello" ); |
461 | | - |
462 | | - CompletionStage<List<Integer>> stage = cursor.listAsync( record -> |
463 | | - { |
464 | | - if ( record.get( "key1" ).asInt() == 2 ) |
465 | | - { |
466 | | - throw error; |
467 | | - } |
468 | | - else |
469 | | - { |
470 | | - recordsProcessed.incrementAndGet(); |
471 | | - return record.get( "key1" ).asInt(); |
472 | | - } |
473 | | - } ); |
474 | | - |
475 | 424 | try |
476 | 425 | { |
477 | | - await( stage ); |
| 426 | + await( cursor.listAsync() ); |
478 | 427 | fail( "Exception expected" ); |
479 | 428 | } |
480 | 429 | catch ( RuntimeException e ) |
481 | 430 | { |
482 | 431 | assertEquals( error, e ); |
483 | 432 | } |
484 | | - assertEquals( 1, recordsProcessed.get() ); |
485 | | - verify( pullAllHandler, times( 2 ) ).nextAsync(); |
486 | | - } |
487 | | - |
488 | | - @Test |
489 | | - public void shouldReturnFailureWhenExists() |
490 | | - { |
491 | | - PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
492 | | - |
493 | | - ServiceUnavailableException error = new ServiceUnavailableException( "Hi" ); |
494 | | - when( pullAllHandler.failureAsync() ).thenReturn( completedFuture( error ) ); |
495 | | - |
496 | | - InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
497 | | - |
498 | | - assertEquals( error, await( cursor.failureAsync() ) ); |
| 433 | + verify( pullAllHandler ).listAsync( Functions.identity() ); |
499 | 434 | } |
500 | 435 |
|
501 | 436 | @Test |
502 | | - public void shouldReturnNullFailureWhenDoesNotExist() |
| 437 | + public void shouldPropagateFailureFromListAsyncWithMapFunction() |
503 | 438 | { |
| 439 | + Function<Record,String> mapFunction = record -> record.get( 0 ).asString(); |
504 | 440 | PullAllResponseHandler pullAllHandler = mock( PullAllResponseHandler.class ); |
505 | | - when( pullAllHandler.failureAsync() ).thenReturn( completedWithNull() ); |
| 441 | + RuntimeException error = new RuntimeException( "Hi" ); |
| 442 | + when( pullAllHandler.listAsync( mapFunction ) ).thenReturn( failedFuture( error ) ); |
506 | 443 |
|
507 | 444 | InternalStatementResultCursor cursor = newCursor( pullAllHandler ); |
508 | 445 |
|
509 | | - assertNull( await( cursor.failureAsync() ) ); |
| 446 | + try |
| 447 | + { |
| 448 | + await( cursor.listAsync( mapFunction ) ); |
| 449 | + fail( "Exception expected" ); |
| 450 | + } |
| 451 | + catch ( RuntimeException e ) |
| 452 | + { |
| 453 | + assertEquals( error, e ); |
| 454 | + } |
| 455 | + verify( pullAllHandler ).listAsync( mapFunction ); |
510 | 456 | } |
511 | 457 |
|
512 | 458 | private static InternalStatementResultCursor newCursor( PullAllResponseHandler pullAllHandler ) |
|
0 commit comments