Skip to content

Commit 21f8a26

Browse files
GetKeyAndValue NPE fix (#50)
* Fix GetKeyAndValue possible NPE Added more test to getKeyAndValue Updated version to 10.1.1 * Remove unnecessary public modifier
1 parent fca46cd commit 21f8a26

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version=10.1.0
1+
version=10.1.1
22

33
org.gradle.jvmargs=-Xmx2g

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -467,26 +467,30 @@ private <T> Map.Entry<String, T> getKeyAndValueFromSettingsMap(Class<T> classOfT
467467
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, setting.getSettingsValue(), setting.getType()));
468468
}
469469

470-
for (TargetingRule targetingRule : setting.getTargetingRules()) {
471-
if (targetingRule.getSimpleValue() != null) {
472-
if (variationId.equals(targetingRule.getSimpleValue().getVariationId())) {
473-
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, targetingRule.getSimpleValue().getValue(), setting.getType()));
470+
if(setting.getTargetingRules() != null) {
471+
for (TargetingRule targetingRule : setting.getTargetingRules()) {
472+
if (targetingRule.getSimpleValue() != null) {
473+
if (variationId.equals(targetingRule.getSimpleValue().getVariationId())) {
474+
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, targetingRule.getSimpleValue().getValue(), setting.getType()));
474475

475-
}
476-
} else if (targetingRule.getPercentageOptions() != null) {
477-
for (PercentageOption percentageRule : targetingRule.getPercentageOptions()) {
478-
if (variationId.equals(percentageRule.getVariationId())) {
479-
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
480476
}
477+
} else if (targetingRule.getPercentageOptions() != null) {
478+
for (PercentageOption percentageRule : targetingRule.getPercentageOptions()) {
479+
if (variationId.equals(percentageRule.getVariationId())) {
480+
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageRule.getValue(), setting.getType()));
481+
}
482+
}
483+
} else {
484+
throw new UnsupportedOperationException("Targeting rule THEN part is missing or invalid.");
481485
}
482-
} else {
483-
throw new UnsupportedOperationException("Targeting rule THEN part is missing or invalid.");
484486
}
485487
}
486488

487-
for (PercentageOption percentageOption : setting.getPercentageOptions()) {
488-
if (variationId.equals(percentageOption.getVariationId())) {
489-
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageOption.getValue(), setting.getType()));
489+
if( setting.getPercentageOptions() != null) {
490+
for (PercentageOption percentageOption : setting.getPercentageOptions()) {
491+
if (variationId.equals(percentageOption.getVariationId())) {
492+
return new AbstractMap.SimpleEntry<>(settingKey, (T) this.parseObject(classOfT, percentageOption.getValue(), setting.getType()));
493+
}
490494
}
491495
}
492496
}

src/main/java/com/configcat/Utils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private Constants() { /* prevent from instantiation*/ }
3030
static final long DISTANT_PAST = 0;
3131
static final String CONFIG_JSON_NAME = "config_v6.json";
3232
static final String SERIALIZATION_FORMAT_VERSION = "v2";
33-
static final String VERSION = "10.1.0";
33+
static final String VERSION = "10.1.1";
3434

3535
static final String SDK_KEY_PROXY_PREFIX = "configcat-proxy/";
3636
static final String SDK_KEY_PREFIX = "configcat-sdk-1";

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

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

1717
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

@@ -64,9 +65,11 @@ void getAllVariationIdsWorks() {
6465
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON));
6566

6667
List<EvaluationDetails<?>> allValueDetails = client.getAllValueDetails(null);
67-
assertEquals(2, allValueDetails.size());
68+
assertEquals(3, allValueDetails.size());
6869
assertEquals("fakeId1", allValueDetails.get(0).getVariationId());
6970
assertEquals("fakeId2", allValueDetails.get(1).getVariationId());
71+
assertEquals("fakeId3", allValueDetails.get(2).getVariationId());
72+
7073
}
7174

7275
@Test
@@ -82,9 +85,11 @@ void getAllVariationIdsAsyncWorks() throws ExecutionException, InterruptedExcept
8285
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON));
8386

8487
List<EvaluationDetails<?>> allValueDetails = client.getAllValueDetailsAsync(null).get();
85-
assertEquals(2, allValueDetails.size());
88+
assertEquals(3, allValueDetails.size());
8689
assertEquals("fakeId1", allValueDetails.get(0).getVariationId());
8790
assertEquals("fakeId2", allValueDetails.get(1).getVariationId());
91+
assertEquals("fakeId3", allValueDetails.get(2).getVariationId());
92+
8893
}
8994

9095
@Test
@@ -93,6 +98,18 @@ void getKeyAndValueWorks() {
9398
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "fakeId2");
9499
assertEquals("key2", result.getKey());
95100
assertFalse(result.getValue());
101+
102+
Map.Entry<String, Boolean> result2 = client.getKeyAndValue(boolean.class, "percentageId2");
103+
assertEquals("key1", result2.getKey());
104+
assertFalse(result2.getValue());
105+
106+
Map.Entry<String, Boolean> result3 = client.getKeyAndValue(boolean.class, "rolloutId1");
107+
assertEquals("key1", result3.getKey());
108+
assertTrue(result3.getValue());
109+
110+
Map.Entry<String, Boolean> result4 = client.getKeyAndValue(boolean.class, "targetPercentageId2");
111+
assertEquals("key3", result4.getKey());
112+
assertFalse(result4.getValue());
96113
}
97114

98115
@Test
@@ -109,4 +126,11 @@ void getKeyAndValueNotFound() {
109126
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "nonexisting");
110127
assertNull(result);
111128
}
129+
130+
@Test
131+
void getKeyAndValueIncorrectTargetingRule() {
132+
server.enqueue(new MockResponse().setResponseCode(200).setBody(TEST_JSON_INCORRECT));
133+
Map.Entry<String, Boolean> result = client.getKeyAndValue(boolean.class, "targetPercentageId2");
134+
assertNull(result);
135+
}
112136
}

0 commit comments

Comments
 (0)