29
29
import org .hamcrest .Matchers ;
30
30
31
31
import java .util .HashMap ;
32
+ import java .util .Map ;
32
33
33
34
import static org .hamcrest .Matchers .equalTo ;
34
35
@@ -38,7 +39,7 @@ public void testSetExistingFields() throws Exception {
38
39
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
39
40
String fieldName = RandomDocumentPicks .randomExistingFieldName (random (), ingestDocument );
40
41
Object fieldValue = RandomDocumentPicks .randomFieldValue (random ());
41
- Processor processor = createSetProcessor (fieldName , fieldValue , true , false );
42
+ Processor processor = createSetProcessor (fieldName , fieldValue , null , true , false );
42
43
processor .execute (ingestDocument );
43
44
assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
44
45
assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -50,7 +51,7 @@ public void testSetNewFields() throws Exception {
50
51
IngestDocument testIngestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
51
52
Object fieldValue = RandomDocumentPicks .randomFieldValue (random ());
52
53
String fieldName = RandomDocumentPicks .addRandomField (random (), testIngestDocument , fieldValue );
53
- Processor processor = createSetProcessor (fieldName , fieldValue , true , false );
54
+ Processor processor = createSetProcessor (fieldName , fieldValue , null , true , false );
54
55
processor .execute (ingestDocument );
55
56
assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
56
57
assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -59,7 +60,7 @@ public void testSetNewFields() throws Exception {
59
60
public void testSetFieldsTypeMismatch () throws Exception {
60
61
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random (), new HashMap <>());
61
62
ingestDocument .setFieldValue ("field" , "value" );
62
- Processor processor = createSetProcessor ("field.inner" , "value" , true , false );
63
+ Processor processor = createSetProcessor ("field.inner" , "value" , null , true , false );
63
64
try {
64
65
processor .execute (ingestDocument );
65
66
fail ("processor execute should have failed" );
@@ -73,7 +74,7 @@ public void testSetNewFieldWithOverrideDisabled() throws Exception {
73
74
IngestDocument ingestDocument = new IngestDocument (new HashMap <>(), new HashMap <>());
74
75
String fieldName = RandomDocumentPicks .randomFieldName (random ());
75
76
Object fieldValue = RandomDocumentPicks .randomFieldValue (random ());
76
- Processor processor = createSetProcessor (fieldName , fieldValue , false , false );
77
+ Processor processor = createSetProcessor (fieldName , fieldValue , null , false , false );
77
78
processor .execute (ingestDocument );
78
79
assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
79
80
assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -83,7 +84,7 @@ public void testSetExistingFieldWithOverrideDisabled() throws Exception {
83
84
IngestDocument ingestDocument = new IngestDocument (new HashMap <>(), new HashMap <>());
84
85
Object fieldValue = "foo" ;
85
86
String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , fieldValue );
86
- Processor processor = createSetProcessor (fieldName , "bar" , false , false );
87
+ Processor processor = createSetProcessor (fieldName , "bar" , null , false , false );
87
88
processor .execute (ingestDocument );
88
89
assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
89
90
assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (fieldValue ));
@@ -94,54 +95,69 @@ public void testSetExistingNullFieldWithOverrideDisabled() throws Exception {
94
95
Object fieldValue = null ;
95
96
Object newValue = "bar" ;
96
97
String fieldName = RandomDocumentPicks .addRandomField (random (), ingestDocument , fieldValue );
97
- Processor processor = createSetProcessor (fieldName , newValue , false , false );
98
+ Processor processor = createSetProcessor (fieldName , newValue , null , false , false );
98
99
processor .execute (ingestDocument );
99
100
assertThat (ingestDocument .hasField (fieldName ), equalTo (true ));
100
101
assertThat (ingestDocument .getFieldValue (fieldName , Object .class ), equalTo (newValue ));
101
102
}
102
103
103
104
public void testSetMetadataExceptVersion () throws Exception {
104
105
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 );
106
107
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
107
108
processor .execute (ingestDocument );
108
109
assertThat (ingestDocument .getFieldValue (randomMetadata .getFieldName (), String .class ), Matchers .equalTo ("_value" ));
109
110
}
110
111
111
112
public void testSetMetadataVersion () throws Exception {
112
113
long version = randomNonNegativeLong ();
113
- Processor processor = createSetProcessor (Metadata .VERSION .getFieldName (), version , true , false );
114
+ Processor processor = createSetProcessor (Metadata .VERSION .getFieldName (), version , null , true , false );
114
115
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
115
116
processor .execute (ingestDocument );
116
117
assertThat (ingestDocument .getFieldValue (Metadata .VERSION .getFieldName (), Long .class ), Matchers .equalTo (version ));
117
118
}
118
119
119
120
public void testSetMetadataVersionType () throws Exception {
120
121
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 );
122
123
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
123
124
processor .execute (ingestDocument );
124
125
assertThat (ingestDocument .getFieldValue (Metadata .VERSION_TYPE .getFieldName (), String .class ), Matchers .equalTo (versionType ));
125
126
}
126
127
127
128
public void testSetMetadataIfSeqNo () throws Exception {
128
129
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 );
130
131
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
131
132
processor .execute (ingestDocument );
132
133
assertThat (ingestDocument .getFieldValue (Metadata .IF_SEQ_NO .getFieldName (), Long .class ), Matchers .equalTo (ifSeqNo ));
133
134
}
134
135
135
136
public void testSetMetadataIfPrimaryTerm () throws Exception {
136
137
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 );
138
139
IngestDocument ingestDocument = RandomDocumentPicks .randomIngestDocument (random ());
139
140
processor .execute (ingestDocument );
140
141
assertThat (ingestDocument .getFieldValue (Metadata .IF_PRIMARY_TERM .getFieldName (), Long .class ), Matchers .equalTo (ifPrimaryTerm ));
141
142
}
142
143
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 ) {
144
160
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 );
146
162
}
147
163
}
0 commit comments