use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class Employee method testNextPageAwareQuery.
@Test
public void testNextPageAwareQuery() {
DatastorePersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(TestEntity.class);
EntityQuery query = StructuredQuery.newEntityQueryBuilder().setKind(persistentEntity.kindName()).setFilter(PropertyFilter.eq("color", "red")).setLimit(2).build();
Slice<TestEntity> results = this.datastoreTemplate.queryEntitiesSlice(query, TestEntity.class, PageRequest.of(0, 2));
List<TestEntity> testEntities = new ArrayList<>();
testEntities.addAll(results.toList());
assertThat(results.hasNext()).isTrue();
results = this.datastoreTemplate.queryEntitiesSlice(query, TestEntity.class, results.nextPageable());
testEntities.addAll(results.toList());
assertThat(results.hasNext()).isFalse();
assertThat(testEntities).contains(testEntityA, testEntityC, testEntityD);
}
use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class GcpDatastoreEmulatorIntegrationTests method testDatastoreEmulatorConfiguration.
@Test
public void testDatastoreEmulatorConfiguration() {
DatastoreOptions.Builder builder = DatastoreOptions.newBuilder();
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpDatastoreAutoConfiguration.class, GcpContextAutoConfiguration.class, DatastoreTransactionManagerAutoConfiguration.class, DatastoreRepositoriesAutoConfiguration.class, GcpDatastoreEmulatorAutoConfiguration.class)).withUserConfiguration(TestConfiguration.class).withPropertyValues("spring.cloud.gcp.project-id=test-project", "spring.cloud.gcp.datastore.namespace=test-namespace", "spring.cloud.gcp.datastore.emulator.port=8181", "spring.cloud.gcp.datastore.emulator.enabled=true", "spring.cloud.gcp.datastore.emulator.consistency=0.9").run((context) -> {
DatastoreTemplate datastore = context.getBean(DatastoreTemplate.class);
Datastore datastoreClient = (Datastore) ((Supplier) context.getBean(context.getBeanNamesForType(ResolvableType.forClassWithGenerics(Supplier.class, Datastore.class))[0])).get();
GcpProjectIdProvider projectIdProvider = context.getBean(GcpProjectIdProvider.class);
builder.setServiceFactory(datastoreOptions -> datastoreClient).setProjectId(projectIdProvider.getProjectId());
EmulatorEntityTest entity = new EmulatorEntityTest();
entity.setProperty("property-test");
datastore.save(entity);
assertThat(entity.getId()).isNotNull();
assertThat(datastore.findById(entity.getId(), EmulatorEntityTest.class).getProperty()).isEqualTo("property-test");
});
Datastore datastore = builder.build().getService();
EntityQuery query = Query.newEntityQueryBuilder().setKind("RandomKind").setFilter(StructuredQuery.PropertyFilter.eq("key", "value")).build();
assertThatExceptionOfType(DatastoreException.class).isThrownBy(() -> datastore.run(query));
}
use of com.google.cloud.datastore.EntityQuery in project spring-cloud-gcp by spring-cloud.
the class PartTreeDatastoreQueryTests method compoundNameConventionTest.
@Test
public void compoundNameConventionTest() throws NoSuchMethodException {
queryWithMockResult("findTop333ByActionAndSymbolAndPriceLessThan" + "AndPriceGreaterThanEqual" + "AndEmbeddedEntityStringFieldEquals" + "AndIdIsNullOrderByIdDesc", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, String.class));
Object[] params = new Object[] { "BUY", "abcd", // this int param requires custom conversion
8, 3.33, "abc" };
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(CompositeFilter.and(PropertyFilter.eq("action", "BUY"), PropertyFilter.eq("ticker", "abcd"), PropertyFilter.lt("price", 8L), PropertyFilter.ge("price", 3.33), PropertyFilter.eq("embeddedEntity.stringField", "abc"), PropertyFilter.isNull("__key__"))).setKind("trades").setOrderBy(OrderBy.desc("__key__")).setLimit(333).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 pageableParam.
@Test
public void pageableParam() throws NoSuchMethodException {
queryWithMockResult("findByActionAndSymbolAndPriceLessThanAndPriceGreater" + "ThanEqualAndIdIsNull", null, getClass().getMethod("tradeMethod", String.class, String.class, double.class, double.class, Pageable.class));
Object[] params = new Object[] { "BUY", "abcd", 8.88, 3.33, PageRequest.of(1, 444, Sort.Direction.DESC, "id") };
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOffset(444).setOrderBy(OrderBy.desc("__key__")).setLimit(444).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 ambiguousSortPageableParam.
@Test
public void ambiguousSortPageableParam() 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, PageRequest.of(1, 444, Sort.Direction.ASC, "price") };
when(this.datastoreTemplate.queryKeysOrEntities(any(), any())).thenAnswer((invocation) -> {
EntityQuery statement = invocation.getArgument(0);
EntityQuery expected = StructuredQuery.newEntityQueryBuilder().setFilter(FILTER).setKind("trades").setOffset(444).setLimit(444).setOrderBy(OrderBy.desc("__key__"), OrderBy.asc("price")).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());
}
Aggregations