|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.ingest.common; |
| 10 | + |
| 11 | +import org.opensearch.OpenSearchException; |
| 12 | +import org.opensearch.OpenSearchParseException; |
| 13 | +import org.opensearch.test.OpenSearchTestCase; |
| 14 | +import org.junit.Before; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 22 | + |
| 23 | +public class RemoveByPatternProcessorFactoryTests extends OpenSearchTestCase { |
| 24 | + |
| 25 | + private RemoveByPatternProcessor.Factory factory; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void init() { |
| 29 | + factory = new RemoveByPatternProcessor.Factory(); |
| 30 | + } |
| 31 | + |
| 32 | + public void testCreateFieldPatterns() throws Exception { |
| 33 | + Map<String, Object> config = new HashMap<>(); |
| 34 | + config.put("field_pattern", "field1*"); |
| 35 | + String processorTag = randomAlphaOfLength(10); |
| 36 | + RemoveByPatternProcessor removeByPatternProcessor = factory.create(null, processorTag, null, config); |
| 37 | + assertThat(removeByPatternProcessor.getTag(), equalTo(processorTag)); |
| 38 | + assertThat(removeByPatternProcessor.getFieldPatterns().get(0), equalTo("field1*")); |
| 39 | + |
| 40 | + Map<String, Object> config2 = new HashMap<>(); |
| 41 | + config2.put("field_pattern", List.of("field1*", "field2*")); |
| 42 | + removeByPatternProcessor = factory.create(null, processorTag, null, config2); |
| 43 | + assertThat(removeByPatternProcessor.getTag(), equalTo(processorTag)); |
| 44 | + assertThat(removeByPatternProcessor.getFieldPatterns().get(0), equalTo("field1*")); |
| 45 | + assertThat(removeByPatternProcessor.getFieldPatterns().get(1), equalTo("field2*")); |
| 46 | + |
| 47 | + Map<String, Object> config3 = new HashMap<>(); |
| 48 | + List<String> patterns = Arrays.asList("foo*", "*", " ", ",", "#", ":", "_"); |
| 49 | + config3.put("field_pattern", patterns); |
| 50 | + Exception exception = expectThrows(OpenSearchParseException.class, () -> factory.create(null, processorTag, null, config3)); |
| 51 | + assertThat( |
| 52 | + exception.getMessage(), |
| 53 | + equalTo( |
| 54 | + "[field_pattern] Validation Failed: " |
| 55 | + + "1: field_pattern [ ] must not contain the following characters [ , \", *, \\, <, |, ,, >, /, ?];" |
| 56 | + + "2: field_pattern [,] must not contain the following characters [ , \", *, \\, <, |, ,, >, /, ?];" |
| 57 | + + "3: field_pattern [#] must not contain a '#';" |
| 58 | + + "4: field_pattern [:] must not contain a ':';" |
| 59 | + + "5: field_pattern [_] must not start with '_';" |
| 60 | + ) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public void testCreateExcludeFieldPatterns() throws Exception { |
| 65 | + Map<String, Object> config = new HashMap<>(); |
| 66 | + config.put("exclude_field_pattern", "field1*"); |
| 67 | + String processorTag = randomAlphaOfLength(10); |
| 68 | + RemoveByPatternProcessor removeByPatternProcessor = factory.create(null, processorTag, null, config); |
| 69 | + assertThat(removeByPatternProcessor.getTag(), equalTo(processorTag)); |
| 70 | + assertThat(removeByPatternProcessor.getExcludeFieldPatterns().get(0), equalTo("field1*")); |
| 71 | + |
| 72 | + Map<String, Object> config2 = new HashMap<>(); |
| 73 | + config2.put("exclude_field_pattern", List.of("field1*", "field2*")); |
| 74 | + removeByPatternProcessor = factory.create(null, processorTag, null, config2); |
| 75 | + assertThat(removeByPatternProcessor.getTag(), equalTo(processorTag)); |
| 76 | + assertThat(removeByPatternProcessor.getExcludeFieldPatterns().get(0), equalTo("field1*")); |
| 77 | + assertThat(removeByPatternProcessor.getExcludeFieldPatterns().get(1), equalTo("field2*")); |
| 78 | + |
| 79 | + Map<String, Object> config3 = new HashMap<>(); |
| 80 | + List<String> patterns = Arrays.asList("foo*", "*", " ", ",", "#", ":", "_"); |
| 81 | + config3.put("exclude_field_pattern", patterns); |
| 82 | + Exception exception = expectThrows(OpenSearchParseException.class, () -> factory.create(null, processorTag, null, config3)); |
| 83 | + assertThat( |
| 84 | + exception.getMessage(), |
| 85 | + equalTo( |
| 86 | + "[exclude_field_pattern] Validation Failed: " |
| 87 | + + "1: exclude_field_pattern [ ] must not contain the following characters [ , \", *, \\, <, |, ,, >, /, ?];" |
| 88 | + + "2: exclude_field_pattern [,] must not contain the following characters [ , \", *, \\, <, |, ,, >, /, ?];" |
| 89 | + + "3: exclude_field_pattern [#] must not contain a '#';" |
| 90 | + + "4: exclude_field_pattern [:] must not contain a ':';" |
| 91 | + + "5: exclude_field_pattern [_] must not start with '_';" |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + public void testCreatePatternsFailed() throws Exception { |
| 97 | + Map<String, Object> config = new HashMap<>(); |
| 98 | + config.put("field_pattern", List.of("foo*")); |
| 99 | + config.put("exclude_field_pattern", List.of("bar*")); |
| 100 | + String processorTag = randomAlphaOfLength(10); |
| 101 | + OpenSearchException exception = expectThrows( |
| 102 | + OpenSearchParseException.class, |
| 103 | + () -> factory.create(null, processorTag, null, config) |
| 104 | + ); |
| 105 | + assertThat(exception.getMessage(), equalTo("[field_pattern] either field_pattern or exclude_field_pattern must be set")); |
| 106 | + |
| 107 | + Map<String, Object> config2 = new HashMap<>(); |
| 108 | + config2.put("field_pattern", null); |
| 109 | + config2.put("exclude_field_pattern", null); |
| 110 | + |
| 111 | + exception = expectThrows(OpenSearchParseException.class, () -> factory.create(null, processorTag, null, config2)); |
| 112 | + assertThat(exception.getMessage(), equalTo("[field_pattern] either field_pattern or exclude_field_pattern must be set")); |
| 113 | + } |
| 114 | +} |
0 commit comments