Skip to content

Commit 79689fd

Browse files
authored
Fix failing SetProcessorTests.testCopyFromOtherField test (#70150)
1 parent f61cd1a commit 79689fd

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/SetProcessorTests.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Map;
2626
import java.util.Set;
2727

28+
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
2829
import static org.hamcrest.Matchers.equalTo;
2930

3031
public class SetProcessorTests extends ESTestCase {
@@ -135,19 +136,47 @@ public void testSetMetadataIfPrimaryTerm() throws Exception {
135136
assertThat(ingestDocument.getFieldValue(Metadata.IF_PRIMARY_TERM.getFieldName(), Long.class), Matchers.equalTo(ifPrimaryTerm));
136137
}
137138

138-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/69876")
139139
public void testCopyFromOtherField() throws Exception {
140140
Map<String, Object> document = new HashMap<>();
141141
Object fieldValue = RandomDocumentPicks.randomFieldValue(random());
142142
document.put("field", fieldValue);
143143

144144
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
145-
String fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
145+
String fieldName;
146+
if (document.size() > 1) {
147+
// select an existing field as target if one exists other than the copy_from field
148+
do {
149+
fieldName = RandomDocumentPicks.randomExistingFieldName(random(), ingestDocument);
150+
} while (fieldName.equals("field") || fieldName.startsWith("field."));
151+
} else {
152+
// otherwise make up a new target field
153+
fieldName = randomAlphaOfLength(6);
154+
}
146155

147156
Processor processor = createSetProcessor(fieldName, null, "field", true, false);
148157
processor.execute(ingestDocument);
149158
assertThat(ingestDocument.hasField(fieldName), equalTo(true));
150-
assertThat(ingestDocument.getFieldValue(fieldName, Object.class), equalTo(fieldValue));
159+
Object copiedValue = ingestDocument.getFieldValue(fieldName, Object.class);
160+
if (fieldValue instanceof Map) {
161+
assertMapEquals(copiedValue, fieldValue);
162+
} else {
163+
assertThat(copiedValue, equalTo(fieldValue));
164+
}
165+
}
166+
167+
private static void assertMapEquals(Object actual, Object expected) {
168+
if (expected instanceof Map) {
169+
Map<?, ?> expectedMap = (Map<?, ?>) expected;
170+
Map<?, ?> actualMap = (Map<?, ?>) actual;
171+
assertThat(actualMap.keySet().toArray(), arrayContainingInAnyOrder(expectedMap.keySet().toArray()));
172+
for (Map.Entry<?, ?> entry : actualMap.entrySet()) {
173+
if (entry.getValue() instanceof Map) {
174+
assertMapEquals(entry.getValue(), expectedMap.get(entry.getKey()));
175+
} else {
176+
assertThat(entry.getValue(), equalTo(expectedMap.get(entry.getKey())));
177+
}
178+
}
179+
}
151180
}
152181

153182
public void testCopyFromDeepCopiesNonPrimitiveMutableTypes() throws Exception {

0 commit comments

Comments
 (0)