From 5922545711728252e747a9edb91708b8ca59b144 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Mon, 12 Nov 2018 13:27:41 -0600 Subject: [PATCH 1/2] HLRC: Update Delete Role to use Optional Remove the old boolean found logic in DeleteRoleResponse and use Optional instead as the return type. --- .../elasticsearch/client/SecurityClient.java | 14 ++--- .../client/security/DeleteRoleResponse.java | 26 +------- .../SecurityDocumentationIT.java | 13 ++-- .../security/DeleteRoleResponseTests.java | 59 ------------------- 4 files changed, 17 insertions(+), 95 deletions(-) delete mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java index d3b38aaf9e9d2..08881ee735c1b 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/SecurityClient.java @@ -48,9 +48,9 @@ import org.elasticsearch.client.security.PutUserResponse; import java.io.IOException; +import java.util.Optional; import static java.util.Collections.emptySet; -import static java.util.Collections.singleton; /** * A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Security APIs. @@ -396,9 +396,9 @@ public void deleteRoleMappingAsync(DeleteRoleMappingRequest request, RequestOpti * @return the response from the delete role call * @throws IOException in case there is a problem sending the request or parsing back the response */ - public DeleteRoleResponse deleteRole(DeleteRoleRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, SecurityRequestConverters::deleteRole, options, - DeleteRoleResponse::fromXContent, singleton(404)); + public Optional deleteRole(DeleteRoleRequest request, RequestOptions options) throws IOException { + return restHighLevelClient.performRequestAndParseOptionalEntity(request, SecurityRequestConverters::deleteRole, options, + DeleteRoleResponse::fromXContent); } /** @@ -409,9 +409,9 @@ public DeleteRoleResponse deleteRole(DeleteRoleRequest request, RequestOptions o * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion */ - public void deleteRoleAsync(DeleteRoleRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, SecurityRequestConverters::deleteRole, options, - DeleteRoleResponse::fromXContent, listener, singleton(404)); + public void deleteRoleAsync(DeleteRoleRequest request, RequestOptions options, ActionListener> listener) { + restHighLevelClient.performRequestAsyncAndParseOptionalEntity(request, SecurityRequestConverters::deleteRole, options, + DeleteRoleResponse::fromXContent, listener); } /** diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java index 3db10bb97029a..2f3be1f8463d0 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/DeleteRoleResponse.java @@ -19,37 +19,17 @@ package org.elasticsearch.client.security; -import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentParser; -import java.io.IOException; - -import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; - /** * Response for a role being deleted from the native realm */ public final class DeleteRoleResponse { - private final boolean found; - - public DeleteRoleResponse(boolean found) { - this.found = found; - } - - public boolean isFound() { - return this.found; - } - - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("delete_role_response", - true, args -> new DeleteRoleResponse((boolean) args[0])); - - static { - PARSER.declareBoolean(constructorArg(), new ParseField("found")); + public DeleteRoleResponse() { } - public static DeleteRoleResponse fromXContent(XContentParser parser) throws IOException { - return PARSER.parse(parser, null); + public static DeleteRoleResponse fromXContent(XContentParser parser) { + return new DeleteRoleResponse(); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index ffa30e16c0468..2c1b372a1e91b 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -70,6 +70,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -712,27 +713,27 @@ public void testDeleteRole() throws Exception { // end::delete-role-request // tag::delete-role-execute - DeleteRoleResponse deleteRoleResponse = client.security().deleteRole(deleteRoleRequest, RequestOptions.DEFAULT); + Optional deleteRoleResponse = client.security().deleteRole(deleteRoleRequest, RequestOptions.DEFAULT); // end::delete-role-execute // tag::delete-role-response - boolean found = deleteRoleResponse.isFound(); // <1> + boolean found = deleteRoleResponse.isPresent(); // <1> // end::delete-role-response assertTrue(found); // check if deleting the already deleted role again will give us a different response deleteRoleResponse = client.security().deleteRole(deleteRoleRequest, RequestOptions.DEFAULT); - assertFalse(deleteRoleResponse.isFound()); + assertFalse(deleteRoleResponse.isPresent()); } { DeleteRoleRequest deleteRoleRequest = new DeleteRoleRequest("testrole"); - ActionListener listener; + ActionListener> listener; //tag::delete-role-execute-listener - listener = new ActionListener() { + listener = new ActionListener>() { @Override - public void onResponse(DeleteRoleResponse deleteRoleResponse) { + public void onResponse(Optional deleteRoleResponse) { // <1> } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java deleted file mode 100644 index a2c08fcf8814e..0000000000000 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/DeleteRoleResponseTests.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.client.security; - -import org.elasticsearch.common.bytes.BytesReference; -import org.elasticsearch.common.xcontent.NamedXContentRegistry; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.test.ESTestCase; - -import java.io.IOException; - -public class DeleteRoleResponseTests extends ESTestCase { - - public void testBasicParsing() throws IOException { - XContentType contentType = randomFrom(XContentType.values()); - final boolean found = randomBoolean(); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject() - .field("found", found).endObject(); - BytesReference bytes = BytesReference.bytes(builder); - - DeleteRoleResponse response = parse(builder.contentType(), bytes); - assertEquals(found, response.isFound()); - } - - public void testParsingWithMissingField() throws IOException { - XContentType contentType = randomFrom(XContentType.values()); - XContentBuilder builder = XContentFactory.contentBuilder(contentType).startObject().endObject(); - BytesReference bytes = BytesReference.bytes(builder); - - expectThrows(IllegalArgumentException.class, () -> parse(builder.contentType(), bytes)); - } - - private DeleteRoleResponse parse(XContentType contentType, BytesReference bytes) throws IOException { - XContentParser parser = XContentFactory.xContent(contentType) - .createParser(NamedXContentRegistry.EMPTY, null, bytes.streamInput()); - parser.nextToken(); - return DeleteRoleResponse.fromXContent(parser); - } - -} From 1ad0eb83d18193b26b493fd321df295d1eed65a8 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Mon, 12 Nov 2018 13:51:26 -0600 Subject: [PATCH 2/2] Fix response --- docs/java-rest/high-level/security/delete-role.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/java-rest/high-level/security/delete-role.asciidoc b/docs/java-rest/high-level/security/delete-role.asciidoc index 0086b89bb6897..dd394c734487d 100644 --- a/docs/java-rest/high-level/security/delete-role.asciidoc +++ b/docs/java-rest/high-level/security/delete-role.asciidoc @@ -1,7 +1,7 @@ -- :api: delete-role :request: DeleteRoleRequest -:response: DeleteRoleResponse +:response: Optional -- [id="{upid}-{api}"]