@@ -404,26 +404,22 @@ public Path pathDoesNotExist() {
404
404
405
405
@ Override
406
406
public <D > Entity <D , ?> entity (Class <D > entityType ) {
407
- D entity = this .delegate .read (this .jsonPath , new TypeRefAdapter <>(entityType ));
408
- return new DefaultEntity <>(entity , this .basePath , this .path , this .delegate );
407
+ return new DefaultEntity <>(new TypeRefAdapter <>(entityType ));
409
408
}
410
409
411
410
@ Override
412
411
public <D > Entity <D , ?> entity (ParameterizedTypeReference <D > entityType ) {
413
- D entity = this .delegate .read (this .jsonPath , new TypeRefAdapter <>(entityType ));
414
- return new DefaultEntity <>(entity , this .basePath , this .path , this .delegate );
412
+ return new DefaultEntity <>(new TypeRefAdapter <>(entityType ));
415
413
}
416
414
417
415
@ Override
418
416
public <D > EntityList <D > entityList (Class <D > elementType ) {
419
- List <D > entity = this .delegate .read (this .jsonPath , new TypeRefAdapter <>(List .class , elementType ));
420
- return new DefaultEntityList <>(entity , this .basePath , this .path , this .delegate );
417
+ return new DefaultEntityList <>(new TypeRefAdapter <>(List .class , elementType ));
421
418
}
422
419
423
420
@ Override
424
421
public <D > EntityList <D > entityList (ParameterizedTypeReference <D > elementType ) {
425
- List <D > entity = this .delegate .read (this .jsonPath , new TypeRefAdapter <>(List .class , elementType ));
426
- return new DefaultEntityList <>(entity , this .basePath , this .path , this .delegate );
422
+ return new DefaultEntityList <>(new TypeRefAdapter <>(List .class , elementType ));
427
423
}
428
424
429
425
@ Override
@@ -470,200 +466,190 @@ private static String joinPaths(@Nullable String basePath, String path) {
470
466
return (basePath != null ? basePath + "." + path : path );
471
467
}
472
468
473
- }
474
-
475
469
476
- /**
477
- * Default {@link GraphQlTester.Entity} implementation.
478
- */
479
- private static class DefaultEntity <D , S extends Entity <D , S >> implements Entity <D , S > {
470
+ /**
471
+ * Default {@link GraphQlTester.Entity} implementation.
472
+ */
473
+ private class DefaultEntity <D , S extends Entity <D , S >> implements Entity <D , S > {
480
474
481
- private final D entity ;
482
-
483
- @ Nullable
484
- private final String basePath ;
475
+ private final D entity ;
485
476
486
- private final String path ;
487
-
488
- private final ResponseDelegate delegate ;
477
+ protected DefaultEntity ( TypeRefAdapter < D > typeAdapter ) {
478
+ this . entity = delegate . read ( jsonPath , typeAdapter );
479
+ }
489
480
490
- protected DefaultEntity (D entity , @ Nullable String basePath , String path , ResponseDelegate delegate ) {
491
- this .entity = entity ;
492
- this .basePath = basePath ;
493
- this .path = path ;
494
- this .delegate = delegate ;
495
- }
481
+ protected D getEntity () {
482
+ return this .entity ;
483
+ }
496
484
497
- protected D getEntity ( ) {
498
- return this . entity ;
499
- }
485
+ protected void doAssert ( Runnable task ) {
486
+ delegate . doAssert ( task ) ;
487
+ }
500
488
501
- protected void doAssert ( Runnable task ) {
502
- this . delegate . doAssert ( task ) ;
503
- }
489
+ protected String getPath ( ) {
490
+ return path ;
491
+ }
504
492
505
- protected String getPath () {
506
- return this .path ;
507
- }
493
+ @ Override
494
+ public Path path (String path ) {
495
+ return forPath (basePath , path , delegate );
496
+ }
508
497
509
- @ Override
510
- public Path path (String path ) {
511
- return DefaultPath . forPath ( this . basePath , path , this . delegate );
512
- }
498
+ @ Override
499
+ public Path path (String path , Consumer < Path > pathConsumer ) {
500
+ return forNestedPath ( basePath , path , delegate , pathConsumer );
501
+ }
513
502
514
- @ Override
515
- public Path path (String path , Consumer <Path > pathConsumer ) {
516
- return DefaultPath .forNestedPath (this .basePath , path , this .delegate , pathConsumer );
517
- }
503
+ @ Override
504
+ public <T extends S > T isEqualTo (Object expected ) {
505
+ delegate .doAssert (() -> AssertionErrors .assertEquals (path , expected , this .entity ));
506
+ return self ();
507
+ }
518
508
519
- @ Override
520
- public <T extends S > T isEqualTo (Object expected ) {
521
- this . delegate .doAssert (() -> AssertionErrors .assertEquals ( this . path , expected , this .entity ));
522
- return self ();
523
- }
509
+ @ Override
510
+ public <T extends S > T isNotEqualTo (Object other ) {
511
+ delegate .doAssert (() -> AssertionErrors .assertNotEquals ( path , other , this .entity ));
512
+ return self ();
513
+ }
524
514
525
- @ Override
526
- public <T extends S > T isNotEqualTo (Object other ) {
527
- this . delegate .doAssert (() -> AssertionErrors .assertNotEquals ( this . path , other , this .entity ));
528
- return self ();
529
- }
515
+ @ Override
516
+ public <T extends S > T isSameAs (Object expected ) {
517
+ delegate .doAssert (() -> AssertionErrors .assertTrue ( path , expected == this .entity ));
518
+ return self ();
519
+ }
530
520
531
- @ Override
532
- public <T extends S > T isSameAs (Object expected ) {
533
- this . delegate .doAssert (() -> AssertionErrors .assertTrue (this . path , expected = = this .entity ));
534
- return self ();
535
- }
521
+ @ Override
522
+ public <T extends S > T isNotSameAs (Object other ) {
523
+ delegate .doAssert (() -> AssertionErrors .assertTrue (path , other ! = this .entity ));
524
+ return self ();
525
+ }
536
526
537
- @ Override
538
- public <T extends S > T isNotSameAs (Object other ) {
539
- this .delegate .doAssert (() -> AssertionErrors .assertTrue (this .path , other != this .entity ));
540
- return self ();
541
- }
527
+ @ Override
528
+ public <T extends S > T matches (Predicate <D > predicate ) {
529
+ delegate
530
+ .doAssert (() -> AssertionErrors .assertTrue (path , predicate .test (this .entity )));
531
+ return self ();
532
+ }
542
533
543
- @ Override
544
- public <T extends S > T matches (Predicate <D > predicate ) {
545
- this .delegate
546
- .doAssert (() -> AssertionErrors .assertTrue (this .path , predicate .test (this .entity )));
547
- return self ();
548
- }
534
+ @ Override
535
+ public <T extends S > T satisfies (Consumer <D > consumer ) {
536
+ delegate .doAssert (() -> consumer .accept (this .entity ));
537
+ return self ();
538
+ }
549
539
550
- @ Override
551
- public <T extends S > T satisfies (Consumer <D > consumer ) {
552
- this .delegate .doAssert (() -> consumer .accept (this .entity ));
553
- return self ();
554
- }
540
+ @ Override
541
+ public D get () {
542
+ return this .entity ;
543
+ }
555
544
556
- @ Override
557
- public D get () {
558
- return this .entity ;
545
+ @ SuppressWarnings ("unchecked" )
546
+ private <T extends S > T self () {
547
+ return (T ) this ;
548
+ }
559
549
}
560
550
561
- @ SuppressWarnings ("unchecked" )
562
- private <T extends S > T self () {
563
- return (T ) this ;
564
- }
565
- }
566
551
552
+ /**
553
+ * Default {@link EntityList} implementation.
554
+ */
555
+ @ SuppressWarnings ("SlowListContainsAll" )
556
+ private final class DefaultEntityList <E >
557
+ extends DefaultEntity <List <E >, EntityList <E >> implements EntityList <E > {
567
558
568
- /**
569
- * Default {@link EntityList} implementation.
570
- */
571
- @ SuppressWarnings ("SlowListContainsAll" )
572
- private static final class DefaultEntityList <E >
573
- extends DefaultEntity <List <E >, EntityList <E >> implements EntityList <E > {
559
+ public DefaultEntityList (TypeRefAdapter <List <E >> typeAdapter ) {
560
+ super (typeAdapter );
561
+ }
574
562
575
- private DefaultEntityList (List <E > entity , @ Nullable String basePath , String path , ResponseDelegate delegate ) {
576
- super (entity , basePath , path , delegate );
577
- }
563
+ @ Override
564
+ @ SuppressWarnings ("unchecked" )
565
+ public EntityList <E > contains (E ... values ) {
566
+ doAssert (() -> {
567
+ List <E > expected = Arrays .asList (values );
568
+ AssertionErrors .assertTrue (
569
+ "Expecting list " + getEntity () + " at path '" + getPath () + "' to contain " + expected ,
570
+ getEntity ().containsAll (expected ));
571
+ });
572
+ return this ;
573
+ }
578
574
579
- @ Override
580
- @ SuppressWarnings ("unchecked" )
581
- public EntityList <E > contains (E ... values ) {
582
- doAssert (() -> {
583
- List <E > expected = Arrays .asList (values );
584
- AssertionErrors .assertTrue (
585
- "Expecting list " + getEntity () + " at path '" + getPath () + "' to contain " + expected ,
586
- getEntity ().containsAll (expected ));
587
- });
588
- return this ;
589
- }
575
+ @ Override
576
+ @ SuppressWarnings ("unchecked" )
577
+ public EntityList <E > doesNotContain (E ... values ) {
578
+ doAssert (() -> {
579
+ List <E > expected = Arrays .asList (values );
580
+ AssertionErrors .assertTrue (
581
+ "Expecting list " + getEntity () + " at path '" + getPath () + "' to not contain " + expected ,
582
+ ! getEntity ().containsAll (expected ));
583
+ });
584
+ return this ;
585
+ }
590
586
591
- @ Override
592
- @ SuppressWarnings ("unchecked" )
593
- public EntityList <E > doesNotContain (E ... values ) {
594
- doAssert (() -> {
595
- List <E > expected = Arrays .asList (values );
596
- AssertionErrors .assertTrue (
597
- "Expecting list " + getEntity () + " at path '" + getPath () + "' to not contain " + expected ,
598
- ! getEntity ().containsAll (expected ));
599
- });
600
- return this ;
601
- }
587
+ @ Override
588
+ @ SuppressWarnings ("unchecked" )
589
+ public EntityList <E > containsExactly (E ... values ) {
590
+ doAssert (() -> {
591
+ List <E > expected = Arrays .asList (values );
592
+ AssertionErrors .assertTrue (
593
+ "Expecting list " + getEntity () + " at path '" + getPath () + "' to contain exactly " + expected ,
594
+ getEntity ().equals (expected ));
595
+ });
596
+ return this ;
597
+ }
602
598
603
- @ Override
604
- @ SuppressWarnings ("unchecked" )
605
- public EntityList <E > containsExactly (E ... values ) {
606
- doAssert (() -> {
607
- List <E > expected = Arrays .asList (values );
608
- AssertionErrors .assertTrue (
609
- "Expecting list " + getEntity () + " at path '" + getPath () + "' to contain exactly " + expected ,
610
- getEntity ().equals (expected ));
611
- });
612
- return this ;
613
- }
599
+ @ Override
600
+ public EntityList <E > hasSize (int size ) {
601
+ doAssert (() -> AssertionErrors .assertTrue (
602
+ "Expecting list " + getEntity () + " at path '" + getPath () + "' to have size == " + size ,
603
+ getEntity ().size () == size ));
604
+ return this ;
605
+ }
614
606
615
- @ Override
616
- public EntityList <E > hasSize (int size ) {
617
- doAssert (() -> AssertionErrors .assertTrue (
618
- "Expecting list " + getEntity () + " at path '" + getPath () + "' to have size == " + size ,
619
- getEntity ().size () == size ));
620
- return this ;
621
- }
607
+ @ Override
608
+ public EntityList <E > hasSizeLessThan (int size ) {
609
+ doAssert (() -> AssertionErrors .assertTrue (
610
+ "Expecting list " + getEntity () + " at path '" + getPath () + "' to have size < " + size ,
611
+ getEntity ().size () < size ));
612
+ return this ;
613
+ }
622
614
623
- @ Override
624
- public EntityList <E > hasSizeLessThan (int size ) {
625
- doAssert (() -> AssertionErrors .assertTrue (
626
- "Expecting list " + getEntity () + " at path '" + getPath () + "' to have size < " + size ,
627
- getEntity ().size () < size ));
628
- return this ;
615
+ @ Override
616
+ public EntityList <E > hasSizeGreaterThan (int size ) {
617
+ doAssert (() -> AssertionErrors .assertTrue (
618
+ "Expecting list " + getEntity () + " at path '" + getPath () + "' to have size > " + size ,
619
+ getEntity ().size () > size ));
620
+ return this ;
621
+ }
629
622
}
630
623
631
- @ Override
632
- public EntityList <E > hasSizeGreaterThan (int size ) {
633
- doAssert (() -> AssertionErrors .assertTrue (
634
- "Expecting list " + getEntity () + " at path '" + getPath () + "' to have size > " + size ,
635
- getEntity ().size () > size ));
636
- return this ;
637
- }
638
- }
639
624
625
+ /**
626
+ * Adapt JSONPath {@link TypeRef} to {@link ParameterizedTypeReference}.
627
+ */
628
+ private static final class TypeRefAdapter <T > extends TypeRef <T > {
640
629
641
- /**
642
- * Adapt JSONPath {@link TypeRef} to {@link ParameterizedTypeReference}.
643
- */
644
- private static final class TypeRefAdapter <T > extends TypeRef <T > {
630
+ private final Type type ;
645
631
646
- private final Type type ;
632
+ TypeRefAdapter (Class <T > clazz ) {
633
+ this .type = clazz ;
634
+ }
647
635
648
- TypeRefAdapter (Class <T > clazz ) {
649
- this .type = clazz ;
650
- }
636
+ TypeRefAdapter (ParameterizedTypeReference <T > typeReference ) {
637
+ this .type = typeReference . getType () ;
638
+ }
651
639
652
- TypeRefAdapter (ParameterizedTypeReference < T > typeReference ) {
653
- this .type = typeReference .getType ();
654
- }
640
+ TypeRefAdapter (Class <?> clazz , Class <?> generic ) {
641
+ this .type = ResolvableType . forClassWithGenerics ( clazz , generic ) .getType ();
642
+ }
655
643
656
- TypeRefAdapter (Class <?> clazz , Class <?> generic ) {
657
- this .type = ResolvableType .forClassWithGenerics (clazz , generic ).getType ();
658
- }
644
+ TypeRefAdapter (Class <?> clazz , ParameterizedTypeReference <?> generic ) {
645
+ this .type = ResolvableType .forClassWithGenerics (clazz , ResolvableType . forType ( generic ) ).getType ();
646
+ }
659
647
660
- TypeRefAdapter (Class <?> clazz , ParameterizedTypeReference <?> generic ) {
661
- this .type = ResolvableType .forClassWithGenerics (clazz , ResolvableType .forType (generic )).getType ();
662
- }
648
+ @ Override
649
+ public Type getType () {
650
+ return this .type ;
651
+ }
663
652
664
- @ Override
665
- public Type getType () {
666
- return this .type ;
667
653
}
668
654
669
655
}
0 commit comments