|
| 1 | +package io.split.engine.evaluator; |
| 2 | + |
| 3 | +import io.split.client.*; |
| 4 | +import io.split.client.dtos.ConditionType; |
| 5 | +import io.split.client.dtos.Partition; |
| 6 | +import io.split.client.dtos.TreatmentLabelAndChangeNumber; |
| 7 | +import io.split.client.exceptions.ChangeNumberExceptionWrapper; |
| 8 | +import io.split.client.impressions.ImpressionsManager; |
| 9 | +import io.split.engine.SDKReadinessGates; |
| 10 | +import io.split.engine.experiments.ParsedCondition; |
| 11 | +import io.split.engine.experiments.ParsedSplit; |
| 12 | +import io.split.engine.experiments.SplitFetcher; |
| 13 | +import io.split.engine.matchers.CombiningMatcher; |
| 14 | +import io.split.engine.metrics.Metrics; |
| 15 | +import org.junit.Test; |
| 16 | +import org.mockito.Mockito; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | + |
| 25 | +public class EvaluatorTest { |
| 26 | + @Test |
| 27 | + 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); |
| 33 | + |
| 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); |
| 41 | + |
| 42 | + assertEquals("control", result.treatment); |
| 43 | + assertEquals("definition not found", result.label); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + 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); |
| 53 | + |
| 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); |
| 65 | + |
| 66 | + assertEquals("defaultTreatment", result.treatment); |
| 67 | + assertEquals("killed", result.label); |
| 68 | + assertEquals(changeNumber, result.changeNumber); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + 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); |
| 86 | + |
| 87 | + Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
| 88 | + |
| 89 | + TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 90 | + |
| 91 | + assertEquals("defaultTreatment", result.treatment); |
| 92 | + assertEquals("default rule", result.label); |
| 93 | + assertEquals(changeNumber, result.changeNumber); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + 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 | + Partition partition = new Partition(); |
| 115 | + partition.treatment = "treatment_test"; |
| 116 | + partition.size = 100; |
| 117 | + partitions.add(partition); |
| 118 | + ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, matcher, partitions, "test label"); |
| 119 | + conditions.add(condition); |
| 120 | + |
| 121 | + ParsedSplit split = new ParsedSplit(splitName, 0, false, "default_treatment", conditions, "tt", changeNumber, 10, 12, 2, configurations); |
| 122 | + |
| 123 | + Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
| 124 | + Mockito.when(condition.matcher().match(matchingKey, bucketingKey, null, splitClient)).thenReturn(true); |
| 125 | + |
| 126 | + TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 127 | + |
| 128 | + assertEquals("default_treatment", result.treatment); |
| 129 | + assertEquals("not in split", result.label); |
| 130 | + assertEquals(changeNumber, result.changeNumber); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + 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 | + Partition partition = new Partition(); |
| 151 | + partition.treatment = "treatment_test"; |
| 152 | + partition.size = 100; |
| 153 | + partitions.add(partition); |
| 154 | + ParsedCondition condition = new ParsedCondition(ConditionType.ROLLOUT, matcher, partitions, "test label"); |
| 155 | + conditions.add(condition); |
| 156 | + |
| 157 | + ParsedSplit split = new ParsedSplit(splitName, 0, false, "default_treatment", conditions, "tt", changeNumber, 60, 18, 2, configurations); |
| 158 | + |
| 159 | + Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
| 160 | + Mockito.when(condition.matcher().match(matchingKey, bucketingKey, null, splitClient)).thenReturn(true); |
| 161 | + |
| 162 | + TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 163 | + |
| 164 | + assertEquals("treatment_test", result.treatment); |
| 165 | + assertEquals("test label", result.label); |
| 166 | + assertEquals(changeNumber, result.changeNumber); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + 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 | + Partition partition = new Partition(); |
| 187 | + partition.treatment = "treatment_test"; |
| 188 | + partition.size = 100; |
| 189 | + partitions.add(partition); |
| 190 | + ParsedCondition condition = new ParsedCondition(ConditionType.WHITELIST, matcher, partitions, "test whitelist label"); |
| 191 | + conditions.add(condition); |
| 192 | + |
| 193 | + ParsedSplit split = new ParsedSplit(splitName, 0, false, "default_treatment", conditions, "tt", changeNumber, 60, 18, 2, configurations); |
| 194 | + |
| 195 | + Mockito.when(splitFetcher.fetch(splitName)).thenReturn(split); |
| 196 | + Mockito.when(condition.matcher().match(matchingKey, bucketingKey, null, splitClient)).thenReturn(true); |
| 197 | + |
| 198 | + TreatmentLabelAndChangeNumber result = evaluator.evaluateFeature(matchingKey, bucketingKey, splitName, null, splitClient); |
| 199 | + |
| 200 | + assertEquals("treatment_test", result.treatment); |
| 201 | + assertEquals("test whitelist label", result.label); |
| 202 | + assertEquals(changeNumber, result.changeNumber); |
| 203 | + } |
| 204 | + |
| 205 | + private SplitClientImpl getSplitClient(SplitFetcher splitFetcher, SDKReadinessGates gates, Evaluator evaluator) { |
| 206 | + SplitFactory container = Mockito.mock(SplitFactory.class); |
| 207 | + ImpressionsManager impressionManager = Mockito.mock(ImpressionsManager.class); |
| 208 | + Metrics metrics = Mockito.mock(Metrics.class); |
| 209 | + EventClient eventClient = Mockito.mock(EventClient.class); |
| 210 | + SplitClientConfig config = Mockito.mock(SplitClientConfig.class); |
| 211 | + |
| 212 | + return new SplitClientImpl(container, splitFetcher, impressionManager, metrics, eventClient, config, gates, evaluator); |
| 213 | + } |
| 214 | +} |
0 commit comments