Search in sources :

Example 1 with TestEntity

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());
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) CommitRequest(com.google.firestore.v1.CommitRequest) TestEntity(com.google.cloud.spring.data.firestore.FirestoreTemplateTests.TestEntity) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CommitResponse(com.google.firestore.v1.CommitResponse) StepVerifier(reactor.test.StepVerifier) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestEntity(com.google.cloud.spring.data.firestore.FirestoreTemplateTests.TestEntity) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Timestamp(com.google.protobuf.Timestamp) FirestoreTemplateTests(com.google.cloud.spring.data.firestore.FirestoreTemplateTests) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) Empty(com.google.protobuf.Empty) StreamObserver(io.grpc.stub.StreamObserver) CommitRequest(com.google.firestore.v1.CommitRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Duration(java.time.Duration) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) FirestoreGrpc(com.google.firestore.v1.FirestoreGrpc) FirestoreDataException(com.google.cloud.spring.data.firestore.FirestoreDataException) Document(com.google.firestore.v1.Document) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.verify(org.mockito.Mockito.verify) FirestoreDefaultClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper) ByteString(com.google.protobuf.ByteString) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) FirestoreMappingContext(com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext) Mockito.mock(org.mockito.Mockito.mock) CommitResponse(com.google.firestore.v1.CommitResponse) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Timestamp(com.google.protobuf.Timestamp) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Test(org.junit.Test)

Aggregations

FirestoreDataException (com.google.cloud.spring.data.firestore.FirestoreDataException)1 FirestoreTemplate (com.google.cloud.spring.data.firestore.FirestoreTemplate)1 FirestoreTemplateTests (com.google.cloud.spring.data.firestore.FirestoreTemplateTests)1 TestEntity (com.google.cloud.spring.data.firestore.FirestoreTemplateTests.TestEntity)1 FirestoreDefaultClassMapper (com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper)1 FirestoreMappingContext (com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext)1 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)1 CommitRequest (com.google.firestore.v1.CommitRequest)1 CommitResponse (com.google.firestore.v1.CommitResponse)1 Document (com.google.firestore.v1.Document)1 FirestoreGrpc (com.google.firestore.v1.FirestoreGrpc)1 GetDocumentRequest (com.google.firestore.v1.GetDocumentRequest)1 RollbackRequest (com.google.firestore.v1.RollbackRequest)1 ByteString (com.google.protobuf.ByteString)1 Empty (com.google.protobuf.Empty)1 Timestamp (com.google.protobuf.Timestamp)1 StreamObserver (io.grpc.stub.StreamObserver)1 Duration (java.time.Duration)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.Test)1