use of com.google.cloud.spring.data.firestore.FirestoreTemplateTests.TestEntity in project spring-cloud-gcp by GoogleCloudPlatform.
the class ReactiveFirestoreTransactionManagerTest method writeTransaction.
@Test
public void writeTransaction() {
FirestoreTemplate template = getFirestoreTemplate();
ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent, this.classMapper);
TransactionalOperator operator = TransactionalOperator.create(txManager);
Timestamp commitTime = Timestamp.newBuilder().setSeconds(3456).build();
doAnswer(invocation -> {
CommitRequest commitRequest = invocation.getArgument(0);
StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
assertThat(commitRequest.getWritesList().get(0).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e2");
assertThat(commitRequest.getWritesList().get(1).getUpdate().getName()).isEqualTo(this.parent + "/testEntities/" + "e3");
assertThat(commitRequest.getWritesList().get(2).getDelete()).isEqualTo(this.parent + "/testEntities/" + "e3");
streamObserver.onNext(CommitResponse.newBuilder().setCommitTime(commitTime).build());
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).commit(any(), any());
TestEntity e2 = new TestEntity("e2", 100L);
TestEntity e3 = new TestEntity("e3", 100L);
template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).flatMap(testEntity -> template.save(e2)).flatMap(testEntity -> template.save(e3)).flatMap(testEntity -> template.delete(Mono.just(testEntity))).then().as(operator::transactional).as(StepVerifier::create).verifyComplete();
assertThat(e2.getUpdateTimestamp().toProto()).isEqualTo(commitTime);
assertThat(e3.getUpdateTimestamp().toProto()).isEqualTo(commitTime);
verify(this.firestoreStub).beginTransaction(any(), any());
verify(this.firestoreStub).commit(any(), any());
GetDocumentRequest request1 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
verify(this.firestoreStub, times(1)).getDocument(eq(request1), any());
}
Aggregations