FutureTaskResultToDomainObjectPromiseWrapper implements Promise<DomainObject> { @Override public DomainObject get() throws PromiseBrokenException, InterruptedException { try { ServiceResponse<DomainObject> serviceResponse = new ServiceResponse<>(); for (Future<TaskResult> futureResult : futureList) { TaskResult result = futureResult.get(); if (result == null) { throw new PromiseBrokenException("Task result is null"); } ServiceResponse<DomainObject> response = (ServiceResponse<DomainObject>) result.getData(); if (!response.getIsSuccess()) throw response.getException(); serviceResponse.addData(response.getDataList()); } if (responseMerger != null) { return responseMerger.mergeResponse(serviceResponse.getDataList()); } else { return serviceResponse.getDataList().get(0); } } catch (ExecutionException exception) { checkAndThrowServiceClientException(exception); checkAndThrowOriginalException(exception); throw new InterruptedException(exception.getMessage()); } catch (CancellationException exception) { throw new PromiseBrokenException(exception); } } FutureTaskResultToDomainObjectPromiseWrapper(Future<TaskResult> future);  FutureTaskResultToDomainObjectPromiseWrapper(Future<TaskResult> future, boolean throwOriginalException);  FutureTaskResultToDomainObjectPromiseWrapper(ResponseMerger<DomainObject> responseMerger);  FutureTaskResultToDomainObjectPromiseWrapper(ResponseMerger<DomainObject> responseMerger, boolean throwOriginalException); @Override boolean isRealized(); @Override boolean isFullfilled(); @Override boolean isBroken(); @Override void await(); @Override void await(long timeout, TimeUnit timeUnit); @Override DomainObject get(); @Override DomainObject get(long timeout, TimeUnit timeUnit); Map<String, String> getHeaders(); @Override synchronized void addListener(PromiseListener promiseListener); void addFutureForTask(Future<TaskResult> future); void addFutureForTask(List<Future<TaskResult>> future); List<Future<TaskResult>> getFutureList();  }
@Test public void testGetNullTaskResult() throws Exception { when(future1.get()).thenReturn(null); exception.expectMessage(equalTo("Task result is null")); exception.expect(PromiseBrokenException.class); wrapper.get(); }
@Test public void testGetFutureCancellationException() throws Exception { when(future1.get()).thenThrow(CancellationException.class); exception.expect(PromiseBrokenException.class); wrapper.get(); }
@Test public void testGetFutureExecutionException() throws Exception { when(future1.get()).thenThrow(ExecutionException.class); exception.expect(InterruptedException.class); wrapper.get(); }
@Test(expected = NullPointerException.class) public void testGetFutureExecutionExceptionWithOriginals() throws Exception { wrapper = spy(new FutureTaskResultToDomainObjectPromiseWrapper(future1, true)); ExecutionException executionException = new ExecutionException(new NullPointerException()); when(future1.get()).thenThrow(executionException); try { wrapper.get(); } catch (PromiseBrokenException e) { Throwable rootCause = ExceptionUtils.getRootCause(e); assertEquals(NullPointerException.class.getName(), rootCause.getClass().getName()); throw (Exception) rootCause; } fail(); }
@Test public void testGetNullTaskResultWithTimeout() throws Exception { when(future1.get(timeout, timeUnit)).thenReturn(null); exception.expectMessage(equalTo("Task result is null")); exception.expect(PromiseBrokenException.class); wrapper.get(timeout, timeUnit); }
@Test public void testGetFutureCancellationExceptionWithTimeout() throws Exception { when(future1.get(timeout, timeUnit)).thenThrow(CancellationException.class); exception.expect(PromiseBrokenException.class); wrapper.get(timeout, timeUnit); }
@Test public void testGetFutureExecutionExceptionWithTimeout() throws Exception { when(future1.get(timeout, timeUnit)).thenThrow(ExecutionException.class); exception.expect(InterruptedException.class); wrapper.get(timeout, timeUnit); }
@Test(expected = NullPointerException.class) public void testGetFutureExecutionExceptionWithOriginalsWithTimeout() throws Exception { wrapper = spy(new FutureTaskResultToDomainObjectPromiseWrapper(future1, true)); ExecutionException executionException = new ExecutionException(new NullPointerException()); when(future1.get(timeout, timeUnit)).thenThrow(executionException); try { wrapper.get(); } catch (PromiseBrokenException e) { Throwable rootCause = ExceptionUtils.getRootCause(e); assertEquals(NullPointerException.class.getName(), rootCause.getClass().getName()); throw (Exception) rootCause; } fail(); }