### Question:
GrpcRootRangeService extends RootRangeServiceImplBase { @Override public void getNamespace(GetNamespaceRequest request, StreamObserver<GetNamespaceResponse> responseObserver) { rs.getNamespace(request).whenComplete( new ResponseHandler<GetNamespaceResponse>(responseObserver) { @Override protected GetNamespaceResponse createErrorResp(Throwable cause) { return GetNamespaceResponse.newBuilder() .setCode(StatusCode.INTERNAL_SERVER_ERROR) .build(); } }); } GrpcRootRangeService(RangeStoreService rs); @Override void createNamespace(CreateNamespaceRequest request,
                                StreamObserver<CreateNamespaceResponse> responseObserver); @Override void deleteNamespace(DeleteNamespaceRequest request,
                                StreamObserver<DeleteNamespaceResponse> responseObserver); @Override void getNamespace(GetNamespaceRequest request,
                             StreamObserver<GetNamespaceResponse> responseObserver); @Override void createStream(CreateStreamRequest request,
                             StreamObserver<CreateStreamResponse> responseObserver); @Override void deleteStream(DeleteStreamRequest request,
                             StreamObserver<DeleteStreamResponse> responseObserver); @Override void getStream(GetStreamRequest request,
                          StreamObserver<GetStreamResponse> responseObserver);  }### Answer:
@Test public void testGetNamespaceSuccess() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService); GetNamespaceResponse getResp = GetNamespaceResponse.newBuilder() .setCode(StatusCode.SUCCESS) .setNsProps(namespaceProps) .build(); GetNamespaceRequest getReq = createGetNamespaceRequest(nsName); when(rangeService.getNamespace(getReq)).thenReturn( CompletableFuture.completedFuture(getResp)); AtomicReference<GetNamespaceResponse> resultHolder = new AtomicReference<>(); AtomicReference<Throwable> exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); StreamObserver<GetNamespaceResponse> streamObserver = new StreamObserver<GetNamespaceResponse>() { @Override public void onNext(GetNamespaceResponse resp) { resultHolder.set(resp); } @Override public void onError(Throwable t) { exceptionHolder.set(t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }; grpcService.getNamespace( GetNamespaceRequest.newBuilder() .setName(nsName) .build(), streamObserver); latch.await(); assertNull(exceptionHolder.get()); assertNotNull(resultHolder.get()); assertTrue(getResp == resultHolder.get()); verify(rangeService, times(1)).getNamespace(getReq); }
@Test public void testGetNamespaceFailure() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService); GetNamespaceResponse getResp = GetNamespaceResponse.newBuilder() .setCode(StatusCode.INTERNAL_SERVER_ERROR) .build(); GetNamespaceRequest getReq = createGetNamespaceRequest(nsName); when(rangeService.getNamespace(getReq)).thenReturn( FutureUtils.exception(CAUSE)); AtomicReference<GetNamespaceResponse> resultHolder = new AtomicReference<>(); AtomicReference<Throwable> exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); StreamObserver<GetNamespaceResponse> streamObserver = new StreamObserver<GetNamespaceResponse>() { @Override public void onNext(GetNamespaceResponse resp) { resultHolder.set(resp); } @Override public void onError(Throwable t) { exceptionHolder.set(t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }; grpcService.getNamespace( GetNamespaceRequest.newBuilder() .setName(nsName) .build(), streamObserver); latch.await(); assertNull(exceptionHolder.get()); assertNotNull(resultHolder.get()); assertEquals(getResp, resultHolder.get()); verify(rangeService, times(1)).getNamespace(getReq); }