14
14
import org .opensearch .ingest .TestTemplateService ;
15
15
import org .opensearch .test .OpenSearchTestCase ;
16
16
17
+ import java .util .List ;
18
+ import java .util .Map ;
19
+
17
20
import static org .hamcrest .Matchers .equalTo ;
18
21
19
22
public class CopyProcessorTests extends OpenSearchTestCase {
@@ -26,8 +29,7 @@ public void testCopyExistingField() throws Exception {
26
29
processor .execute (ingestDocument );
27
30
assertThat (ingestDocument .hasField (targetFieldName ), equalTo (true ));
28
31
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 );
31
33
32
34
Processor processorWithEmptyTarget = createCopyProcessor (sourceFieldName , "" , false , false , false );
33
35
assertThrows (
@@ -75,7 +77,7 @@ public void testCopyWithRemoveSource() throws Exception {
75
77
Processor processor = createCopyProcessor (sourceFieldName , targetFieldName , false , true , false );
76
78
processor .execute (ingestDocument );
77
79
assertThat (ingestDocument .hasField (targetFieldName ), equalTo (true ));
78
- assertThat (ingestDocument .getFieldValue (targetFieldName , Object .class ), equalTo ( sourceValue ) );
80
+ assertDeepCopiedObjectEquals (ingestDocument .getFieldValue (targetFieldName , Object .class ), sourceValue );
79
81
assertThat (ingestDocument .hasField (sourceFieldName ), equalTo (false ));
80
82
}
81
83
@@ -97,12 +99,30 @@ public void testCopyToExistingField() throws Exception {
97
99
Processor processorWithTargetNullValue = createCopyProcessor (sourceFieldName , targetFieldWithNullValue , false , false , false );
98
100
processorWithTargetNullValue .execute (ingestDocument );
99
101
assertThat (ingestDocument .hasField (targetFieldWithNullValue ), equalTo (true ));
100
- assertThat (ingestDocument .getFieldValue (targetFieldWithNullValue , Object .class ), equalTo ( sourceValue ) );
102
+ assertDeepCopiedObjectEquals (ingestDocument .getFieldValue (targetFieldWithNullValue , Object .class ), sourceValue );
101
103
102
104
Processor processorWithOverrideTargetIsTrue = createCopyProcessor (sourceFieldName , targetFieldName , false , false , true );
103
105
processorWithOverrideTargetIsTrue .execute (ingestDocument );
104
106
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
+ }
106
126
}
107
127
108
128
private static Processor createCopyProcessor (
0 commit comments