Search in sources :

Example 1 with DatastoreTemplate

use of org.springframework.cloud.gcp.data.datastore.core.DatastoreTemplate 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)

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 Supplier (java.util.function.Supplier)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Matchers.is (org.hamcrest.Matchers.is)1 Assume.assumeThat (org.junit.Assume.assumeThat)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1 Mockito.mock (org.mockito.Mockito.mock)1 AutoConfigurationPackage (org.springframework.boot.autoconfigure.AutoConfigurationPackage)1 AutoConfigurations (org.springframework.boot.autoconfigure.AutoConfigurations)1 ApplicationContextRunner (org.springframework.boot.test.context.runner.ApplicationContextRunner)1 GcpContextAutoConfiguration (org.springframework.cloud.gcp.autoconfigure.core.GcpContextAutoConfiguration)1