use of com.google.firestore.v1beta1.GetDocumentRequest in project grpc-gcp-java by GoogleCloudPlatform.
the class UpdateDocument method updateDocumentCall.
public void updateDocumentCall() {
System.out.println("\n:: Updating a Document ::\n");
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();
Document doc;
try {
doc = blockingStub.getDocument(getDocumentRequest);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
return;
}
HashMap<String, Value> fieldsMap;
MakeFieldsMap mfm = new MakeFieldsMap();
fieldsMap = mfm.makeFieldsMap();
doc = doc.toBuilder().putAllFields(fieldsMap).build();
Iterator it = fieldsMap.entrySet().iterator();
DocumentMask docMask = DocumentMask.newBuilder().build();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
docMask = docMask.toBuilder().addFieldPaths(pair.getKey().toString()).build();
}
UpdateDocumentRequest updateDocumentRequest = UpdateDocumentRequest.newBuilder().setDocument(doc).setMask(docMask).build();
try {
blockingStub.updateDocument(updateDocumentRequest);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
return;
}
System.out.println("Success!");
Menu menu = new Menu();
menu.draw();
}
use of com.google.firestore.v1beta1.GetDocumentRequest in project java-firestore by googleapis.
the class FirestoreClientTest method getDocumentExceptionTest.
@Test
public void getDocumentExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockFirestore.addException(exception);
try {
GetDocumentRequest request = GetDocumentRequest.newBuilder().setName("name3373707").setMask(DocumentMask.newBuilder().build()).build();
client.getDocument(request);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by spring-cloud.
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());
FirestoreTemplate template = new FirestoreTemplate(this.firestoreStub, this.parent, new FirestoreDefaultClassMapper(), new FirestoreMappingContext());
StepVerifier.setDefaultTimeout(Duration.ofSeconds(5));
return template;
}
use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by spring-cloud.
the class ReactiveFirestoreTransactionManagerTest method writeTransaction.
@Test
public void writeTransaction() {
FirestoreTemplate template = getFirestoreTemplate();
ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
TransactionalOperator operator = TransactionalOperator.create(txManager);
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().build());
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).commit(any(), any());
template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e2", 100L))).flatMap(testEntity -> template.save(new FirestoreTemplateTests.TestEntity("e3", 100L))).flatMap(testEntity -> template.delete(Mono.just(testEntity))).then().as(operator::transactional).as(StepVerifier::create).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());
}
use of com.google.firestore.v1beta1.GetDocumentRequest in project spring-cloud-gcp by spring-cloud.
the class FirestoreTemplateTests method existsByIdNotFoundTest.
@Test
public void existsByIdNotFoundTest() {
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.onError(new RuntimeException("NOT_FOUND: Document"));
streamObserver.onCompleted();
return null;
}).when(this.firestoreStub).getDocument(eq(request), any());
StepVerifier.create(this.firestoreTemplate.existsById(Mono.just("e1"), TestEntity.class)).expectNext(Boolean.FALSE).verifyComplete();
verify(this.firestoreStub, times(1)).getDocument(eq(request), any());
verify(this.firestoreStub, times(1)).getDocument(any(), any());
}
Aggregations