3636import java .util .List ;
3737import java .util .Map ;
3838
39+ import static org .elasticsearch .action .index .IndexResponseTests .assertDocWriteResponse ;
3940import static org .elasticsearch .common .xcontent .XContentHelper .toXContent ;
4041
4142public class DeleteResponseTests extends ESTestCase {
@@ -61,12 +62,12 @@ public void testToAndFromXContent() throws IOException {
6162 final XContentType xContentType = randomFrom (XContentType .values ());
6263
6364 // Create a random DeleteResponse and converts it to XContent in bytes
64- DeleteResponse response = randomDeleteResponse ();
65- BytesReference responseBytes = toXContent (response , xContentType );
65+ DeleteResponse deleteResponse = randomDeleteResponse ();
66+ BytesReference deleteResponseBytes = toXContent (deleteResponse , xContentType );
6667
6768 // Parse the XContent bytes to obtain a parsed
6869 DeleteResponse parsedDeleteResponse ;
69- try (XContentParser parser = createParser (xContentType .xContent (), responseBytes )) {
70+ try (XContentParser parser = createParser (xContentType .xContent (), deleteResponseBytes )) {
7071 parsedDeleteResponse = DeleteResponse .fromXContent (parser );
7172 assertNull (parser .nextToken ());
7273 }
@@ -78,77 +79,7 @@ public void testToAndFromXContent() throws IOException {
7879 // Print the parsed object out and test that the output is the same as the original output
7980 BytesReference parsedDeleteResponseBytes = toXContent (parsedDeleteResponse , xContentType );
8081 try (XContentParser parser = createParser (xContentType .xContent (), parsedDeleteResponseBytes )) {
81- assertDeleteResponse (response , parser .map ());
82- }
83- }
84-
85- private static void assertDeleteResponse (DeleteResponse expected , Map <String , Object > actual ) {
86- assertEquals (expected .getIndex (), actual .get ("_index" ));
87- assertEquals (expected .getType (), actual .get ("_type" ));
88- assertEquals (expected .getId (), actual .get ("_id" ));
89- assertEquals (expected .getVersion (), ((Integer ) actual .get ("_version" )).longValue ());
90- assertEquals (expected .getResult ().getLowercase (), actual .get ("result" ));
91- if (expected .forcedRefresh ()) {
92- assertTrue ((Boolean ) actual .get ("forced_refresh" ));
93- } else {
94- assertFalse (actual .containsKey ("forced_refresh" ));
95- }
96- if (expected .getSeqNo () >= 0 ) {
97- assertEquals (expected .getSeqNo (), ((Integer ) actual .get ("_seq_no" )).longValue ());
98- } else {
99- assertFalse (actual .containsKey ("_seq_no" ));
100- }
101-
102- Map <String , Object > actualShards = (Map <String , Object >) actual .get ("_shards" );
103- assertNotNull (actualShards );
104- assertEquals (expected .getShardInfo ().getTotal (), actualShards .get ("total" ));
105- assertEquals (expected .getShardInfo ().getSuccessful (), actualShards .get ("successful" ));
106- assertEquals (expected .getShardInfo ().getFailed (), actualShards .get ("failed" ));
107-
108- List <Map <String , Object >> actualFailures = (List <Map <String , Object >>) actualShards .get ("failures" );
109- if (CollectionUtils .isEmpty (expected .getShardInfo ().getFailures ())) {
110- assertNull (actualFailures );
111- } else {
112- assertEquals (expected .getShardInfo ().getFailures ().length , actualFailures .size ());
113- for (int i = 0 ; i < expected .getShardInfo ().getFailures ().length ; i ++) {
114- ReplicationResponse .ShardInfo .Failure failure = expected .getShardInfo ().getFailures ()[i ];
115- Map <String , Object > actualFailure = actualFailures .get (i );
116-
117- assertEquals (failure .index (), actualFailure .get ("_index" ));
118- assertEquals (failure .shardId (), actualFailure .get ("_shard" ));
119- assertEquals (failure .nodeId (), actualFailure .get ("_node" ));
120- assertEquals (failure .status (), RestStatus .valueOf ((String ) actualFailure .get ("status" )));
121- assertEquals (failure .primary (), actualFailure .get ("primary" ));
122-
123- Throwable cause = failure .getCause ();
124- Map <String , Object > actualClause = (Map <String , Object >) actualFailure .get ("reason" );
125- assertNotNull (actualClause );
126- while (cause != null ) {
127- // The expected IndexResponse has been converted in XContent, then the resulting bytes have been
128- // parsed to create a new parsed IndexResponse. During this process, the type of the exceptions
129- // have been lost.
130- assertEquals ("exception" , actualClause .get ("type" ));
131- String expectedMessage = "Elasticsearch exception [type=" + ElasticsearchException .getExceptionName (cause )
132- + ", reason=" + cause .getMessage () + "]" ;
133- assertEquals (expectedMessage , actualClause .get ("reason" ));
134-
135- if (cause instanceof ElasticsearchException ) {
136- ElasticsearchException ex = (ElasticsearchException ) cause ;
137- Map <String , Object > actualHeaders = (Map <String , Object >) actualClause .get ("header" );
138-
139- // When a DeleteResponse is converted to XContent, the exception headers that start with "es."
140- // are added to the XContent as fields with the prefix removed. Other headers are added under
141- // a "header" root object.
142- // In the test, the "es." prefix is lost when the XContent is generating, so when the parsed
143- // IndexResponse is converted back to XContent all exception headers are under the "header" object.
144- for (String name : ex .getHeaderKeys ()) {
145- assertEquals (ex .getHeader (name ).get (0 ), actualHeaders .get (name .replaceFirst ("es." , "" )));
146- }
147- }
148- actualClause = (Map <String , Object >) actualClause .get ("caused_by" );
149- cause = cause .getCause ();
150- }
151- }
82+ assertDocWriteResponse (deleteResponse , parser .map ());
15283 }
15384 }
15485
@@ -158,9 +89,9 @@ private static DeleteResponse randomDeleteResponse() {
15889 String id = randomAsciiOfLength (5 );
15990 long seqNo = randomIntBetween (-2 , 5 );
16091 long version = (long ) randomIntBetween (0 , 5 );
161- boolean created = randomBoolean ();
92+ boolean found = randomBoolean ();
16293
163- DeleteResponse response = new DeleteResponse (shardId , type , id , seqNo , version , created );
94+ DeleteResponse response = new DeleteResponse (shardId , type , id , seqNo , version , found );
16495 response .setForcedRefresh (randomBoolean ());
16596 response .setShardInfo (RandomObjects .randomShardInfo (random (), randomBoolean ()));
16697 return response ;
0 commit comments