package fault;

import fault.utils.ResilientPatternAction;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by timbrooks on 6/11/15.
 */
public class LoadBalancerTest {

    @Mock
    private ServiceExecutor executor1;
    @Mock
    private ServiceExecutor executor2;
    @Mock
    private LoadBalancerStrategy strategy;
    @Mock
    private ResilientPatternAction<String, Map<String, Object>> action;
    @Captor
    private ArgumentCaptor<ResilientAction<String>> actionCaptor;

    private Map<String, Object> context1;
    private Map<String, Object> context2;
    private LoadBalancer<Map<String, Object>> balancer;


    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        context1 = new HashMap<>();
        context1.put("port", 8000);
        context2 = new HashMap<>();
        context2.put("host", 8001);
        Map<ServiceExecutor, Map<String, Object>> map = new LinkedHashMap<>();
        map.put(executor1, context1);
        map.put(executor2, context2);

        balancer = new LoadBalancer<>(strategy, map);
    }

    @Test
    public void actionCalledWithCorrectContext() throws Exception {
        when(strategy.nextExectutorIndex()).thenReturn(0);
        balancer.performAction(action);
        verify(executor1).performAction(actionCaptor.capture());
        actionCaptor.getValue().run();
        verify(action).run(context1);

        when(strategy.nextExectutorIndex()).thenReturn(1);
        balancer.performAction(action);
        verify(executor2).performAction(actionCaptor.capture());
        actionCaptor.getValue().run();
        verify(action).run(context2);
    }

}

@@ -9,16 +9,58 @@
 fault;%0A%0A
+import fault.concurrent.ResilientPromise;%0A
 import f
@@ -352,34 +352,8 @@
 Map;
-%0Aimport java.util.TreeMap;
 %0A%0Aim
@@ -381,21 +381,18 @@
 to.M
-ockito.verify
+atchers.eq
 ;%0Aim
@@ -423,20 +423,17 @@
 Mockito.
-when
+*
 ;%0A%0A/**%0A 
@@ -471,16 +471,47 @@
 15.%0A */%0A
+@SuppressWarnings(%22unchecked%22)%0A
 public c
@@ -1459,17 +1459,24 @@
 ic void 
-a
+performA
 ctionCal
@@ -2008,10 +2008,897 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void submitActionCalledWithCorrectArguments() throws Exception %7B%0A        long timeout = 100L;%0A        ResilientPromise%3CString%3E promise = mock(ResilientPromise.class);%0A        ResilientCallback%3CString%3E callback = mock(ResilientCallback.class);%0A%0A        when(strategy.nextExectutorIndex()).thenReturn(0);%0A        balancer.submitAction(action, promise, callback, timeout);%0A        verify(executor1).submitAction(actionCaptor.capture(), eq(promise), eq(callback), eq(timeout));%0A        actionCaptor.getValue().run();%0A        verify(action).run(context1);%0A%0A        when(strategy.nextExectutorIndex()).thenReturn(1);%0A        balancer.submitAction(action, promise, callback, timeout);%0A        verify(executor2).submitAction(actionCaptor.capture(), eq(promise), eq(callback), eq(timeout));%0A        actionCaptor.getValue().run();%0A        verify(action).run(context2);%0A    %7D%0A%0A
 %7D%0A

Add tests for correct arguments