Search in sources :

Example 21 with GetDocumentRequest

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

the class FirestoreTemplateTests method findByIdErrorTest.

@Test
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) BeforeEach(org.junit.jupiter.api.BeforeEach) 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) Timestamp(com.google.cloud.Timestamp) 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) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Precondition(com.google.firestore.v1.Precondition) StructuredQuery(com.google.firestore.v1.StructuredQuery) Mockito.times(org.mockito.Mockito.times) Mono(reactor.core.publisher.Mono) Mockito.verify(org.mockito.Mockito.verify) DocumentId(com.google.cloud.firestore.annotation.DocumentId) FirestoreDefaultClassMapper(com.google.cloud.spring.data.firestore.mapping.FirestoreDefaultClassMapper) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) UpdateTime(com.google.cloud.spring.data.firestore.mapping.UpdateTime) FirestoreMappingContext(com.google.cloud.spring.data.firestore.mapping.FirestoreMappingContext) FirestoreStub(com.google.firestore.v1.FirestoreGrpc.FirestoreStub) RunQueryResponse(com.google.firestore.v1.RunQueryResponse) Mockito.mock(org.mockito.Mockito.mock) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Test(org.junit.jupiter.api.Test)

Example 22 with GetDocumentRequest

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

the class FirestoreTemplateTests method findAllByIdTest.

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

Example 23 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project java-firestore by googleapis.

the class FirestoreClientTest method getDocumentTest.

@Test
public void getDocumentTest() throws Exception {
    Document expectedResponse = Document.newBuilder().setName("name3373707").putAllFields(new HashMap<String, Value>()).setCreateTime(Timestamp.newBuilder().build()).setUpdateTime(Timestamp.newBuilder().build()).build();
    mockFirestore.addResponse(expectedResponse);
    GetDocumentRequest request = GetDocumentRequest.newBuilder().setName("name3373707").setMask(DocumentMask.newBuilder().build()).build();
    Document actualResponse = client.getDocument(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockFirestore.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetDocumentRequest actualRequest = ((GetDocumentRequest) actualRequests.get(0));
    Assert.assertEquals(request.getName(), actualRequest.getName());
    Assert.assertEquals(request.getMask(), actualRequest.getMask());
    Assert.assertEquals(request.getTransaction(), actualRequest.getTransaction());
    Assert.assertEquals(request.getReadTime(), actualRequest.getReadTime());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) Document(com.google.firestore.v1.Document) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) Test(org.junit.Test)

Example 24 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class Write method writeCall.

public void writeCall() {
    System.out.println(":: Starting Write Stream ::");
    FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    FirestoreStub firestoreStub = new GRPCFirebaseClientFactory().createFirebaseClient().getFirestoreStub();
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter document name: ");
    String docName = sc.next();
    GetDocumentRequest getDocumentRequest = GetDocumentRequest.newBuilder().setName("projects/firestoretestclient/databases/(default)/documents/GrpcTestData/" + docName).build();
    Document doc;
    try {
        doc = blockingStub.getDocument(getDocumentRequest);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    // Retrieve initial stream token and stream id
    WriteRequest writeRequest = WriteRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").build();
    StreamObserver<WriteResponse> writeResponseStreamObserver = new StreamObserver<WriteResponse>() {

        @Override
        public void onNext(WriteResponse writeResponse) {
            Write.streamToken = writeResponse.getStreamToken();
            Write.streamId = writeResponse.getStreamId();
        }

        @Override
        public void onError(Throwable throwable) {
            System.out.println(throwable.getMessage() + throwable.getCause());
        }

        @Override
        public void onCompleted() {
        }
    };
    StreamObserver<WriteRequest> writeRequestStreamObserver = firestoreStub.write(writeResponseStreamObserver);
    writeRequestStreamObserver.onNext(writeRequest);
    String fieldName = "";
    String fieldValue = "";
    while (!fieldName.matches("DONE")) {
        System.out.print("Field Name (Enter DONE to quit): ");
        fieldName = sc.next();
        if (!fieldName.matches("DONE")) {
            System.out.print("Field Value: ");
            fieldValue = sc.next();
            Value fsValue = Value.newBuilder().setStringValue(fieldValue).build();
            doc = doc.toBuilder().putFields(fieldName, fsValue).build();
            DocumentMask docMask = DocumentMask.newBuilder().addFieldPaths(fieldName).build();
            com.google.firestore.v1beta1.Write currentWrite = com.google.firestore.v1beta1.Write.newBuilder().setUpdate(doc).setUpdateMask(docMask).build();
            WriteRequest currentWriteRequest = WriteRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").setStreamToken(Write.streamToken).setStreamId(Write.streamId).addWrites(currentWrite).build();
            writeRequestStreamObserver.onNext(currentWriteRequest);
        }
    }
    writeRequestStreamObserver.onCompleted();
    System.out.println("Finished streaming writes!");
    Menu menu = new Menu();
    menu.draw();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) Scanner(java.util.Scanner) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) FirestoreStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreStub) ByteString(com.google.protobuf.ByteString) com.google.firestore.v1beta1(com.google.firestore.v1beta1) Menu(org.roguewave.grpc.util.gfx.Menu)

Example 25 with GetDocumentRequest

use of com.google.firestore.v1beta1.GetDocumentRequest in project grpc-gcp-java by GoogleCloudPlatform.

the class GetDocument method getDocumentCall.

public void getDocumentCall() {
    System.out.println("\n:: Getting a Document ::\n");
    FirestoreGrpc.FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter document name: ");
    String docName = sc.next();
    GetDocumentRequest getDocumentRequest = GetDocumentRequest.newBuilder().setName("projects/firestoretestclient/databases/(default)/documents/GrpcTestData/" + docName).build();
    try {
        Document doc = blockingStub.getDocument(getDocumentRequest);
        DrawDocument dd = new DrawDocument();
        dd.draw(doc);
    } catch (Exception e) {
        System.out.println("Error during call: " + e.getMessage() + e.getCause());
        return;
    }
    Menu menu = new Menu();
    menu.draw();
}
Also used : GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) Scanner(java.util.Scanner) Menu(org.roguewave.grpc.util.gfx.Menu) FirestoreGrpc(com.google.firestore.v1beta1.FirestoreGrpc) GetDocumentRequest(com.google.firestore.v1beta1.GetDocumentRequest) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument) Document(com.google.firestore.v1beta1.Document) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument)

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