51
51
import static org .elasticsearch .action .support .WriteRequest .RefreshPolicy ;
52
52
import static org .elasticsearch .common .util .set .Sets .newHashSet ;
53
53
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
54
+ import static org .hamcrest .Matchers .containsString ;
54
55
import static org .hamcrest .Matchers .equalTo ;
55
56
import static org .hamcrest .Matchers .hasEntry ;
56
57
import static org .hamcrest .Matchers .hasSize ;
@@ -89,6 +90,10 @@ public void testParser() throws Exception {
89
90
if (writeTimeout != null ) {
90
91
builder .field (IndexAction .Field .TIMEOUT .getPreferredName (), writeTimeout .millis ());
91
92
}
93
+ DocWriteRequest .OpType opType = randomBoolean () ? DocWriteRequest .OpType .fromId (randomFrom (new Byte [] { 0 , 1 })) : null ;
94
+ if (opType != null ) {
95
+ builder .field (IndexAction .Field .OP_TYPE .getPreferredName (), opType .getLowercase ());
96
+ }
92
97
builder .endObject ();
93
98
IndexActionFactory actionParser = new IndexActionFactory (Settings .EMPTY , client );
94
99
XContentParser parser = createParser (builder );
@@ -102,6 +107,9 @@ public void testParser() throws Exception {
102
107
if (timestampField != null ) {
103
108
assertThat (executable .action ().executionTimeField , equalTo (timestampField ));
104
109
}
110
+ if (opType != null ) {
111
+ assertThat (executable .action ().opType , equalTo (opType ));
112
+ }
105
113
assertThat (executable .action ().timeout , equalTo (writeTimeout ));
106
114
}
107
115
@@ -130,20 +138,47 @@ public void testParserFailure() throws Exception {
130
138
.endObject ());
131
139
}
132
140
141
+ public void testOpTypeThatCannotBeParsed () throws Exception {
142
+ expectParseFailure (jsonBuilder ()
143
+ .startObject ()
144
+ .field (IndexAction .Field .OP_TYPE .getPreferredName (), randomAlphaOfLength (10 ))
145
+ .endObject (),
146
+ "failed to parse op_type value for field [op_type]" );
147
+ }
148
+
149
+ public void testUnsupportedOpType () throws Exception {
150
+ expectParseFailure (jsonBuilder ()
151
+ .startObject ()
152
+ .field (IndexAction .Field .OP_TYPE .getPreferredName (),
153
+ randomFrom (DocWriteRequest .OpType .UPDATE .name (), DocWriteRequest .OpType .DELETE .name ()))
154
+ .endObject (),
155
+ "op_type value for field [op_type] must be [index] or [create]" );
156
+ }
157
+
158
+ private void expectParseFailure (XContentBuilder builder , String expectedMessage ) throws Exception {
159
+ expectFailure (ElasticsearchParseException .class , builder , expectedMessage );
160
+ }
161
+
133
162
private void expectParseFailure (XContentBuilder builder ) throws Exception {
134
163
expectFailure (ElasticsearchParseException .class , builder );
135
164
}
136
165
137
166
private void expectFailure (Class clazz , XContentBuilder builder ) throws Exception {
167
+ expectFailure (clazz , builder , null );
168
+ }
169
+
170
+ private void expectFailure (Class clazz , XContentBuilder builder , String expectedMessage ) throws Exception {
138
171
IndexActionFactory actionParser = new IndexActionFactory (Settings .EMPTY , client );
139
172
XContentParser parser = createParser (builder );
140
173
parser .nextToken ();
141
- expectThrows (clazz , () ->
142
- actionParser .parseExecutable (randomAlphaOfLength (4 ), randomAlphaOfLength (5 ), parser ));
174
+ Throwable t = expectThrows (clazz , () -> actionParser .parseExecutable (randomAlphaOfLength (4 ), randomAlphaOfLength (5 ), parser ));
175
+ if (expectedMessage != null ) {
176
+ assertThat (t .getMessage (), containsString (expectedMessage ));
177
+ }
143
178
}
144
179
145
180
public void testUsingParameterIdWithBulkOrIdFieldThrowsIllegalState () {
146
- final IndexAction action = new IndexAction ("test-index" , "123" , null , null , null , refreshPolicy );
181
+ final IndexAction action = new IndexAction ("test-index" , "123" , null , null , null , null , refreshPolicy );
147
182
final ExecutableIndexAction executable = new ExecutableIndexAction (action , logger , client ,
148
183
TimeValue .timeValueSeconds (30 ), TimeValue .timeValueSeconds (30 ));
149
184
final Map <String , Object > docWithId = Map .of (
@@ -191,7 +226,7 @@ public void testThatIndexTypeIdDynamically() throws Exception {
191
226
192
227
final IndexAction action = new IndexAction (configureIndexDynamically ? null : "my_index" ,
193
228
configureIdDynamically ? null : "my_id" ,
194
- null , null , null , refreshPolicy );
229
+ null , null , null , null , refreshPolicy );
195
230
final ExecutableIndexAction executable = new ExecutableIndexAction (action , logger , client ,
196
231
TimeValue .timeValueSeconds (30 ), TimeValue .timeValueSeconds (30 ));
197
232
@@ -211,7 +246,7 @@ public void testThatIndexTypeIdDynamically() throws Exception {
211
246
}
212
247
213
248
public void testThatIndexActionCanBeConfiguredWithDynamicIndexNameAndBulk () throws Exception {
214
- final IndexAction action = new IndexAction (null , null , null , null , null , refreshPolicy );
249
+ final IndexAction action = new IndexAction (null , null , null , null , null , null , refreshPolicy );
215
250
final ExecutableIndexAction executable = new ExecutableIndexAction (action , logger , client ,
216
251
TimeValue .timeValueSeconds (30 ), TimeValue .timeValueSeconds (30 ));
217
252
@@ -239,7 +274,7 @@ public void testThatIndexActionCanBeConfiguredWithDynamicIndexNameAndBulk() thro
239
274
public void testConfigureIndexInMapAndAction () {
240
275
String fieldName = "_index" ;
241
276
final IndexAction action = new IndexAction ("my_index" ,
242
- null ,null , null , null , refreshPolicy );
277
+ null , null , null , null , null , refreshPolicy );
243
278
final ExecutableIndexAction executable = new ExecutableIndexAction (action , logger , client ,
244
279
TimeValue .timeValueSeconds (30 ), TimeValue .timeValueSeconds (30 ));
245
280
@@ -258,8 +293,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
258
293
String docId = randomAlphaOfLength (5 );
259
294
String timestampField = randomFrom ("@timestamp" , null );
260
295
261
- IndexAction action = new IndexAction ("test-index" , docIdAsParam ? docId : null , timestampField , null , null ,
262
- refreshPolicy );
296
+ IndexAction action = new IndexAction ("test-index" , docIdAsParam ? docId : null , null , timestampField , null , null , refreshPolicy );
263
297
ExecutableIndexAction executable = new ExecutableIndexAction (action , logger , client , TimeValue .timeValueSeconds (30 ),
264
298
TimeValue .timeValueSeconds (30 ));
265
299
ZonedDateTime executionTime = DateUtils .nowWithMillisResolution ();
@@ -308,7 +342,7 @@ public void testIndexActionExecuteSingleDoc() throws Exception {
308
342
}
309
343
310
344
public void testFailureResult () throws Exception {
311
- IndexAction action = new IndexAction ("test-index" , null , "@timestamp" , null , null , refreshPolicy );
345
+ IndexAction action = new IndexAction ("test-index" , null , null , "@timestamp" , null , null , refreshPolicy );
312
346
ExecutableIndexAction executable = new ExecutableIndexAction (action , logger , client ,
313
347
TimeValue .timeValueSeconds (30 ), TimeValue .timeValueSeconds (30 ));
314
348
0 commit comments