|
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,9 +250,23 @@ 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 | + |
246 | 268 | @Test |
247 | 269 | public void clauseWithNullOperatorDoesNotMatch() throws Exception { |
248 | | - // Operator will be null if it failed to be unmarshaled from JSON, i.e. it's not a supported enum value |
249 | 270 | Clause badClause = new Clause("name", null, Arrays.asList(js("Bob")), false); |
250 | 271 | FeatureFlag f = booleanFlagWithClauses(badClause); |
251 | 272 | LDUser user = new LDUser.Builder("key").name("Bob").build(); |
|
0 commit comments