|
20 | 20 |
|
21 | 21 | import com.fasterxml.jackson.databind.JsonNode; |
22 | 22 | import java.io.IOException; |
| 23 | +import java.util.Arrays; |
23 | 24 | import java.util.Collections; |
24 | 25 | import java.util.HashMap; |
25 | 26 | import java.util.Map; |
@@ -130,4 +131,84 @@ public void testJsonString() { |
130 | 131 | Assert.assertTrue(output.contains(VALUE)); |
131 | 132 | Assert.assertFalse(output.contains(SECRET)); |
132 | 133 | } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testArrayOfObjects() { |
| 137 | + Map<String, Object> item1 = new HashMap<>(); |
| 138 | + item1.put("name", "item1"); |
| 139 | + item1.put("password", "SECRET"); |
| 140 | + |
| 141 | + Map<String, Object> item2 = new HashMap<>(); |
| 142 | + item2.put("name", "item2"); |
| 143 | + item2.put("secret", "SECRET"); |
| 144 | + |
| 145 | + Map<String, Object> root = new HashMap<>(); |
| 146 | + root.put("items", Arrays.asList(item1, item2)); |
| 147 | + |
| 148 | + String output = _obfuscator.toJsonString(root); |
| 149 | + Assert.assertTrue(output.contains("item1")); |
| 150 | + Assert.assertTrue(output.contains("item2")); |
| 151 | + Assert.assertFalse(output.contains(SECRET)); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testNestedArrayOfObjects() { |
| 156 | + Map<String, Object> innerItem = new HashMap<>(); |
| 157 | + innerItem.put("token", "SECRET"); |
| 158 | + innerItem.put("host", "localhost"); |
| 159 | + |
| 160 | + Map<String, Object> outerItem = new HashMap<>(); |
| 161 | + outerItem.put("connections", Collections.singletonList(innerItem)); |
| 162 | + outerItem.put("label", "cluster-1"); |
| 163 | + |
| 164 | + Map<String, Object> root = new HashMap<>(); |
| 165 | + root.put("clusters", Collections.singletonList(outerItem)); |
| 166 | + |
| 167 | + String output = _obfuscator.toJsonString(root); |
| 168 | + Assert.assertTrue(output.contains("localhost")); |
| 169 | + Assert.assertTrue(output.contains("cluster-1")); |
| 170 | + Assert.assertFalse(output.contains(SECRET)); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testArrayOfObjectsViaJsonString() { |
| 175 | + String json = "{\"items\":[{\"user\":\"admin\",\"password\":\"SECRET\"}," |
| 176 | + + "{\"user\":\"reader\",\"token\":\"SECRET\"}]}"; |
| 177 | + String output = _obfuscator.toJsonString(json); |
| 178 | + Assert.assertTrue(output.contains("admin")); |
| 179 | + Assert.assertTrue(output.contains("reader")); |
| 180 | + Assert.assertFalse(output.contains(SECRET)); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void testTopLevelArray() |
| 185 | + throws IOException { |
| 186 | + JsonNode node = JsonUtils.stringToJsonNode("[{\"password\":\"SECRET\",\"host\":\"db1\"},{\"token\":\"SECRET\"," |
| 187 | + + "\"host\":\"db2\"}]"); |
| 188 | + String output = _obfuscator.toJsonString(node); |
| 189 | + Assert.assertTrue(output.contains("db1")); |
| 190 | + Assert.assertTrue(output.contains("db2")); |
| 191 | + Assert.assertFalse(output.contains(SECRET)); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void testArrayWithMixedContent() { |
| 196 | + Map<String, Object> root = new HashMap<>(); |
| 197 | + root.put("tags", Arrays.asList("public", "production")); |
| 198 | + root.put("credentials", Arrays.asList( |
| 199 | + createEntry("apiKey", "SECRET"), |
| 200 | + createEntry("apiKey", "SECRET") |
| 201 | + )); |
| 202 | + |
| 203 | + String output = _obfuscator.toJsonString(root); |
| 204 | + Assert.assertTrue(output.contains("public")); |
| 205 | + Assert.assertTrue(output.contains("production")); |
| 206 | + Assert.assertFalse(output.contains(SECRET)); |
| 207 | + } |
| 208 | + |
| 209 | + private static Map<String, Object> createEntry(String key, String value) { |
| 210 | + Map<String, Object> map = new HashMap<>(); |
| 211 | + map.put(key, value); |
| 212 | + return map; |
| 213 | + } |
133 | 214 | } |
0 commit comments