|
21 | 21 | import com.google.common.io.Resources; |
22 | 22 | import org.graylog2.lookup.AllowedAuxiliaryPathChecker; |
23 | 23 | import org.graylog2.plugin.lookup.LookupCachePurge; |
| 24 | +import org.graylog2.plugin.lookup.LookupPreview; |
24 | 25 | import org.graylog2.plugin.lookup.LookupResult; |
25 | 26 | import org.junit.Rule; |
26 | 27 | import org.junit.Test; |
@@ -236,6 +237,30 @@ public void testConfigValidationFailsIfValueColumnMissingForNonMultiValueLookup( |
236 | 237 | .contains("Value column must be set unless multi-value lookup is enabled."); |
237 | 238 | } |
238 | 239 |
|
| 240 | + @Test |
| 241 | + public void testGetPreview() throws Exception { |
| 242 | + final Config config = baseConfig(); |
| 243 | + csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", config, new MetricRegistry(), pathChecker)); |
| 244 | + when(pathChecker.fileIsInAllowedPath(isA(Path.class))).thenReturn(true); |
| 245 | + csvFileDataAdapter.doStart(); |
| 246 | + |
| 247 | + LookupPreview previewResult = csvFileDataAdapter.getPreview(1); |
| 248 | + assertThat(previewResult).satisfies(r -> { |
| 249 | + assertThat(r.supported()).isTrue(); |
| 250 | + assertThat(r.total()).isEqualTo(2); |
| 251 | + assertThat(r.results()).hasSize(1); |
| 252 | + }); |
| 253 | + |
| 254 | + previewResult = csvFileDataAdapter.getPreview(3); |
| 255 | + assertThat(previewResult).satisfies(r -> { |
| 256 | + assertThat(r.supported()).isTrue(); |
| 257 | + assertThat(r.total()).isEqualTo(2); |
| 258 | + assertThat(r.results()).hasSize(2); |
| 259 | + assertThat(r.results().get("foo")).isEqualTo("23"); |
| 260 | + assertThat(r.results().get("bar")).isEqualTo("42"); |
| 261 | + }); |
| 262 | + } |
| 263 | + |
239 | 264 | @Test |
240 | 265 | public void testCIDRLookups() throws Exception { |
241 | 266 | final Config config = cidrLookupConfig(); |
@@ -292,6 +317,23 @@ public void testMultiValueLookups() throws Exception { |
292 | 317 | assertThat(csvFileDataAdapter.doGet("999999")).isEqualTo(LookupResult.empty()); |
293 | 318 | } |
294 | 319 |
|
| 320 | + @Test |
| 321 | + public void refreshMultiValueSuccess() throws Exception { |
| 322 | + csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", multiValueConfig(), new MetricRegistry(), pathChecker)); |
| 323 | + when(pathChecker.fileIsInAllowedPath(isA(Path.class))).thenReturn(true); |
| 324 | + csvFileDataAdapter.doStart(); |
| 325 | + csvFileDataAdapter.doRefresh(cachePurge); |
| 326 | + assertFalse(csvFileDataAdapter.getError().isPresent()); |
| 327 | + |
| 328 | + LookupResult result1 = csvFileDataAdapter.doGet("000001"); |
| 329 | + assertThat(result1.multiValue()) |
| 330 | + .containsEntry("first_name", "Adam") |
| 331 | + .containsEntry("last_name", "Alpha") |
| 332 | + .containsEntry("username", "aalpha") |
| 333 | + .containsEntry("phone", "123-4567") |
| 334 | + .containsEntry("address", "123 Sleepy Hollow Lane"); |
| 335 | + } |
| 336 | + |
295 | 337 | @Test |
296 | 338 | public void testMultiValueCIDRLookups() throws Exception { |
297 | 339 | csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", multiValueCidrConfig(), new MetricRegistry(), pathChecker)); |
@@ -325,6 +367,27 @@ public void testMultiValueCIDRLookups() throws Exception { |
325 | 367 | assertThat(csvFileDataAdapter.doGet("8.8.8.8")).isEqualTo(LookupResult.empty()); |
326 | 368 | } |
327 | 369 |
|
| 370 | + @Test |
| 371 | + public void testMultiValuePreview() throws Exception { |
| 372 | + csvFileDataAdapter = spy(new CSVFileDataAdapter("id", "name", multiValueConfig(), new MetricRegistry(), pathChecker)); |
| 373 | + when(pathChecker.fileIsInAllowedPath(isA(Path.class))).thenReturn(true); |
| 374 | + csvFileDataAdapter.doStart(); |
| 375 | + |
| 376 | + LookupPreview previewResult = csvFileDataAdapter.getPreview(3); |
| 377 | + assertThat(previewResult).satisfies(r -> { |
| 378 | + assertThat(r.supported()).isTrue(); |
| 379 | + assertThat(r.total()).isEqualTo(10); |
| 380 | + assertThat(r.results()).hasSize(3); |
| 381 | + }); |
| 382 | + |
| 383 | + previewResult = csvFileDataAdapter.getPreview(15); |
| 384 | + assertThat(previewResult).satisfies(r -> { |
| 385 | + assertThat(r.supported()).isTrue(); |
| 386 | + assertThat(r.total()).isEqualTo(10); |
| 387 | + assertThat(r.results()).hasSize(10); |
| 388 | + }); |
| 389 | + } |
| 390 | + |
328 | 391 | private Config baseConfig() { |
329 | 392 | return Config.builder() |
330 | 393 | .type(NAME) |
|
0 commit comments