Skip to content

Commit 0315dcc

Browse files
committed
Use now common methods with index/update
Brought by #22229
1 parent 718a6b9 commit 0315dcc

File tree

2 files changed

+13
-85
lines changed

2 files changed

+13
-85
lines changed

core/src/main/java/org/elasticsearch/action/delete/DeleteResponse.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
6565
return builder;
6666
}
6767

68-
/**
69-
* ConstructingObjectParser used to parse the {@link IndexResponse}. We use a ObjectParser here
70-
* because most fields are parsed by the parent abstract class {@link DocWriteResponse} and it's
71-
* not easy to parse part of the fields in the parent class and other fields in the children class
72-
* using the usual streamed parsing method.
73-
*/
7468
private static final ConstructingObjectParser<DeleteResponse, Void> PARSER;
7569
static {
7670
PARSER = new ConstructingObjectParser<>(DeleteResponse.class.getName(),
@@ -80,9 +74,12 @@ public XContentBuilder innerToXContent(XContentBuilder builder, Params params) t
8074
String type = (String) args[1];
8175
String id = (String) args[2];
8276
long version = (long) args[3];
83-
long seqNo = (args[5] != null) ? (long) args[5] : SequenceNumbersService.UNASSIGNED_SEQ_NO;
84-
boolean found = (boolean) args[6];
85-
return new DeleteResponse(shardId, type, id, seqNo, version, found);
77+
ShardInfo shardInfo = (ShardInfo) args[5];
78+
long seqNo = (args[6] != null) ? (long) args[6] : SequenceNumbersService.UNASSIGNED_SEQ_NO;
79+
boolean found = (boolean) args[7];
80+
DeleteResponse deleteResponse = new DeleteResponse(shardId, type, id, seqNo, version, found);
81+
deleteResponse.setShardInfo(shardInfo);
82+
return deleteResponse;
8683
});
8784
DocWriteResponse.declareParserFields(PARSER);
8885
PARSER.declareBoolean(constructorArg(), new ParseField(FOUND));

core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.List;
3737
import java.util.Map;
3838

39+
import static org.elasticsearch.action.index.IndexResponseTests.assertDocWriteResponse;
3940
import static org.elasticsearch.common.xcontent.XContentHelper.toXContent;
4041

4142
public 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

Comments
 (0)