diff --git a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/ConfigurationUtils.java b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/ConfigurationUtils.java index 94fb6d14587..e3f77b4a141 100644 --- a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/ConfigurationUtils.java +++ b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/ConfigurationUtils.java @@ -84,13 +84,7 @@ public final class ConfigurationUtils { if (value == null) { throw new IllegalArgumentException("required property [" + propertyName + "] is missing"); } - if (value instanceof List) { - @SuppressWarnings("unchecked") - List stringList = (List) value; - return stringList; - } else { - throw new IllegalArgumentException("property [" + propertyName + "] isn't a list, but of type [" + value.getClass().getName() + "]"); - } + return readStringList(propertyName, value); } /** @@ -103,6 +97,10 @@ public final class ConfigurationUtils { if (value == null) { return null; } + return readStringList(propertyName, value); + } + + private static List readStringList(String propertyName, Object value) { if (value instanceof List) { @SuppressWarnings("unchecked") List stringList = (List) value; @@ -117,54 +115,14 @@ public final class ConfigurationUtils { * * If the property value isn't of type map an {@link IllegalArgumentException} is thrown. */ - public static Map> readOptionalStringListMap(Map configuration, String propertyName) { + public static Map readOptionalMap(Map configuration, String propertyName) { Object value = configuration.remove(propertyName); if (value == null) { return null; } if (value instanceof Map) { @SuppressWarnings("unchecked") - Map> stringList = (Map>) value; - return stringList; - } else { - throw new IllegalArgumentException("property [" + propertyName + "] isn't a map, but of type [" + value.getClass().getName() + "]"); - } - } - - /** - * Returns and removes the specified property of type map from the specified configuration map. - * - * If the property value isn't of type map an {@link IllegalArgumentException} is thrown. - */ - public static Map readOptionalStringMap(Map configuration, String propertyName) { - Object value = configuration.remove(propertyName); - - if (value == null) { - return null; - } - - if (value instanceof Map) { - Map map = (Map) value; - return map; - } else { - throw new IllegalArgumentException("property [" + propertyName + "] isn't a map, but of type [" + value.getClass().getName() + "]"); - } - } - - /** - * Returns and removes the specified property of type map from the specified configuration map. - * - * If the property value isn't of type map an {@link IllegalArgumentException} is thrown. - */ - public static Map readOptionalObjectMap(Map configuration, String propertyName) { - Object value = configuration.remove(propertyName); - - if (value == null) { - return null; - } - - if (value instanceof Map) { - Map map = (Map) value; + Map map = (Map) value; return map; } else { throw new IllegalArgumentException("property [" + propertyName + "] isn't a map, but of type [" + value.getClass().getName() + "]"); diff --git a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/GsubExpression.java b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/GsubExpression.java new file mode 100644 index 00000000000..c62577d1cc8 --- /dev/null +++ b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/GsubExpression.java @@ -0,0 +1,50 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.ingest.processor.mutate; + +import java.util.regex.Pattern; + +/** + * Represents a gsub expression containing the field name, the pattern to look for and its string replacement. + */ +public class GsubExpression { + + private final String fieldName; + private final Pattern pattern; + private final String replacement; + + public GsubExpression(String fieldName, Pattern pattern, String replacement) { + this.fieldName = fieldName; + this.pattern = pattern; + this.replacement = replacement; + } + + public String getFieldName() { + return fieldName; + } + + public Pattern getPattern() { + return pattern; + } + + public String getReplacement() { + return replacement; + } +} diff --git a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/MutateProcessor.java b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/MutateProcessor.java index ab85a9dbd04..d09454b1016 100644 --- a/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/MutateProcessor.java +++ b/plugins/ingest/src/main/java/org/elasticsearch/ingest/processor/mutate/MutateProcessor.java @@ -20,7 +20,6 @@ package org.elasticsearch.ingest.processor.mutate; import org.elasticsearch.common.Booleans; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.ingest.Data; import org.elasticsearch.ingest.processor.ConfigurationUtils; import org.elasticsearch.ingest.processor.Processor; @@ -39,23 +38,16 @@ public final class MutateProcessor implements Processor { private final Map rename; private final Map convert; private final Map split; - private final Map> gsub; + private final List gsub; private final Map join; private final List remove; private final List trim; private final List uppercase; private final List lowercase; - public MutateProcessor(Map update, - Map rename, - Map convert, - Map split, - Map> gsub, - Map join, - List remove, - List trim, - List uppercase, - List lowercase) { + public MutateProcessor(Map update, Map rename, Map convert, + Map split, List gsub, Map join, + List remove, List trim, List uppercase, List lowercase) { this.update = update; this.rename = rename; this.convert = convert; @@ -68,43 +60,43 @@ public final class MutateProcessor implements Processor { this.lowercase = lowercase; } - public Map getUpdate() { + Map getUpdate() { return update; } - public Map getRename() { + Map getRename() { return rename; } - public Map getConvert() { + Map getConvert() { return convert; } - public Map getSplit() { + Map getSplit() { return split; } - public Map> getGsub() { + List getGsub() { return gsub; } - public Map getJoin() { + Map getJoin() { return join; } - public List getRemove() { + List getRemove() { return remove; } - public List getTrim() { + List getTrim() { return trim; } - public List getUppercase() { + List getUppercase() { return uppercase; } - public List getLowercase() { + List getLowercase() { return lowercase; } @@ -213,16 +205,14 @@ public final class MutateProcessor implements Processor { } private void doGsub(Data data) { - for (Map.Entry> entry : gsub.entrySet()) { - String fieldName = entry.getKey(); - Tuple matchAndReplace = entry.getValue(); - String oldVal = data.getProperty(fieldName); + for (GsubExpression gsubExpression : gsub) { + String oldVal = data.getProperty(gsubExpression.getFieldName()); if (oldVal == null) { - throw new IllegalArgumentException("Field \"" + fieldName + "\" is null, cannot match pattern."); + throw new IllegalArgumentException("Field \"" + gsubExpression.getFieldName() + "\" is null, cannot match pattern."); } - Matcher matcher = matchAndReplace.v1().matcher(oldVal); - String newVal = matcher.replaceAll(matchAndReplace.v2()); - data.addField(entry.getKey(), newVal); + Matcher matcher = gsubExpression.getPattern().matcher(oldVal); + String newVal = matcher.replaceAll(gsubExpression.getReplacement()); + data.addField(gsubExpression.getFieldName(), newVal); } } @@ -285,28 +275,28 @@ public final class MutateProcessor implements Processor { public static final class Factory implements Processor.Factory { @Override public MutateProcessor create(Map config) throws IOException { - Map update = ConfigurationUtils.readOptionalObjectMap(config, "update"); - Map rename = ConfigurationUtils.readOptionalStringMap(config, "rename"); - Map convert = ConfigurationUtils.readOptionalStringMap(config, "convert"); - Map split = ConfigurationUtils.readOptionalStringMap(config, "split"); - Map> gsubConfig = ConfigurationUtils.readOptionalStringListMap(config, "gsub"); - Map join = ConfigurationUtils.readOptionalStringMap(config, "join"); + Map update = ConfigurationUtils.readOptionalMap(config, "update"); + Map rename = ConfigurationUtils.readOptionalMap(config, "rename"); + Map convert = ConfigurationUtils.readOptionalMap(config, "convert"); + Map split = ConfigurationUtils.readOptionalMap(config, "split"); + Map> gsubConfig = ConfigurationUtils.readOptionalMap(config, "gsub"); + Map join = ConfigurationUtils.readOptionalMap(config, "join"); List remove = ConfigurationUtils.readOptionalStringList(config, "remove"); List trim = ConfigurationUtils.readOptionalStringList(config, "trim"); List uppercase = ConfigurationUtils.readOptionalStringList(config, "uppercase"); List lowercase = ConfigurationUtils.readOptionalStringList(config, "lowercase"); // pre-compile regex patterns - Map> gsub = null; + List gsubExpressions = null; if (gsubConfig != null) { - gsub = new HashMap<>(); + gsubExpressions = new ArrayList<>(); for (Map.Entry> entry : gsubConfig.entrySet()) { List searchAndReplace = entry.getValue(); if (searchAndReplace.size() != 2) { - throw new IllegalArgumentException("Invalid search and replace values (" + Arrays.toString(searchAndReplace.toArray()) + ") for field: " + entry.getKey()); + throw new IllegalArgumentException("Invalid search and replace values " + searchAndReplace + " for field: " + entry.getKey()); } Pattern searchPattern = Pattern.compile(searchAndReplace.get(0)); - gsub.put(entry.getKey(), new Tuple<>(searchPattern, searchAndReplace.get(1))); + gsubExpressions.add(new GsubExpression(entry.getKey(), searchPattern, searchAndReplace.get(1))); } } @@ -315,7 +305,7 @@ public final class MutateProcessor implements Processor { (rename == null) ? null : Collections.unmodifiableMap(rename), (convert == null) ? null : Collections.unmodifiableMap(convert), (split == null) ? null : Collections.unmodifiableMap(split), - (gsub == null) ? null : Collections.unmodifiableMap(gsub), + (gsubExpressions == null) ? null : Collections.unmodifiableList(gsubExpressions), (join == null) ? null : Collections.unmodifiableMap(join), (remove == null) ? null : Collections.unmodifiableList(remove), (trim == null) ? null : Collections.unmodifiableList(trim), diff --git a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorFactoryTests.java b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorFactoryTests.java index ccc3e223adf..017d0465563 100644 --- a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorFactoryTests.java +++ b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorFactoryTests.java @@ -19,30 +19,183 @@ package org.elasticsearch.ingest.processor.mutate; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.test.ESTestCase; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.regex.Pattern; import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; public class MutateProcessorFactoryTests extends ESTestCase { - public void testCreate() throws Exception { + public void testCreateUpdate() throws Exception { MutateProcessor.Factory factory = new MutateProcessor.Factory(); Map config = new HashMap<>(); Map update = new HashMap<>(); update.put("foo", 123); config.put("update", update); MutateProcessor processor = factory.create(config); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getTrim(), nullValue()); assertThat(processor.getUpdate(), equalTo(update)); } + public void testCreateRename() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + Map rename = new HashMap<>(); + rename.put("foo", "bar"); + config.put("rename", rename); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getTrim(), nullValue()); + assertThat(processor.getRename(), equalTo(rename)); + } + + public void testCreateRemove() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + List remove = Collections.singletonList("foo"); + config.put("remove", remove); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getTrim(), nullValue()); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getRemove(), equalTo(remove)); + } + + public void testCreateConvert() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + Map convert = new HashMap<>(); + convert.put("foo", "integer"); + config.put("convert", convert); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getTrim(), nullValue()); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getConvert(), equalTo(convert)); + } + + public void testCreateJoin() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + Map join = new HashMap<>(); + join.put("foo", "bar"); + config.put("join", join); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getTrim(), nullValue()); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getJoin(), equalTo(join)); + } + + public void testCreateSplit() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + Map split = new HashMap<>(); + split.put("foo", "bar"); + config.put("split", split); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getTrim(), nullValue()); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getSplit(), equalTo(split)); + } + + public void testCreateLowercase() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + List lowercase = Collections.singletonList("foo"); + config.put("lowercase", lowercase); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getTrim(), nullValue()); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getLowercase(), equalTo(lowercase)); + } + + public void testCreateUppercase() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + List uppercase = Collections.singletonList("foo"); + config.put("uppercase", uppercase); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getTrim(), nullValue()); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getUppercase(), equalTo(uppercase)); + } + + public void testCreateTrim() throws Exception { + MutateProcessor.Factory factory = new MutateProcessor.Factory(); + Map config = new HashMap<>(); + List trim = Collections.singletonList("foo"); + config.put("trim", trim); + MutateProcessor processor = factory.create(config); + assertThat(processor.getUpdate(), nullValue()); + assertThat(processor.getGsub(), nullValue()); + assertThat(processor.getConvert(), nullValue()); + assertThat(processor.getJoin(), nullValue()); + assertThat(processor.getRemove(), nullValue()); + assertThat(processor.getUppercase(), nullValue()); + assertThat(processor.getSplit(), nullValue()); + assertThat(processor.getLowercase(), nullValue()); + assertThat(processor.getRename(), nullValue()); + assertThat(processor.getTrim(), equalTo(trim)); + } + public void testCreateGsubPattern() throws Exception { MutateProcessor.Factory factory = new MutateProcessor.Factory(); Map config = new HashMap<>(); @@ -50,32 +203,26 @@ public class MutateProcessorFactoryTests extends ESTestCase { gsub.put("foo", Arrays.asList("\\s.*e\\s", "")); config.put("gsub", gsub); - Map> compiledGsub = new HashMap<>(); - Pattern searchPattern = Pattern.compile("\\s.*e\\s"); - compiledGsub.put("foo", new Tuple<>(searchPattern, "")); - MutateProcessor processor = factory.create(config); - for (Map.Entry> entry : compiledGsub.entrySet()) { - Tuple actualSearchAndReplace = processor.getGsub().get(entry.getKey()); - assertThat(actualSearchAndReplace, notNullValue()); - assertThat(actualSearchAndReplace.v1().pattern(), equalTo(entry.getValue().v1().pattern())); - assertThat(actualSearchAndReplace.v2(), equalTo(entry.getValue().v2())); - } + assertThat(processor.getGsub().size(), equalTo(1)); + GsubExpression gsubExpression = processor.getGsub().get(0); + assertThat(gsubExpression.getFieldName(), equalTo("foo")); + assertThat(gsubExpression.getPattern().pattern(), equalTo(Pattern.compile("\\s.*e\\s").pattern())); + assertThat(gsubExpression.getReplacement(), equalTo("")); } - public void testCreateGsubPattern_InvalidFormat() throws Exception { + public void testCreateGsubPatternInvalidFormat() throws Exception { MutateProcessor.Factory factory = new MutateProcessor.Factory(); Map config = new HashMap<>(); Map> gsub = new HashMap<>(); - gsub.put("foo", Arrays.asList("only_one")); + gsub.put("foo", Collections.singletonList("only_one")); config.put("gsub", gsub); try { factory.create(config); - fail(); + fail("processor creation should have failed"); } catch (IllegalArgumentException e) { - assertThat(e.getMessage(), equalTo("Invalid search and replace values ([only_one]) for field: foo")); + assertThat(e.getMessage(), equalTo("Invalid search and replace values [only_one] for field: foo")); } } - } diff --git a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorTests.java b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorTests.java index 55066c4e2e7..f5420eb30ee 100644 --- a/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorTests.java +++ b/plugins/ingest/src/test/java/org/elasticsearch/ingest/processor/mutate/MutateProcessorTests.java @@ -25,10 +25,8 @@ import org.elasticsearch.test.ESTestCase; import org.junit.Before; import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; +import java.util.regex.Pattern; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -36,9 +34,7 @@ import static org.hamcrest.Matchers.nullValue; public class MutateProcessorTests extends ESTestCase { - private static final MutateProcessor.Factory FACTORY = new MutateProcessor.Factory(); private Data data; - private Map config; @Before public void setData() { @@ -54,25 +50,23 @@ public class MutateProcessorTests extends ESTestCase { document.put("fizz", fizz); data = new Data("index", "type", "id", document); - config = new HashMap<>(); } public void testUpdate() throws IOException { Map update = new HashMap<>(); update.put("foo", 123); - config.put("update", update); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(update, null, null, null, null, null, null, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("foo"), equalTo(123)); } public void testRename() throws IOException { Map rename = new HashMap<>(); rename.put("foo", "bar"); - config.put("rename", rename); - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, rename, null, null, null, null, null, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("bar"), equalTo("bar")); assertThat(data.containsProperty("foo"), is(false)); } @@ -80,66 +74,56 @@ public class MutateProcessorTests extends ESTestCase { public void testConvert() throws IOException { Map convert = new HashMap<>(); convert.put("num", "integer"); - config.put("convert", convert); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, null, convert, null, null, null, null, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("num"), equalTo(64)); } - public void testConvert_NullField() throws IOException { + public void testConvertNullField() throws IOException { Map convert = new HashMap<>(); convert.put("null", "integer"); - config.put("convert", convert); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, null, convert, null, null, null, null, null, null, null); try { processor.execute(data); - fail(); + fail("processor execute should have failed"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("Field \"null\" is null, cannot be converted to a/an integer")); } } - public void testConvert_List() throws IOException { + public void testConvertList() throws IOException { Map convert = new HashMap<>(); convert.put("arr", "integer"); - config.put("convert", convert); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, null, convert, null, null, null, null, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("arr"), equalTo(Arrays.asList(1, 2, 3))); } public void testSplit() throws IOException { - HashMap split = new HashMap<>(); + Map split = new HashMap<>(); split.put("ip", "\\."); - config.put("split", split); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, null, null, split, null, null, null, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("ip"), equalTo(Arrays.asList("127", "0", "0", "1"))); } public void testGsub() throws IOException { - HashMap> gsub = new HashMap<>(); - gsub.put("ip", Arrays.asList("\\.", "-")); - config.put("gsub", gsub); - - Processor processor = FACTORY.create(config); + List gsubExpressions = Collections.singletonList(new GsubExpression("ip", Pattern.compile("\\."), "-")); + Processor processor = new MutateProcessor(null, null, null, null, gsubExpressions, null, null, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("ip"), equalTo("127-0-0-1")); } public void testGsub_NullValue() throws IOException { - HashMap> gsub = new HashMap<>(); - gsub.put("null_field", Arrays.asList("\\.", "-")); - config.put("gsub", gsub); - - Processor processor = FACTORY.create(config); + List gsubExpressions = Collections.singletonList(new GsubExpression("null_field", Pattern.compile("\\."), "-")); + Processor processor = new MutateProcessor(null, null, null, null, gsubExpressions, null, null, null, null, null); try { processor.execute(data); - fail(); + fail("processor execution should have failed"); } catch (IllegalArgumentException e) { assertThat(e.getMessage(), equalTo("Field \"null_field\" is null, cannot match pattern.")); } @@ -148,46 +132,43 @@ public class MutateProcessorTests extends ESTestCase { public void testJoin() throws IOException { HashMap join = new HashMap<>(); join.put("arr", "-"); - config.put("join", join); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, null, null, null, null, join, null, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("arr"), equalTo("1-2-3")); } public void testRemove() throws IOException { List remove = Arrays.asList("foo", "ip"); - config.put("remove", remove); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, null, null, null, null, null, remove, null, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(5)); assertThat(data.getProperty("foo"), nullValue()); assertThat(data.getProperty("ip"), nullValue()); } public void testTrim() throws IOException { List trim = Arrays.asList("to_strip", "foo"); - config.put("trim", trim); - - Processor processor = FACTORY.create(config); + Processor processor = new MutateProcessor(null, null, null, null, null, null, null, trim, null, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("foo"), equalTo("bar")); assertThat(data.getProperty("to_strip"), equalTo("clean")); } public void testUppercase() throws IOException { - List uppercase = Arrays.asList("foo"); - config.put("uppercase", uppercase); - Processor processor = FACTORY.create(config); + List uppercase = Collections.singletonList("foo"); + Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, uppercase, null); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("foo"), equalTo("BAR")); } public void testLowercase() throws IOException { - List lowercase = Arrays.asList("alpha"); - config.put("lowercase", lowercase); - Processor processor = FACTORY.create(config); + List lowercase = Collections.singletonList("alpha"); + Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, null, lowercase); processor.execute(data); + assertThat(data.getDocument().size(), equalTo(7)); assertThat(data.getProperty("alpha"), equalTo("abcd")); } }