|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client; |
| 21 | + |
| 22 | +import org.apache.http.client.methods.HttpDelete; |
| 23 | +import org.apache.http.client.methods.HttpPut; |
| 24 | +import org.elasticsearch.common.bytes.BytesArray; |
| 25 | +import org.elasticsearch.common.xcontent.XContentType; |
| 26 | +import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest; |
| 27 | +import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest; |
| 28 | +import org.elasticsearch.test.ESTestCase; |
| 29 | + |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import static org.hamcrest.Matchers.is; |
| 35 | +import static org.hamcrest.Matchers.nullValue; |
| 36 | + |
| 37 | +public class WatcherRequestConvertersTests extends ESTestCase { |
| 38 | + |
| 39 | + public void testXPackPutWatch() throws Exception { |
| 40 | + PutWatchRequest putWatchRequest = new PutWatchRequest(); |
| 41 | + String watchId = randomAlphaOfLength(10); |
| 42 | + putWatchRequest.setId(watchId); |
| 43 | + String body = randomAlphaOfLength(20); |
| 44 | + putWatchRequest.setSource(new BytesArray(body), XContentType.JSON); |
| 45 | + |
| 46 | + Map<String, String> expectedParams = new HashMap<>(); |
| 47 | + if (randomBoolean()) { |
| 48 | + putWatchRequest.setActive(false); |
| 49 | + expectedParams.put("active", "false"); |
| 50 | + } |
| 51 | + |
| 52 | + if (randomBoolean()) { |
| 53 | + long version = randomLongBetween(10, 100); |
| 54 | + putWatchRequest.setVersion(version); |
| 55 | + expectedParams.put("version", String.valueOf(version)); |
| 56 | + } |
| 57 | + |
| 58 | + Request request = WatcherRequestConverters.putWatch(putWatchRequest); |
| 59 | + assertEquals(HttpPut.METHOD_NAME, request.getMethod()); |
| 60 | + assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint()); |
| 61 | + assertEquals(expectedParams, request.getParameters()); |
| 62 | + assertThat(request.getEntity().getContentType().getValue(), is(XContentType.JSON.mediaTypeWithoutParameters())); |
| 63 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 64 | + request.getEntity().writeTo(bos); |
| 65 | + assertThat(bos.toString("UTF-8"), is(body)); |
| 66 | + } |
| 67 | + |
| 68 | + public void testXPackDeleteWatch() { |
| 69 | + DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest(); |
| 70 | + String watchId = randomAlphaOfLength(10); |
| 71 | + deleteWatchRequest.setId(watchId); |
| 72 | + |
| 73 | + Request request = WatcherRequestConverters.deleteWatch(deleteWatchRequest); |
| 74 | + assertEquals(HttpDelete.METHOD_NAME, request.getMethod()); |
| 75 | + assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint()); |
| 76 | + assertThat(request.getEntity(), nullValue()); |
| 77 | + } |
| 78 | +} |
0 commit comments