Search in sources :

Example 16 with GetDocumentRequest

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

the class FirestoreTemplateTests method findAllByIdTest.

@Test
public void findAllByIdTest() {
    GetDocumentRequest request1 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/e1").build();
    GetDocumentRequest request2 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/e2").build();
    GetDocumentRequest request3 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/e3").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(request1), any());
    doAnswer(invocation -> {
        StreamObserver<com.google.firestore.v1.Document> streamObserver = invocation.getArgument(1);
        streamObserver.onNext(buildDocument("e2", 200L));
        streamObserver.onCompleted();
        return null;
    }).when(this.firestoreStub).getDocument(eq(request2), any());
    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(eq(request3), any());
    StepVerifier.create(this.firestoreTemplate.findAllById(Flux.just("e1", "e2"), TestEntity.class)).expectNext(new TestEntity("e1", 100L), new TestEntity("e2", 200L)).verifyComplete();
    verify(this.firestoreStub, times(1)).getDocument(eq(request1), any());
}
Also used : GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Test(org.junit.Test)

Example 17 with GetDocumentRequest

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

the class FirestoreTemplateTests method findByIdTest.

@Test
public void findByIdTest() {
    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(any(), any());
    StepVerifier.create(this.firestoreTemplate.findById(Mono.just("e1"), TestEntity.class)).expectNext(new TestEntity("e1", 100L)).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 18 with GetDocumentRequest

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

the class FirestoreTemplateTests method findByIdErrorTest.

@Test
public void findByIdErrorTest() {
    doAnswer(invocation -> {
        StreamObserver<com.google.firestore.v1.Document> streamObserver = invocation.getArgument(1);
        streamObserver.onError(new RuntimeException("Firestore error"));
        return null;
    }).when(this.firestoreStub).getDocument(any(), any());
    StepVerifier.create(this.firestoreTemplate.findById(Mono.just("e1"), TestEntity.class)).expectErrorMatches(e -> e instanceof FirestoreDataException && e.getMessage().contains("Firestore error") && e.getMessage().contains("Error while reading entries by id")).verify();
    GetDocumentRequest request = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").build();
    verify(this.firestoreStub, times(1)).getDocument(eq(request), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) CommitResponse(com.google.firestore.v1.CommitResponse) RunQueryRequest(com.google.firestore.v1.RunQueryRequest) StepVerifier(reactor.test.StepVerifier) Write(com.google.firestore.v1.Write) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) HashMap(java.util.HashMap) Builder(com.google.firestore.v1.Document.Builder) DocumentMask(com.google.firestore.v1.DocumentMask) StreamObserver(io.grpc.stub.StreamObserver) Value(com.google.firestore.v1.Value) CommitRequest(com.google.firestore.v1.CommitRequest) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) FirestoreDefaultClassMapper(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreDefaultClassMapper) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Before(org.junit.Before) StructuredQuery(com.google.firestore.v1.StructuredQuery) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) DocumentId(com.google.cloud.firestore.annotation.DocumentId) Mockito.verify(org.mockito.Mockito.verify) Objects(java.util.Objects) Flux(reactor.core.publisher.Flux) FirestoreStub(com.google.firestore.v1.FirestoreGrpc.FirestoreStub) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) FirestoreMappingContext(org.springframework.cloud.gcp.data.firestore.mapping.FirestoreMappingContext) Mockito.mock(org.mockito.Mockito.mock) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Test(org.junit.Test)

Example 19 with GetDocumentRequest

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

the class FirestoreTemplateTests method existsByIdTest.

@Test
public 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.Test)

Example 20 with GetDocumentRequest

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

the class FirestoreTemplateTests method withParentTest_entityReference.

@Test
void withParentTest_entityReference() {
    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(any(), any());
    this.firestoreTemplate.withParent(new TestEntity("parent", 0L)).findById(Mono.just("child"), TestEntity.class).block();
    GetDocumentRequest request = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/parent/testEntities/child").build();
    verify(this.firestoreStub, times(1)).getDocument(eq(request), 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