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());
}
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());
}
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()));
}
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();
}
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();
}
Aggregations