1 package net.secodo.jcircuitbreaker.breakstrategy;
2
3 import net.secodo.jcircuitbreaker.task.Task;
4 import org.junit.Test;
5 import net.secodo.jcircuitbreaker.breaker.execution.ExecutionContext;
6 import static org.hamcrest.CoreMatchers.equalTo;
7 import static org.junit.Assert.assertThat;
8 import static org.mockito.Matchers.any;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.when;
11 import static net.secodo.jcircuitbreaker.breakstrategy.SimpleStrategyDsl.allOf;
12 import static net.secodo.jcircuitbreaker.breakstrategy.SimpleStrategyDsl.anyOf;
13
14
15 @SuppressWarnings("unchecked")
16 public class SimpleStrategyDslTest {
17 @Test
18 public void shouldBreakCorrectlyWhenUsingDsl() {
19
20 BreakStrategy strategy1 = mock(BreakStrategy.class);
21 BreakStrategy strategy2 = mock(BreakStrategy.class);
22 BreakStrategy strategy3 = mock(BreakStrategy.class);
23 BreakStrategy strategy4 = mock(BreakStrategy.class);
24
25
26 final BreakStrategy resultingStrategy = anyOf(strategy1, strategy2, allOf(strategy3, strategy4));
27
28
29
30 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
31 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
32 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
33 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
34
35 boolean resultingValue = resultingStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class));
36
37 assertThat(resultingValue, equalTo(true));
38
39
40
41 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
42 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
43 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
44 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
45
46 resultingValue = resultingStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class));
47
48 assertThat(resultingValue, equalTo(false));
49
50
51
52 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
53 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
54 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
55 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
56
57 resultingValue = resultingStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class));
58
59 assertThat(resultingValue, equalTo(false));
60
61
62
63
64 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
65 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
66 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
67 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
68
69 resultingValue = resultingStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class));
70
71 assertThat(resultingValue, equalTo(false));
72
73
74
75 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
76 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
77 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
78 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
79
80 resultingValue = resultingStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class));
81
82 assertThat(resultingValue, equalTo(true));
83
84
85
86 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
87 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
88 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
89 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
90
91 resultingValue = resultingStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class));
92
93 assertThat(resultingValue, equalTo(true));
94
95
96
97 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
98 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
99 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
100 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
101
102 resultingValue = resultingStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class));
103
104 assertThat(resultingValue, equalTo(true));
105
106
107 }
108
109 @Test
110 public void shouldWorkCorrectlyWithNewDslDefinedAsInstance() throws Exception {
111
112 class MyTestDsl extends SimpleStrategyDsl {
113 public BreakStrategy firstOf(BreakStrategy... strategies) {
114 if (strategies.length == 0) {
115 throw new IllegalArgumentException("Should be at least one strategy method defined");
116 }
117
118 return strategies[0];
119 }
120
121 }
122
123 BreakStrategy strategy1 = mock(BreakStrategy.class);
124 BreakStrategy strategy2 = mock(BreakStrategy.class);
125 BreakStrategy strategy3 = mock(BreakStrategy.class);
126 BreakStrategy strategy4 = mock(BreakStrategy.class);
127
128 when(strategy1.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(false);
129 when(strategy2.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
130 when(strategy3.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
131 when(strategy4.shouldBreak(any(Task.class), any(ExecutionContext.class))).thenReturn(true);
132
133
134 final BreakStrategy breakStrategy = new MyTestDsl().firstOf(strategy1, strategy2, strategy3, strategy4);
135
136
137 assertThat(breakStrategy, equalTo(strategy1));
138 assertThat(breakStrategy.shouldBreak(mock(Task.class), mock(ExecutionContext.class)), equalTo(false));
139
140 }
141
142
143 }