Skip to content

Commit c529265

Browse files
authored
Fix flaky-test for CopyProcessorTests (opensearch-project#11982)
Signed-off-by: Gao Binlong <gbinlong@amazon.com>
1 parent 48fdb4f commit c529265

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

modules/ingest-common/src/test/java/org/opensearch/ingest/common/CopyProcessorTests.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import org.opensearch.ingest.TestTemplateService;
1515
import org.opensearch.test.OpenSearchTestCase;
1616

17+
import java.util.List;
18+
import java.util.Map;
19+
1720
import static org.hamcrest.Matchers.equalTo;
1821

1922
public class CopyProcessorTests extends OpenSearchTestCase {
@@ -26,8 +29,7 @@ public void testCopyExistingField() throws Exception {
2629
processor.execute(ingestDocument);
2730
assertThat(ingestDocument.hasField(targetFieldName), equalTo(true));
2831
Object sourceValue = ingestDocument.getFieldValue(sourceFieldName, Object.class);
29-
assertThat(ingestDocument.getFieldValue(targetFieldName, Object.class), equalTo(sourceValue));
30-
assertThat(ingestDocument.getFieldValue(sourceFieldName, Object.class), equalTo(sourceValue));
32+
assertDeepCopiedObjectEquals(ingestDocument.getFieldValue(targetFieldName, Object.class), sourceValue);
3133

3234
Processor processorWithEmptyTarget = createCopyProcessor(sourceFieldName, "", false, false, false);
3335
assertThrows(
@@ -75,7 +77,7 @@ public void testCopyWithRemoveSource() throws Exception {
7577
Processor processor = createCopyProcessor(sourceFieldName, targetFieldName, false, true, false);
7678
processor.execute(ingestDocument);
7779
assertThat(ingestDocument.hasField(targetFieldName), equalTo(true));
78-
assertThat(ingestDocument.getFieldValue(targetFieldName, Object.class), equalTo(sourceValue));
80+
assertDeepCopiedObjectEquals(ingestDocument.getFieldValue(targetFieldName, Object.class), sourceValue);
7981
assertThat(ingestDocument.hasField(sourceFieldName), equalTo(false));
8082
}
8183

@@ -97,12 +99,30 @@ public void testCopyToExistingField() throws Exception {
9799
Processor processorWithTargetNullValue = createCopyProcessor(sourceFieldName, targetFieldWithNullValue, false, false, false);
98100
processorWithTargetNullValue.execute(ingestDocument);
99101
assertThat(ingestDocument.hasField(targetFieldWithNullValue), equalTo(true));
100-
assertThat(ingestDocument.getFieldValue(targetFieldWithNullValue, Object.class), equalTo(sourceValue));
102+
assertDeepCopiedObjectEquals(ingestDocument.getFieldValue(targetFieldWithNullValue, Object.class), sourceValue);
101103

102104
Processor processorWithOverrideTargetIsTrue = createCopyProcessor(sourceFieldName, targetFieldName, false, false, true);
103105
processorWithOverrideTargetIsTrue.execute(ingestDocument);
104106
assertThat(ingestDocument.hasField(targetFieldName), equalTo(true));
105-
assertThat(ingestDocument.getFieldValue(targetFieldName, Object.class), equalTo(sourceValue));
107+
assertDeepCopiedObjectEquals(ingestDocument.getFieldValue(targetFieldName, Object.class), sourceValue);
108+
}
109+
110+
@SuppressWarnings("unchecked")
111+
private static void assertDeepCopiedObjectEquals(Object expected, Object actual) {
112+
if (expected instanceof Map) {
113+
Map<String, Object> expectedMap = (Map<String, Object>) expected;
114+
Map<String, Object> actualMap = (Map<String, Object>) actual;
115+
assertEquals(expectedMap.size(), actualMap.size());
116+
for (Map.Entry<String, Object> expectedEntry : expectedMap.entrySet()) {
117+
assertDeepCopiedObjectEquals(expectedEntry.getValue(), actualMap.get(expectedEntry.getKey()));
118+
}
119+
} else if (expected instanceof List) {
120+
assertArrayEquals(((List<?>) expected).toArray(), ((List<?>) actual).toArray());
121+
} else if (expected instanceof byte[]) {
122+
assertArrayEquals((byte[]) expected, (byte[]) actual);
123+
} else {
124+
assertEquals(expected, actual);
125+
}
106126
}
107127

108128
private static Processor createCopyProcessor(

0 commit comments

Comments
 (0)