BlobServerPortHandler extends AbstractRestHandler<DispatcherGateway, EmptyRequestBody, BlobServerPortResponseBody, EmptyMessageParameters> { @Override protected CompletableFuture<BlobServerPortResponseBody> handleRequest(@Nonnull HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull DispatcherGateway gateway) throws RestHandlerException { return gateway .getBlobServerPort(timeout) .thenApply(BlobServerPortResponseBody::new) .exceptionally(error -> { throw new CompletionException(new RestHandlerException( "Failed to retrieve blob server port.", HttpResponseStatus.INTERNAL_SERVER_ERROR, ExceptionUtils.stripCompletionException(error))); }); } BlobServerPortHandler(CompletableFuture<String> localRestAddress, GatewayRetriever<DispatcherGateway> leaderRetriever, Time timeout);   }
@Test public void testPortRetrieval() throws Exception { DispatcherGateway mockGateway = mock(DispatcherGateway.class); when(mockGateway.getBlobServerPort(any(Time.class))) .thenReturn(CompletableFuture.completedFuture(PORT)); GatewayRetriever<DispatcherGateway> mockGatewayRetriever = mock(GatewayRetriever.class); BlobServerPortHandler handler = new BlobServerPortHandler( CompletableFuture.completedFuture("http: mockGatewayRetriever, RpcUtils.INF_TIMEOUT); BlobServerPortResponseBody portResponse = handler .handleRequest(new HandlerRequest<>(EmptyRequestBody.getInstance(), EmptyMessageParameters.getInstance()), mockGateway) .get(); Assert.assertEquals(PORT, portResponse.port); }
@Test public void testPortRetrievalFailureHandling() throws Exception { DispatcherGateway mockGateway = mock(DispatcherGateway.class); when(mockGateway.getBlobServerPort(any(Time.class))) .thenReturn(FutureUtils.completedExceptionally(new TestException())); GatewayRetriever<DispatcherGateway> mockGatewayRetriever = mock(GatewayRetriever.class); BlobServerPortHandler handler = new BlobServerPortHandler( CompletableFuture.completedFuture("http: mockGatewayRetriever, RpcUtils.INF_TIMEOUT); try { handler .handleRequest(new HandlerRequest<>(EmptyRequestBody.getInstance(), EmptyMessageParameters.getInstance()), mockGateway) .get(); Assert.fail(); } catch (ExecutionException ee) { RestHandlerException rhe = (RestHandlerException) ee.getCause(); Assert.assertEquals(TestException.class, rhe.getCause().getClass()); Assert.assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, rhe.getHttpResponseStatus()); } }