Skip to content

Commit

Permalink
Fix flaky-test for CopyProcessorTests (#11982)
Browse files Browse the repository at this point in the history
Signed-off-by: Gao Binlong <[email protected]>
(cherry picked from commit c529265)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Jan 23, 2024
1 parent c40fcff commit 01daf4e
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.opensearch.ingest.TestTemplateService;
import org.opensearch.test.OpenSearchTestCase;

import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.equalTo;

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

Processor processorWithEmptyTarget = createCopyProcessor(sourceFieldName, "", false, false, false);
assertThrows(
Expand Down Expand Up @@ -75,7 +77,7 @@ public void testCopyWithRemoveSource() throws Exception {
Processor processor = createCopyProcessor(sourceFieldName, targetFieldName, false, true, false);
processor.execute(ingestDocument);
assertThat(ingestDocument.hasField(targetFieldName), equalTo(true));
assertThat(ingestDocument.getFieldValue(targetFieldName, Object.class), equalTo(sourceValue));
assertDeepCopiedObjectEquals(ingestDocument.getFieldValue(targetFieldName, Object.class), sourceValue);
assertThat(ingestDocument.hasField(sourceFieldName), equalTo(false));
}

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

Processor processorWithOverrideTargetIsTrue = createCopyProcessor(sourceFieldName, targetFieldName, false, false, true);
processorWithOverrideTargetIsTrue.execute(ingestDocument);
assertThat(ingestDocument.hasField(targetFieldName), equalTo(true));
assertThat(ingestDocument.getFieldValue(targetFieldName, Object.class), equalTo(sourceValue));
assertDeepCopiedObjectEquals(ingestDocument.getFieldValue(targetFieldName, Object.class), sourceValue);
}

@SuppressWarnings("unchecked")
private static void assertDeepCopiedObjectEquals(Object expected, Object actual) {
if (expected instanceof Map) {
Map<String, Object> expectedMap = (Map<String, Object>) expected;
Map<String, Object> actualMap = (Map<String, Object>) actual;
assertEquals(expectedMap.size(), actualMap.size());
for (Map.Entry<String, Object> expectedEntry : expectedMap.entrySet()) {
assertDeepCopiedObjectEquals(expectedEntry.getValue(), actualMap.get(expectedEntry.getKey()));
}
} else if (expected instanceof List) {
assertArrayEquals(((List<?>) expected).toArray(), ((List<?>) actual).toArray());
} else if (expected instanceof byte[]) {
assertArrayEquals((byte[]) expected, (byte[]) actual);
} else {
assertEquals(expected, actual);
}
}

private static Processor createCopyProcessor(
Expand Down

0 comments on commit 01daf4e

Please sign in to comment.