Search in sources :

Example 6 with EntityQuery

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);
}
Also used : ArrayList(java.util.ArrayList) ProjectionEntityQuery(com.google.cloud.datastore.ProjectionEntityQuery) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 7 with EntityQuery

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));
}
Also used : DatastoreTransactionManagerAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.DatastoreTransactionManagerAutoConfiguration) Query(com.google.cloud.datastore.Query) AutoConfigurationPackage(org.springframework.boot.autoconfigure.AutoConfigurationPackage) GcpDatastoreAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.GcpDatastoreAutoConfiguration) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpDatastoreEmulatorAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.GcpDatastoreEmulatorAutoConfiguration) Datastore(com.google.cloud.datastore.Datastore) Supplier(java.util.function.Supplier) GcpProjectIdProvider(org.springframework.cloud.gcp.core.GcpProjectIdProvider) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ResolvableType(org.springframework.core.ResolvableType) StructuredQuery(com.google.cloud.datastore.StructuredQuery) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) Credentials(com.google.auth.Credentials) Test(org.junit.Test) DatastoreException(com.google.cloud.datastore.DatastoreException) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) EntityQuery(com.google.cloud.datastore.EntityQuery) Matchers.is(org.hamcrest.Matchers.is) Bean(org.springframework.context.annotation.Bean) DatastoreRepositoriesAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.datastore.DatastoreRepositoriesAutoConfiguration) Id(org.springframework.data.annotation.Id) GcpContextAutoConfiguration(org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration) Mockito.mock(org.mockito.Mockito.mock) Datastore(com.google.cloud.datastore.Datastore) DatastoreTemplate(org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Supplier(java.util.function.Supplier) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) DatastoreException(com.google.cloud.datastore.DatastoreException) EntityQuery(com.google.cloud.datastore.EntityQuery) GcpProjectIdProvider(org.springframework.cloud.gcp.core.GcpProjectIdProvider) Test(org.junit.Test)

Example 8 with EntityQuery

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());
}
Also used : EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 9 with EntityQuery

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());
}
Also used : Pageable(org.springframework.data.domain.Pageable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Example 10 with EntityQuery

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());
}
Also used : Pageable(org.springframework.data.domain.Pageable) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.Test)

Aggregations

EntityQuery (com.google.cloud.datastore.EntityQuery)15 Test (org.junit.Test)11 DatastoreResultsIterable (org.springframework.cloud.gcp.data.datastore.core.DatastoreResultsIterable)4 Pageable (org.springframework.data.domain.Pageable)3 ProjectionEntityQuery (com.google.cloud.datastore.ProjectionEntityQuery)2 StructuredQuery (com.google.cloud.datastore.StructuredQuery)2 ArrayList (java.util.ArrayList)2 Sort (org.springframework.data.domain.Sort)2 CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 Credentials (com.google.auth.Credentials)1 BaseKey (com.google.cloud.datastore.BaseKey)1 Cursor (com.google.cloud.datastore.Cursor)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 Entity (com.google.cloud.datastore.Entity)1 IncompleteKey (com.google.cloud.datastore.IncompleteKey)1 Key (com.google.cloud.datastore.Key)1 KeyQuery (com.google.cloud.datastore.KeyQuery)1 Query (com.google.cloud.datastore.Query)1