OperationFutures { @SuppressWarnings("unchecked") public static final <ResponseT, MetadataT> OperationFuture<ResponseT, MetadataT> immediateOperationFuture( final OperationSnapshot completedSnapshot) { Preconditions.checkArgument( completedSnapshot.isDone(), "given snapshot must already be completed"); final ApiFuture<MetadataT> metadataFuture = ApiFutures.immediateFuture((MetadataT) completedSnapshot.getMetadata()); final ApiFuture<OperationSnapshot> initialFuture = ApiFutures.immediateFuture(completedSnapshot); return new OperationFuture<ResponseT, MetadataT>() { @Override public String getName() { return completedSnapshot.getName(); } @Override public ApiFuture<MetadataT> getMetadata() { return metadataFuture; } @Override public ApiFuture<MetadataT> peekMetadata() { return metadataFuture; } @Override public ApiFuture<OperationSnapshot> getInitialFuture() { return initialFuture; } @Override public RetryingFuture<OperationSnapshot> getPollingFuture() { throw new UnsupportedOperationException("Not implemented: getPollingFuture()."); } @Override public void addListener(Runnable runnable, Executor executor) { initialFuture.addListener(runnable, executor); } @Override public ResponseT get(long time, TimeUnit unit) throws ExecutionException { return get(); } @Override public ResponseT get() throws ExecutionException { if (completedSnapshot.getErrorCode().getCode().equals(StatusCode.Code.OK)) { return (ResponseT) completedSnapshot.getResponse(); } throw new ExecutionException( new ApiException(null, completedSnapshot.getErrorCode(), false)); } @Override public boolean isDone() { return true; } @Override public boolean isCancelled() { return false; } @Override public boolean cancel(boolean b) { return false; } }; } private  OperationFutures(); @SuppressWarnings("unchecked") static final OperationFuture<ResponseT, MetadataT> immediateOperationFuture(
          final OperationSnapshot completedSnapshot);  }
@Test(expected = IllegalArgumentException.class) public void testNotDone() { OperationFutures.immediateOperationFuture( FakeOperationSnapshot.newBuilder() .setName("required") .setDone(false) .setErrorCode(FakeStatusCode.of(StatusCode.Code.OK)) .build()); }
@Test public void testCompleted() throws Exception { OperationFuture<String, Integer> future = OperationFutures.<String, Integer>immediateOperationFuture( FakeOperationSnapshot.newBuilder() .setName("myName") .setDone(true) .setResponse("theResponse") .setMetadata(42) .setErrorCode(FakeStatusCode.of(StatusCode.Code.OK)) .build()); assertThat(future.getName()).isEqualTo("myName"); assertThat(future.get()).isEqualTo("theResponse"); assertThat(future.getMetadata().get()).isEqualTo(42); }
@Test public void testFailed() throws Exception { OperationFuture<String, Integer> future = OperationFutures.<String, Integer>immediateOperationFuture( FakeOperationSnapshot.newBuilder() .setName("myName") .setDone(true) .setMetadata(42) .setErrorCode(FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT)) .build()); assertThat(future.getName()).isEqualTo("myName"); assertThat(future.getMetadata().get()).isEqualTo(42); try { future.get(); fail(); } catch (ExecutionException e) { assertThat(e.getCause()).isInstanceOf(ApiException.class); } }