|
2 | 2 |
|
3 | 3 | import com.flagsmith.flagengine.identities.IdentityModel; |
4 | 4 | import com.flagsmith.flagengine.identities.traits.TraitModel; |
| 5 | +import com.flagsmith.flagengine.segments.SegmentConditionModel; |
5 | 6 | import com.flagsmith.flagengine.segments.SegmentEvaluator; |
6 | 7 | import com.flagsmith.flagengine.segments.SegmentModel; |
| 8 | +import com.flagsmith.flagengine.segments.SegmentRuleModel; |
| 9 | +import com.flagsmith.flagengine.segments.constants.SegmentConditions; |
| 10 | +import com.flagsmith.flagengine.segments.constants.SegmentRules; |
7 | 11 |
|
8 | 12 | import static com.flagsmith.flagengine.unit.segments.IdentitySegmentFixtures.*; |
9 | 13 |
|
10 | 14 | import org.testng.Assert; |
11 | 15 | import org.testng.annotations.DataProvider; |
12 | 16 | import org.testng.annotations.Test; |
13 | 17 |
|
| 18 | +import java.util.ArrayList; |
14 | 19 | import java.util.Arrays; |
15 | 20 | import java.util.List; |
16 | 21 |
|
@@ -52,4 +57,49 @@ public void testIdentityInSegment(SegmentModel segment, List<TraitModel> identit |
52 | 57 |
|
53 | 58 | Assert.assertTrue(actualResult.equals(expectedResponse)); |
54 | 59 | } |
| 60 | + |
| 61 | + @DataProvider(name = "traitExistenceChecks") |
| 62 | + public Object[][] traitExistenceChecks() { |
| 63 | + return new Object[][] { |
| 64 | + new Object[] {SegmentConditions.IS_SET, "foo", new ArrayList<>(), false}, |
| 65 | + new Object[] {SegmentConditions.IS_NOT_SET, "foo", new ArrayList<>(), true}, |
| 66 | + new Object[] {SegmentConditions.IS_SET, "foo", new ArrayList<>(Arrays.asList( |
| 67 | + new TraitModel("foo", "bar"))), true}, |
| 68 | + new Object[] {SegmentConditions.IS_NOT_SET, "foo", new ArrayList<>(Arrays.asList( |
| 69 | + new TraitModel("foo", "bar"))), false}, |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + @Test(dataProvider = "traitExistenceChecks") |
| 74 | + public void testTraitExistenceConditions(SegmentConditions conditionOperator, String conditionProperty, |
| 75 | + List<TraitModel> traitModels, Boolean expectedResult) { |
| 76 | + // Given |
| 77 | + // An identity to test with which has the traits as defined in the DataProvider |
| 78 | + IdentityModel identityModel = new IdentityModel(); |
| 79 | + identityModel.setIdentifier("foo"); |
| 80 | + identityModel.setIdentityTraits(traitModels); |
| 81 | + identityModel.setEnvironmentApiKey("api-key"); |
| 82 | + |
| 83 | + // And a segment which has the operator and property value as defined in the DataProvider |
| 84 | + SegmentConditionModel segmentCondition = new SegmentConditionModel(); |
| 85 | + segmentCondition.setOperator(conditionOperator); |
| 86 | + segmentCondition.setProperty_(conditionProperty); |
| 87 | + segmentCondition.setValue(null); |
| 88 | + |
| 89 | + SegmentRuleModel segmentRule = new SegmentRuleModel(); |
| 90 | + segmentRule.setConditions(new ArrayList<>(Arrays.asList(segmentCondition))); |
| 91 | + segmentRule.setType(SegmentRules.ALL_RULE.getRule()); |
| 92 | + |
| 93 | + SegmentModel segment = new SegmentModel(); |
| 94 | + segment.setName("testSegment"); |
| 95 | + segment.setRules(new ArrayList<>(Arrays.asList(segmentRule))); |
| 96 | + |
| 97 | + // When |
| 98 | + // We evaluate whether the identity is in the segment |
| 99 | + Boolean inSegment = SegmentEvaluator.evaluateIdentityInSegment(identityModel, segment, null); |
| 100 | + |
| 101 | + // Then |
| 102 | + // The result is as we expect from the DataProvider definition |
| 103 | + Assert.assertEquals(inSegment, expectedResult); |
| 104 | + } |
55 | 105 | } |
0 commit comments