|
1 | 1 | package com.launchdarkly.client; |
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonArray; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | + |
| 8 | +import org.junit.Assert; |
3 | 9 | import org.junit.Before; |
4 | 10 | import org.junit.Test; |
5 | 11 |
|
|
13 | 19 | import static com.launchdarkly.client.VersionedDataKind.FEATURES; |
14 | 20 | import static com.launchdarkly.client.VersionedDataKind.SEGMENTS; |
15 | 21 | import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertNotNull; |
16 | 23 | import static org.junit.Assert.assertNull; |
17 | 24 |
|
18 | 25 | public class FeatureFlagTest { |
@@ -243,6 +250,48 @@ public void clauseCanBeNegated() throws Exception { |
243 | 250 | assertEquals(jbool(false), f.evaluate(user, featureStore).getValue()); |
244 | 251 | } |
245 | 252 |
|
| 253 | + @Test |
| 254 | + public void clauseWithUnsupportedOperatorStringIsUnmarshalledWithNullOperator() throws Exception { |
| 255 | + // This just verifies that GSON will give us a null in this case instead of throwing an exception, |
| 256 | + // so we fail as gracefully as possible if a new operator type has been added in the application |
| 257 | + // and the SDK hasn't been upgraded yet. |
| 258 | + String badClauseJson = "{\"attribute\":\"name\",\"operator\":\"doesSomethingUnsupported\",\"values\":[\"x\"]}"; |
| 259 | + Gson gson = new Gson(); |
| 260 | + Clause clause = gson.fromJson(badClauseJson, Clause.class); |
| 261 | + assertNotNull(clause); |
| 262 | + |
| 263 | + JsonElement json = gson.toJsonTree(clause); |
| 264 | + String expectedJson = "{\"attribute\":\"name\",\"values\":[\"x\"],\"negate\":false}"; |
| 265 | + assertEquals(gson.fromJson(expectedJson, JsonElement.class), json); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void clauseWithNullOperatorDoesNotMatch() throws Exception { |
| 270 | + Clause badClause = new Clause("name", null, Arrays.asList(js("Bob")), false); |
| 271 | + FeatureFlag f = booleanFlagWithClauses(badClause); |
| 272 | + LDUser user = new LDUser.Builder("key").name("Bob").build(); |
| 273 | + |
| 274 | + assertEquals(jbool(false), f.evaluate(user, featureStore).getValue()); |
| 275 | + } |
| 276 | + |
| 277 | + @Test |
| 278 | + public void clauseWithNullOperatorDoesNotStopSubsequentRuleFromMatching() throws Exception { |
| 279 | + Clause badClause = new Clause("name", null, Arrays.asList(js("Bob")), false); |
| 280 | + Rule badRule = new Rule(Arrays.asList(badClause), 1, null); |
| 281 | + Clause goodClause = new Clause("name", Operator.in, Arrays.asList(js("Bob")), false); |
| 282 | + Rule goodRule = new Rule(Arrays.asList(goodClause), 1, null); |
| 283 | + FeatureFlag f = new FeatureFlagBuilder("feature") |
| 284 | + .on(true) |
| 285 | + .rules(Arrays.asList(badRule, goodRule)) |
| 286 | + .fallthrough(fallthroughVariation(0)) |
| 287 | + .offVariation(0) |
| 288 | + .variations(jbool(false), jbool(true)) |
| 289 | + .build(); |
| 290 | + LDUser user = new LDUser.Builder("key").name("Bob").build(); |
| 291 | + |
| 292 | + assertEquals(jbool(true), f.evaluate(user, featureStore).getValue()); |
| 293 | + } |
| 294 | + |
246 | 295 | @Test |
247 | 296 | public void testSegmentMatchClauseRetrievesSegmentFromStore() throws Exception { |
248 | 297 | Segment segment = new Segment.Builder("segkey") |
|
0 commit comments