### Question:
GrpcTableService extends TableServiceImplBase { @Override public void put(PutRequest request, StreamObserver<PutResponse> responseObserver) { rangeStore.put(request).whenComplete( new ResponseHandler<PutResponse>(responseObserver) { @Override protected PutResponse createErrorResp(Throwable cause) { return PutResponse.newBuilder() .setHeader(ResponseHeader.newBuilder() .setCode(StatusCode.INTERNAL_SERVER_ERROR) .setRoutingHeader(request.getHeader()) .build()) .build(); } }); } GrpcTableService(RangeStoreService store); @Override void range(RangeRequest request,
                      StreamObserver<RangeResponse> responseObserver); @Override void put(PutRequest request,
                    StreamObserver<PutResponse> responseObserver); @Override void delete(DeleteRangeRequest request,
                       StreamObserver<DeleteRangeResponse> responseObserver); @Override void txn(TxnRequest request,
                    StreamObserver<TxnResponse> responseObserver); @Override void increment(IncrementRequest request,
                          StreamObserver<IncrementResponse> responseObserver);  }### Answer:
@Test public void testPutSuccess() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcTableService grpcService = new GrpcTableService(rangeService); PutRequest request = PutRequest .newBuilder() .setKey(TEST_KEY) .setValue(TEST_VAL) .setHeader(ROUTING_HEADER) .build(); PutResponse response = PutResponse.newBuilder() .setHeader(ResponseHeader.newBuilder() .setCode(StatusCode.SUCCESS) .setRoutingHeader(ROUTING_HEADER) .build()) .build(); when(rangeService.put(request)).thenReturn( CompletableFuture.completedFuture(response)); TestResponseObserver<PutResponse> responseObserver = new TestResponseObserver<>(); grpcService.put( request, responseObserver); responseObserver.verifySuccess(response); verify(rangeService, times(1)).put(request); }
@Test public void testPutFailure() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcTableService grpcService = new GrpcTableService(rangeService); PutRequest request = PutRequest .newBuilder() .setKey(TEST_KEY) .setValue(TEST_VAL) .setHeader(ROUTING_HEADER) .build(); PutResponse response = PutResponse.newBuilder() .setHeader(ResponseHeader.newBuilder() .setCode(StatusCode.INTERNAL_SERVER_ERROR) .setRoutingHeader(ROUTING_HEADER) .build()) .build(); when(rangeService.put(request)).thenReturn( FutureUtils.exception(CAUSE)); TestResponseObserver<PutResponse> responseObserver = new TestResponseObserver<>(); grpcService.put( request, responseObserver); responseObserver.verifySuccess(response); verify(rangeService, times(1)).put(request); }
@Test public void testPutException() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcTableService grpcService = new GrpcTableService(rangeService); PutRequest request = PutRequest .newBuilder() .setKey(TEST_KEY) .setValue(TEST_VAL) .setHeader(ROUTING_HEADER) .build(); when(rangeService.put(request)).thenReturn( FutureUtils.exception(new StatusRuntimeException(Status.NOT_FOUND))); TestResponseObserver<PutResponse> responseObserver = new TestResponseObserver<>(); grpcService.put( request, responseObserver); responseObserver.verifyException(Status.NOT_FOUND); verify(rangeService, times(1)).put(request); }