View Javadoc
1   package net.secodo.jcircuitbreaker.breakstrategy.impl.dsl;
2   
3   import net.secodo.jcircuitbreaker.breaker.execution.ExecutionContext;
4   import net.secodo.jcircuitbreaker.breakstrategy.BreakStrategy;
5   import net.secodo.jcircuitbreaker.task.Task;
6   import org.junit.Assert;
7   import org.junit.Test;
8   import static org.hamcrest.CoreMatchers.equalTo;
9   import static org.mockito.Matchers.any;
10  import static org.mockito.Mockito.mock;
11  import static org.mockito.Mockito.when;
12  
13  
14  @SuppressWarnings("unchecked")
15  public class DslAndStrategyTest {
16    @Test
17    public void shouldBreakIfBothStrategiesDecidesToBreak() throws Exception {
18      // given
19      BreakStrategy strategy1 = mock(BreakStrategy.class);
20      BreakStrategy strategy2 = mock(BreakStrategy.class);
21  
22      when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
23      when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
24  
25      // when
26      final boolean breakResult = new DslAndStrategy(strategy1, strategy2).shouldBreak(mock(Task.class),
27        mock(ExecutionContext.class));
28  
29      // then
30      Assert.assertThat(breakResult, equalTo(true));
31    }
32  
33    @Test
34    public void shouldNotBreakIfFirstStrategyDecidesToBreakAndSecondNot() throws Exception {
35      // given
36      BreakStrategy strategy1 = mock(BreakStrategy.class);
37      BreakStrategy strategy2 = mock(BreakStrategy.class);
38  
39      when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
40      when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
41  
42      // when
43      final boolean breakResult = new DslAndStrategy(strategy1, strategy2).shouldBreak(mock(Task.class),
44        mock(ExecutionContext.class));
45  
46      // then
47      Assert.assertThat(breakResult, equalTo(false));
48    }
49  
50    @Test
51    public void shouldNotBreakIfFirstStrategyDecidesNotToBreakAndSecondToBreak() throws Exception {
52      // given
53      BreakStrategy strategy1 = mock(BreakStrategy.class);
54      BreakStrategy strategy2 = mock(BreakStrategy.class);
55  
56      when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
57      when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
58  
59      // when
60      final boolean breakResult = new DslAndStrategy(strategy1, strategy2).shouldBreak(mock(Task.class),
61        mock(ExecutionContext.class));
62  
63      // then
64      Assert.assertThat(breakResult, equalTo(false));
65    }
66  
67    @Test
68    public void shouldNotBreakIfBothStrategiesDecidesNotToBreak() throws Exception {
69      // given
70      BreakStrategy strategy1 = mock(BreakStrategy.class);
71      BreakStrategy strategy2 = mock(BreakStrategy.class);
72  
73      when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
74      when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
75  
76      // when
77      final boolean breakResult = new DslAndStrategy(strategy1, strategy2).shouldBreak(mock(Task.class),
78        mock(ExecutionContext.class));
79  
80      // then
81      Assert.assertThat(breakResult, equalTo(false));
82    }
83  
84  
85  }