/*-
 * -\-\-
 * async-google-pubsub-client
 * --
 * Copyright (C) 2016 - 2017 Spotify AB
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -/-/-
 */

package com.spotify.google.cloud.pubsub.client;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class PubsubFutureTest {

  @Rule public ExpectedException exception = ExpectedException.none();

  @Mock RequestInfo requestInfo;

  @Test
  public void testToCompletableFutureErrorPropagation() throws Exception {
    final PubsubFuture<Void> f1 = new PubsubFuture<>(requestInfo);
    final PubsubFuture<Void> f2 = f1.toCompletableFuture();

    final Exception cause = new Exception();

    f1.fail(cause);

    assertThat(f2.isDone(), is(true));
    assertThat(f2.isCompletedExceptionally(), is(true));

    exception.expectCause(is(cause));

    f2.get();
  }

  @Test
  public void testToCompletableFutureResultPropagation() throws Exception {
    final PubsubFuture<String> f1 = new PubsubFuture<>(requestInfo);
    final PubsubFuture<String> f2 = f1.toCompletableFuture();

    final String value = "foo";

    f1.succeed(value);

    assertThat(f2.get(), is(value));
  }
}

@@ -746,17 +746,25 @@
 tchers.i
-s
+nstanceOf
 ;%0Aimport
@@ -779,31 +779,28 @@
 org.
-junit.Assert.assertThat
+hamcrest.Matchers.is
 ;%0Aim
@@ -832,42 +832,20 @@
 ert.
-fail;%0A%0Aimport java.io.IOException;
+assertThat;%0A
 %0Aimp
@@ -873,13 +873,14 @@
 ent.
-Execu
+Comple
 tion
@@ -922,92 +922,30 @@
 ent.
-TimeUnit;%0Aimport java.util.concurrent.TimeoutException;%0Aimport org.hamcrest.Matchers
+atomic.AtomicReference
 ;%0Aim
@@ -2056,11 +2056,738 @@
 e));%0A  %7D
+%0A%0A  @Test%0A  public void testCompletionExceptionPropagation() throws Exception %7B%0A    final PubsubFuture%3CString%3E f1 = new PubsubFuture%3C%3E(requestInfo);%0A    final PubsubFuture%3CString%3E f2 = f1.thenApply(r -%3E r);%0A%0A    final AtomicReference%3CThrowable%3E whenCompleteThrowable = new AtomicReference%3C%3E();%0A    f2.whenComplete((v, t) -%3E whenCompleteThrowable.set(t));%0A%0A    final Exception cause = new Exception();%0A    f1.fail(cause);%0A%0A    assertThat(f2.isDone(), is(true));%0A    assertThat(f2.isCompletedExceptionally(), is(true));%0A%0A    assertThat(whenCompleteThrowable.get(), instanceOf(CompletionException.class));%0A    assertThat(whenCompleteThrowable.get().getCause(), is(cause));%0A%0A    exception.expectCause(is(cause));%0A%0A    f2.get();%0A  %7D
 %0A%7D%0A

add another failing test