|
8 | 8 |
|
9 | 9 | package org.elasticsearch.ingest.common;
|
10 | 10 |
|
| 11 | +import org.elasticsearch.ingest.IngestDocument; |
| 12 | +import org.elasticsearch.ingest.Processor; |
| 13 | +import org.elasticsearch.ingest.RandomDocumentPicks; |
| 14 | +import org.elasticsearch.test.ESTestCase; |
| 15 | + |
11 | 16 | import java.util.ArrayList;
|
12 | 17 | import java.util.Collections;
|
13 | 18 | import java.util.HashMap;
|
14 | 19 | import java.util.List;
|
15 | 20 | import java.util.Locale;
|
16 | 21 | import java.util.Map;
|
17 | 22 |
|
18 |
| -import org.elasticsearch.ingest.IngestDocument; |
19 |
| -import org.elasticsearch.ingest.Processor; |
20 |
| -import org.elasticsearch.ingest.RandomDocumentPicks; |
21 |
| -import org.elasticsearch.test.ESTestCase; |
22 |
| - |
23 | 23 | import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
|
24 | 24 | import static org.elasticsearch.ingest.common.ConvertProcessor.Type;
|
25 | 25 | import static org.hamcrest.Matchers.containsString;
|
@@ -307,6 +307,77 @@ public void testConvertBooleanError() throws Exception {
|
307 | 307 | }
|
308 | 308 | }
|
309 | 309 |
|
| 310 | + public void testConvertIpV4() throws Exception { |
| 311 | + // valid ipv4 address |
| 312 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); |
| 313 | + String fieldName = RandomDocumentPicks.randomFieldName(random()); |
| 314 | + String targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random())); |
| 315 | + String validIpV4 = "192.168.1.1"; |
| 316 | + ingestDocument.setFieldValue(fieldName, validIpV4); |
| 317 | + |
| 318 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false); |
| 319 | + processor.execute(ingestDocument); |
| 320 | + assertThat(ingestDocument.getFieldValue(targetField, String.class), equalTo(validIpV4)); |
| 321 | + |
| 322 | + // invalid ipv4 address |
| 323 | + IngestDocument ingestDocument2 = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); |
| 324 | + fieldName = RandomDocumentPicks.randomFieldName(random()); |
| 325 | + targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random())); |
| 326 | + String invalidIpV4 = "192.168.1.256"; |
| 327 | + ingestDocument2.setFieldValue(fieldName, invalidIpV4); |
| 328 | + |
| 329 | + Processor processor2 = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false); |
| 330 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor2.execute(ingestDocument2)); |
| 331 | + assertThat(e.getMessage(), containsString("'" + invalidIpV4 + "' is not an IP string literal.")); |
| 332 | + } |
| 333 | + |
| 334 | + public void testConvertIpV6() throws Exception { |
| 335 | + // valid ipv6 address |
| 336 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); |
| 337 | + String fieldName = RandomDocumentPicks.randomFieldName(random()); |
| 338 | + String targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random())); |
| 339 | + String validIpV6 = "2001:db8:3333:4444:5555:6666:7777:8888"; |
| 340 | + ingestDocument.setFieldValue(fieldName, validIpV6); |
| 341 | + |
| 342 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false); |
| 343 | + processor.execute(ingestDocument); |
| 344 | + assertThat(ingestDocument.getFieldValue(targetField, String.class), equalTo(validIpV6)); |
| 345 | + |
| 346 | + // invalid ipv6 address |
| 347 | + IngestDocument ingestDocument2 = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>()); |
| 348 | + fieldName = RandomDocumentPicks.randomFieldName(random()); |
| 349 | + targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random())); |
| 350 | + String invalidIpV6 = "2001:db8:3333:4444:5555:6666:7777:88888"; |
| 351 | + ingestDocument2.setFieldValue(fieldName, invalidIpV6); |
| 352 | + |
| 353 | + Processor processor2 = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false); |
| 354 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor2.execute(ingestDocument2)); |
| 355 | + assertThat(e.getMessage(), containsString("'" + invalidIpV6 + "' is not an IP string literal.")); |
| 356 | + } |
| 357 | + |
| 358 | + public void testConvertIpList() throws Exception { |
| 359 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 360 | + int numItems = randomIntBetween(1, 10); |
| 361 | + List<String> fieldValue = new ArrayList<>(); |
| 362 | + List<String> expectedList = new ArrayList<>(); |
| 363 | + for (int j = 0; j < numItems; j++) { |
| 364 | + String value; |
| 365 | + if (randomBoolean()) { |
| 366 | + // ipv4 value |
| 367 | + value = "192.168.1." + randomIntBetween(0, 255); |
| 368 | + } else { |
| 369 | + // ipv6 value |
| 370 | + value = "2001:db8:3333:4444:5555:6666:7777:" + Long.toString(randomLongBetween(0, 65535), 16); |
| 371 | + } |
| 372 | + fieldValue.add(value); |
| 373 | + expectedList.add(value); |
| 374 | + } |
| 375 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue); |
| 376 | + Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, fieldName, Type.IP, false); |
| 377 | + processor.execute(ingestDocument); |
| 378 | + assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList)); |
| 379 | + } |
| 380 | + |
310 | 381 | public void testConvertString() throws Exception {
|
311 | 382 | IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
312 | 383 | Object fieldValue;
|
|
0 commit comments