/*-
 * -\-\-
 * Flo Workflow Definition
 * --
 * Copyright (C) 2016 - 2018 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.flo.context;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.spotify.flo.EvalContext;
import com.spotify.flo.Task;
import com.spotify.flo.TaskOperator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class EvalContextTest {

  @Mock private TaskOperator.Listener listener;
  @Mock private TaskOperator<String, String, String> operator;
  private Task<String> task;

  @Captor
  private ArgumentCaptor<TaskOperator.Listener> opListenerCaptor;

  @Before
  public void setup() {
    task = Task.named("test")
        .ofType(String.class)
        .operator(operator)
        .process((oprtr) -> oprtr + " result");
  }

  @Test
  public void listenerIsCalled() {
    final EvalContext baseContext = EvalContext.sync();
    final EvalContext targetContext = spy(EvalContext.sync());
    when(targetContext.listener()).thenReturn(listener);

    baseContext.evaluateInternal(task, targetContext).get();

    verify(targetContext, times(1)).listener();
    verify(operator, times(1)).perform(anyObject(), opListenerCaptor.capture());
    assertEquals(opListenerCaptor.getValue(), listener);
  }
}

@@ -695,53 +695,8 @@
 t;%0A%0A
-import static org.junit.Assert.assertEquals;%0A
 impo
@@ -768,59 +768,20 @@
 to.M
-ockito.spy;%0Aimport static org.mockito.Mockito.ti
+atchers.sa
 me
-s
 ;%0Aim
@@ -823,48 +823,8 @@
 ify;
-%0Aimport static org.mockito.Mockito.when;
 %0A%0Aim
@@ -1008,70 +1008,8 @@
 th;%0A
-import org.mockito.ArgumentCaptor;%0Aimport org.mockito.Captor;%0A
 impo
@@ -1289,85 +1289,8 @@
 k;%0A%0A
-  @Captor%0A  private ArgumentCaptor%3CTaskOperator.Listener%3E opListenerCaptor;%0A%0A
   @B
@@ -1428,36 +1428,14 @@
 ess(
-(oprtr) -%3E oprtr + %22 result%22
+x -%3E x
 );%0A 
@@ -1579,87 +1579,143 @@
 t = 
-spy(EvalContext.sync());%0A    when(targetContext.listener()).thenR
+new ForwardingEvalContext(EvalContext.sync()) %7B%0A      @Override%0A      public TaskOperator.Listener listener() %7B%0A        r
 eturn
-(
+ 
 listener
 );%0A%0A
@@ -1710,17 +1710,31 @@
 listener
-)
+;%0A      %7D%0A    %7D
 ;%0A%0A    b
@@ -1792,57 +1792,8 @@
 t();
-%0A%0A    verify(targetContext, times(1)).listener();
 %0A   
@@ -1812,18 +1812,8 @@
 ator
-, times(1)
 ).pe
@@ -1835,91 +1835,22 @@
 (), 
-opListenerCaptor.capture());%0A    assertEquals(opListenerCaptor.getValue(), listener
+same(listener)
 );%0A 

simplify and remove spy