Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky test for CopyProcessorTests #11982

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading