1616
1717public 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