-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add fromxcontent methods to delete response #22712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
core/src/test/java/org/elasticsearch/action/delete/DeleteResponseTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.action.delete; | ||
|
|
||
| import org.elasticsearch.action.DocWriteResponse; | ||
| import org.elasticsearch.action.support.replication.ReplicationResponse; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
| import org.elasticsearch.index.shard.ShardId; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.test.RandomObjects; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.action.index.IndexResponseTests.assertDocWriteResponse; | ||
| import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; | ||
|
|
||
| public class DeleteResponseTests extends ESTestCase { | ||
|
|
||
| public void testToXContent() throws IOException { | ||
| { | ||
| DeleteResponse response = new DeleteResponse(new ShardId("index", "index_uuid", 0), "type", "id", 5, true); | ||
| String output = Strings.toString(response); | ||
| assertEquals("{\"found\":true,\"_index\":\"index\",\"_type\":\"type\",\"_id\":\"id\",\"_version\":5,\"result\":\"deleted\"," + | ||
| "\"_shards\":null}", output); | ||
| } | ||
| { | ||
| DeleteResponse response = new DeleteResponse(new ShardId("index", "index_uuid", 0), "type", "id", 7, true); | ||
| response.setForcedRefresh(true); | ||
| response.setShardInfo(new ReplicationResponse.ShardInfo(10, 5)); | ||
| String output = Strings.toString(response); | ||
| assertEquals("{\"found\":true,\"_index\":\"index\",\"_type\":\"type\",\"_id\":\"id\",\"_version\":7,\"result\":\"deleted\"," + | ||
| "\"forced_refresh\":true,\"_shards\":{\"total\":10,\"successful\":5,\"failed\":0}}", output); | ||
| } | ||
| } | ||
|
|
||
| public void testToAndFromXContent() throws IOException { | ||
| final XContentType xContentType = randomFrom(XContentType.values()); | ||
|
|
||
| // Create a random DeleteResponse and converts it to XContent in bytes | ||
| DeleteResponse deleteResponse = randomDeleteResponse(); | ||
| BytesReference deleteResponseBytes = toXContent(deleteResponse, xContentType); | ||
|
|
||
| // Parse the XContent bytes to obtain a parsed | ||
| DeleteResponse parsedDeleteResponse; | ||
| try (XContentParser parser = createParser(xContentType.xContent(), deleteResponseBytes)) { | ||
| parsedDeleteResponse = DeleteResponse.fromXContent(parser); | ||
| assertNull(parser.nextToken()); | ||
| } | ||
|
|
||
| // We can't use equals() to compare the original and the parsed delete response | ||
| // because the random delete response can contain shard failures with exceptions, | ||
| // and those exceptions are not parsed back with the same types. | ||
|
|
||
| // Print the parsed object out and test that the output is the same as the original output | ||
| BytesReference parsedDeleteResponseBytes = toXContent(parsedDeleteResponse, xContentType); | ||
| try (XContentParser parser = createParser(xContentType.xContent(), parsedDeleteResponseBytes)) { | ||
| assertDeleteResponse(deleteResponse, parser.map()); | ||
| } | ||
| } | ||
|
|
||
| private static void assertDeleteResponse(DeleteResponse expected, Map<String, Object> actual) { | ||
| assertDocWriteResponse(expected, actual); | ||
| if (expected.getResult() == DocWriteResponse.Result.DELETED) { | ||
| assertTrue((boolean) actual.get("found")); | ||
| } else { | ||
| assertFalse((boolean) actual.get("found")); | ||
| } | ||
| } | ||
|
|
||
| private static DeleteResponse randomDeleteResponse() { | ||
| ShardId shardId = new ShardId(randomAsciiOfLength(5), randomAsciiOfLength(5), randomIntBetween(0, 5)); | ||
| String type = randomAsciiOfLength(5); | ||
| String id = randomAsciiOfLength(5); | ||
| long version = (long) randomIntBetween(0, 5); | ||
| boolean found = randomBoolean(); | ||
|
|
||
| DeleteResponse response = new DeleteResponse(shardId, type, id, version, found); | ||
| response.setForcedRefresh(randomBoolean()); | ||
| response.setShardInfo(RandomObjects.randomShardInfo(random(), randomBoolean())); | ||
| return response; | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index -> delete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I fixed that as well in master branch.