package fault.java;

import fault.java.circuit.ICircuitBreaker;
import fault.java.messages.ScheduleMessage;
import fault.java.metrics.IActionMetrics;
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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


/**
 * Created by timbrooks on 11/20/14.
 */
public class ServiceExecutorTest {

    @Mock
    private ICircuitBreaker circuitBreaker;
    @Mock
    private IActionMetrics actionMetrics;
    @Mock
    private ResilientAction<Object> resilientAction;
    @Mock
    private ManagingRunnable managingRunnable;
    @Captor
    private ArgumentCaptor<ScheduleMessage<Object>> messageCaptor;

    private ServiceExecutor serviceExecutor;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        doNothing().when(managingRunnable).run();
        serviceExecutor = new ServiceExecutor(actionMetrics, circuitBreaker, managingRunnable);
    }

    @Test
    public void testExceptionThrownIfCircuitIsOpen() {
        when(circuitBreaker.isOpen()).thenReturn(true);
        try {
            serviceExecutor.performAction(resilientAction, 1000);
        } catch (RuntimeException e) {
            assertEquals(e.getMessage(), "Circuit is Open");
            return;
        }
        fail();
    }

    @Test
    public void testScheduleActionIsSubmittedIfCircuitIsClosed() {
        when(circuitBreaker.isOpen()).thenReturn(false);
        ResilientPromise<Object> resilientPromise = serviceExecutor.performAction(resilientAction, 1000);

        verify(managingRunnable).submit(messageCaptor.capture());

        ScheduleMessage scheduleMessage = messageCaptor.getValue();
        assertEquals(resilientAction, scheduleMessage.action);
        assertEquals(resilientPromise, scheduleMessage.promise);
    }
}

@@ -2060,11 +2060,166 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testShutdownCallsShutdownOnRunnable() %7B%0A        serviceExecutor.shutdown();%0A%0A        verify(managingRunnable).shutdown();%0A    %7D
 %0A%7D%0A

Add test for shutdown