// Path: src/main/java/com/spotify/trickle/GraphExceptionWrapper.java
// public static Throwable wrapException(Throwable t,
//                                       TraverseState.FutureCallInformation currentCall,
//                                       TraverseState traverseState) {
//   return new GraphExecutionException(t, asCallInfo(currentCall), callInfos(traverseState));
// }
// 
// Path: src/test/java/com/spotify/trickle/Util.java
// static Matcher<Throwable> hasAncestor(final Throwable expected) {
//   return new TypeSafeMatcher<Throwable>() {
//     @Override
//     protected boolean matchesSafely(Throwable item) {
//       for (Throwable cause = item ; cause != null ; cause = cause.getCause()) {
//         if (cause.equals(expected)) {
//           return true;
//         }
//       }
// 
//       return false;
//     }
// 
//     @Override
//     public void describeTo(Description description) {
//       description.appendText("with parent cause " + expected);
//     }
//   };
// }


import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static com.spotify.trickle.GraphExceptionWrapper.wrapException;
import static com.spotify.trickle.Util.hasAncestor;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/*
 * Copyright 2013-2014 Spotify AB. All rights reserved.
 *
 * The contents of this file are 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.trickle;






@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public class GraphExceptionWrapperTest {

  private static final List<NodeInfo> NO_ARGS = Collections.emptyList();
  private static final List<ListenableFuture<?>> NO_VALUES = emptyList();

  Throwable t;
  TraverseState traverseState;
  TraverseState.FutureCallInformation currentCall;

  NodeInfo currentNodeInfo;
  List<ListenableFuture<?>> currentNodeValues;

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

  @Before
  public void setUp() throws Exception {
    t = new RuntimeException("the original problem");

    Map<Input<?>, Object> emptyMap = Collections.emptyMap();
    traverseState = new TraverseState(emptyMap, MoreExecutors.sameThreadExecutor(), true);

    List<? extends NodeInfo> currentNodeParameters = ImmutableList.of(
        new FakeNodeInfo("arg1", Collections .<NodeInfo>emptyList()),
        new FakeNodeInfo("argument 2", Collections .<NodeInfo>emptyList())
    );

    currentNodeInfo = new FakeNodeInfo("the node", currentNodeParameters);
    currentNodeValues = ImmutableList.<ListenableFuture<?>>of(
        immediateFuture("value 1"),
        immediateFuture("andra värdet")
    );
    currentCall = new TraverseState.FutureCallInformation(currentNodeInfo, currentNodeValues);
  }

  @Test
  public void shouldHaveOriginalExceptionAsCause() throws Exception {
// Path: src/main/java/com/spotify/trickle/GraphExceptionWrapper.java
// public static Throwable wrapException(Throwable t,
//                                       TraverseState.FutureCallInformation currentCall,
//                                       TraverseState traverseState) {
//   return new GraphExecutionException(t, asCallInfo(currentCall), callInfos(traverseState));
// }
// 
// Path: src/test/java/com/spotify/trickle/Util.java
// static Matcher<Throwable> hasAncestor(final Throwable expected) {
//   return new TypeSafeMatcher<Throwable>() {
//     @Override
//     protected boolean matchesSafely(Throwable item) {
//       for (Throwable cause = item ; cause != null ; cause = cause.getCause()) {
//         if (cause.equals(expected)) {
//           return true;
//         }
//       }
// 
//       return false;
//     }
// 
//     @Override
//     public void describeTo(Description description) {
//       description.appendText("with parent cause " + expected);
//     }
//   };
// }


// Path: src/test/java/com/spotify/trickle/GraphExceptionWrapperTest.java
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static com.spotify.trickle.GraphExceptionWrapper.wrapException;
import static com.spotify.trickle.Util.hasAncestor;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/*
 * Copyright 2013-2014 Spotify AB. All rights reserved.
 *
 * The contents of this file are 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.trickle;






@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public class GraphExceptionWrapperTest {

  private static final List<NodeInfo> NO_ARGS = Collections.emptyList();
  private static final List<ListenableFuture<?>> NO_VALUES = emptyList();

  Throwable t;
  TraverseState traverseState;
  TraverseState.FutureCallInformation currentCall;

  NodeInfo currentNodeInfo;
  List<ListenableFuture<?>> currentNodeValues;

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

  @Before
  public void setUp() throws Exception {
    t = new RuntimeException("the original problem");

    Map<Input<?>, Object> emptyMap = Collections.emptyMap();
    traverseState = new TraverseState(emptyMap, MoreExecutors.sameThreadExecutor(), true);

    List<? extends NodeInfo> currentNodeParameters = ImmutableList.of(
        new FakeNodeInfo("arg1", Collections .<NodeInfo>emptyList()),
        new FakeNodeInfo("argument 2", Collections .<NodeInfo>emptyList())
    );

    currentNodeInfo = new FakeNodeInfo("the node", currentNodeParameters);
    currentNodeValues = ImmutableList.<ListenableFuture<?>>of(
        immediateFuture("value 1"),
        immediateFuture("andra värdet")
    );
    currentCall = new TraverseState.FutureCallInformation(currentNodeInfo, currentNodeValues);
  }

  @Test
  public void shouldHaveOriginalExceptionAsCause() throws Exception {
    assertThat(wrapException(t, currentCall, traverseState).getCause(), equalTo(t));