3535import com .google .gcloud .datastore .StructuredQuery .PropertyFilter ;
3636import com .google .gcloud .datastore .testing .LocalGcdHelper ;
3737import com .google .gcloud .spi .DatastoreRpc ;
38+ import com .google .gcloud .spi .DatastoreRpc .DatastoreRpcException ;
3839import com .google .gcloud .spi .DatastoreRpc .DatastoreRpcException .Reason ;
3940import com .google .gcloud .spi .DatastoreRpcFactory ;
4041
4950import org .junit .runners .JUnit4 ;
5051
5152import java .io .IOException ;
53+ import java .util .ArrayList ;
5254import java .util .Collections ;
55+ import java .util .HashSet ;
5356import java .util .Iterator ;
5457import java .util .List ;
58+ import java .util .Set ;
5559
5660@ RunWith (JUnit4 .class )
5761public class DatastoreTest {
@@ -520,7 +524,7 @@ public void testGet() {
520524 }
521525
522526 @ Test
523- public void testGetArray () {
527+ public void testGetArrayNoDeferredResults () {
524528 datastore .put (ENTITY3 );
525529 Iterator <Entity > result =
526530 datastore .fetch (KEY1 , Key .builder (KEY1 ).name ("bla" ).build (), KEY2 , KEY3 ).iterator ();
@@ -546,7 +550,65 @@ public void testGetArray() {
546550 // expected - no such property
547551 }
548552 assertFalse (result .hasNext ());
549- // TODO(ozarov): construct a test to verify more results
553+ }
554+
555+ public void testGetArrayDeferredResults () throws DatastoreRpcException {
556+ List <DatastoreV1 .Key > keysPb = new ArrayList <>();
557+ keysPb .add (KEY1 .toPb ());
558+ keysPb .add (KEY2 .toPb ());
559+ keysPb .add (KEY3 .toPb ());
560+ keysPb .add (KEY4 .toPb ());
561+ keysPb .add (KEY5 .toPb ());
562+ List <DatastoreV1 .LookupRequest > lookupRequests = new ArrayList <>();
563+ lookupRequests .add (DatastoreV1 .LookupRequest .newBuilder ().addAllKey (keysPb ).build ());
564+ lookupRequests .add (
565+ DatastoreV1 .LookupRequest .newBuilder ()
566+ .addKey (keysPb .get (2 ))
567+ .addKey (keysPb .get (3 ))
568+ .addKey (keysPb .get (5 ))
569+ .build ());
570+ lookupRequests .add (DatastoreV1 .LookupRequest .newBuilder ().addKey (keysPb .get (5 )).build ());
571+ Entity entity4 = Entity .builder (KEY4 ).set ("value" , StringValue .of ("value" )).build ();
572+ Entity entity5 = Entity .builder (KEY5 ).set ("value" , "value" ).build ();
573+ List <DatastoreV1 .LookupResponse > lookupResponses = new ArrayList <>();
574+ lookupResponses .add (
575+ DatastoreV1 .LookupResponse .newBuilder ()
576+ .addFound (EntityResult .newBuilder ().setEntity (ENTITY1 .toPb ()))
577+ .addFound (EntityResult .newBuilder ().setEntity (entity4 .toPb ()))
578+ .addDeferred (KEY2 .toPb ())
579+ .addDeferred (KEY3 .toPb ())
580+ .addDeferred (KEY5 .toPb ())
581+ .build ());
582+ lookupResponses .add (
583+ DatastoreV1 .LookupResponse .newBuilder ()
584+ .addFound (EntityResult .newBuilder ().setEntity (ENTITY3 .toPb ()))
585+ .addFound (EntityResult .newBuilder ().setEntity (entity4 .toPb ()))
586+ .addDeferred (KEY5 .toPb ())
587+ .build ());
588+ lookupResponses .add (
589+ DatastoreV1 .LookupResponse .newBuilder ()
590+ .addFound (EntityResult .newBuilder ().setEntity (entity5 .toPb ()))
591+ .build ());
592+ DatastoreRpcFactory rpcFactoryMock = EasyMock .createStrictMock (DatastoreRpcFactory .class );
593+ DatastoreRpc rpcMock = EasyMock .createStrictMock (DatastoreRpc .class );
594+ EasyMock .expect (rpcFactoryMock .create (EasyMock .anyObject (DatastoreOptions .class )))
595+ .andReturn (rpcMock );
596+ for (int i = 0 ; i < lookupRequests .size (); i ++) {
597+ EasyMock .expect (rpcMock .lookup (lookupRequests .get (i ))).andReturn (lookupResponses .get (i ));
598+ }
599+ EasyMock .replay (rpcFactoryMock , rpcMock );
600+ DatastoreOptions options =
601+ this .options .toBuilder ()
602+ .retryParams (RetryParams .getDefaultInstance ())
603+ .serviceRpcFactory (rpcFactoryMock )
604+ .build ();
605+ Datastore datastore = DatastoreFactory .instance ().get (options );
606+ Iterator <Entity > iter = datastore .get (KEY1 , KEY2 , KEY3 , KEY4 , KEY5 );
607+ Set <Entity > foundEntities = new HashSet <>();
608+ while (iter .hasNext ()) {
609+ foundEntities .add (iter .next ());
610+ }
611+ assertEquals (foundEntities .size (), 5 );
550612 }
551613
552614 @ Test
0 commit comments