|
1 | 1 | package com.launchdarkly.client; |
2 | 2 |
|
| 3 | +import com.google.common.collect.ImmutableList; |
3 | 4 | import com.google.gson.Gson; |
| 5 | +import com.google.gson.JsonArray; |
4 | 6 | import com.google.gson.JsonElement; |
| 7 | +import com.google.gson.JsonObject; |
5 | 8 | import com.google.gson.JsonPrimitive; |
6 | 9 | import com.google.gson.reflect.TypeToken; |
7 | 10 |
|
|
10 | 13 | import java.lang.reflect.Type; |
11 | 14 | import java.util.Map; |
12 | 15 |
|
| 16 | +import static com.launchdarkly.client.TestUtil.jbool; |
| 17 | +import static com.launchdarkly.client.TestUtil.jdouble; |
| 18 | +import static com.launchdarkly.client.TestUtil.jint; |
| 19 | +import static com.launchdarkly.client.TestUtil.js; |
13 | 20 | import static org.junit.Assert.assertEquals; |
14 | 21 | import static org.junit.Assert.assertNull; |
15 | 22 |
|
@@ -233,4 +240,44 @@ public void getValueReturnsNullIfNotFound() { |
233 | 240 | .build(); |
234 | 241 | assertNull(user.getValueForEvaluation("height")); |
235 | 242 | } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void canAddCustomAttrWithListOfStrings() { |
| 246 | + LDUser user = new LDUser.Builder("key") |
| 247 | + .customString("foo", ImmutableList.of("a", "b")) |
| 248 | + .build(); |
| 249 | + JsonElement expectedAttr = makeCustomAttrWithListOfValues("foo", js("a"), js("b")); |
| 250 | + JsonObject jo = LDConfig.DEFAULT.gson.toJsonTree(user).getAsJsonObject(); |
| 251 | + assertEquals(expectedAttr, jo.get("custom")); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + public void canAddCustomAttrWithListOfNumbers() { |
| 256 | + LDUser user = new LDUser.Builder("key") |
| 257 | + .customNumber("foo", ImmutableList.<Number>of(new Integer(1), new Double(2))) |
| 258 | + .build(); |
| 259 | + JsonElement expectedAttr = makeCustomAttrWithListOfValues("foo", jint(1), jdouble(2)); |
| 260 | + JsonObject jo = LDConfig.DEFAULT.gson.toJsonTree(user).getAsJsonObject(); |
| 261 | + assertEquals(expectedAttr, jo.get("custom")); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + public void canAddCustomAttrWithListOfMixedValues() { |
| 266 | + LDUser user = new LDUser.Builder("key") |
| 267 | + .customValues("foo", ImmutableList.<JsonElement>of(js("a"), jint(1), jbool(true))) |
| 268 | + .build(); |
| 269 | + JsonElement expectedAttr = makeCustomAttrWithListOfValues("foo", js("a"), jint(1), jbool(true)); |
| 270 | + JsonObject jo = LDConfig.DEFAULT.gson.toJsonTree(user).getAsJsonObject(); |
| 271 | + assertEquals(expectedAttr, jo.get("custom")); |
| 272 | + } |
| 273 | + |
| 274 | + private JsonElement makeCustomAttrWithListOfValues(String name, JsonElement... values) { |
| 275 | + JsonObject ret = new JsonObject(); |
| 276 | + JsonArray a = new JsonArray(); |
| 277 | + for (JsonElement v: values) { |
| 278 | + a.add(v); |
| 279 | + } |
| 280 | + ret.add(name, a); |
| 281 | + return ret; |
| 282 | + } |
236 | 283 | } |
0 commit comments