Skip to content

Commit

Permalink
Add trackImpressions to DTO (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea authored Dec 16, 2024

Verified

This commit was signed with the committer’s verified signature.
renovate-bot Mend Renovate
1 parent 6c522e6 commit b45fb8c
Showing 9 changed files with 190 additions and 14 deletions.
19 changes: 10 additions & 9 deletions src/main/java/io/split/android/client/SplitManagerImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package io.split.android.client;

import static io.split.android.client.utils.Utils.checkNotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.split.android.client.api.SplitView;
import io.split.android.client.dtos.Partition;
import io.split.android.client.dtos.Split;
@@ -13,15 +22,6 @@
import io.split.android.engine.experiments.ParsedSplit;
import io.split.android.engine.experiments.SplitParser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static io.split.android.client.utils.Utils.checkNotNull;

public class SplitManagerImpl implements SplitManager {

private final SplitsStorage _splitsStorage;
@@ -144,6 +144,7 @@ private SplitView toSplitView(ParsedSplit parsedSplit) {
splitView.configs = parsedSplit.configurations();
splitView.sets = new ArrayList<>(parsedSplit.sets() == null ? new HashSet<>() : parsedSplit.sets());
splitView.defaultTreatment = parsedSplit.defaultTreatment();
splitView.trackImpressions = parsedSplit.trackImpressions();

Set<String> treatments = new HashSet<>();
for (ParsedCondition condition : parsedSplit.parsedConditions()) {
1 change: 1 addition & 0 deletions src/main/java/io/split/android/client/api/SplitView.java
Original file line number Diff line number Diff line change
@@ -21,4 +21,5 @@ public class SplitView {
@NonNull
public List<String> sets = new ArrayList<>();
public String defaultTreatment;
public boolean trackImpressions;
}
3 changes: 3 additions & 0 deletions src/main/java/io/split/android/client/dtos/Split.java
Original file line number Diff line number Diff line change
@@ -49,4 +49,7 @@ public class Split {
@Nullable
@SerializedName("sets")
public Set<String> sets;

@SerializedName("trackImpressions")
public boolean trackImpressions = true;
}
15 changes: 12 additions & 3 deletions src/main/java/io/split/android/engine/experiments/ParsedSplit.java
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ public class ParsedSplit {
private final int mAlgo;
private final Map<String, String> mConfigurations;
private final Set<String> mSets;
private final boolean mTrackImpressions;

public ParsedSplit(
String feature,
@@ -36,7 +37,8 @@ public ParsedSplit(
int trafficAllocationSeed,
int algo,
Map<String, String> configurations,
Set<String> sets
Set<String> sets,
boolean trackImpressions
) {
mSplit = feature;
mSeed = seed;
@@ -47,6 +49,7 @@ public ParsedSplit(
mChangeNumber = changeNumber;
mAlgo = algo;
mConfigurations = configurations;
mTrackImpressions = trackImpressions;

if (mDefaultTreatment == null) {
throw new IllegalArgumentException("DefaultTreatment is null");
@@ -104,6 +107,10 @@ public Set<String> sets() {
return mSets;
}

public boolean trackImpressions() {
return mTrackImpressions;
}

@Override
public int hashCode() {
int result = 17;
@@ -116,6 +123,7 @@ public int hashCode() {
result = 31 * result + (int) (mChangeNumber ^ (mChangeNumber >>> 32));
result = 31 * result + (mAlgo ^ (mAlgo >>> 32));
result = 31 * result + ((mSets != null) ? mSets.hashCode() : 0);
result = 31 * result + (mTrackImpressions ? 1 : 0);
return result;
}

@@ -135,7 +143,8 @@ public boolean equals(Object obj) {
&& mChangeNumber == other.mChangeNumber
&& mAlgo == other.mAlgo
&& (Objects.equals(mConfigurations, other.mConfigurations))
&& (Objects.equals(mSets, other.mSets));
&& (Objects.equals(mSets, other.mSets)
&& mTrackImpressions == other.mTrackImpressions);

}

@@ -146,7 +155,7 @@ public String toString() {
", default treatment:" + mDefaultTreatment +
", parsedConditions:" + mParsedCondition +
", trafficTypeName:" + mTrafficTypeName + ", changeNumber:" + mChangeNumber +
", algo:" + mAlgo + ", config:" + mConfigurations + ", sets:" + mSets;
", algo:" + mAlgo + ", config:" + mConfigurations + ", sets:" + mSets + ", trackImpressions:" + mTrackImpressions;

}
}
Original file line number Diff line number Diff line change
@@ -124,7 +124,8 @@ private ParsedSplit parseWithoutExceptionHandling(Split split, String matchingKe
split.trafficAllocationSeed,
split.algo,
split.configurations,
split.sets);
split.sets,
split.trackImpressions);
}

private CombiningMatcher toMatcher(MatcherGroup matcherGroup, String matchingKey) throws UnsupportedMatcherException {
14 changes: 14 additions & 0 deletions src/test/java/io/split/android/client/SplitManagerImplTest.java
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

@@ -186,6 +187,19 @@ public void defaultTreatmentIsPresentWhenFetchingMultipleSplits() {
assertEquals("some_treatment", splitNames.get(0).defaultTreatment);
}

@Test
public void trackImpressionsIsPresent() {
Split split = SplitHelper.createSplit("FeatureName", 123, true,
"some_treatment", Arrays.asList(getTestCondition()),
"traffic", 456L, 1, null);
split.trackImpressions = false;
when(mSplitsStorage.get("FeatureName")).thenReturn(split);

SplitView featureFlag = mSplitManager.split("FeatureName");

assertFalse(featureFlag.trackImpressions);
}

private Condition getTestCondition() {
return SplitHelper.createCondition(CombiningMatcher.of(new AllKeysMatcher()), Arrays.asList(ConditionsTestUtil.partition("off", 10)));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package io.split.android.client.dtos;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import io.split.android.client.utils.Json;

public class SplitDeserializationTest {

@Test
public void trackImpressionsDefaultsToTrueWhenNotPresentInSplit() {
assertTrue(Json.fromJson(getTestSplit(null), Split.class).trackImpressions);
}

@Test
public void trackImpressionsValueIsParsedCorrectly() {
assertTrue(Json.fromJson(getTestSplit(true), Split.class).trackImpressions);
assertFalse(Json.fromJson(getTestSplit(false), Split.class).trackImpressions);
}

private String getTestSplit(Boolean trackImpressions) {
return "{\n" +
((trackImpressions != null) ? "\"trackImpressions\": " + trackImpressions + ",\n" : "") +
" \"trafficTypeName\": \"client\",\n" +
" \"name\": \"workm\",\n" +
" \"trafficAllocation\": 100,\n" +
" \"trafficAllocationSeed\": 147392224,\n" +
" \"seed\": 524417105,\n" +
" \"status\": \"ACTIVE\",\n" +
" \"killed\": false,\n" +
" \"defaultTreatment\": \"on\",\n" +
" \"changeNumber\": 1602796638344,\n" +
" \"algo\": 2,\n" +
" \"configurations\": {},\n" +
" \"conditions\": [\n" +
" {\n" +
" \"conditionType\": \"ROLLOUT\",\n" +
" \"matcherGroup\": {\n" +
" \"combiner\": \"AND\",\n" +
" \"matchers\": [\n" +
" {\n" +
" \"keySelector\": {\n" +
" \"trafficType\": \"client\",\n" +
" \"attribute\": null\n" +
" },\n" +
" \"matcherType\": \"IN_SEGMENT\",\n" +
" \"negate\": false,\n" +
" \"userDefinedSegmentMatcherData\": {\n" +
" \"segmentName\": \"new_segment\"\n" +
" },\n" +
" \"whitelistMatcherData\": null,\n" +
" \"unaryNumericMatcherData\": null,\n" +
" \"betweenMatcherData\": null,\n" +
" \"booleanMatcherData\": null,\n" +
" \"dependencyMatcherData\": null,\n" +
" \"stringMatcherData\": null\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"partitions\": [\n" +
" {\n" +
" \"treatment\": \"on\",\n" +
" \"size\": 0\n" +
" },\n" +
" {\n" +
" \"treatment\": \"off\",\n" +
" \"size\": 0\n" +
" },\n" +
" {\n" +
" \"treatment\": \"free\",\n" +
" \"size\": 100\n" +
" },\n" +
" {\n" +
" \"treatment\": \"conta\",\n" +
" \"size\": 0\n" +
" }\n" +
" ],\n" +
" \"label\": \"in segment new_segment\"\n" +
" },\n" +
" {\n" +
" \"conditionType\": \"ROLLOUT\",\n" +
" \"matcherGroup\": {\n" +
" \"combiner\": \"AND\",\n" +
" \"matchers\": [\n" +
" {\n" +
" \"keySelector\": {\n" +
" \"trafficType\": \"client\",\n" +
" \"attribute\": null\n" +
" },\n" +
" \"matcherType\": \"ALL_KEYS\",\n" +
" \"negate\": false,\n" +
" \"userDefinedSegmentMatcherData\": null,\n" +
" \"whitelistMatcherData\": null,\n" +
" \"unaryNumericMatcherData\": null,\n" +
" \"betweenMatcherData\": null,\n" +
" \"booleanMatcherData\": null,\n" +
" \"dependencyMatcherData\": null,\n" +
" \"stringMatcherData\": null\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"partitions\": [\n" +
" {\n" +
" \"treatment\": \"on\",\n" +
" \"size\": 100\n" +
" },\n" +
" {\n" +
" \"treatment\": \"off\",\n" +
" \"size\": 0\n" +
" },\n" +
" {\n" +
" \"treatment\": \"free\",\n" +
" \"size\": 0\n" +
" },\n" +
" {\n" +
" \"treatment\": \"conta\",\n" +
" \"size\": 0\n" +
" }\n" +
" ],\n" +
" \"label\": \"default rule\"\n" +
" }\n" +
" ]\n" +
" }";
}
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,9 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -495,6 +497,22 @@ public void inLargeSegmentMatcherParsingTest() {
verify(mMySegmentsStorageContainer, never()).getStorageForKey("matching_key");
}

@Test
public void trackImpressionsParsingTest(){
SplitParser parser = createParser();

Split split = makeSplit("splitName", Collections.emptyList());
split.trackImpressions = false;
Split split2 = makeSplit("splitName", Collections.emptyList());
split2.trackImpressions = true;

ParsedSplit actual = parser.parse(split);
ParsedSplit actual2 = parser.parse(split2);

assertFalse(actual.trackImpressions());
assertTrue(actual2.trackImpressions());
}

private void set_matcher_test(Condition c, io.split.android.engine.matchers.Matcher m) {

SplitParser parser = createParser();
@@ -536,6 +554,7 @@ private Split makeSplit(String name, List<Condition> conditions, long changeNumb
split.algo = 1;
split.configurations = configurations;
split.sets = Collections.emptySet();
split.trackImpressions = true;
return split;
}

3 changes: 2 additions & 1 deletion src/test/java/io/split/android/helpers/SplitHelper.java
Original file line number Diff line number Diff line change
@@ -122,7 +122,8 @@ public static ParsedSplit createParsedSplit(
seed,
algo,
configurations,
Collections.emptySet()
Collections.emptySet(),
true
);
}

0 comments on commit b45fb8c

Please sign in to comment.