|
| 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.rest.action.admin.cluster; |
| 21 | + |
| 22 | +import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; |
| 23 | +import org.elasticsearch.action.support.ActiveShardCount; |
| 24 | +import org.elasticsearch.cluster.health.ClusterHealthStatus; |
| 25 | +import org.elasticsearch.common.Priority; |
| 26 | +import org.elasticsearch.common.unit.TimeValue; |
| 27 | +import org.elasticsearch.rest.RestRequest; |
| 28 | +import org.elasticsearch.test.ESTestCase; |
| 29 | +import org.elasticsearch.test.rest.FakeRestRequest; |
| 30 | + |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +import static org.hamcrest.Matchers.equalTo; |
| 35 | + |
| 36 | +public class RestClusterHealthActionTests extends ESTestCase { |
| 37 | + |
| 38 | + public void testFromRequest() { |
| 39 | + Map<String, String> params = new HashMap<>(); |
| 40 | + String index = "index"; |
| 41 | + boolean local = randomBoolean(); |
| 42 | + String masterTimeout = randomTimeValue(); |
| 43 | + String timeout = randomTimeValue(); |
| 44 | + ClusterHealthStatus waitForStatus = randomFrom(ClusterHealthStatus.values()); |
| 45 | + boolean waitForNoRelocatingShards = randomBoolean(); |
| 46 | + boolean waitForNoInitializingShards = randomBoolean(); |
| 47 | + int waitForActiveShards = randomIntBetween(1, 3); |
| 48 | + String waitForNodes = "node"; |
| 49 | + Priority waitForEvents = randomFrom(Priority.values()); |
| 50 | + |
| 51 | + params.put("index", index); |
| 52 | + params.put("local", String.valueOf(local)); |
| 53 | + params.put("master_timeout", masterTimeout); |
| 54 | + params.put("timeout", timeout); |
| 55 | + params.put("wait_for_status", waitForStatus.name()); |
| 56 | + if (waitForNoRelocatingShards || randomBoolean()) { |
| 57 | + params.put("wait_for_no_relocating_shards", String.valueOf(waitForNoRelocatingShards)); |
| 58 | + } |
| 59 | + if (waitForNoInitializingShards || randomBoolean()) { |
| 60 | + params.put("wait_for_no_initializing_shards", String.valueOf(waitForNoInitializingShards)); |
| 61 | + } |
| 62 | + params.put("wait_for_active_shards", String.valueOf(waitForActiveShards)); |
| 63 | + params.put("wait_for_nodes", waitForNodes); |
| 64 | + params.put("wait_for_events", waitForEvents.name()); |
| 65 | + |
| 66 | + FakeRestRequest restRequest = buildRestRequest(params); |
| 67 | + ClusterHealthRequest clusterHealthRequest = RestClusterHealthAction.fromRequest(restRequest); |
| 68 | + assertThat(clusterHealthRequest.indices().length, equalTo(1)); |
| 69 | + assertThat(clusterHealthRequest.indices()[0], equalTo(index)); |
| 70 | + assertThat(clusterHealthRequest.local(), equalTo(local)); |
| 71 | + assertThat(clusterHealthRequest.masterNodeTimeout(), equalTo(TimeValue.parseTimeValue(masterTimeout, "test"))); |
| 72 | + assertThat(clusterHealthRequest.timeout(), equalTo(TimeValue.parseTimeValue(timeout, "test"))); |
| 73 | + assertThat(clusterHealthRequest.waitForStatus(), equalTo(waitForStatus)); |
| 74 | + assertThat(clusterHealthRequest.waitForNoRelocatingShards(), equalTo(waitForNoRelocatingShards)); |
| 75 | + assertThat(clusterHealthRequest.waitForNoInitializingShards(), equalTo(waitForNoInitializingShards)); |
| 76 | + assertThat(clusterHealthRequest.waitForActiveShards(), equalTo(ActiveShardCount.parseString(String.valueOf(waitForActiveShards)))); |
| 77 | + assertThat(clusterHealthRequest.waitForNodes(), equalTo(waitForNodes)); |
| 78 | + assertThat(clusterHealthRequest.waitForEvents(), equalTo(waitForEvents)); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + private FakeRestRequest buildRestRequest(Map<String, String> params) { |
| 83 | + return new FakeRestRequest.Builder(xContentRegistry()) |
| 84 | + .withMethod(RestRequest.Method.GET) |
| 85 | + .withPath("/_cluster/health") |
| 86 | + .withParams(params) |
| 87 | + .build(); |
| 88 | + } |
| 89 | +} |
0 commit comments