Search in sources :

Example 1 with WriteResponse

use of com.google.firestore.v1.WriteResponse 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 WriteResponse

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

the class RemoteSerializer method decodeMutationResult.

public MutationResult decodeMutationResult(com.google.firestore.v1.WriteResult proto, SnapshotVersion commitVersion) {
    // NOTE: Deletes don't have an updateTime but the commit timestamp from the containing
    // CommitResponse or WriteResponse indicates essentially that the delete happened no later than
    // that. For our purposes we don't care exactly when the delete happened so long as we can tell
    // when an update on the watch stream is at or later than that change.
    SnapshotVersion version = decodeVersion(proto.getUpdateTime());
    if (SnapshotVersion.NONE.equals(version)) {
        version = commitVersion;
    }
    int transformResultsCount = proto.getTransformResultsCount();
    List<Value> transformResults = new ArrayList<>(transformResultsCount);
    for (int i = 0; i < transformResultsCount; i++) {
        transformResults.add(proto.getTransformResults(i));
    }
    return new MutationResult(version, transformResults);
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ObjectValue(com.google.firebase.firestore.model.ObjectValue) Value(com.google.firestore.v1.Value) Int32Value(com.google.protobuf.Int32Value) ArrayValue(com.google.firestore.v1.ArrayValue) ArrayList(java.util.ArrayList) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 3 with WriteResponse

use of com.google.firestore.v1.WriteResponse 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

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