Skip to content

Commit

Permalink
Add more getKeyAndValue test. (#46)
Browse files Browse the repository at this point in the history
Fix possible NPE.
Update version to 9.1.1
  • Loading branch information
novalisdenahi authored Apr 23, 2024
1 parent 4309d14 commit 2c6fe1b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=9.1.0
version=9.1.1
32 changes: 18 additions & 14 deletions src/main/java/com/configcat/ConfigCatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,25 +517,29 @@ private <T> Map.Entry<String, T> getKeyAndValueFromSettingsMap(Class<T> classOfT
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, setting.getSettingsValue(), setting.getType()));
}

for (TargetingRule targetingRule : setting.getTargetingRules()) {
if (targetingRule.getSimpleValue() != null) {
if (variationId.equals(targetingRule.getSimpleValue().getVariationId())) {
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, targetingRule.getSimpleValue().getValue(), setting.getType()));
}
} else if (targetingRule.getPercentageOptions() != null) {
for (PercentageOption percentageRule : targetingRule.getPercentageOptions()) {
if (variationId.equals(percentageRule.getVariationId())) {
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
if(setting.getTargetingRules() != null) {
for (TargetingRule targetingRule : setting.getTargetingRules()) {
if (targetingRule.getSimpleValue() != null) {
if (variationId.equals(targetingRule.getSimpleValue().getVariationId())) {
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, targetingRule.getSimpleValue().getValue(), setting.getType()));
}
} else if (targetingRule.getPercentageOptions() != null) {
for (PercentageOption percentageRule : targetingRule.getPercentageOptions()) {
if (variationId.equals(percentageRule.getVariationId())) {
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
}
}
} else {
throw new UnsupportedOperationException("Targeting rule THEN part is missing or invalid.");
}
} else {
throw new UnsupportedOperationException("Targeting rule THEN part is missing or invalid.");
}
}

for (PercentageOption percentageRule : setting.getPercentageOptions()) {
if (variationId.equals(percentageRule.getVariationId())) {
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
if(setting.getPercentageOptions() != null) {
for (PercentageOption percentageRule : setting.getPercentageOptions()) {
if (variationId.equals(percentageRule.getVariationId())) {
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/configcat/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ private Constants() { /* prevent from instantiation*/ }
static final long DISTANT_PAST = 0;
static final String CONFIG_JSON_NAME = "config_v6.json";
static final String SERIALIZATION_FORMAT_VERSION = "v2";
static final String VERSION = "9.1.0";
static final String VERSION = "9.1.1";

static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/";
static final String SDK_KEY_PREFIX = "configcat-sdk-1";
Expand Down
34 changes: 32 additions & 2 deletions src/test/java/com/configcat/VariationIdTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

public class VariationIdTests {

private static final String TEST_JSON = "{ p: { s: 'test-salt' }, f: { key1: { v: { b: true }, i: 'fakeId1', p: [] ,r: [] }, key2: { v: { b: false }, i: 'fakeId2', p: [] ,r: [] } } }";
private static final String TEST_JSON = "{ 'p':{ 'u': 'https://cdn-global.configcat.com', 'r': '0 ', 's': 'test-salt'}, 'f':{ 'key1':{ 't':0, 'r':[ { 'c':[ { 'u':{ 'a': 'Email', 'c': 2 , 'l ':[ '@configcat.com' ] } } ], 's':{ 'v': { 'b':true }, 'i': 'rolloutId1' } }, { 'c': [ { 'u' :{ 'a': 'Email', 'c': 2, 'l' : [ '@test.com' ] } } ], 's' : { 'v' : { 'b': false }, 'i': 'rolloutId2' } } ], 'p':[ { 'p':50, 'v' : { 'b': true }, 'i' : 'percentageId1' }, { 'p' : 50, 'v' : { 'b': false }, 'i': 'percentageId2' } ], 'v':{ 'b':true }, 'i': 'fakeId1' }, 'key2': { 't':0, 'v': { 'b': false }, 'i': 'fakeId2' }, 'key3': { 't': 0, 'r':[ { 'c': [ { 'u':{ 'a': 'Email', 'c':2, 'l':[ '@configcat.com' ] } } ], 'p': [{ 'p':50, 'v':{ 'b': true }, 'i' : 'targetPercentageId1' }, { 'p': 50, 'v': { 'b':false }, 'i' : 'targetPercentageId2' } ] } ], 'v':{ 'b': false }, 'i': 'fakeId3' } } }";
private static final String TEST_JSON_INCORRECT = "{ 'p':{ 'u': 'https://cdn-global.configcat.com', 'r': '0 ', 's': 'test-salt' }, 'f' :{ 'incorrect' : { 't': 0, 'r': [ {'c': [ {'u': {'a': 'Email', 'c': 2, 'l': ['@configcat.com'] } } ] } ],'v': {'b': false}, 'i': 'incorrectId' } } }";
private ConfigCatClient client;
private MockWebServer server;

Expand Down Expand Up @@ -57,9 +58,18 @@ public void getAllVariationIdsWorks() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON));

List<EvaluationDetails<Object>> allValueDetails = client.getAllValueDetails(null);
assertEquals(2, allValueDetails.size());
assertEquals(3, allValueDetails.size());
assertEquals("fakeId1", allValueDetails.get(0).getVariationId());
assertEquals("fakeId2", allValueDetails.get(1).getVariationId());
assertEquals("fakeId3", allValueDetails.get(2).getVariationId());
}

@Test
public void getAllVariationIdsWorksEmpty() {
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));

List<EvaluationDetails<Object>> allValueDetails = client.getAllValueDetails(null);
assertEquals(0, allValueDetails.size());
}

@Test
Expand All @@ -68,6 +78,19 @@ public void getKeyAndValueWorks() {
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "fakeId2");
assertEquals("key2", result.getKey());
assertFalse(result.getValue());

Map.Entry<String, Boolean> result2 = client.getKeyAndValue(boolean.class, "percentageId2");
assertEquals("key1", result2.getKey());
assertFalse(result2.getValue());

Map.Entry<String, Boolean> result3 = client.getKeyAndValue(boolean.class, "rolloutId1");
assertEquals("key1", result3.getKey());
assertTrue(result3.getValue());

Map.Entry<String, Boolean> result4 = client.getKeyAndValue(boolean.class, "targetPercentageId2");
assertEquals("key3", result4.getKey());
assertFalse(result4.getValue());

}

@Test
Expand All @@ -84,4 +107,11 @@ public void getKeyAndValueNotFound() {
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "nonexisting");
assertNull(result);
}

@Test
public void getKeyAndValueIncorrectTargetingRule() {
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON_INCORRECT));
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "targetPercentageId2");
assertNull(result);
}
}

0 comments on commit 2c6fe1b

Please sign in to comment.