use of com.google.cloud.datastore.DatastoreReaderWriter in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplateTests method performTransactionTest.
@Test
public void performTransactionTest() {
DatastoreReaderWriter transactionContext = mock(DatastoreReaderWriter.class);
when(this.datastore.runInTransaction(any())).thenAnswer((invocation) -> {
TransactionCallable<String> callable = invocation.getArgument(0);
return callable.run(transactionContext);
});
List<Entity> e1 = Collections.singletonList(this.e1);
when(transactionContext.fetch(ArgumentMatchers.<Key[]>any())).thenReturn(e1);
String finalResult = this.datastoreTemplate.performTransaction((datastoreOperations) -> {
datastoreOperations.save(this.ob2);
datastoreOperations.findById("ignored", TestEntity.class);
return "all done";
});
assertThat(finalResult).isEqualTo("all done");
verify(transactionContext, times(1)).put(ArgumentMatchers.<FullEntity[]>any());
verify(transactionContext, times(2)).fetch((Key[]) any());
}
use of com.google.cloud.datastore.DatastoreReaderWriter in project spring-cloud-gcp by spring-cloud.
the class DatastoreTemplate method computeReferencedField.
private <T> T computeReferencedField(BaseEntity entity, ReadContext context, DatastorePersistentProperty referenceProperty, String fieldName, Class<T> type) {
T referenced;
if (referenceProperty.isLazyLoaded()) {
DatastoreReaderWriter originalTx = getDatastoreReadWriter();
referenced = LazyUtil.wrapSimpleLazyProxy(() -> {
if (getDatastoreReadWriter() != originalTx) {
throw new DatastoreDataException("Lazy load should be invoked within the same transaction");
}
return (T) findReferenced(entity, referenceProperty, context);
}, type, entity.getValue(fieldName));
} else {
referenced = (T) findReferenced(entity, referenceProperty, context);
}
return referenced;
}
Aggregations