Mutate processor improvements
Remove code duplications from ConfigurationUtils Make sure that the mutate processor doesn't use Tuple as that would require to depend on core. Also make sure that the MutateProcessor tests don't end up testing the factory as well. Make processor getters package private as they are only needed in tests. Add new tests to MutateProcessorFactoryTests
This commit is contained in:
parent
2bde384825
commit
2b31f4fff7
|
@ -84,13 +84,7 @@ public final class ConfigurationUtils {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
throw new IllegalArgumentException("required property [" + propertyName + "] is missing");
|
throw new IllegalArgumentException("required property [" + propertyName + "] is missing");
|
||||||
}
|
}
|
||||||
if (value instanceof List) {
|
return readStringList(propertyName, value);
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
List<String> stringList = (List<String>) value;
|
|
||||||
return stringList;
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("property [" + propertyName + "] isn't a list, but of type [" + value.getClass().getName() + "]");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -103,6 +97,10 @@ public final class ConfigurationUtils {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return readStringList(propertyName, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> readStringList(String propertyName, Object value) {
|
||||||
if (value instanceof List) {
|
if (value instanceof List) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
List<String> stringList = (List<String>) value;
|
List<String> stringList = (List<String>) value;
|
||||||
|
@ -117,54 +115,14 @@ public final class ConfigurationUtils {
|
||||||
*
|
*
|
||||||
* If the property value isn't of type map an {@link IllegalArgumentException} is thrown.
|
* If the property value isn't of type map an {@link IllegalArgumentException} is thrown.
|
||||||
*/
|
*/
|
||||||
public static Map<String, List<String>> readOptionalStringListMap(Map<String, Object> configuration, String propertyName) {
|
public static <T> Map<String, T> readOptionalMap(Map<String, Object> configuration, String propertyName) {
|
||||||
Object value = configuration.remove(propertyName);
|
Object value = configuration.remove(propertyName);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (value instanceof Map) {
|
if (value instanceof Map) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, List<String>> stringList = (Map<String, List<String>>) value;
|
Map<String, T> map = (Map<String, T>) 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<String, String> readOptionalStringMap(Map<String, Object> configuration, String propertyName) {
|
|
||||||
Object value = configuration.remove(propertyName);
|
|
||||||
|
|
||||||
if (value == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value instanceof Map) {
|
|
||||||
Map<String, String> map = (Map<String, String>) 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<String, Object> readOptionalObjectMap(Map<String, Object> configuration, String propertyName) {
|
|
||||||
Object value = configuration.remove(propertyName);
|
|
||||||
|
|
||||||
if (value == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (value instanceof Map) {
|
|
||||||
Map<String, Object> map = (Map<String, Object>) value;
|
|
||||||
return map;
|
return map;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("property [" + propertyName + "] isn't a map, but of type [" + value.getClass().getName() + "]");
|
throw new IllegalArgumentException("property [" + propertyName + "] isn't a map, but of type [" + value.getClass().getName() + "]");
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,7 +20,6 @@
|
||||||
package org.elasticsearch.ingest.processor.mutate;
|
package org.elasticsearch.ingest.processor.mutate;
|
||||||
|
|
||||||
import org.elasticsearch.common.Booleans;
|
import org.elasticsearch.common.Booleans;
|
||||||
import org.elasticsearch.common.collect.Tuple;
|
|
||||||
import org.elasticsearch.ingest.Data;
|
import org.elasticsearch.ingest.Data;
|
||||||
import org.elasticsearch.ingest.processor.ConfigurationUtils;
|
import org.elasticsearch.ingest.processor.ConfigurationUtils;
|
||||||
import org.elasticsearch.ingest.processor.Processor;
|
import org.elasticsearch.ingest.processor.Processor;
|
||||||
|
@ -39,23 +38,16 @@ public final class MutateProcessor implements Processor {
|
||||||
private final Map<String, String> rename;
|
private final Map<String, String> rename;
|
||||||
private final Map<String, String> convert;
|
private final Map<String, String> convert;
|
||||||
private final Map<String, String> split;
|
private final Map<String, String> split;
|
||||||
private final Map<String, Tuple<Pattern, String>> gsub;
|
private final List<GsubExpression> gsub;
|
||||||
private final Map<String, String> join;
|
private final Map<String, String> join;
|
||||||
private final List<String> remove;
|
private final List<String> remove;
|
||||||
private final List<String> trim;
|
private final List<String> trim;
|
||||||
private final List<String> uppercase;
|
private final List<String> uppercase;
|
||||||
private final List<String> lowercase;
|
private final List<String> lowercase;
|
||||||
|
|
||||||
public MutateProcessor(Map<String, Object> update,
|
public MutateProcessor(Map<String, Object> update, Map<String, String> rename, Map<String, String> convert,
|
||||||
Map<String, String> rename,
|
Map<String, String> split, List<GsubExpression> gsub, Map<String, String> join,
|
||||||
Map<String, String> convert,
|
List<String> remove, List<String> trim, List<String> uppercase, List<String> lowercase) {
|
||||||
Map<String, String> split,
|
|
||||||
Map<String, Tuple<Pattern, String>> gsub,
|
|
||||||
Map<String, String> join,
|
|
||||||
List<String> remove,
|
|
||||||
List<String> trim,
|
|
||||||
List<String> uppercase,
|
|
||||||
List<String> lowercase) {
|
|
||||||
this.update = update;
|
this.update = update;
|
||||||
this.rename = rename;
|
this.rename = rename;
|
||||||
this.convert = convert;
|
this.convert = convert;
|
||||||
|
@ -68,43 +60,43 @@ public final class MutateProcessor implements Processor {
|
||||||
this.lowercase = lowercase;
|
this.lowercase = lowercase;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> getUpdate() {
|
Map<String, Object> getUpdate() {
|
||||||
return update;
|
return update;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getRename() {
|
Map<String, String> getRename() {
|
||||||
return rename;
|
return rename;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getConvert() {
|
Map<String, String> getConvert() {
|
||||||
return convert;
|
return convert;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getSplit() {
|
Map<String, String> getSplit() {
|
||||||
return split;
|
return split;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Tuple<Pattern, String>> getGsub() {
|
List<GsubExpression> getGsub() {
|
||||||
return gsub;
|
return gsub;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> getJoin() {
|
Map<String, String> getJoin() {
|
||||||
return join;
|
return join;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getRemove() {
|
List<String> getRemove() {
|
||||||
return remove;
|
return remove;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getTrim() {
|
List<String> getTrim() {
|
||||||
return trim;
|
return trim;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getUppercase() {
|
List<String> getUppercase() {
|
||||||
return uppercase;
|
return uppercase;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getLowercase() {
|
List<String> getLowercase() {
|
||||||
return lowercase;
|
return lowercase;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,16 +205,14 @@ public final class MutateProcessor implements Processor {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void doGsub(Data data) {
|
private void doGsub(Data data) {
|
||||||
for (Map.Entry<String, Tuple<Pattern, String>> entry : gsub.entrySet()) {
|
for (GsubExpression gsubExpression : gsub) {
|
||||||
String fieldName = entry.getKey();
|
String oldVal = data.getProperty(gsubExpression.getFieldName());
|
||||||
Tuple<Pattern, String> matchAndReplace = entry.getValue();
|
|
||||||
String oldVal = data.getProperty(fieldName);
|
|
||||||
if (oldVal == null) {
|
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);
|
Matcher matcher = gsubExpression.getPattern().matcher(oldVal);
|
||||||
String newVal = matcher.replaceAll(matchAndReplace.v2());
|
String newVal = matcher.replaceAll(gsubExpression.getReplacement());
|
||||||
data.addField(entry.getKey(), newVal);
|
data.addField(gsubExpression.getFieldName(), newVal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,28 +275,28 @@ public final class MutateProcessor implements Processor {
|
||||||
public static final class Factory implements Processor.Factory<MutateProcessor> {
|
public static final class Factory implements Processor.Factory<MutateProcessor> {
|
||||||
@Override
|
@Override
|
||||||
public MutateProcessor create(Map<String, Object> config) throws IOException {
|
public MutateProcessor create(Map<String, Object> config) throws IOException {
|
||||||
Map<String, Object> update = ConfigurationUtils.readOptionalObjectMap(config, "update");
|
Map<String, Object> update = ConfigurationUtils.readOptionalMap(config, "update");
|
||||||
Map<String, String> rename = ConfigurationUtils.readOptionalStringMap(config, "rename");
|
Map<String, String> rename = ConfigurationUtils.readOptionalMap(config, "rename");
|
||||||
Map<String, String> convert = ConfigurationUtils.readOptionalStringMap(config, "convert");
|
Map<String, String> convert = ConfigurationUtils.readOptionalMap(config, "convert");
|
||||||
Map<String, String> split = ConfigurationUtils.readOptionalStringMap(config, "split");
|
Map<String, String> split = ConfigurationUtils.readOptionalMap(config, "split");
|
||||||
Map<String, List<String>> gsubConfig = ConfigurationUtils.readOptionalStringListMap(config, "gsub");
|
Map<String, List<String>> gsubConfig = ConfigurationUtils.readOptionalMap(config, "gsub");
|
||||||
Map<String, String> join = ConfigurationUtils.readOptionalStringMap(config, "join");
|
Map<String, String> join = ConfigurationUtils.readOptionalMap(config, "join");
|
||||||
List<String> remove = ConfigurationUtils.readOptionalStringList(config, "remove");
|
List<String> remove = ConfigurationUtils.readOptionalStringList(config, "remove");
|
||||||
List<String> trim = ConfigurationUtils.readOptionalStringList(config, "trim");
|
List<String> trim = ConfigurationUtils.readOptionalStringList(config, "trim");
|
||||||
List<String> uppercase = ConfigurationUtils.readOptionalStringList(config, "uppercase");
|
List<String> uppercase = ConfigurationUtils.readOptionalStringList(config, "uppercase");
|
||||||
List<String> lowercase = ConfigurationUtils.readOptionalStringList(config, "lowercase");
|
List<String> lowercase = ConfigurationUtils.readOptionalStringList(config, "lowercase");
|
||||||
|
|
||||||
// pre-compile regex patterns
|
// pre-compile regex patterns
|
||||||
Map<String, Tuple<Pattern, String>> gsub = null;
|
List<GsubExpression> gsubExpressions = null;
|
||||||
if (gsubConfig != null) {
|
if (gsubConfig != null) {
|
||||||
gsub = new HashMap<>();
|
gsubExpressions = new ArrayList<>();
|
||||||
for (Map.Entry<String, List<String>> entry : gsubConfig.entrySet()) {
|
for (Map.Entry<String, List<String>> entry : gsubConfig.entrySet()) {
|
||||||
List<String> searchAndReplace = entry.getValue();
|
List<String> searchAndReplace = entry.getValue();
|
||||||
if (searchAndReplace.size() != 2) {
|
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));
|
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),
|
(rename == null) ? null : Collections.unmodifiableMap(rename),
|
||||||
(convert == null) ? null : Collections.unmodifiableMap(convert),
|
(convert == null) ? null : Collections.unmodifiableMap(convert),
|
||||||
(split == null) ? null : Collections.unmodifiableMap(split),
|
(split == null) ? null : Collections.unmodifiableMap(split),
|
||||||
(gsub == null) ? null : Collections.unmodifiableMap(gsub),
|
(gsubExpressions == null) ? null : Collections.unmodifiableList(gsubExpressions),
|
||||||
(join == null) ? null : Collections.unmodifiableMap(join),
|
(join == null) ? null : Collections.unmodifiableMap(join),
|
||||||
(remove == null) ? null : Collections.unmodifiableList(remove),
|
(remove == null) ? null : Collections.unmodifiableList(remove),
|
||||||
(trim == null) ? null : Collections.unmodifiableList(trim),
|
(trim == null) ? null : Collections.unmodifiableList(trim),
|
||||||
|
|
|
@ -19,30 +19,183 @@
|
||||||
|
|
||||||
package org.elasticsearch.ingest.processor.mutate;
|
package org.elasticsearch.ingest.processor.mutate;
|
||||||
|
|
||||||
import org.elasticsearch.common.collect.Tuple;
|
|
||||||
import org.elasticsearch.test.ESTestCase;
|
import org.elasticsearch.test.ESTestCase;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.notNullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
|
||||||
public class MutateProcessorFactoryTests extends ESTestCase {
|
public class MutateProcessorFactoryTests extends ESTestCase {
|
||||||
|
|
||||||
public void testCreate() throws Exception {
|
public void testCreateUpdate() throws Exception {
|
||||||
MutateProcessor.Factory factory = new MutateProcessor.Factory();
|
MutateProcessor.Factory factory = new MutateProcessor.Factory();
|
||||||
Map<String, Object> config = new HashMap<>();
|
Map<String, Object> config = new HashMap<>();
|
||||||
Map<String, Object> update = new HashMap<>();
|
Map<String, Object> update = new HashMap<>();
|
||||||
update.put("foo", 123);
|
update.put("foo", 123);
|
||||||
config.put("update", update);
|
config.put("update", update);
|
||||||
MutateProcessor processor = factory.create(config);
|
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));
|
assertThat(processor.getUpdate(), equalTo(update));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testCreateRename() throws Exception {
|
||||||
|
MutateProcessor.Factory factory = new MutateProcessor.Factory();
|
||||||
|
Map<String, Object> config = new HashMap<>();
|
||||||
|
Map<String, Object> 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<String, Object> config = new HashMap<>();
|
||||||
|
List<String> 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<String, Object> config = new HashMap<>();
|
||||||
|
Map<String, Object> 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<String, Object> config = new HashMap<>();
|
||||||
|
Map<String, Object> 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<String, Object> config = new HashMap<>();
|
||||||
|
Map<String, Object> 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<String, Object> config = new HashMap<>();
|
||||||
|
List<String> 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<String, Object> config = new HashMap<>();
|
||||||
|
List<String> 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<String, Object> config = new HashMap<>();
|
||||||
|
List<String> 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 {
|
public void testCreateGsubPattern() throws Exception {
|
||||||
MutateProcessor.Factory factory = new MutateProcessor.Factory();
|
MutateProcessor.Factory factory = new MutateProcessor.Factory();
|
||||||
Map<String, Object> config = new HashMap<>();
|
Map<String, Object> config = new HashMap<>();
|
||||||
|
@ -50,32 +203,26 @@ public class MutateProcessorFactoryTests extends ESTestCase {
|
||||||
gsub.put("foo", Arrays.asList("\\s.*e\\s", "<word_ending_with_e>"));
|
gsub.put("foo", Arrays.asList("\\s.*e\\s", "<word_ending_with_e>"));
|
||||||
config.put("gsub", gsub);
|
config.put("gsub", gsub);
|
||||||
|
|
||||||
Map<String, Tuple<Pattern, String>> compiledGsub = new HashMap<>();
|
|
||||||
Pattern searchPattern = Pattern.compile("\\s.*e\\s");
|
|
||||||
compiledGsub.put("foo", new Tuple<>(searchPattern, "<word_ending_with_e>"));
|
|
||||||
|
|
||||||
MutateProcessor processor = factory.create(config);
|
MutateProcessor processor = factory.create(config);
|
||||||
for (Map.Entry<String, Tuple<Pattern, String>> entry : compiledGsub.entrySet()) {
|
assertThat(processor.getGsub().size(), equalTo(1));
|
||||||
Tuple<Pattern, String> actualSearchAndReplace = processor.getGsub().get(entry.getKey());
|
GsubExpression gsubExpression = processor.getGsub().get(0);
|
||||||
assertThat(actualSearchAndReplace, notNullValue());
|
assertThat(gsubExpression.getFieldName(), equalTo("foo"));
|
||||||
assertThat(actualSearchAndReplace.v1().pattern(), equalTo(entry.getValue().v1().pattern()));
|
assertThat(gsubExpression.getPattern().pattern(), equalTo(Pattern.compile("\\s.*e\\s").pattern()));
|
||||||
assertThat(actualSearchAndReplace.v2(), equalTo(entry.getValue().v2()));
|
assertThat(gsubExpression.getReplacement(), equalTo("<word_ending_with_e>"));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCreateGsubPattern_InvalidFormat() throws Exception {
|
public void testCreateGsubPatternInvalidFormat() throws Exception {
|
||||||
MutateProcessor.Factory factory = new MutateProcessor.Factory();
|
MutateProcessor.Factory factory = new MutateProcessor.Factory();
|
||||||
Map<String, Object> config = new HashMap<>();
|
Map<String, Object> config = new HashMap<>();
|
||||||
Map<String, List<String>> gsub = new HashMap<>();
|
Map<String, List<String>> gsub = new HashMap<>();
|
||||||
gsub.put("foo", Arrays.asList("only_one"));
|
gsub.put("foo", Collections.singletonList("only_one"));
|
||||||
config.put("gsub", gsub);
|
config.put("gsub", gsub);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
factory.create(config);
|
factory.create(config);
|
||||||
fail();
|
fail("processor creation should have failed");
|
||||||
} catch (IllegalArgumentException e) {
|
} 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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,10 +25,8 @@ import org.elasticsearch.test.ESTestCase;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
import java.util.regex.Pattern;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
@ -36,9 +34,7 @@ import static org.hamcrest.Matchers.nullValue;
|
||||||
|
|
||||||
|
|
||||||
public class MutateProcessorTests extends ESTestCase {
|
public class MutateProcessorTests extends ESTestCase {
|
||||||
private static final MutateProcessor.Factory FACTORY = new MutateProcessor.Factory();
|
|
||||||
private Data data;
|
private Data data;
|
||||||
private Map<String, Object> config;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setData() {
|
public void setData() {
|
||||||
|
@ -54,25 +50,23 @@ public class MutateProcessorTests extends ESTestCase {
|
||||||
document.put("fizz", fizz);
|
document.put("fizz", fizz);
|
||||||
|
|
||||||
data = new Data("index", "type", "id", document);
|
data = new Data("index", "type", "id", document);
|
||||||
config = new HashMap<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testUpdate() throws IOException {
|
public void testUpdate() throws IOException {
|
||||||
Map<String, Object> update = new HashMap<>();
|
Map<String, Object> update = new HashMap<>();
|
||||||
update.put("foo", 123);
|
update.put("foo", 123);
|
||||||
config.put("update", update);
|
Processor processor = new MutateProcessor(update, null, null, null, null, null, null, null, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("foo"), equalTo(123));
|
assertThat(data.getProperty("foo"), equalTo(123));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRename() throws IOException {
|
public void testRename() throws IOException {
|
||||||
Map<String, String> rename = new HashMap<>();
|
Map<String, String> rename = new HashMap<>();
|
||||||
rename.put("foo", "bar");
|
rename.put("foo", "bar");
|
||||||
config.put("rename", rename);
|
Processor processor = new MutateProcessor(null, rename, null, null, null, null, null, null, null, null);
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("bar"), equalTo("bar"));
|
assertThat(data.getProperty("bar"), equalTo("bar"));
|
||||||
assertThat(data.containsProperty("foo"), is(false));
|
assertThat(data.containsProperty("foo"), is(false));
|
||||||
}
|
}
|
||||||
|
@ -80,66 +74,56 @@ public class MutateProcessorTests extends ESTestCase {
|
||||||
public void testConvert() throws IOException {
|
public void testConvert() throws IOException {
|
||||||
Map<String, String> convert = new HashMap<>();
|
Map<String, String> convert = new HashMap<>();
|
||||||
convert.put("num", "integer");
|
convert.put("num", "integer");
|
||||||
config.put("convert", convert);
|
Processor processor = new MutateProcessor(null, null, convert, null, null, null, null, null, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("num"), equalTo(64));
|
assertThat(data.getProperty("num"), equalTo(64));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testConvert_NullField() throws IOException {
|
public void testConvertNullField() throws IOException {
|
||||||
Map<String, String> convert = new HashMap<>();
|
Map<String, String> convert = new HashMap<>();
|
||||||
convert.put("null", "integer");
|
convert.put("null", "integer");
|
||||||
config.put("convert", convert);
|
Processor processor = new MutateProcessor(null, null, convert, null, null, null, null, null, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
try {
|
try {
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
fail();
|
fail("processor execute should have failed");
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
assertThat(e.getMessage(), equalTo("Field \"null\" is null, cannot be converted to a/an integer"));
|
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<String, String> convert = new HashMap<>();
|
Map<String, String> convert = new HashMap<>();
|
||||||
convert.put("arr", "integer");
|
convert.put("arr", "integer");
|
||||||
config.put("convert", convert);
|
Processor processor = new MutateProcessor(null, null, convert, null, null, null, null, null, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("arr"), equalTo(Arrays.asList(1, 2, 3)));
|
assertThat(data.getProperty("arr"), equalTo(Arrays.asList(1, 2, 3)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSplit() throws IOException {
|
public void testSplit() throws IOException {
|
||||||
HashMap<String, String> split = new HashMap<>();
|
Map<String, String> split = new HashMap<>();
|
||||||
split.put("ip", "\\.");
|
split.put("ip", "\\.");
|
||||||
config.put("split", split);
|
Processor processor = new MutateProcessor(null, null, null, split, null, null, null, null, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("ip"), equalTo(Arrays.asList("127", "0", "0", "1")));
|
assertThat(data.getProperty("ip"), equalTo(Arrays.asList("127", "0", "0", "1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGsub() throws IOException {
|
public void testGsub() throws IOException {
|
||||||
HashMap<String, List<String>> gsub = new HashMap<>();
|
List<GsubExpression> gsubExpressions = Collections.singletonList(new GsubExpression("ip", Pattern.compile("\\."), "-"));
|
||||||
gsub.put("ip", Arrays.asList("\\.", "-"));
|
Processor processor = new MutateProcessor(null, null, null, null, gsubExpressions, null, null, null, null, null);
|
||||||
config.put("gsub", gsub);
|
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("ip"), equalTo("127-0-0-1"));
|
assertThat(data.getProperty("ip"), equalTo("127-0-0-1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGsub_NullValue() throws IOException {
|
public void testGsub_NullValue() throws IOException {
|
||||||
HashMap<String, List<String>> gsub = new HashMap<>();
|
List<GsubExpression> gsubExpressions = Collections.singletonList(new GsubExpression("null_field", Pattern.compile("\\."), "-"));
|
||||||
gsub.put("null_field", Arrays.asList("\\.", "-"));
|
Processor processor = new MutateProcessor(null, null, null, null, gsubExpressions, null, null, null, null, null);
|
||||||
config.put("gsub", gsub);
|
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
try {
|
try {
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
fail();
|
fail("processor execution should have failed");
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
assertThat(e.getMessage(), equalTo("Field \"null_field\" is null, cannot match pattern."));
|
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 {
|
public void testJoin() throws IOException {
|
||||||
HashMap<String, String> join = new HashMap<>();
|
HashMap<String, String> join = new HashMap<>();
|
||||||
join.put("arr", "-");
|
join.put("arr", "-");
|
||||||
config.put("join", join);
|
Processor processor = new MutateProcessor(null, null, null, null, null, join, null, null, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("arr"), equalTo("1-2-3"));
|
assertThat(data.getProperty("arr"), equalTo("1-2-3"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRemove() throws IOException {
|
public void testRemove() throws IOException {
|
||||||
List<String> remove = Arrays.asList("foo", "ip");
|
List<String> remove = Arrays.asList("foo", "ip");
|
||||||
config.put("remove", remove);
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, remove, null, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(5));
|
||||||
assertThat(data.getProperty("foo"), nullValue());
|
assertThat(data.getProperty("foo"), nullValue());
|
||||||
assertThat(data.getProperty("ip"), nullValue());
|
assertThat(data.getProperty("ip"), nullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testTrim() throws IOException {
|
public void testTrim() throws IOException {
|
||||||
List<String> trim = Arrays.asList("to_strip", "foo");
|
List<String> trim = Arrays.asList("to_strip", "foo");
|
||||||
config.put("trim", trim);
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, trim, null, null);
|
||||||
|
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("foo"), equalTo("bar"));
|
assertThat(data.getProperty("foo"), equalTo("bar"));
|
||||||
assertThat(data.getProperty("to_strip"), equalTo("clean"));
|
assertThat(data.getProperty("to_strip"), equalTo("clean"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testUppercase() throws IOException {
|
public void testUppercase() throws IOException {
|
||||||
List<String> uppercase = Arrays.asList("foo");
|
List<String> uppercase = Collections.singletonList("foo");
|
||||||
config.put("uppercase", uppercase);
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, uppercase, null);
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("foo"), equalTo("BAR"));
|
assertThat(data.getProperty("foo"), equalTo("BAR"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testLowercase() throws IOException {
|
public void testLowercase() throws IOException {
|
||||||
List<String> lowercase = Arrays.asList("alpha");
|
List<String> lowercase = Collections.singletonList("alpha");
|
||||||
config.put("lowercase", lowercase);
|
Processor processor = new MutateProcessor(null, null, null, null, null, null, null, null, null, lowercase);
|
||||||
Processor processor = FACTORY.create(config);
|
|
||||||
processor.execute(data);
|
processor.execute(data);
|
||||||
|
assertThat(data.getDocument().size(), equalTo(7));
|
||||||
assertThat(data.getProperty("alpha"), equalTo("abcd"));
|
assertThat(data.getProperty("alpha"), equalTo("abcd"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue