use of com.google.cloud.datastore.QueryResults in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplateTests method setup.
@Before
public void setup() {
this.datastoreTemplate = new DatastoreTemplate(() -> this.datastore, this.datastoreEntityConverter, new DatastoreMappingContext(), this.objectToKeyFactory);
when(this.datastoreEntityConverter.getConversions()).thenReturn(this.readWriteConversions);
// The readWriteConversions are only mocked for purposes of collection-conversion
// for
// descendants. no other conversions take place in the template.
doAnswer((invocation) -> new LinkedList<>(invocation.getArgument(0))).when(this.readWriteConversions).convertOnRead(any(), any(), (Class) any());
this.ob1 = new TestEntity();
this.ob2 = new TestEntity();
this.ob1.id = "value1";
this.ob2.id = "value2";
Entity ce1 = Entity.newBuilder(this.keyChild1).build();
Query childTestEntityQuery = Query.newEntityQueryBuilder().setKind("child_entity").setFilter(PropertyFilter.hasAncestor(this.key1)).build();
this.childEntity1 = createChildEntity();
this.ob1.childEntities = new LinkedList<>();
this.childEntity2 = new ChildEntity();
this.ob1.childEntities.add(this.childEntity2);
this.childEntity3 = new ChildEntity();
this.ob1.childEntities.add(this.childEntity3);
this.childEntity4 = new ChildEntity();
this.ob1.singularReference = this.childEntity4;
this.ob1.multipleReference = new LinkedList<>();
this.childEntity5 = new ChildEntity();
this.ob1.multipleReference.add(this.childEntity5);
this.childEntity6 = new ChildEntity();
this.ob1.multipleReference.add(this.childEntity6);
this.ob1.lazyMultipleReference = new LinkedList<>();
this.childEntity7 = new ChildEntity();
this.ob1.lazyMultipleReference.add(this.childEntity7);
// mocked query results for entities and child entities.
QueryResults childTestEntityQueryResults = mock(QueryResults.class);
doAnswer((invocation) -> {
Arrays.asList(ce1).iterator().forEachRemaining(invocation.getArgument(0));
return null;
}).when(childTestEntityQueryResults).forEachRemaining(any());
QueryResults testEntityQueryResults = mock(QueryResults.class);
doAnswer((invocation) -> {
Arrays.asList(this.e1, this.e2).iterator().forEachRemaining(invocation.getArgument(0));
return null;
}).when(testEntityQueryResults).forEachRemaining(any());
setUpConverters(ce1, childTestEntityQuery, childTestEntityQueryResults, testEntityQueryResults);
}
use of com.google.cloud.datastore.QueryResults in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method findAll.
@Override
public <T> DatastoreResultsCollection<T> findAll(Class<T> entityClass, DatastoreQueryOptions queryOptions) {
DatastorePersistentEntity<?> persistentEntity = this.datastoreMappingContext.getPersistentEntity(entityClass);
EntityQuery.Builder builder = Query.newEntityQueryBuilder().setKind(persistentEntity.kindName());
applyQueryOptions(builder, queryOptions, persistentEntity);
Query query = builder.build();
QueryResults queryResults = getDatastoreReadWriter().run(query);
Collection<T> convertedResults = convertEntitiesForRead(queryResults, entityClass);
maybeEmitEvent(new AfterQueryEvent(convertedResults, query));
return new DatastoreResultsCollection<>(convertedResults, queryResults != null ? queryResults.getCursorAfter() : null);
}
use of com.google.cloud.datastore.QueryResults in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method queryKeysOrEntities.
@Override
public <T> DatastoreResultsIterable<?> queryKeysOrEntities(Query query, Class<T> entityClass) {
QueryResults results = getDatastoreReadWriter().run(query);
DatastoreResultsIterable resultsIterable;
if (results.getResultClass() == Key.class) {
resultsIterable = new DatastoreResultsIterable(results, results.getCursorAfter());
} else {
resultsIterable = new DatastoreResultsIterable<>(convertEntitiesForRead(results, entityClass), results.getCursorAfter());
}
maybeEmitEvent(new AfterQueryEvent(resultsIterable, query));
return resultsIterable;
}
Aggregations