|
12 | 12 | import io.split.engine.experiments.SplitFetcher; |
13 | 13 | import io.split.engine.matchers.CombiningMatcher; |
14 | 14 | import io.split.engine.metrics.Metrics; |
| 15 | +import org.junit.Before; |
15 | 16 | import org.junit.Test; |
16 | 17 | import org.mockito.Mockito; |
17 | 18 |
|
|
23 | 24 | import static org.junit.Assert.assertEquals; |
24 | 25 |
|
25 | 26 | public class EvaluatorTest { |
| 27 | + private SplitFetcher _splitFetcher; |
| 28 | + private SDKReadinessGates _gates; |
| 29 | + private Evaluator _evaluator; |
| 30 | + private SplitClientImpl _splitClient; |
| 31 | + private CombiningMatcher _matcher; |
| 32 | + private Map<String, String> _configurations; |
| 33 | + private List<ParsedCondition> _conditions; |
| 34 | + private List<Partition> _partitions; |
| 35 | + |
| 36 | + private String _matchingKey = "test"; |
| 37 | + private String _bucketingKey = "test"; |
| 38 | + private String _splitName = "split_name_test"; |
| 39 | + private Long _changeNumber = 123123L; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void before() { |
| 43 | + _splitFetcher = Mockito.mock(SplitFetcher.class); |
| 44 | + _gates = Mockito.mock(SDKReadinessGates.class); |
| 45 | + _evaluator = new EvaluatorImp(_gates, _splitFetcher); |
| 46 | + _splitClient = getSplitClient(_splitFetcher, _gates, _evaluator); |
| 47 | + _matcher = Mockito.mock(CombiningMatcher.class); |
| 48 | + |
| 49 | + _configurations = new HashMap<>(); |
| 50 | + _conditions = new ArrayList<>(); |
| 51 | + _partitions = new ArrayList<>(); |
| 52 | + } |
| 53 | + |
26 | 54 | @Test |
27 | 55 | public void evaluateWhenSplitNameDoesNotExistReturnControl() throws ChangeNumberExceptionWrapper { |
28 | | - SplitFetcher splitFetcher = Mockito.mock(SplitFetcher.class); |
29 | | - SDKReadinessGates gates = Mockito.mock(SDKReadinessGates.class); |
30 | | - Evaluator evaluator = new EvaluatorImp(gates, splitFetcher); |
31 | | - |
32 | | - SplitClientImpl splitClient = getSplitClient(splitFetcher, gates, evaluator); |
| 56 | + Mockito.when(_splitFetcher.fetch(_splitName)).thenReturn(null); |
33 | 57 |
|
34 | | - String matchingKey = "test"; |
35 | | - String bucketingKey = "test"; |
36 | | - String split = "split_name_test"; |
37 | | - |
38 | | - Mockito.when(splitFetcher.fetch(split)).thenReturn(null); |
39 | | - |
40 | | - TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, split, null, splitClient); |
| 58 | + TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(_matchingKey, _bucketingKey, _splitName, null, _splitClient); |
41 | 59 |
|
42 | 60 | assertEquals("control", result.treatment); |
43 | 61 | assertEquals("definition not found", result.label); |
44 | 62 | } |
45 | 63 |
|
46 | 64 | @Test |
47 | 65 | public void evaluateWhenSplitIsKilledReturnDefaultTreatment() throws ChangeNumberExceptionWrapper { |
48 | | - SplitFetcher splitFetcher = Mockito.mock(SplitFetcher.class); |
49 | | - SDKReadinessGates gates = Mockito.mock(SDKReadinessGates.class); |
50 | | - Evaluator evaluator = new EvaluatorImp(gates, splitFetcher); |
51 | | - |
52 | | - SplitClientImpl splitClient = getSplitClient(splitFetcher, gates, evaluator); |
| 66 | + ParsedSplit split = ParsedSplit.createParsedSplitForTests(_splitName, 0, true, "defaultTreatment", _conditions, "tt", _changeNumber, 2); |
| 67 | + Mockito.when(_splitFetcher.fetch(_splitName)).thenReturn(split); |
53 | 68 |
|
54 | | - String matchingKey = "test"; |
55 | | - String bucketingKey = "test"; |
56 | | - String splitName = "split_name_test"; |
57 | | - Long changeNumber = 123123L; |
58 | | - List<ParsedCondition> conditions = new ArrayList<>(); |
59 | | - |
60 | | - ParsedSplit split = ParsedSplit.createParsedSplitForTests(splitName, 0, true, "defaultTreatment", conditions, "tt", changeNumber, 2); |
61 | | - |
62 | | - Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
63 | | - |
64 | | - TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 69 | + TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(_matchingKey, _bucketingKey, _splitName, null, _splitClient); |
65 | 70 |
|
66 | 71 | assertEquals("defaultTreatment", result.treatment); |
67 | 72 | assertEquals("killed", result.label); |
68 | | - assertEquals(changeNumber, result.changeNumber); |
| 73 | + assertEquals(_changeNumber, result.changeNumber); |
69 | 74 | } |
70 | 75 |
|
71 | 76 | @Test |
72 | 77 | public void evaluateWithoutConditionsReturnDefaultTreatment() throws ChangeNumberExceptionWrapper { |
73 | | - SplitFetcher splitFetcher = Mockito.mock(SplitFetcher.class); |
74 | | - SDKReadinessGates gates = Mockito.mock(SDKReadinessGates.class); |
75 | | - Evaluator evaluator = new EvaluatorImp(gates, splitFetcher); |
76 | | - |
77 | | - SplitClientImpl splitClient = getSplitClient(splitFetcher, gates, evaluator); |
78 | | - |
79 | | - String matchingKey = "test"; |
80 | | - String bucketingKey = "test"; |
81 | | - String splitName = "split_name_test"; |
82 | | - Long changeNumber = 123123L; |
83 | | - List<ParsedCondition> conditions = new ArrayList<>(); |
84 | | - |
85 | | - ParsedSplit split = ParsedSplit.createParsedSplitForTests(splitName, 0, false, "defaultTreatment", conditions, "tt", changeNumber, 2); |
| 78 | + ParsedSplit split = ParsedSplit.createParsedSplitForTests(_splitName, 0, false, "defaultTreatment", _conditions, "tt", _changeNumber, 2); |
| 79 | + Mockito.when(_splitFetcher.fetch(_splitName)).thenReturn(split); |
86 | 80 |
|
87 | | - Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
88 | | - |
89 | | - TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 81 | + TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(_matchingKey, _bucketingKey, _splitName, null, _splitClient); |
90 | 82 |
|
91 | 83 | assertEquals("defaultTreatment", result.treatment); |
92 | 84 | assertEquals("default rule", result.label); |
93 | | - assertEquals(changeNumber, result.changeNumber); |
| 85 | + assertEquals(_changeNumber, result.changeNumber); |
94 | 86 | } |
95 | 87 |
|
96 | 88 | @Test |
97 | 89 | public void evaluateWithRollOutConditionBucketIsBiggerTrafficAllocationReturnDefaultTreatment() throws ChangeNumberExceptionWrapper { |
98 | | - SplitFetcher splitFetcher = Mockito.mock(SplitFetcher.class); |
99 | | - SDKReadinessGates gates = Mockito.mock(SDKReadinessGates.class); |
100 | | - Evaluator evaluator = new EvaluatorImp(gates, splitFetcher); |
101 | | - |
102 | | - SplitClientImpl splitClient = getSplitClient(splitFetcher, gates, evaluator); |
103 | | - |
104 | | - String matchingKey = "test"; |
105 | | - String bucketingKey = "test"; |
106 | | - String splitName = "split_name_test"; |
107 | | - Long changeNumber = 123123L; |
108 | | - Map<String, String> configurations = new HashMap<>(); |
109 | | - List<ParsedCondition> conditions = new ArrayList<>(); |
110 | | - |
111 | | - CombiningMatcher matcher = Mockito.mock(CombiningMatcher.class); |
112 | | - |
113 | | - List<Partition> partitions = new ArrayList<>(); |
114 | 90 | Partition partition = new Partition(); |
115 | 91 | partition.treatment = "treatment_test"; |
116 | 92 | partition.size = 100; |
117 | | - partitions.add(partition); |
118 | | - ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, matcher, partitions, "test label"); |
119 | | - conditions.add(condition); |
| 93 | + _partitions.add(partition); |
| 94 | + ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, _matcher,_partitions, "test label"); |
| 95 | + _conditions.add(condition); |
120 | 96 |
|
121 | | - ParsedSplit split = new ParsedSplit(splitName, 0, false, "default_treatment", conditions, "tt", changeNumber, 10, 12, 2, configurations); |
| 97 | + ParsedSplit split = new ParsedSplit(_splitName, 0, false, "default_treatment", _conditions, "tt", _changeNumber, 10, 12, 2, _configurations); |
122 | 98 |
|
123 | | - Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
124 | | - Mockito.when(condition.matcher().match(matchingKey, bucketingKey, null, splitClient)).thenReturn(true); |
| 99 | + Mockito.when(_splitFetcher.fetch(_splitName)).thenReturn(split); |
| 100 | + Mockito.when(condition.matcher().match(_matchingKey, _bucketingKey, null, _splitClient)).thenReturn(true); |
125 | 101 |
|
126 | | - TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 102 | + TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(_matchingKey, _bucketingKey, _splitName, null, _splitClient); |
127 | 103 |
|
128 | 104 | assertEquals("default_treatment", result.treatment); |
129 | 105 | assertEquals("not in split", result.label); |
130 | | - assertEquals(changeNumber, result.changeNumber); |
| 106 | + assertEquals(_changeNumber, result.changeNumber); |
131 | 107 | } |
132 | 108 |
|
133 | 109 | @Test |
134 | 110 | public void evaluateWithRollOutConditionTrafficAllocationIsBiggerBucketReturnTreatment() throws ChangeNumberExceptionWrapper { |
135 | | - SplitFetcher splitFetcher = Mockito.mock(SplitFetcher.class); |
136 | | - SDKReadinessGates gates = Mockito.mock(SDKReadinessGates.class); |
137 | | - Evaluator evaluator = new EvaluatorImp(gates, splitFetcher); |
138 | | - CombiningMatcher matcher = Mockito.mock(CombiningMatcher.class); |
139 | | - |
140 | | - SplitClientImpl splitClient = getSplitClient(splitFetcher, gates, evaluator); |
141 | | - |
142 | | - String matchingKey = "test"; |
143 | | - String bucketingKey = "test"; |
144 | | - String splitName = "split_name_test"; |
145 | | - Long changeNumber = 123123L; |
146 | | - Map<String, String> configurations = new HashMap<>(); |
147 | | - List<ParsedCondition> conditions = new ArrayList<>(); |
148 | | - List<Partition> partitions = new ArrayList<>(); |
149 | | - |
150 | 111 | Partition partition = new Partition(); |
151 | 112 | partition.treatment = "treatment_test"; |
152 | 113 | partition.size = 100; |
153 | | - partitions.add(partition); |
154 | | - ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, matcher, partitions, "test label"); |
155 | | - conditions.add(condition); |
| 114 | + _partitions.add(partition); |
| 115 | + ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, _matcher, _partitions, "test label"); |
| 116 | + _conditions.add(condition); |
156 | 117 |
|
157 | | - ParsedSplit split = new ParsedSplit(splitName, 0, false, "default_treatment", conditions, "tt", changeNumber, 60, 18, 2, configurations); |
| 118 | + ParsedSplit split = new ParsedSplit(_splitName, 0, false, "default_treatment", _conditions, "tt", _changeNumber, 60, 18, 2, _configurations); |
158 | 119 |
|
159 | | - Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
160 | | - Mockito.when(condition.matcher().match(matchingKey, bucketingKey, null, splitClient)).thenReturn(true); |
| 120 | + Mockito.when(_splitFetcher.fetch(_splitName)).thenReturn(split); |
| 121 | + Mockito.when(condition.matcher().match(_matchingKey, _bucketingKey, null, _splitClient)).thenReturn(true); |
161 | 122 |
|
162 | | - TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 123 | + TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(_matchingKey, _bucketingKey, _splitName, null, _splitClient); |
163 | 124 |
|
164 | 125 | assertEquals("treatment_test", result.treatment); |
165 | 126 | assertEquals("test label", result.label); |
166 | | - assertEquals(changeNumber, result.changeNumber); |
| 127 | + assertEquals(_changeNumber, result.changeNumber); |
167 | 128 | } |
168 | 129 |
|
169 | 130 | @Test |
170 | 131 | public void evaluateWithWhitelistConditionReturnTreatment() throws ChangeNumberExceptionWrapper { |
171 | | - SplitFetcher splitFetcher = Mockito.mock(SplitFetcher.class); |
172 | | - SDKReadinessGates gates = Mockito.mock(SDKReadinessGates.class); |
173 | | - Evaluator evaluator = new EvaluatorImp(gates, splitFetcher); |
174 | | - CombiningMatcher matcher = Mockito.mock(CombiningMatcher.class); |
175 | | - |
176 | | - SplitClientImpl splitClient = getSplitClient(splitFetcher, gates, evaluator); |
177 | | - |
178 | | - String matchingKey = "test"; |
179 | | - String bucketingKey = "test"; |
180 | | - String splitName = "split_name_test"; |
181 | | - Long changeNumber = 123123L; |
182 | | - Map<String, String> configurations = new HashMap<>(); |
183 | | - List<ParsedCondition> conditions = new ArrayList<>(); |
184 | | - List<Partition> partitions = new ArrayList<>(); |
185 | | - |
186 | 132 | Partition partition = new Partition(); |
187 | 133 | partition.treatment = "treatment_test"; |
188 | 134 | partition.size = 100; |
189 | | - partitions.add(partition); |
190 | | - ParsedCondition condition = new ParsedCondition(ConditionType.WHITELIST, matcher, partitions, "test whitelist label"); |
191 | | - conditions.add(condition); |
| 135 | + _partitions.add(partition); |
| 136 | + ParsedCondition condition = new ParsedCondition(ConditionType.WHITELIST, _matcher, _partitions, "test whitelist label"); |
| 137 | + _conditions.add(condition); |
192 | 138 |
|
193 | | - ParsedSplit split = new ParsedSplit(splitName, 0, false, "default_treatment", conditions, "tt", changeNumber, 60, 18, 2, configurations); |
| 139 | + ParsedSplit split = new ParsedSplit(_splitName, 0, false, "default_treatment", _conditions, "tt", _changeNumber, 60, 18, 2, _configurations); |
194 | 140 |
|
195 | | - Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
196 | | - Mockito.when(condition.matcher().match(matchingKey, bucketingKey, null, splitClient)).thenReturn(true); |
| 141 | + Mockito.when(_splitFetcher.fetch(_splitName)).thenReturn(split); |
| 142 | + Mockito.when(condition.matcher().match(_matchingKey, _bucketingKey, null, _splitClient)).thenReturn(true); |
197 | 143 |
|
198 | | - TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 144 | + TreatmentLabelAndChangeNumber result = _evaluator.evaluateFeature(_matchingKey, _bucketingKey, _splitName, null, _splitClient); |
199 | 145 |
|
200 | 146 | assertEquals("treatment_test", result.treatment); |
201 | 147 | assertEquals("test whitelist label", result.label); |
202 | | - assertEquals(changeNumber, result.changeNumber); |
| 148 | + assertEquals(_changeNumber, result.changeNumber); |
203 | 149 | } |
204 | 150 |
|
205 | 151 | private SplitClientImpl getSplitClient(SplitFetcher splitFetcher, SDKReadinessGates gates, Evaluator evaluator) { |
|
0 commit comments