|
| 1 | +package com.launchdarkly.sdk; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.hasEntry; |
| 5 | +import static org.hamcrest.Matchers.is; |
| 6 | +import static org.hamcrest.Matchers.not; |
| 7 | +import static org.hamcrest.Matchers.nullValue; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +@SuppressWarnings("javadoc") |
| 15 | +public class LDContextEncoderTest extends BaseTest { |
| 16 | + @Test |
| 17 | + public void nullContextReturnsEmptyMap() { |
| 18 | + Map<String, Object> result = LDContextEncoder.encode(null); |
| 19 | + assertThat(result.isEmpty(), is(true)); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void invalidContextReturnsEmptyMap() { |
| 24 | + LDContext invalid = LDContext.create(""); |
| 25 | + assertThat(invalid.isValid(), is(false)); |
| 26 | + assertThat(LDContextEncoder.encode(invalid).isEmpty(), is(true)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void singleKindContextIncludesKindKeyAndAnonymous() { |
| 31 | + LDContext context = LDContext.builder("user-key").build(); |
| 32 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 33 | + assertThat(result.get("kind"), is((Object) "user")); |
| 34 | + assertThat(result.get("key"), is((Object) "user-key")); |
| 35 | + assertThat(result.containsKey("anonymous"), is(true)); |
| 36 | + assertThat(result.get("anonymous"), is((Object) Boolean.FALSE)); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void singleKindContextIncludesNameWhenPresent() { |
| 41 | + LDContext context = LDContext.builder("user-key").name("Bob").build(); |
| 42 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 43 | + assertThat(result.get("name"), is((Object) "Bob")); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void singleKindContextOmitsNameWhenNull() { |
| 48 | + LDContext context = LDContext.builder("user-key").build(); |
| 49 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 50 | + assertThat(result.containsKey("name"), is(false)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void anonymousAlwaysPresentEvenWhenFalse() { |
| 55 | + LDContext context = LDContext.builder("key").anonymous(false).build(); |
| 56 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 57 | + assertThat(result.containsKey("anonymous"), is(true)); |
| 58 | + assertThat(result.get("anonymous"), is((Object) Boolean.FALSE)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void anonymousTrueIsPresentWhenSet() { |
| 63 | + LDContext context = LDContext.builder("key").anonymous(true).build(); |
| 64 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 65 | + assertThat(result.get("anonymous"), is((Object) Boolean.TRUE)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void singleKindContextIncludesCustomAttributes() { |
| 70 | + LDContext context = LDContext.builder("user-key") |
| 71 | + .set("tier", "gold") |
| 72 | + .set("score", 42) |
| 73 | + .build(); |
| 74 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 75 | + assertThat(result.get("tier"), is((Object) "gold")); |
| 76 | + assertThat(result.get("score"), is((Object) 42L)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void customAttributeObjectValueIsDecoded() { |
| 81 | + LDContext context = LDContext.builder("user-key") |
| 82 | + .set("address", LDValue.buildObject().put("city", "Oakland").build()) |
| 83 | + .build(); |
| 84 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + Map<String, Object> address = (Map<String, Object>) result.get("address"); |
| 87 | + assertThat(address, is(not(nullValue()))); |
| 88 | + assertThat(address.get("city"), is((Object) "Oakland")); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void customAttributeArrayValueIsDecoded() { |
| 93 | + LDContext context = LDContext.builder("user-key") |
| 94 | + .set("tags", LDValue.buildArray().add("a").add("b").build()) |
| 95 | + .build(); |
| 96 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 97 | + @SuppressWarnings("unchecked") |
| 98 | + List<Object> tags = (List<Object>) result.get("tags"); |
| 99 | + assertThat(tags, is(not(nullValue()))); |
| 100 | + assertThat(tags.get(0), is((Object) "a")); |
| 101 | + assertThat(tags.get(1), is((Object) "b")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void multiKindContextHasKindMultiAndFullyQualifiedKey() { |
| 106 | + LDContext multi = LDContext.createMulti( |
| 107 | + LDContext.builder("user-key").build(), |
| 108 | + LDContext.builder(ContextKind.of("org"), "org-key").build()); |
| 109 | + Map<String, Object> result = LDContextEncoder.encode(multi); |
| 110 | + assertThat(result.get("kind"), is((Object) "multi")); |
| 111 | + assertThat(result.get("key"), is((Object) multi.getFullyQualifiedKey())); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void multiKindContextContainsPerKindObjects() { |
| 116 | + LDContext multi = LDContext.createMulti( |
| 117 | + LDContext.builder("user-key").name("Bob").build(), |
| 118 | + LDContext.builder(ContextKind.of("org"), "org-key").set("tier", "gold").build()); |
| 119 | + Map<String, Object> result = LDContextEncoder.encode(multi); |
| 120 | + |
| 121 | + @SuppressWarnings("unchecked") |
| 122 | + Map<String, Object> userMap = (Map<String, Object>) result.get("user"); |
| 123 | + assertThat(userMap, is(not(nullValue()))); |
| 124 | + assertThat(userMap.get("key"), is((Object) "user-key")); |
| 125 | + assertThat(userMap.get("name"), is((Object) "Bob")); |
| 126 | + |
| 127 | + @SuppressWarnings("unchecked") |
| 128 | + Map<String, Object> orgMap = (Map<String, Object>) result.get("org"); |
| 129 | + assertThat(orgMap, is(not(nullValue()))); |
| 130 | + assertThat(orgMap.get("key"), is((Object) "org-key")); |
| 131 | + assertThat(orgMap.get("tier"), is((Object) "gold")); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void multiKindPerKindObjectsOmitKind() { |
| 136 | + LDContext multi = LDContext.createMulti( |
| 137 | + LDContext.builder("user-key").build(), |
| 138 | + LDContext.builder(ContextKind.of("org"), "org-key").build()); |
| 139 | + Map<String, Object> result = LDContextEncoder.encode(multi); |
| 140 | + |
| 141 | + @SuppressWarnings("unchecked") |
| 142 | + Map<String, Object> userMap = (Map<String, Object>) result.get("user"); |
| 143 | + assertThat(userMap.containsKey("kind"), is(false)); |
| 144 | + |
| 145 | + @SuppressWarnings("unchecked") |
| 146 | + Map<String, Object> orgMap = (Map<String, Object>) result.get("org"); |
| 147 | + assertThat(orgMap.containsKey("kind"), is(false)); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void multiKindNestedContextsIncludeAnonymous() { |
| 152 | + LDContext multi = LDContext.createMulti( |
| 153 | + LDContext.builder("user-key").build(), |
| 154 | + LDContext.builder(ContextKind.of("org"), "org-key").anonymous(true).build()); |
| 155 | + Map<String, Object> result = LDContextEncoder.encode(multi); |
| 156 | + |
| 157 | + @SuppressWarnings("unchecked") |
| 158 | + Map<String, Object> userMap = (Map<String, Object>) result.get("user"); |
| 159 | + assertThat(userMap.containsKey("anonymous"), is(true)); |
| 160 | + assertThat(userMap.get("anonymous"), is((Object) Boolean.FALSE)); |
| 161 | + |
| 162 | + @SuppressWarnings("unchecked") |
| 163 | + Map<String, Object> orgMap = (Map<String, Object>) result.get("org"); |
| 164 | + assertThat(orgMap.get("anonymous"), is((Object) Boolean.TRUE)); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void multiKindNestedContextIncludesNameOnlyWhenPresent() { |
| 169 | + LDContext multi = LDContext.createMulti( |
| 170 | + LDContext.builder("user-key").name("Alice").build(), |
| 171 | + LDContext.builder(ContextKind.of("device"), "device-key").build()); |
| 172 | + Map<String, Object> result = LDContextEncoder.encode(multi); |
| 173 | + |
| 174 | + @SuppressWarnings("unchecked") |
| 175 | + Map<String, Object> userMap = (Map<String, Object>) result.get("user"); |
| 176 | + assertThat(userMap.get("name"), is((Object) "Alice")); |
| 177 | + |
| 178 | + @SuppressWarnings("unchecked") |
| 179 | + Map<String, Object> deviceMap = (Map<String, Object>) result.get("device"); |
| 180 | + assertThat(deviceMap.containsKey("name"), is(false)); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void customKindContextIsEncoded() { |
| 185 | + LDContext context = LDContext.builder(ContextKind.of("device"), "device-123").build(); |
| 186 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 187 | + assertThat(result.get("kind"), is((Object) "device")); |
| 188 | + assertThat(result.get("key"), is((Object) "device-123")); |
| 189 | + } |
| 190 | + |
| 191 | + @Test |
| 192 | + public void nestedObjectCustomAttributeIsDeeplyTraversable() { |
| 193 | + LDContext context = LDContext.builder("user-key") |
| 194 | + .set("meta", LDValue.buildObject() |
| 195 | + .put("level1", LDValue.buildObject().put("level2", "deep-value").build()) |
| 196 | + .build()) |
| 197 | + .build(); |
| 198 | + Map<String, Object> result = LDContextEncoder.encode(context); |
| 199 | + @SuppressWarnings("unchecked") |
| 200 | + Map<String, Object> meta = (Map<String, Object>) result.get("meta"); |
| 201 | + @SuppressWarnings("unchecked") |
| 202 | + Map<String, Object> level1 = (Map<String, Object>) meta.get("level1"); |
| 203 | + assertThat(level1.get("level2"), is((Object) "deep-value")); |
| 204 | + } |
| 205 | +} |
0 commit comments