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));
}
Aggregations