use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method nullSort.
@Test
public void nullSort() throws NoSuchMethodException {
queryWithMockResult("findByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNullOrderByIdDesc", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Sort.class));
Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, null };
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOrderBy(OrderBy.desc("__key__")).build();
assertThat(statement).isEqualTo(expected);
return EMPTY_RESPONSE;
});
when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);
this.partTreeDatastoreQuery.execute(params);
verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), any());
}
use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method prepareSliceResults.
private void prepareSliceResults(int offset, Integer queryLimit, Boolean hasNext) {
Cursor cursor = Cursor.copyFrom("abc".getBytes());
List<Integer> datastoreMatchingRecords = Arrays.asList(3, 4, 5);
when(this.datastoreTemplate.queryEntitiesSlice(isA(EntityQuery.class), any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOffset(offset).setOrderBy(OrderBy.desc("__key__")).setLimit(queryLimit).build();
assertThat(statement).isEqualTo(expected);
return new SliceImpl(new DatastoreResultsIterable(datastoreMatchingRecords.iterator(), cursor).toList(), Pageable.unpaged(), hasNext);
});
}
use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method nullPageable.
@Test
public void nullPageable() throws NoSuchMethodException {
queryWithMockResult("findTop333ByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNullOrderByIdDesc", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Pageable.class));
Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, null };
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setLimit(333).setOrderBy(OrderBy.desc("__key__")).build();
assertThat(statement).isEqualTo(expected);
return EMPTY_RESPONSE;
});
when(this.queryMethod.getCollectionReturnType()).thenReturn(List.class);
this.partTreeDatastoreQuery.execute(params);
verify(this.datastoreTemplate, times(1)).queryKeysOrEntities(any(), any());
}
use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method nonCollectionReturnType.
@Test
public void nonCollectionReturnType() throws NoSuchMethodException {
Trade trade = new Trade();
queryWithMockResult("findByAction", null, getClass().getMethod("findByAction", String.class), true, null);
Object[] params = new Object[] { "BUY" };
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(PropertyFilter.eq("action", "BUY")).setKind("trades").setLimit(1).build();
assertThat(statement).isEqualTo(expected);
List<Trade> results = Collections.singletonList(trade);
return new DatastoreResultsIterable(results.iterator(), null);
});
assertThat(this.partTreeDatastoreQuery.execute(params)).isEqualTo(trade);
}
use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method resolveDescendantProperties.
private <T> void resolveDescendantProperties(DatastorePersistentEntity datastorePersistentEntity, BaseEntity entity, T convertedObject, ReadContext context) {
datastorePersistentEntity.doWithDescendantProperties((descendantPersistentProperty) -> {
Class descendantType = descendantPersistentProperty.getComponentType();
Key entityKey = (Key) entity.getKey();
Key ancestorKey = KeyUtil.getKeyWithoutAncestors(entityKey);
EntityQuery descendantQuery = Query.newEntityQueryBuilder().setKind(this.datastoreMappingContext.getPersistentEntity(descendantType).kindName()).setFilter(PropertyFilter.hasAncestor(ancestorKey)).build();
List entities = convertEntitiesForRead(getDatastoreReadWriter().run(descendantQuery), descendantType, context);
datastorePersistentEntity.getPropertyAccessor(convertedObject).setProperty(descendantPersistentProperty, // Converting the collection type.
this.datastoreEntityConverter.getConversions().convertOnRead(entities, descendantPersistentProperty.getType(), descendantType));
});
}
Aggregations