-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HLRC: split watcher request converters (#33442)
In an effort to encapsulate the different clients, the request converters are being shuffled around. This splits the WatcherClient request converters.
- Loading branch information
Showing
3 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
62 changes: 62 additions & 0 deletions
62
client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherRequestConverters.java
This file contains 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,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; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...rest-high-level/src/test/java/org/elasticsearch/client/WatcherRequestConvertersTests.java
This file contains 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,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<String, String> 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()); | ||
} | ||
} |