@Override
    protected CompletableFuture<JobExecutionResultResponseBody> handleRequest(
            @Nonnull final HandlerRequest<EmptyRequestBody> request,
            @Nonnull final RestfulGateway gateway)
            throws RestHandlerException {

        final JobID jobId = request.getPathParameter(JobIDPathParameter.class);

        final CompletableFuture<JobStatus> jobStatusFuture =
                gateway.requestJobStatus(jobId, timeout);

        return jobStatusFuture
                .thenCompose(
                        jobStatus -> {
                            if (jobStatus.isGloballyTerminalState()) {
                                return gateway.requestJobResult(jobId, timeout)
                                        .thenApply(JobExecutionResultResponseBody::created);
                            } else {
                                return CompletableFuture.completedFuture(
                                        JobExecutionResultResponseBody.inProgress());
                            }
                        })
                .exceptionally(
                        throwable -> {
                            throw propagateException(throwable);
                        });
    }
@Test
    void testResultInProgress() throws Exception {
        final TestingRestfulGateway testingRestfulGateway =
                new TestingRestfulGateway.Builder()
                        .setRequestJobStatusFunction(
                                jobId -> CompletableFuture.completedFuture(JobStatus.RUNNING))
                        .build();

        final JobExecutionResultResponseBody responseBody =
                jobExecutionResultHandler.handleRequest(testRequest, testingRestfulGateway).get();

        assertThat(responseBody.getStatus().getId()).isEqualTo(QueueStatus.Id.IN_PROGRESS);
    }