Search in sources :

Example 6 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by spring-cloud.

the class FirestoreTemplateTests method findByIdNotFoundTest.

@Test
public void findByIdNotFoundTest() {
    doAnswer(invocation -> {
        StreamObserver<com.google.firestore.v1.Document> streamObserver = invocation.getArgument(1);
        streamObserver.onError(new RuntimeException("NOT_FOUND: Document"));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    StepVerifier.create(this.firestoreTemplate.findById(Mono.just("e1"), TestEntity.class)).verifyComplete();
    GetDocumentRequest request = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").build();
    verify(this.firestoreStub, times(1)).getDocument(eq(request), any());
}
Also used : GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Test(org.junit.Test)

Example 7 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManagerTest method triggerCommitCorrectly.

@Test
void triggerCommitCorrectly() {
    FirestoreTemplate template = getFirestoreTemplate();
    ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent, this.classMapper);
    TransactionalOperator operator = TransactionalOperator.create(txManager);
    template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).concatWith(template.findById(Mono.just("e2"), FirestoreTemplateTests.TestEntity.class)).as(operator::transactional).as(StepVerifier::create).expectNext(new FirestoreTemplateTests.TestEntity("e1", 100L), new FirestoreTemplateTests.TestEntity("e2", 100L)).verifyComplete();
    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());
    GetDocumentRequest request2 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e2").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
    verify(this.firestoreStub, times(1)).getDocument(eq(request2), any());
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) TestEntity(com.google.cloud.spring.data.firestore.FirestoreTemplateTests.TestEntity) StepVerifier(reactor.test.StepVerifier) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Test(org.junit.jupiter.api.Test)

Example 8 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManagerTest method writeTransaction.

@Test
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) 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) Test(org.junit.jupiter.api.Test) 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.jupiter.api.Test)

Example 9 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by GoogleCloudPlatform.

the class ReactiveFirestoreTransactionManagerTest method getFirestoreTemplate.

private FirestoreTemplate getFirestoreTemplate() {
    doAnswer(invocation -> {
        StreamObserver<BeginTransactionResponse> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(BeginTransactionResponse.newBuilder().setTransaction(ByteString.copyFromUtf8("transaction1")).build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).beginTransaction(any(), any());
    doAnswer(invocation -> {
        CommitRequest commitRequest = invocation.getArgument(0);
        StreamObserver<CommitResponse> streamObserver = invocation.getArgument(1);
        assertThat(commitRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onNext(CommitResponse.newBuilder().build());
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).commit(any(), any());
    doAnswer(invocation -> {
        RollbackRequest rollbackRequest = invocation.getArgument(0);
        StreamObserver<Empty> streamObserver = invocation.getArgument(1);
        assertThat(rollbackRequest.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).rollback(any(), any());
    doAnswer(invocation -> {
        GetDocumentRequest request = invocation.getArgument(0);
        StreamObserver<Document> streamObserver = invocation.getArgument(1);
        assertThat(request.getTransaction()).isEqualTo(ByteString.copyFromUtf8("transaction1"));
        String name = request.getName();
        streamObserver.onNext(FirestoreTemplateTests.buildDocument(name.substring(name.length() - 2), 100L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    FirestoreMappingContext mappingContext = new FirestoreMappingContext();
    FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(mappingContext), mappingContext);
    StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
    return template;
}
Also used : CommitRequest(com.google.firestore.v1.CommitRequest) CommitResponse(com.google.firestore.v1.CommitResponse) ByteString(com.google.protobuf.ByteString) RollbackRequest(com.google.firestore.v1.RollbackRequest) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Document(com.google.firestore.v1.Document) BeginTransactionResponse(com.google.firestore.v1.BeginTransactionResponse) FirestoreMappingContext(com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext) FirestoreTemplate(com.google.cloud.spring.data.firestore.FirestoreTemplate) Empty(com.google.protobuf.Empty) FirestoreDefaultClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper)

Example 10 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by GoogleCloudPlatform.

the class FirestoreTemplateTests method existsByIdTest.

@Test
void existsByIdTest() {
    GetDocumentRequest request = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").setMask(DocumentMask.newBuilder().addFieldPaths("__name__").build()).build();
    doAnswer(invocation -> {
        StreamObserver<com.google.firestore.v1.Document> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(buildDocument("e1", 100L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(eq(request), any());
    StepVerifier.create(this.firestoreTemplate.existsById(Mono.just("e1"), TestEntity.class)).expectNext(Boolean.TRUE).verifyComplete();
    verify(this.firestoreStub, times(1)).getDocument(eq(request), any());
    verify(this.firestoreStub, times(1)).getDocument(any(), any());
}
Also used : GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Test(org.junit.jupiter.api.Test)

Aggregations

GetDocumentRequest (com.google.firestore.v1.GetDocumentRequest)22 Test (org.junit.Test)10 Test (org.junit.jupiter.api.Test)10 CommitRequest (com.google.firestore.v1.CommitRequest)6 CommitResponse (com.google.firestore.v1.CommitResponse)6 StepVerifier (reactor.test.StepVerifier)6 Document (com.google.firestore.v1.Document)5 StreamObserver (io.grpc.stub.StreamObserver)5 BeginTransactionResponse (com.google.firestore.v1.BeginTransactionResponse)4 RollbackRequest (com.google.firestore.v1.RollbackRequest)4 ByteString (com.google.protobuf.ByteString)4 Empty (com.google.protobuf.Empty)4 HashMap (java.util.HashMap)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.verify (org.mockito.Mockito.verify)4 Mono (reactor.core.publisher.Mono)4 FirestoreTemplate (com.google.cloud.spring.data.firestore.FirestoreTemplate)3