### Question:
GrpcRootRangeService extends RootRangeServiceImplBase { @Override public void createNamespace(CreateNamespaceRequest request, StreamObserver<CreateNamespaceResponse> responseObserver) { rs.createNamespace(request).whenComplete( new ResponseHandler<CreateNamespaceResponse>(responseObserver) { @Override protected CreateNamespaceResponse createErrorResp(Throwable cause) { return CreateNamespaceResponse.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 testCreateNamespaceSuccess() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService); CreateNamespaceResponse createResp = CreateNamespaceResponse.newBuilder() .setCode(StatusCode.SUCCESS) .setNsProps(namespaceProps) .build(); CreateNamespaceRequest createReq = createCreateNamespaceRequest(nsName, namespaceConf); when(rangeService.createNamespace(createReq)).thenReturn( CompletableFuture.completedFuture(createResp)); AtomicReference<CreateNamespaceResponse> resultHolder = new AtomicReference<>(); AtomicReference<Throwable> exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); StreamObserver<CreateNamespaceResponse> streamObserver = new StreamObserver<CreateNamespaceResponse>() { @Override public void onNext(CreateNamespaceResponse resp) { resultHolder.set(resp); } @Override public void onError(Throwable t) { exceptionHolder.set(t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }; grpcService.createNamespace( CreateNamespaceRequest.newBuilder() .setName(nsName) .setNsConf(namespaceConf) .build(), streamObserver); latch.await(); assertNull(exceptionHolder.get()); assertNotNull(resultHolder.get()); assertTrue(createResp == resultHolder.get()); verify(rangeService, times(1)).createNamespace(createReq); }
@Test public void testCreateNamespaceFailure() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService); CreateNamespaceResponse createResp = CreateNamespaceResponse.newBuilder() .setCode(StatusCode.INTERNAL_SERVER_ERROR) .build(); CreateNamespaceRequest createReq = createCreateNamespaceRequest(nsName, namespaceConf); when(rangeService.createNamespace(createReq)).thenReturn( FutureUtils.exception(CAUSE)); AtomicReference<CreateNamespaceResponse> resultHolder = new AtomicReference<>(); AtomicReference<Throwable> exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); StreamObserver<CreateNamespaceResponse> streamObserver = new StreamObserver<CreateNamespaceResponse>() { @Override public void onNext(CreateNamespaceResponse resp) { resultHolder.set(resp); } @Override public void onError(Throwable t) { exceptionHolder.set(t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }; grpcService.createNamespace( CreateNamespaceRequest.newBuilder() .setName(nsName) .setNsConf(namespaceConf) .build(), streamObserver); latch.await(); assertNull(exceptionHolder.get()); assertNotNull(resultHolder.get()); assertEquals(createResp, resultHolder.get()); verify(rangeService, times(1)).createNamespace(createReq); }