2929import org .hamcrest .Matchers ;
3030
3131import java .util .HashMap ;
32+ import java .util .Map ;
3233
3334import static org .hamcrest .Matchers .equalTo ;
3435
@@ -38,7 +39,7 @@ public void testSetExistingFields() throws Exception {
3839 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
3940 String fieldName = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
4041 Object fieldValue = RandomDocumentPicks .randomFieldValue (random ());
41- Processor processor = createSetProcessor (fieldName , fieldValue , true , false );
42+ Processor processor = createSetProcessor (fieldName , fieldValue , null , true , false );
4243 processor .execute (ingestDocument );
4344 assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
4445 assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -50,7 +51,7 @@ public void testSetNewFields() throws Exception {
5051 IngestDocument testIngestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
5152 Object fieldValue = RandomDocumentPicks .randomFieldValue (random ());
5253 String fieldName = RandomDocumentPicks .addRandomField (random (), testIngestDocument , fieldValue );
53- Processor processor = createSetProcessor (fieldName , fieldValue , true , false );
54+ Processor processor = createSetProcessor (fieldName , fieldValue , null , true , false );
5455 processor .execute (ingestDocument );
5556 assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
5657 assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -59,7 +60,7 @@ public void testSetNewFields() throws Exception {
5960 public void testSetFieldsTypeMismatch () throws Exception {
6061 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
6162 ingestDocument .setFieldValue ("field" , "value" );
62- Processor processor = createSetProcessor ("field.inner" , "value" , true , false );
63+ Processor processor = createSetProcessor ("field.inner" , "value" , null , true , false );
6364 try {
6465 processor .execute (ingestDocument );
6566 fail ("processor execute should have failed" );
@@ -73,7 +74,7 @@ public void testSetNewFieldWithOverrideDisabled() throws Exception {
7374 IngestDocument ingestDocument = new IngestDocument (new HashMap <>(), new HashMap <>());
7475 String fieldName = RandomDocumentPicks .randomFieldName (random ());
7576 Object fieldValue = RandomDocumentPicks .randomFieldValue (random ());
76- Processor processor = createSetProcessor (fieldName , fieldValue , false , false );
77+ Processor processor = createSetProcessor (fieldName , fieldValue , null , false , false );
7778 processor .execute (ingestDocument );
7879 assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
7980 assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -83,7 +84,7 @@ public void testSetExistingFieldWithOverrideDisabled() throws Exception {
8384 IngestDocument ingestDocument = new IngestDocument (new HashMap <>(), new HashMap <>());
8485 Object fieldValue = "foo" ;
8586 String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , fieldValue );
86- Processor processor = createSetProcessor (fieldName , "bar" , false , false );
87+ Processor processor = createSetProcessor (fieldName , "bar" , null , false , false );
8788 processor .execute (ingestDocument );
8889 assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
8990 assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -94,54 +95,69 @@ public void testSetExistingNullFieldWithOverrideDisabled() throws Exception {
9495 Object fieldValue = null ;
9596 Object newValue = "bar" ;
9697 String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , fieldValue );
97- Processor processor = createSetProcessor (fieldName , newValue , false , false );
98+ Processor processor = createSetProcessor (fieldName , newValue , null , false , false );
9899 processor .execute (ingestDocument );
99100 assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
100101 assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (newValue ));
101102 }
102103
103104 public void testSetMetadataExceptVersion () throws Exception {
104105 Metadata randomMetadata = randomFrom (Metadata .INDEX , Metadata .ID , Metadata .ROUTING );
105- Processor processor = createSetProcessor (randomMetadata .getFieldName (), "_value" , true , false );
106+ Processor processor = createSetProcessor (randomMetadata .getFieldName (), "_value" , null , true , false );
106107 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
107108 processor .execute (ingestDocument );
108109 assertThat (ingestDocument .getFieldValue (randomMetadata .getFieldName (), String .class ), Matchers .equalTo ("_value" ));
109110 }
110111
111112 public void testSetMetadataVersion () throws Exception {
112113 long version = randomNonNegativeLong ();
113- Processor processor = createSetProcessor (Metadata .VERSION .getFieldName (), version , true , false );
114+ Processor processor = createSetProcessor (Metadata .VERSION .getFieldName (), version , null , true , false );
114115 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
115116 processor .execute (ingestDocument );
116117 assertThat (ingestDocument .getFieldValue (Metadata .VERSION .getFieldName (), Long .class ), Matchers .equalTo (version ));
117118 }
118119
119120 public void testSetMetadataVersionType () throws Exception {
120121 String versionType = randomFrom ("internal" , "external" , "external_gte" );
121- Processor processor = createSetProcessor (Metadata .VERSION_TYPE .getFieldName (), versionType , true , false );
122+ Processor processor = createSetProcessor (Metadata .VERSION_TYPE .getFieldName (), versionType , null , true , false );
122123 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
123124 processor .execute (ingestDocument );
124125 assertThat (ingestDocument .getFieldValue (Metadata .VERSION_TYPE .getFieldName (), String .class ), Matchers .equalTo (versionType ));
125126 }
126127
127128 public void testSetMetadataIfSeqNo () throws Exception {
128129 long ifSeqNo = randomNonNegativeLong ();
129- Processor processor = createSetProcessor (Metadata .IF_SEQ_NO .getFieldName (), ifSeqNo , true , false );
130+ Processor processor = createSetProcessor (Metadata .IF_SEQ_NO .getFieldName (), ifSeqNo , null , true , false );
130131 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
131132 processor .execute (ingestDocument );
132133 assertThat (ingestDocument .getFieldValue (Metadata .IF_SEQ_NO .getFieldName (), Long .class ), Matchers .equalTo (ifSeqNo ));
133134 }
134135
135136 public void testSetMetadataIfPrimaryTerm () throws Exception {
136137 long ifPrimaryTerm = randomNonNegativeLong ();
137- Processor processor = createSetProcessor (Metadata .IF_PRIMARY_TERM .getFieldName (), ifPrimaryTerm , true , false );
138+ Processor processor = createSetProcessor (Metadata .IF_PRIMARY_TERM .getFieldName (), ifPrimaryTerm , null , true , false );
138139 IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
139140 processor .execute (ingestDocument );
140141 assertThat (ingestDocument .getFieldValue (Metadata .IF_PRIMARY_TERM .getFieldName (), Long .class ), Matchers .equalTo (ifPrimaryTerm ));
141142 }
142143
143- private static Processor createSetProcessor (String fieldName , Object fieldValue , boolean overrideEnabled , boolean ignoreEmptyValue ) {
144+ public void testCopyFromOtherField () throws Exception {
145+ Map <String , Object > document = new HashMap <>();
146+ Object fieldValue = RandomDocumentPicks .randomFieldValue (random ());
147+ document .put ("field" , fieldValue );
148+
149+ IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), document );
150+ String fieldName = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
151+
152+ Processor processor = createSetProcessor (fieldName , null , "field" , true , false );
153+ processor .execute (ingestDocument );
154+ assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
155+ assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
156+ }
157+
158+ private static Processor createSetProcessor (String fieldName , Object fieldValue , String copyFrom , boolean overrideEnabled ,
159+ boolean ignoreEmptyValue ) {
144160 return new SetProcessor (randomAlphaOfLength (10 ), null , new TestTemplateService .MockTemplateScript .Factory (fieldName ),
145- ValueSource .wrap (fieldValue , TestTemplateService .instance ()), overrideEnabled , ignoreEmptyValue );
161+ ValueSource .wrap (fieldValue , TestTemplateService .instance ()), copyFrom , overrideEnabled , ignoreEmptyValue );
146162 }
147163}
0 commit comments