|
@@ -20,6 +20,7 @@ import org.elasticsearch.test.ESTestCase;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.List;
|
|
@@ -51,13 +52,13 @@ public class AppendProcessorTests extends ESTestCase {
|
|
|
if (randomBoolean()) {
|
|
|
Object value = scalar.randomValue();
|
|
|
values.add(value);
|
|
|
- appendProcessor = createAppendProcessor(field, value, true);
|
|
|
+ appendProcessor = createAppendProcessor(field, value, null, true);
|
|
|
} else {
|
|
|
int valuesSize = randomIntBetween(0, 10);
|
|
|
for (int i = 0; i < valuesSize; i++) {
|
|
|
values.add(scalar.randomValue());
|
|
|
}
|
|
|
- appendProcessor = createAppendProcessor(field, values, true);
|
|
|
+ appendProcessor = createAppendProcessor(field, values, null, true);
|
|
|
}
|
|
|
appendProcessor.execute(ingestDocument);
|
|
|
Object fieldValue = ingestDocument.getFieldValue(field, Object.class);
|
|
@@ -80,13 +81,13 @@ public class AppendProcessorTests extends ESTestCase {
|
|
|
if (randomBoolean()) {
|
|
|
Object value = scalar.randomValue();
|
|
|
values.add(value);
|
|
|
- appendProcessor = createAppendProcessor(field, value, true);
|
|
|
+ appendProcessor = createAppendProcessor(field, value, null, true);
|
|
|
} else {
|
|
|
int valuesSize = randomIntBetween(0, 10);
|
|
|
for (int i = 0; i < valuesSize; i++) {
|
|
|
values.add(scalar.randomValue());
|
|
|
}
|
|
|
- appendProcessor = createAppendProcessor(field, values, true);
|
|
|
+ appendProcessor = createAppendProcessor(field, values, null, true);
|
|
|
}
|
|
|
appendProcessor.execute(ingestDocument);
|
|
|
List<?> list = ingestDocument.getFieldValue(field, List.class);
|
|
@@ -104,13 +105,13 @@ public class AppendProcessorTests extends ESTestCase {
|
|
|
if (randomBoolean()) {
|
|
|
Object value = scalar.randomValue();
|
|
|
values.add(value);
|
|
|
- appendProcessor = createAppendProcessor(field, value, true);
|
|
|
+ appendProcessor = createAppendProcessor(field, value, null, true);
|
|
|
} else {
|
|
|
int valuesSize = randomIntBetween(0, 10);
|
|
|
for (int i = 0; i < valuesSize; i++) {
|
|
|
values.add(scalar.randomValue());
|
|
|
}
|
|
|
- appendProcessor = createAppendProcessor(field, values, true);
|
|
|
+ appendProcessor = createAppendProcessor(field, values, null, true);
|
|
|
}
|
|
|
appendProcessor.execute(ingestDocument);
|
|
|
List<?> fieldValue = ingestDocument.getFieldValue(field, List.class);
|
|
@@ -128,7 +129,7 @@ public class AppendProcessorTests extends ESTestCase {
|
|
|
|
|
|
List<Object> valuesToAppend = new ArrayList<>();
|
|
|
valuesToAppend.add(originalValue);
|
|
|
- Processor appendProcessor = createAppendProcessor(field, valuesToAppend, false);
|
|
|
+ Processor appendProcessor = createAppendProcessor(field, valuesToAppend, null, false);
|
|
|
appendProcessor.execute(ingestDocument);
|
|
|
Object fieldValue = ingestDocument.getFieldValue(field, Object.class);
|
|
|
assertThat(fieldValue, not(instanceOf(List.class)));
|
|
@@ -143,7 +144,7 @@ public class AppendProcessorTests extends ESTestCase {
|
|
|
List<Object> valuesToAppend = new ArrayList<>();
|
|
|
String newValue = randomValueOtherThan(originalValue, () -> randomAlphaOfLengthBetween(1, 10));
|
|
|
valuesToAppend.add(newValue);
|
|
|
- Processor appendProcessor = createAppendProcessor(field, valuesToAppend, false);
|
|
|
+ Processor appendProcessor = createAppendProcessor(field, valuesToAppend, null, false);
|
|
|
appendProcessor.execute(ingestDocument);
|
|
|
List<?> list = ingestDocument.getFieldValue(field, List.class);
|
|
|
assertThat(list.size(), equalTo(2));
|
|
@@ -172,19 +173,166 @@ public class AppendProcessorTests extends ESTestCase {
|
|
|
Collections.sort(valuesToAppend);
|
|
|
|
|
|
// attempt to append both new and existing values
|
|
|
- Processor appendProcessor = createAppendProcessor(originalField, valuesToAppend, false);
|
|
|
+ Processor appendProcessor = createAppendProcessor(originalField, valuesToAppend, null, false);
|
|
|
appendProcessor.execute(ingestDocument);
|
|
|
List<?> fieldValue = ingestDocument.getFieldValue(originalField, List.class);
|
|
|
assertThat(fieldValue, sameInstance(list));
|
|
|
assertThat(fieldValue, containsInAnyOrder(expectedValues.toArray()));
|
|
|
}
|
|
|
|
|
|
- private static Processor createAppendProcessor(String fieldName, Object fieldValue, boolean allowDuplicates) {
|
|
|
+ public void testCopyFromOtherFieldSimple() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
+ ingestDocument.setFieldValue("foo", 1);
|
|
|
+ ingestDocument.setFieldValue("bar", 2);
|
|
|
+ ingestDocument.setFieldValue("baz", new ArrayList<>(List.of(3)));
|
|
|
+
|
|
|
+ createAppendProcessor("bar", null, "foo", false).execute(ingestDocument);
|
|
|
+ createAppendProcessor("baz", null, "bar", false).execute(ingestDocument);
|
|
|
+ createAppendProcessor("quux", null, "baz", false).execute(ingestDocument);
|
|
|
+
|
|
|
+ Map<String, Object> result = ingestDocument.getCtxMap().getSource();
|
|
|
+ assertThat(result.get("foo"), equalTo(1));
|
|
|
+ assertThat(result.get("bar"), equalTo(List.of(2, 1)));
|
|
|
+ assertThat(result.get("baz"), equalTo(List.of(3, 2, 1)));
|
|
|
+ assertThat(result.get("quux"), equalTo(List.of(3, 2, 1)));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testCopyFromOtherField() throws Exception {
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
|
|
|
+
|
|
|
+ // generate values, add some to a target field, the rest to a source field
|
|
|
+ int size = randomIntBetween(0, 10);
|
|
|
+ Set<String> allValues = Stream.generate(() -> randomAlphaOfLengthBetween(1, 10)).limit(size).collect(Collectors.toSet());
|
|
|
+ List<String> originalValues = randomSubsetOf(allValues);
|
|
|
+ List<String> additionalValues = new ArrayList<>(Sets.difference(new HashSet<>(allValues), new HashSet<>(originalValues)));
|
|
|
+ List<String> targetFieldValue = new ArrayList<>(originalValues);
|
|
|
+ String targetField = RandomDocumentPicks.addRandomField(random(), ingestDocument, targetFieldValue);
|
|
|
+ String sourceField = RandomDocumentPicks.addRandomField(random(), ingestDocument, additionalValues);
|
|
|
+
|
|
|
+ Processor appendProcessor = createAppendProcessor(targetField, null, sourceField, false);
|
|
|
+ appendProcessor.execute(ingestDocument);
|
|
|
+ List<?> fieldValue = ingestDocument.getFieldValue(targetField, List.class);
|
|
|
+ assertThat(fieldValue, sameInstance(targetFieldValue));
|
|
|
+ assertThat(fieldValue, containsInAnyOrder(allValues.toArray()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testCopyFromCopiesNonPrimitiveMutableTypes() throws Exception {
|
|
|
+ final String sourceField = "sourceField";
|
|
|
+ final String targetField = "targetField";
|
|
|
+ Processor processor = createAppendProcessor(targetField, null, sourceField, false);
|
|
|
+
|
|
|
+ // map types
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+ Map<String, Object> sourceMap = new HashMap<>();
|
|
|
+ sourceMap.put("foo", "bar");
|
|
|
+ document.put(sourceField, sourceMap);
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ IngestDocument output = processor.execute(ingestDocument);
|
|
|
+ sourceMap.put("foo", "not-bar");
|
|
|
+ Map<?, ?> outputMap = (Map<?, ?>) output.getFieldValue(targetField, List.class).getFirst();
|
|
|
+ assertThat(outputMap.get("foo"), equalTo("bar"));
|
|
|
+
|
|
|
+ // set types
|
|
|
+ document = new HashMap<>();
|
|
|
+ Set<String> sourceSet = randomUnique(() -> randomAlphaOfLength(5), 5);
|
|
|
+ Set<String> preservedSet = new HashSet<>(sourceSet);
|
|
|
+ document.put(sourceField, sourceSet);
|
|
|
+ ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ sourceSet.add(randomValueOtherThanMany(sourceSet::contains, () -> randomAlphaOfLength(5)));
|
|
|
+ Set<?> outputSet = (Set<?>) ingestDocument.getFieldValue(targetField, List.class).getFirst();
|
|
|
+ assertThat(outputSet, equalTo(preservedSet));
|
|
|
+
|
|
|
+ // list types (the outer list isn't used, but an inner list should be copied)
|
|
|
+ document = new HashMap<>();
|
|
|
+ List<String> sourceList = randomList(1, 5, () -> randomAlphaOfLength(5));
|
|
|
+ List<String> preservedList = new ArrayList<>(sourceList);
|
|
|
+ List<List<String>> wrappedSourceList = List.of(sourceList);
|
|
|
+ document.put(sourceField, wrappedSourceList);
|
|
|
+ ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ sourceList.add(randomValueOtherThanMany(sourceList::contains, () -> randomAlphaOfLength(5)));
|
|
|
+ List<?> unwrappedOutputList = (List<?>) ingestDocument.getFieldValue(targetField, List.class).getFirst();
|
|
|
+ assertThat(unwrappedOutputList, equalTo(preservedList));
|
|
|
+
|
|
|
+ // byte[] types
|
|
|
+ document = new HashMap<>();
|
|
|
+ byte[] sourceBytes = randomByteArrayOfLength(10);
|
|
|
+ byte[] preservedBytes = new byte[sourceBytes.length];
|
|
|
+ System.arraycopy(sourceBytes, 0, preservedBytes, 0, sourceBytes.length);
|
|
|
+ document.put(sourceField, sourceBytes);
|
|
|
+ ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ sourceBytes[0] = sourceBytes[0] == 0 ? (byte) 1 : (byte) 0;
|
|
|
+ byte[] outputBytes = (byte[]) ingestDocument.getFieldValue(targetField, List.class).getFirst();
|
|
|
+ assertThat(outputBytes, equalTo(preservedBytes));
|
|
|
+
|
|
|
+ // Date types
|
|
|
+ document = new HashMap<>();
|
|
|
+ Date sourceDate = new Date();
|
|
|
+ Date preservedDate = new Date(sourceDate.getTime());
|
|
|
+ document.put(sourceField, sourceDate);
|
|
|
+ ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ processor.execute(ingestDocument);
|
|
|
+ sourceDate.setTime(sourceDate.getTime() + 1);
|
|
|
+ Date outputDate = (Date) ingestDocument.getFieldValue(targetField, List.class).getFirst();
|
|
|
+ assertThat(outputDate, equalTo(preservedDate));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testCopyFromDeepCopiesNonPrimitiveMutableTypes() throws Exception {
|
|
|
+ final String sourceField = "sourceField";
|
|
|
+ final String targetField = "targetField";
|
|
|
+ Processor processor = createAppendProcessor(targetField, null, sourceField, false);
|
|
|
+ Map<String, Object> document = new HashMap<>();
|
|
|
+
|
|
|
+ // a root map with values of map, set, list, bytes, date
|
|
|
+ Map<String, Object> sourceMap = new HashMap<>();
|
|
|
+ sourceMap.put("foo", "bar");
|
|
|
+ Set<String> sourceSet = randomUnique(() -> randomAlphaOfLength(5), 5);
|
|
|
+ List<String> sourceList = randomList(1, 5, () -> randomAlphaOfLength(5));
|
|
|
+ byte[] sourceBytes = randomByteArrayOfLength(10);
|
|
|
+ Date sourceDate = new Date();
|
|
|
+ Map<String, Object> root = new HashMap<>();
|
|
|
+ root.put("foo", "bar");
|
|
|
+ root.put("map", sourceMap);
|
|
|
+ root.put("set", sourceSet);
|
|
|
+ root.put("list", sourceList);
|
|
|
+ root.put("bytes", sourceBytes);
|
|
|
+ root.put("date", sourceDate);
|
|
|
+
|
|
|
+ Set<String> preservedSet = new HashSet<>(sourceSet);
|
|
|
+ List<String> preservedList = new ArrayList<>(sourceList);
|
|
|
+ byte[] preservedBytes = new byte[sourceBytes.length];
|
|
|
+ System.arraycopy(sourceBytes, 0, preservedBytes, 0, sourceBytes.length);
|
|
|
+ Date preservedDate = new Date(sourceDate.getTime());
|
|
|
+
|
|
|
+ document.put(sourceField, root);
|
|
|
+ IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
|
|
|
+ IngestDocument output = processor.execute(ingestDocument);
|
|
|
+ Map<?, ?> outputRoot = (Map<?, ?>) output.getFieldValue(targetField, List.class).getFirst();
|
|
|
+
|
|
|
+ root.put("foo", "not-bar");
|
|
|
+ sourceMap.put("foo", "not-bar");
|
|
|
+ sourceSet.add(randomValueOtherThanMany(sourceSet::contains, () -> randomAlphaOfLength(5)));
|
|
|
+ sourceList.add(randomValueOtherThanMany(sourceList::contains, () -> randomAlphaOfLength(5)));
|
|
|
+ sourceBytes[0] = sourceBytes[0] == 0 ? (byte) 1 : (byte) 0;
|
|
|
+ sourceDate.setTime(sourceDate.getTime() + 1);
|
|
|
+
|
|
|
+ assertThat(outputRoot.get("foo"), equalTo("bar"));
|
|
|
+ assertThat(((Map<?, ?>) outputRoot.get("map")).get("foo"), equalTo("bar"));
|
|
|
+ assertThat(((Set<?>) outputRoot.get("set")), equalTo(preservedSet));
|
|
|
+ assertThat(((List<?>) outputRoot.get("list")), equalTo(preservedList));
|
|
|
+ assertThat(((byte[]) outputRoot.get("bytes")), equalTo(preservedBytes));
|
|
|
+ assertThat(((Date) outputRoot.get("date")), equalTo(preservedDate));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Processor createAppendProcessor(String fieldName, Object fieldValue, String copyFrom, boolean allowDuplicates) {
|
|
|
return new AppendProcessor(
|
|
|
randomAlphaOfLength(10),
|
|
|
null,
|
|
|
new TestTemplateService.MockTemplateScript.Factory(fieldName),
|
|
|
ValueSource.wrap(fieldValue, TestTemplateService.instance()),
|
|
|
+ copyFrom,
|
|
|
allowDuplicates
|
|
|
);
|
|
|
}
|