Search in sources :

Example 1 with DatastoreTemplate

use of com.google.cloud.spring.data.datastore.core.DatastoreTemplate in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpDatastoreEmulatorIntegrationTests method testDatastoreEmulatorConfiguration.

@Test
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 : Query(com.google.cloud.datastore.Query) AutoConfigurationPackage(org.springframework.boot.autoconfigure.AutoConfigurationPackage) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Datastore(com.google.cloud.datastore.Datastore) Supplier(java.util.function.Supplier) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) EnabledIfSystemProperty(org.junit.jupiter.api.condition.EnabledIfSystemProperty) ResolvableType(org.springframework.core.ResolvableType) StructuredQuery(com.google.cloud.datastore.StructuredQuery) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) DatastoreTransactionManagerAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.DatastoreTransactionManagerAutoConfiguration) GcpDatastoreAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.GcpDatastoreAutoConfiguration) Credentials(com.google.auth.Credentials) DatastoreException(com.google.cloud.datastore.DatastoreException) DatastoreOptions(com.google.cloud.datastore.DatastoreOptions) EntityQuery(com.google.cloud.datastore.EntityQuery) Test(org.junit.jupiter.api.Test) GcpContextAutoConfiguration(com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration) DatastoreRepositoriesAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.DatastoreRepositoriesAutoConfiguration) DatastoreTemplate(com.google.cloud.spring.data.datastore.core.DatastoreTemplate) GcpDatastoreEmulatorAutoConfiguration(com.google.cloud.spring.autoconfigure.datastore.GcpDatastoreEmulatorAutoConfiguration) Bean(org.springframework.context.annotation.Bean) Id(org.springframework.data.annotation.Id) Mockito.mock(org.mockito.Mockito.mock) Datastore(com.google.cloud.datastore.Datastore) DatastoreTemplate(com.google.cloud.spring.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(com.google.cloud.spring.core.GcpProjectIdProvider) Test(org.junit.jupiter.api.Test)

Aggregations

CredentialsProvider (com.google.api.gax.core.CredentialsProvider)1 Credentials (com.google.auth.Credentials)1 Datastore (com.google.cloud.datastore.Datastore)1 DatastoreException (com.google.cloud.datastore.DatastoreException)1 DatastoreOptions (com.google.cloud.datastore.DatastoreOptions)1 EntityQuery (com.google.cloud.datastore.EntityQuery)1 Query (com.google.cloud.datastore.Query)1 StructuredQuery (com.google.cloud.datastore.StructuredQuery)1 GcpContextAutoConfiguration (com.google.cloud.spring.autoconfigure.core.GcpContextAutoConfiguration)1 DatastoreRepositoriesAutoConfiguration (com.google.cloud.spring.autoconfigure.datastore.DatastoreRepositoriesAutoConfiguration)1 DatastoreTransactionManagerAutoConfiguration (com.google.cloud.spring.autoconfigure.datastore.DatastoreTransactionManagerAutoConfiguration)1 GcpDatastoreAutoConfiguration (com.google.cloud.spring.autoconfigure.datastore.GcpDatastoreAutoConfiguration)1 GcpDatastoreEmulatorAutoConfiguration (com.google.cloud.spring.autoconfigure.datastore.GcpDatastoreEmulatorAutoConfiguration)1 GcpProjectIdProvider (com.google.cloud.spring.core.GcpProjectIdProvider)1 DatastoreTemplate (com.google.cloud.spring.data.datastore.core.DatastoreTemplate)1 Supplier (java.util.function.Supplier)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Test (org.junit.jupiter.api.Test)1 EnabledIfSystemProperty (org.junit.jupiter.api.condition.EnabledIfSystemProperty)1