package fault.java.circuit;

import fault.java.metrics.IActionMetrics;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

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

    @Mock
    private IActionMetrics actionMetrics;

    private ICircuitBreaker defaultCircuitBreaker;

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

    @Test
    public void testCircuitIsClosedByDefault() {
        BreakerConfig breakerConfig = new BreakerConfig.BreakerConfigBuilder().failureThreshold(20)
                .timePeriodInMillis(5000).build();
        defaultCircuitBreaker = new DefaultCircuitBreaker(actionMetrics, breakerConfig);
        assertFalse(defaultCircuitBreaker.isOpen());
    }

    @Test
    public void testCircuitOpensWhenFailuresGreaterThanThreshold() {
        int timePeriodInMillis = 1000;
        BreakerConfig breakerConfig = new BreakerConfig.BreakerConfigBuilder().failureThreshold(5).timePeriodInMillis
                (timePeriodInMillis).build();
        defaultCircuitBreaker = new DefaultCircuitBreaker(actionMetrics, breakerConfig);

        assertFalse(defaultCircuitBreaker.isOpen());

        when(actionMetrics.getFailuresForTimePeriod(timePeriodInMillis)).thenReturn(6);
        defaultCircuitBreaker.informBreakerOfResult(false);

        assertTrue(defaultCircuitBreaker.isOpen());
    }

    @Test
    public void testOpenCircuitClosesAfterSuccess() {
        int timePeriodInMillis = 1000;
        BreakerConfig breakerConfig = new BreakerConfig.BreakerConfigBuilder().failureThreshold(5).timePeriodInMillis
                (timePeriodInMillis).build();
        defaultCircuitBreaker = new DefaultCircuitBreaker(actionMetrics, breakerConfig);

        assertFalse(defaultCircuitBreaker.isOpen());

        when(actionMetrics.getFailuresForTimePeriod(timePeriodInMillis)).thenReturn(6);
        defaultCircuitBreaker.informBreakerOfResult(false);

        assertTrue(defaultCircuitBreaker.isOpen());

        defaultCircuitBreaker.informBreakerOfResult(true);

        assertFalse(defaultCircuitBreaker.isOpen());
    }

}

@@ -264,16 +264,99 @@
 rtTrue;%0A
+import static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 import s
@@ -556,24 +556,17 @@
 Breaker 
-defaultC
+c
 ircuitBr
@@ -876,32 +876,25 @@
 ();%0A        
-defaultC
+c
 ircuitBreake
@@ -970,32 +970,25 @@
 assertFalse(
-defaultC
+c
 ircuitBreake
@@ -1293,32 +1293,25 @@
 ();%0A        
-defaultC
+c
 ircuitBreake
@@ -1388,32 +1388,25 @@
 assertFalse(
-defaultC
+c
 ircuitBreake
@@ -1511,32 +1511,25 @@
 6);%0A        
-defaultC
+c
 ircuitBreake
@@ -1576,32 +1576,25 @@
  assertTrue(
-defaultC
+c
 ircuitBreake
@@ -1884,32 +1884,25 @@
 ();%0A        
-defaultC
+c
 ircuitBreake
@@ -1979,32 +1979,25 @@
 assertFalse(
-defaultC
+c
 ircuitBreake
@@ -2102,32 +2102,25 @@
 6);%0A        
-defaultC
+c
 ircuitBreake
@@ -2175,102 +2175,643 @@
 rue(
-defaultCircuitBreaker.isOpen());%0A%0A        defaultCircuitBreaker.informBreakerOfResult(true
+circuitBreaker.isOpen());%0A%0A        circuitBreaker.informBreakerOfResult(true);%0A%0A        assertFalse(circuitBreaker.isOpen());%0A    %7D%0A%0A    @Test%0A    public void testSettingBreakerConfigChangesConfig() %7B%0A        BreakerConfig breakerConfig = new BreakerConfig.BreakerConfigBuilder().failureThreshold(10).timePeriodInMillis%0A                (1000).build();%0A        circuitBreaker = new DefaultCircuitBreaker(actionMetrics, breakerConfig);%0A        when(actionMetrics.getFailuresForTimePeriod(1000)).thenReturn(6);%0A%0A        circuitBreaker.informBreakerOfResult(false);%0A%0A        verify(actionMetrics, times(1)).getFailuresForTimePeriod(1000
 );%0A
-%0A
     
@@ -2830,16 +2830,467 @@
 lse(
-defaultC
+circuitBreaker.isOpen());%0A%0A        BreakerConfig newBreakerConfig = new BreakerConfig.BreakerConfigBuilder().failureThreshold(5)%0A                .timePeriodInMillis(2000).build();%0A        circuitBreaker.setBreakerConfig(newBreakerConfig);%0A        when(actionMetrics.getFailuresForTimePeriod(2000)).thenReturn(6);%0A        circuitBreaker.informBreakerOfResult(false);%0A%0A        verify(actionMetrics, times(1)).getFailuresForTimePeriod(2000);%0A        assertTrue(c
 ircu

Add a test for swapping circuitbreaker configs