From 9c9ee29fe9d68d685ca5f6848146fc3b0f504b63 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Wed, 5 Sep 2018 15:41:45 -0500 Subject: [PATCH] HLRC: split watcher request converters In an effort to encapsulate the different clients, the request converters are being shuffled around. This splits the WatcherClient request converters. --- .../client/RequestConverters.java | 35 +-------- .../elasticsearch/client/WatcherClient.java | 8 +- .../client/WatcherRequestConverters.java | 62 +++++++++++++++ .../client/RequestConvertersTests.java | 45 +---------- .../client/WatcherRequestConvertersTests.java | 78 +++++++++++++++++++ 5 files changed, 146 insertions(+), 82 deletions(-) create mode 100644 client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java create mode 100644 client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java index 89521b5e9b06f..eed82dc6fb16e 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java @@ -111,13 +111,11 @@ import org.elasticsearch.index.reindex.UpdateByQueryRequest; import org.elasticsearch.protocol.xpack.XPackInfoRequest; import org.elasticsearch.protocol.xpack.XPackUsageRequest; +import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; import org.elasticsearch.protocol.xpack.license.DeleteLicenseRequest; import org.elasticsearch.protocol.xpack.license.GetLicenseRequest; import org.elasticsearch.protocol.xpack.license.PutLicenseRequest; import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; -import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; import org.elasticsearch.rest.action.search.RestSearchAction; import org.elasticsearch.script.mustache.MultiSearchTemplateRequest; import org.elasticsearch.script.mustache.SearchTemplateRequest; @@ -1159,37 +1157,6 @@ static Request xPackGraphExplore(GraphExploreRequest exploreRequest) throws IOEx return request; } - static Request xPackWatcherPutWatch(PutWatchRequest putWatchRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("watcher") - .addPathPartAsIs("watch") - .addPathPart(putWatchRequest.getId()) - .build(); - - Request request = new Request(HttpPut.METHOD_NAME, endpoint); - Params params = new Params(request).withVersion(putWatchRequest.getVersion()); - if (putWatchRequest.isActive() == false) { - params.putParam("active", "false"); - } - ContentType contentType = createContentType(putWatchRequest.xContentType()); - BytesReference source = putWatchRequest.getSource(); - request.setEntity(new ByteArrayEntity(source.toBytesRef().bytes, 0, source.length(), contentType)); - return request; - } - - static Request xPackWatcherDeleteWatch(DeleteWatchRequest deleteWatchRequest) { - String endpoint = new EndpointBuilder() - .addPathPartAsIs("_xpack") - .addPathPartAsIs("watcher") - .addPathPartAsIs("watch") - .addPathPart(deleteWatchRequest.getId()) - .build(); - - Request request = new Request(HttpDelete.METHOD_NAME, endpoint); - return request; - } - static Request xpackUsage(XPackUsageRequest usageRequest) { Request request = new Request(HttpGet.METHOD_NAME, "/_xpack/usage"); Params parameters = new Params(request); diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java index 48487926f024b..b1a3eb3f87bf9 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java @@ -47,7 +47,7 @@ public final class WatcherClient { * @throws IOException in case there is a problem sending the request or parsing back the response */ public PutWatchResponse putWatch(PutWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::xPackWatcherPutWatch, options, + return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::putWatch, options, PutWatchResponse::fromXContent, emptySet()); } @@ -61,7 +61,7 @@ public PutWatchResponse putWatch(PutWatchRequest request, RequestOptions options */ public void putWatchAsync(PutWatchRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackWatcherPutWatch, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::putWatch, options, PutWatchResponse::fromXContent, listener, emptySet()); } @@ -75,7 +75,7 @@ public void putWatchAsync(PutWatchRequest request, RequestOptions options, * @throws IOException in case there is a problem sending the request or parsing back the response */ public DeleteWatchResponse deleteWatch(DeleteWatchRequest request, RequestOptions options) throws IOException { - return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::xPackWatcherDeleteWatch, options, + return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::deleteWatch, options, DeleteWatchResponse::fromXContent, singleton(404)); } @@ -88,7 +88,7 @@ public DeleteWatchResponse deleteWatch(DeleteWatchRequest request, RequestOption * @param listener the listener to be notified upon request completion */ public void deleteWatchAsync(DeleteWatchRequest request, RequestOptions options, ActionListener listener) { - restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackWatcherDeleteWatch, options, + restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::deleteWatch, options, DeleteWatchResponse::fromXContent, listener, singleton(404)); } } diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java new file mode 100644 index 0000000000000..3b52d1c7b9943 --- /dev/null +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java @@ -0,0 +1,62 @@ +/* + * 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; + +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.ContentType; +import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; +import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; + +public class WatcherRequestConverters { + + static Request putWatch(PutWatchRequest putWatchRequest) { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_xpack") + .addPathPartAsIs("watcher") + .addPathPartAsIs("watch") + .addPathPart(putWatchRequest.getId()) + .build(); + + Request request = new Request(HttpPut.METHOD_NAME, endpoint); + RequestConverters.Params params = new RequestConverters.Params(request).withVersion(putWatchRequest.getVersion()); + if (putWatchRequest.isActive() == false) { + params.putParam("active", "false"); + } + ContentType contentType = RequestConverters.createContentType(putWatchRequest.xContentType()); + BytesReference source = putWatchRequest.getSource(); + request.setEntity(new ByteArrayEntity(source.toBytesRef().bytes, 0, source.length(), contentType)); + return request; + } + + static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) { + String endpoint = new RequestConverters.EndpointBuilder() + .addPathPartAsIs("_xpack") + .addPathPartAsIs("watcher") + .addPathPartAsIs("watch") + .addPathPart(deleteWatchRequest.getId()) + .build(); + + Request request = new Request(HttpDelete.METHOD_NAME, endpoint); + return request; + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java index a6845448b8299..fa65bc4e1df8a 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/RequestConvertersTests.java @@ -127,11 +127,9 @@ import org.elasticsearch.index.reindex.RemoteInfo; import org.elasticsearch.index.reindex.UpdateByQueryRequest; import org.elasticsearch.protocol.xpack.XPackInfoRequest; -import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; -import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; import org.elasticsearch.protocol.xpack.graph.GraphExploreRequest; import org.elasticsearch.protocol.xpack.graph.Hop; -import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; +import org.elasticsearch.protocol.xpack.migration.IndexUpgradeInfoRequest; import org.elasticsearch.repositories.fs.FsRepository; import org.elasticsearch.rest.action.search.RestSearchAction; import org.elasticsearch.script.Script; @@ -152,7 +150,6 @@ import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.RandomObjects; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @@ -2628,35 +2625,6 @@ public void testGetMigrationAssistance() { assertEquals(expectedParams, request.getParameters()); } - public void testXPackPutWatch() throws Exception { - PutWatchRequest putWatchRequest = new PutWatchRequest(); - String watchId = randomAlphaOfLength(10); - putWatchRequest.setId(watchId); - String body = randomAlphaOfLength(20); - putWatchRequest.setSource(new BytesArray(body), XContentType.JSON); - - Map expectedParams = new HashMap<>(); - if (randomBoolean()) { - putWatchRequest.setActive(false); - expectedParams.put("active", "false"); - } - - if (randomBoolean()) { - long version = randomLongBetween(10, 100); - putWatchRequest.setVersion(version); - expectedParams.put("version", String.valueOf(version)); - } - - Request request = RequestConverters.xPackWatcherPutWatch(putWatchRequest); - assertEquals(HttpPut.METHOD_NAME, request.getMethod()); - assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint()); - assertEquals(expectedParams, request.getParameters()); - assertThat(request.getEntity().getContentType().getValue(), is(XContentType.JSON.mediaTypeWithoutParameters())); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - request.getEntity().writeTo(bos); - assertThat(bos.toString("UTF-8"), is(body)); - } - public void testGraphExplore() throws Exception { Map expectedParams = new HashMap<>(); @@ -2686,17 +2654,6 @@ public void testGraphExplore() throws Exception { assertToXContentBody(graphExploreRequest, request.getEntity()); } - public void testXPackDeleteWatch() { - DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest(); - String watchId = randomAlphaOfLength(10); - deleteWatchRequest.setId(watchId); - - Request request = RequestConverters.xPackWatcherDeleteWatch(deleteWatchRequest); - assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); - assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint()); - assertThat(request.getEntity(), nullValue()); - } - /** * Randomize the {@link FetchSourceContext} request parameters. */ diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java new file mode 100644 index 0000000000000..203d0826c6d96 --- /dev/null +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java @@ -0,0 +1,78 @@ +/* + * 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; + +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpPut; +import org.elasticsearch.common.bytes.BytesArray; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; +import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; +import org.elasticsearch.test.ESTestCase; + +import java.io.ByteArrayOutputStream; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; + +public class WatcherRequestConvertersTests extends ESTestCase { + + public void testXPackPutWatch() throws Exception { + PutWatchRequest putWatchRequest = new PutWatchRequest(); + String watchId = randomAlphaOfLength(10); + putWatchRequest.setId(watchId); + String body = randomAlphaOfLength(20); + putWatchRequest.setSource(new BytesArray(body), XContentType.JSON); + + Map expectedParams = new HashMap<>(); + if (randomBoolean()) { + putWatchRequest.setActive(false); + expectedParams.put("active", "false"); + } + + if (randomBoolean()) { + long version = randomLongBetween(10, 100); + putWatchRequest.setVersion(version); + expectedParams.put("version", String.valueOf(version)); + } + + Request request = WatcherRequestConverters.putWatch(putWatchRequest); + assertEquals(HttpPut.METHOD_NAME, request.getMethod()); + assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint()); + assertEquals(expectedParams, request.getParameters()); + assertThat(request.getEntity().getContentType().getValue(), is(XContentType.JSON.mediaTypeWithoutParameters())); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + request.getEntity().writeTo(bos); + assertThat(bos.toString("UTF-8"), is(body)); + } + + public void testXPackDeleteWatch() { + DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest(); + String watchId = randomAlphaOfLength(10); + deleteWatchRequest.setId(watchId); + + Request request = WatcherRequestConverters.deleteWatch(deleteWatchRequest); + assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); + assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint()); + assertThat(request.getEntity(), nullValue()); + } +}