Search in sources :

Example 1 with ResponseObserver

use of com.google.api.gax.rpc.ResponseObserver in project java-bigtable-hbase by googleapis.

the class TestDataClientVeneerApi method testReadRowsAsyncWithStreamOb.

@Test
public void testReadRowsAsyncWithStreamOb() {
    final Exception readException = new Exception();
    Query request = Query.create(TABLE_ID).rowKey(ROW_KEY);
    StreamObserver<Result> resultStreamOb = new StreamObserver<Result>() {

        @Override
        public void onNext(Result result) {
            assertResult(EXPECTED_RESULT, result);
        }

        @Override
        public void onError(Throwable throwable) {
            assertEquals(readException, throwable);
        }

        @Override
        public void onCompleted() {
        }
    };
    when(mockDataClient.readRowsCallable(Mockito.<RowResultAdapter>any())).thenReturn(mockStreamingCallable);
    doAnswer(new Answer() {

        int count = 0;

        @Override
        public Object answer(InvocationOnMock invocationOnMock) {
            ResponseObserver<Result> resObserver = invocationOnMock.getArgument(1);
            resObserver.onStart(null);
            resObserver.onResponse(EXPECTED_RESULT);
            if (count == 0) {
                resObserver.onComplete();
            } else {
                resObserver.onError(readException);
            }
            count++;
            return null;
        }
    }).when(mockStreamingCallable).call(Mockito.<Query>any(), Mockito.<ResponseObserver<Result>>any());
    dataClientWrapper.readRowsAsync(request, resultStreamOb);
    dataClientWrapper.readRowsAsync(request, resultStreamOb);
    verify(mockDataClient, times(2)).readRowsCallable(Mockito.<RowResultAdapter>any());
    verify(mockStreamingCallable, times(2)).call(Mockito.<Query>any(), Mockito.<ResponseObserver<Result>>any(), Mockito.any(GrpcCallContext.class));
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Query(com.google.cloud.bigtable.data.v2.models.Query) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResponseObserver(com.google.api.gax.rpc.ResponseObserver) Test(org.junit.Test)

Aggregations

GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)1 ResponseObserver (com.google.api.gax.rpc.ResponseObserver)1 Query (com.google.cloud.bigtable.data.v2.models.Query)1 StreamObserver (io.grpc.stub.StreamObserver)1 IOException (java.io.IOException)1 Result (org.apache.hadoop.hbase.client.Result)1 Test (org.junit.Test)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1