From 51fcb72549420dc3c5d4e21b329d0fc6279fbeb1 Mon Sep 17 00:00:00 2001 From: Dan Hermann Date: Wed, 16 Sep 2020 08:04:20 -0500 Subject: [PATCH] Fix failure in AppendProcessorTests.testAppendingToListWithDuplicatesDisallowed (#62385) --- .../ingest/common/AppendProcessorTests.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java index 78af437be7352..8cdb63278d7f1 100644 --- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java +++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.ingest.common; +import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.ingest.IngestDocument; import org.elasticsearch.ingest.IngestDocument.Metadata; import org.elasticsearch.ingest.Processor; @@ -34,6 +35,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; @@ -192,23 +195,26 @@ public void testAppendingUniqueValueToScalar() throws Exception { public void testAppendingToListWithDuplicatesDisallowed() throws Exception { IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); - List list = new ArrayList<>(); int size = randomIntBetween(0, 10); - for (int i = 0; i < size; i++) { - list.add(randomAlphaOfLengthBetween(1, 10)); - } + List list = Stream.generate(() -> randomAlphaOfLengthBetween(1, 10)).limit(size).collect(Collectors.toList()); String originalField = RandomDocumentPicks.addRandomField(random(), ingestDocument, list); List expectedValues = new ArrayList<>(list); List existingValues = randomSubsetOf(list); - int uniqueValuesSize = randomIntBetween(0, 10); - Set uniqueValues = new HashSet<>(uniqueValuesSize); - while (uniqueValues.size() < uniqueValuesSize) { - uniqueValues.add(randomValueOtherThanMany(list::contains, () -> randomAlphaOfLengthBetween(1, 10))); - } + + // generate new values + int nonexistingValuesSize = randomIntBetween(0, 10); + Set newValues = Stream.generate(() -> randomAlphaOfLengthBetween(1, 10)) + .limit(nonexistingValuesSize) + .collect(Collectors.toSet()); + + // create a set using the new values making sure there are no overlapping values already present in the existing values + Set nonexistingValues = Sets.difference(newValues, new HashSet<>(existingValues)); List valuesToAppend = new ArrayList<>(existingValues); - valuesToAppend.addAll(uniqueValues); - expectedValues.addAll(uniqueValues); + valuesToAppend.addAll(nonexistingValues); + expectedValues.addAll(nonexistingValues); Collections.sort(valuesToAppend); + + // attempt to append both new and existing values Processor appendProcessor = createAppendProcessor(originalField, valuesToAppend, false); appendProcessor.execute(ingestDocument); List fieldValue = ingestDocument.getFieldValue(originalField, List.class);