|
22 | 22 | import java.util.List; |
23 | 23 | import java.util.Map; |
24 | 24 | import java.util.UUID; |
| 25 | +import java.util.regex.Pattern; |
25 | 26 |
|
26 | 27 | import static org.hamcrest.CoreMatchers.is; |
27 | 28 | import static org.hamcrest.CoreMatchers.equalTo; |
@@ -138,6 +139,129 @@ public void testNestedSelectEntriesProcessor() { |
138 | 139 | assertThat(editedRecords.get(0).getData().get("nested/nested2/key2", String.class), equalTo(value2)); |
139 | 140 | } |
140 | 141 |
|
| 142 | + @Test |
| 143 | + public void testSelectEntriesProcessorWithIncludeKeysRegex() { |
| 144 | + when(mockConfig.getIncludeKeys()).thenReturn(null); |
| 145 | + when(mockConfig.getIncludeKeysRegex()).thenReturn(List.of(Pattern.compile("include.*"), Pattern.compile("other.*"))); |
| 146 | + when(mockConfig.getSelectWhen()).thenReturn(null); |
| 147 | + final String value1 = UUID.randomUUID().toString(); |
| 148 | + final String value2 = UUID.randomUUID().toString(); |
| 149 | + final SelectEntriesProcessor processor = createObjectUnderTest(); |
| 150 | + final Record<Event> record = getEvent("thisisamessage"); |
| 151 | + record.getData().put("include-key", value1); |
| 152 | + record.getData().put("include-key2", value2); |
| 153 | + record.getData().put("other-key1", value2); |
| 154 | + record.getData().put("exclude-key", value1); |
| 155 | + record.getData().put("exclude-key2", value2); |
| 156 | + final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record)); |
| 157 | + assertThat(editedRecords.get(0).getData().containsKey("include-key"), is(true)); |
| 158 | + assertThat(editedRecords.get(0).getData().containsKey("include-key2"), is(true)); |
| 159 | + assertThat(editedRecords.get(0).getData().containsKey("other-key1"), is(true)); |
| 160 | + assertThat(editedRecords.get(0).getData().containsKey("message"), is(false)); |
| 161 | + assertThat(editedRecords.get(0).getData().containsKey("exclude-key"), is(false)); |
| 162 | + assertThat(editedRecords.get(0).getData().containsKey("exclude-key2"), is(false)); |
| 163 | + |
| 164 | + assertThat(editedRecords.get(0).getData().get("include-key", String.class), equalTo(value1)); |
| 165 | + assertThat(editedRecords.get(0).getData().get("include-key2", String.class), equalTo(value2)); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testSelectEntriesProcessorWithIncludeKeys_and_IncludeKeysRegex() { |
| 170 | + when(mockConfig.getIncludeKeys()).thenReturn(List.of("exclude-key", "message")); |
| 171 | + when(mockConfig.getIncludeKeysRegex()).thenReturn(List.of(Pattern.compile("include.*"))); |
| 172 | + when(mockConfig.getSelectWhen()).thenReturn(null); |
| 173 | + final String value1 = UUID.randomUUID().toString(); |
| 174 | + final String value2 = UUID.randomUUID().toString(); |
| 175 | + final SelectEntriesProcessor processor = createObjectUnderTest(); |
| 176 | + final Record<Event> record = getEvent("thisisamessage"); |
| 177 | + record.getData().put("include-key", value1); |
| 178 | + record.getData().put("include-key2", value2); |
| 179 | + record.getData().put("exclude-key", value1); |
| 180 | + record.getData().put("exclude-key2", value2); |
| 181 | + final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record)); |
| 182 | + assertThat(editedRecords.get(0).getData().containsKey("include-key"), is(true)); |
| 183 | + assertThat(editedRecords.get(0).getData().containsKey("include-key2"), is(true)); |
| 184 | + assertThat(editedRecords.get(0).getData().containsKey("message"), is(true)); |
| 185 | + assertThat(editedRecords.get(0).getData().containsKey("exclude-key"), is(true)); |
| 186 | + assertThat(editedRecords.get(0).getData().containsKey("exclude-key2"), is(false)); |
| 187 | + |
| 188 | + assertThat(editedRecords.get(0).getData().get("include-key", String.class), equalTo(value1)); |
| 189 | + assertThat(editedRecords.get(0).getData().get("include-key2", String.class), equalTo(value2)); |
| 190 | + assertThat(editedRecords.get(0).getData().get("exclude-key", String.class), equalTo(value1)); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testSelectEntriesProcessorWithIncludeKeysRegexPointerThatDoesNotExist() { |
| 195 | + when(mockConfig.getIncludeKeys()).thenReturn(List.of("message")); |
| 196 | + when(mockConfig.getIncludeKeysRegex()).thenReturn(List.of(Pattern.compile("include.*"))); |
| 197 | + when(mockConfig.getSelectWhen()).thenReturn(null); |
| 198 | + when(mockConfig.getIncludeKeysRegexPointer()).thenReturn("/nested"); |
| 199 | + final String value1 = UUID.randomUUID().toString(); |
| 200 | + final String value2 = UUID.randomUUID().toString(); |
| 201 | + final SelectEntriesProcessor processor = createObjectUnderTest(); |
| 202 | + final Record<Event> record = getEvent("thisisamessage"); |
| 203 | + record.getData().put("include-key", value1); |
| 204 | + record.getData().put("include-key2", value2); |
| 205 | + record.getData().put("exclude-key", value1); |
| 206 | + record.getData().put("exclude-key2", value2); |
| 207 | + final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record)); |
| 208 | + assertThat(editedRecords.get(0).getData().containsKey("include-key"), is(false)); |
| 209 | + assertThat(editedRecords.get(0).getData().containsKey("include-key2"), is(false)); |
| 210 | + assertThat(editedRecords.get(0).getData().containsKey("message"), is(true)); |
| 211 | + assertThat(editedRecords.get(0).getData().containsKey("exclude-key"), is(false)); |
| 212 | + assertThat(editedRecords.get(0).getData().containsKey("exclude-key2"), is(false)); |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + public void testSelectEntriesProcessorWithIncludeKeysRegexPointerThatDoesExist() { |
| 217 | + when(mockConfig.getIncludeKeys()).thenReturn(Collections.emptyList()); |
| 218 | + when(mockConfig.getIncludeKeysRegex()).thenReturn(List.of(Pattern.compile("include.*"))); |
| 219 | + when(mockConfig.getSelectWhen()).thenReturn(null); |
| 220 | + when(mockConfig.getIncludeKeysRegexPointer()).thenReturn("/nested"); |
| 221 | + final String value1 = UUID.randomUUID().toString(); |
| 222 | + final String value2 = UUID.randomUUID().toString(); |
| 223 | + final SelectEntriesProcessor processor = createObjectUnderTest(); |
| 224 | + final Record<Event> record = getEvent("thisisamessage"); |
| 225 | + final Map<String, Object> nestedData = new HashMap<>(); |
| 226 | + nestedData.put("include-key", value1); |
| 227 | + nestedData.put("include-key2", value2); |
| 228 | + nestedData.put("exclude-key", value1); |
| 229 | + nestedData.put("exclude-key2", value2); |
| 230 | + |
| 231 | + record.getData().put("/nested", nestedData); |
| 232 | + final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record)); |
| 233 | + assertThat(editedRecords.get(0).getData().containsKey("/nested/include-key"), is(true)); |
| 234 | + assertThat(editedRecords.get(0).getData().containsKey("/nested/include-key2"), is(true)); |
| 235 | + assertThat(editedRecords.get(0).getData().containsKey("message"), is(false)); |
| 236 | + assertThat(editedRecords.get(0).getData().containsKey("/nested/exclude-key"), is(false)); |
| 237 | + assertThat(editedRecords.get(0).getData().containsKey("/nested/exclude-key2"), is(false)); |
| 238 | + } |
| 239 | + |
| 240 | + @Test |
| 241 | + public void testSelectEntriesProcessorWithIncludeKeysRegexPointerThatDoesExist_and_include_keys() { |
| 242 | + when(mockConfig.getIncludeKeys()).thenReturn(List.of("message", "/nested/exclude-key")); |
| 243 | + when(mockConfig.getIncludeKeysRegex()).thenReturn(List.of(Pattern.compile("include.*"))); |
| 244 | + when(mockConfig.getSelectWhen()).thenReturn(null); |
| 245 | + when(mockConfig.getIncludeKeysRegexPointer()).thenReturn("/nested"); |
| 246 | + final String value1 = UUID.randomUUID().toString(); |
| 247 | + final String value2 = UUID.randomUUID().toString(); |
| 248 | + final SelectEntriesProcessor processor = createObjectUnderTest(); |
| 249 | + final Record<Event> record = getEvent("thisisamessage"); |
| 250 | + final Map<String, Object> nestedData = new HashMap<>(); |
| 251 | + nestedData.put("include-key", value1); |
| 252 | + nestedData.put("include-key2", value2); |
| 253 | + nestedData.put("exclude-key", value1); |
| 254 | + nestedData.put("exclude-key2", value2); |
| 255 | + |
| 256 | + record.getData().put("/nested", nestedData); |
| 257 | + final List<Record<Event>> editedRecords = (List<Record<Event>>) processor.doExecute(Collections.singletonList(record)); |
| 258 | + assertThat(editedRecords.get(0).getData().containsKey("/nested/include-key"), is(true)); |
| 259 | + assertThat(editedRecords.get(0).getData().containsKey("/nested/include-key2"), is(true)); |
| 260 | + assertThat(editedRecords.get(0).getData().containsKey("message"), is(true)); |
| 261 | + assertThat(editedRecords.get(0).getData().containsKey("/nested/exclude-key"), is(true)); |
| 262 | + assertThat(editedRecords.get(0).getData().containsKey("exclude-key2"), is(false)); |
| 263 | + } |
| 264 | + |
141 | 265 |
|
142 | 266 | private SelectEntriesProcessor createObjectUnderTest() { |
143 | 267 | return new SelectEntriesProcessor(pluginMetrics, mockConfig, expressionEvaluator); |
|
0 commit comments