Skip to content

Commit 2c6fe1b

Browse files
Add more getKeyAndValue test. (#46)
Fix possible NPE. Update version to 9.1.1
1 parent 4309d14 commit 2c6fe1b

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=9.1.0
1+
version=9.1.1

src/main/java/com/configcat/ConfigCatClient.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -517,25 +517,29 @@ private <T> Map.Entry<String, T> getKeyAndValueFromSettingsMap(Class<T> classOfT
517517
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, setting.getSettingsValue(), setting.getType()));
518518
}
519519

520-
for (TargetingRule targetingRule : setting.getTargetingRules()) {
521-
if (targetingRule.getSimpleValue() != null) {
522-
if (variationId.equals(targetingRule.getSimpleValue().getVariationId())) {
523-
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, targetingRule.getSimpleValue().getValue(), setting.getType()));
524-
}
525-
} else if (targetingRule.getPercentageOptions() != null) {
526-
for (PercentageOption percentageRule : targetingRule.getPercentageOptions()) {
527-
if (variationId.equals(percentageRule.getVariationId())) {
528-
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
520+
if(setting.getTargetingRules() != null) {
521+
for (TargetingRule targetingRule : setting.getTargetingRules()) {
522+
if (targetingRule.getSimpleValue() != null) {
523+
if (variationId.equals(targetingRule.getSimpleValue().getVariationId())) {
524+
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, targetingRule.getSimpleValue().getValue(), setting.getType()));
525+
}
526+
} else if (targetingRule.getPercentageOptions() != null) {
527+
for (PercentageOption percentageRule : targetingRule.getPercentageOptions()) {
528+
if (variationId.equals(percentageRule.getVariationId())) {
529+
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
530+
}
529531
}
532+
} else {
533+
throw new UnsupportedOperationException("Targeting rule THEN part is missing or invalid.");
530534
}
531-
} else {
532-
throw new UnsupportedOperationException("Targeting rule THEN part is missing or invalid.");
533535
}
534536
}
535537

536-
for (PercentageOption percentageRule : setting.getPercentageOptions()) {
537-
if (variationId.equals(percentageRule.getVariationId())) {
538-
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
538+
if(setting.getPercentageOptions() != null) {
539+
for (PercentageOption percentageRule : setting.getPercentageOptions()) {
540+
if (variationId.equals(percentageRule.getVariationId())) {
541+
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
542+
}
539543
}
540544
}
541545
}

src/main/java/com/configcat/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private Constants() { /* prevent from instantiation*/ }
77
static final long DISTANT_PAST = 0;
88
static final String CONFIG_JSON_NAME = "config_v6.json";
99
static final String SERIALIZATION_FORMAT_VERSION = "v2";
10-
static final String VERSION = "9.1.0";
10+
static final String VERSION = "9.1.1";
1111

1212
static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/";
1313
static final String SDK_KEY_PREFIX = "configcat-sdk-1";

src/test/java/com/configcat/VariationIdTests.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
public class VariationIdTests {
1818

19-
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: [] } } }";
19+
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' } } }";
20+
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' } } }";
2021
private ConfigCatClient client;
2122
private MockWebServer server;
2223

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

5960
List<EvaluationDetails<Object>> allValueDetails = client.getAllValueDetails(null);
60-
assertEquals(2, allValueDetails.size());
61+
assertEquals(3, allValueDetails.size());
6162
assertEquals("fakeId1", allValueDetails.get(0).getVariationId());
6263
assertEquals("fakeId2", allValueDetails.get(1).getVariationId());
64+
assertEquals("fakeId3", allValueDetails.get(2).getVariationId());
65+
}
66+
67+
@Test
68+
public void getAllVariationIdsWorksEmpty() {
69+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
70+
71+
List<EvaluationDetails<Object>> allValueDetails = client.getAllValueDetails(null);
72+
assertEquals(0, allValueDetails.size());
6373
}
6474

6575
@Test
@@ -68,6 +78,19 @@ public void getKeyAndValueWorks() {
6878
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "fakeId2");
6979
assertEquals("key2", result.getKey());
7080
assertFalse(result.getValue());
81+
82+
Map.Entry<String, Boolean> result2 = client.getKeyAndValue(boolean.class, "percentageId2");
83+
assertEquals("key1", result2.getKey());
84+
assertFalse(result2.getValue());
85+
86+
Map.Entry<String, Boolean> result3 = client.getKeyAndValue(boolean.class, "rolloutId1");
87+
assertEquals("key1", result3.getKey());
88+
assertTrue(result3.getValue());
89+
90+
Map.Entry<String, Boolean> result4 = client.getKeyAndValue(boolean.class, "targetPercentageId2");
91+
assertEquals("key3", result4.getKey());
92+
assertFalse(result4.getValue());
93+
7194
}
7295

7396
@Test
@@ -84,4 +107,11 @@ public void getKeyAndValueNotFound() {
84107
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "nonexisting");
85108
assertNull(result);
86109
}
110+
111+
@Test
112+
public void getKeyAndValueIncorrectTargetingRule() {
113+
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON_INCORRECT));
114+
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "targetPercentageId2");
115+
assertNull(result);
116+
}
87117
}

0 commit comments

Comments
 (0)