Search in sources :

Example 1 with WriteRequest

use of com.google.firestore.v1.WriteRequest in project java-firestore by googleapis.

the class FirestoreClientTest method writeTest.

@Test
public void writeTest() throws Exception {
    WriteResponse expectedResponse = WriteResponse.newBuilder().setStreamId("streamId1790933179").setStreamToken(ByteString.EMPTY).addAllWriteResults(new ArrayList<WriteResult>()).setCommitTime(Timestamp.newBuilder().build()).build();
    mockFirestore.addResponse(expectedResponse);
    WriteRequest request = WriteRequest.newBuilder().setDatabase("database1789464955").setStreamId("streamId1790933179").addAllWrites(new ArrayList<Write>()).setStreamToken(ByteString.EMPTY).putAllLabels(new HashMap<String, String>()).build();
    MockStreamObserver<WriteResponse> responseObserver = new MockStreamObserver<>();
    BidiStreamingCallable<WriteRequest, WriteResponse> callable = client.writeCallable();
    ApiStreamObserver<WriteRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    requestObserver.onCompleted();
    List<WriteResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : WriteResult(com.google.firestore.v1.WriteResult) HashMap(java.util.HashMap) BatchWriteRequest(com.google.firestore.v1.BatchWriteRequest) WriteRequest(com.google.firestore.v1.WriteRequest) WriteResponse(com.google.firestore.v1.WriteResponse) BatchWriteResponse(com.google.firestore.v1.BatchWriteResponse) ArrayList(java.util.ArrayList) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) Test(org.junit.Test)

Example 2 with WriteRequest

use of com.google.firestore.v1.WriteRequest in project firebase-android-sdk by firebase.

the class WatchStream method unwatchTarget.

/**
 * Unregisters interest in the results of the query associated with the given target ID.
 */
public void unwatchTarget(int targetId) {
    hardAssert(isOpen(), "Unwatching targets requires an open stream");
    ListenRequest request = ListenRequest.newBuilder().setDatabase(serializer.databaseName()).setRemoveTarget(targetId).build();
    writeRequest(request);
}
Also used : ListenRequest(com.google.firestore.v1.ListenRequest)

Example 3 with WriteRequest

use of com.google.firestore.v1.WriteRequest in project firebase-android-sdk by firebase.

the class WriteStream method writeMutations.

/**
 * Sends a list of mutations to the Firestore backend to apply
 *
 * @param mutations The mutations
 */
void writeMutations(List<Mutation> mutations) {
    hardAssert(isOpen(), "Writing mutations requires an opened stream");
    hardAssert(handshakeComplete, "Handshake must be complete before writing mutations");
    WriteRequest.Builder request = WriteRequest.newBuilder();
    for (Mutation mutation : mutations) {
        request.addWrites(serializer.encodeMutation(mutation));
    }
    request.setStreamToken(lastStreamToken);
    writeRequest(request.build());
}
Also used : WriteRequest(com.google.firestore.v1.WriteRequest) Mutation(com.google.firebase.firestore.model.mutation.Mutation)

Example 4 with WriteRequest

use of com.google.firestore.v1.WriteRequest in project java-firestore by googleapis.

the class FirestoreClientTest method writeExceptionTest.

@Test
public void writeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockFirestore.addException(exception);
    WriteRequest request = WriteRequest.newBuilder().setDatabase("database1789464955").setStreamId("streamId1790933179").addAllWrites(new ArrayList<Write>()).setStreamToken(ByteString.EMPTY).putAllLabels(new HashMap<String, String>()).build();
    MockStreamObserver<WriteResponse> responseObserver = new MockStreamObserver<>();
    BidiStreamingCallable<WriteRequest, WriteResponse> callable = client.writeCallable();
    ApiStreamObserver<WriteRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    try {
        List<WriteResponse> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
        InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
        Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) HashMap(java.util.HashMap) BatchWriteRequest(com.google.firestore.v1.BatchWriteRequest) WriteRequest(com.google.firestore.v1.WriteRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) ArrayList(java.util.ArrayList) WriteResponse(com.google.firestore.v1.WriteResponse) BatchWriteResponse(com.google.firestore.v1.BatchWriteResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

WriteRequest (com.google.firestore.v1.WriteRequest)3 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)2 BatchWriteRequest (com.google.firestore.v1.BatchWriteRequest)2 BatchWriteResponse (com.google.firestore.v1.BatchWriteResponse)2 WriteResponse (com.google.firestore.v1.WriteResponse)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 Mutation (com.google.firebase.firestore.model.mutation.Mutation)1 ListenRequest (com.google.firestore.v1.ListenRequest)1 WriteResult (com.google.firestore.v1.WriteResult)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ExecutionException (java.util.concurrent.ExecutionException)1