|
| 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.HttpGet; |
| 23 | +import org.apache.http.client.methods.HttpPut; |
| 24 | +import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; |
| 25 | +import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest; |
| 26 | +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; |
| 27 | +import org.elasticsearch.action.support.ActiveShardCount; |
| 28 | +import org.elasticsearch.action.support.master.AcknowledgedRequest; |
| 29 | +import org.elasticsearch.cluster.health.ClusterHealthStatus; |
| 30 | +import org.elasticsearch.common.Priority; |
| 31 | +import org.elasticsearch.test.ESTestCase; |
| 32 | +import org.hamcrest.CoreMatchers; |
| 33 | +import org.junit.Assert; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Locale; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 41 | +import static org.hamcrest.Matchers.nullValue; |
| 42 | + |
| 43 | +public class ClusterRequestConvertersTests extends ESTestCase { |
| 44 | + |
| 45 | + public void testClusterPutSettings() throws IOException { |
| 46 | + ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest(); |
| 47 | + Map<String, String> expectedParams = new HashMap<>(); |
| 48 | + RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); |
| 49 | + RequestConvertersTests.setRandomTimeout(request::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams); |
| 50 | + |
| 51 | + Request expectedRequest = ClusterRequestConverters.clusterPutSettings(request); |
| 52 | + Assert.assertEquals("/_cluster/settings", expectedRequest.getEndpoint()); |
| 53 | + Assert.assertEquals(HttpPut.METHOD_NAME, expectedRequest.getMethod()); |
| 54 | + Assert.assertEquals(expectedParams, expectedRequest.getParameters()); |
| 55 | + } |
| 56 | + |
| 57 | + public void testClusterGetSettings() throws IOException { |
| 58 | + ClusterGetSettingsRequest request = new ClusterGetSettingsRequest(); |
| 59 | + Map<String, String> expectedParams = new HashMap<>(); |
| 60 | + RequestConvertersTests.setRandomMasterTimeout(request, expectedParams); |
| 61 | + request.includeDefaults(ESTestCase.randomBoolean()); |
| 62 | + if (request.includeDefaults()) { |
| 63 | + expectedParams.put("include_defaults", String.valueOf(true)); |
| 64 | + } |
| 65 | + |
| 66 | + Request expectedRequest = ClusterRequestConverters.clusterGetSettings(request); |
| 67 | + Assert.assertEquals("/_cluster/settings", expectedRequest.getEndpoint()); |
| 68 | + Assert.assertEquals(HttpGet.METHOD_NAME, expectedRequest.getMethod()); |
| 69 | + Assert.assertEquals(expectedParams, expectedRequest.getParameters()); |
| 70 | + } |
| 71 | + |
| 72 | + public void testClusterHealth() { |
| 73 | + ClusterHealthRequest healthRequest = new ClusterHealthRequest(); |
| 74 | + Map<String, String> expectedParams = new HashMap<>(); |
| 75 | + RequestConvertersTests.setRandomLocal(healthRequest, expectedParams); |
| 76 | + String timeoutType = ESTestCase.randomFrom("timeout", "masterTimeout", "both", "none"); |
| 77 | + String timeout = ESTestCase.randomTimeValue(); |
| 78 | + String masterTimeout = ESTestCase.randomTimeValue(); |
| 79 | + switch (timeoutType) { |
| 80 | + case "timeout": |
| 81 | + healthRequest.timeout(timeout); |
| 82 | + expectedParams.put("timeout", timeout); |
| 83 | + // If Master Timeout wasn't set it uses the same value as Timeout |
| 84 | + expectedParams.put("master_timeout", timeout); |
| 85 | + break; |
| 86 | + case "masterTimeout": |
| 87 | + expectedParams.put("timeout", "30s"); |
| 88 | + healthRequest.masterNodeTimeout(masterTimeout); |
| 89 | + expectedParams.put("master_timeout", masterTimeout); |
| 90 | + break; |
| 91 | + case "both": |
| 92 | + healthRequest.timeout(timeout); |
| 93 | + expectedParams.put("timeout", timeout); |
| 94 | + healthRequest.masterNodeTimeout(timeout); |
| 95 | + expectedParams.put("master_timeout", timeout); |
| 96 | + break; |
| 97 | + case "none": |
| 98 | + expectedParams.put("timeout", "30s"); |
| 99 | + expectedParams.put("master_timeout", "30s"); |
| 100 | + break; |
| 101 | + default: |
| 102 | + throw new UnsupportedOperationException(); |
| 103 | + } |
| 104 | + RequestConvertersTests.setRandomWaitForActiveShards(healthRequest::waitForActiveShards, ActiveShardCount.NONE, expectedParams); |
| 105 | + if (ESTestCase.randomBoolean()) { |
| 106 | + ClusterHealthRequest.Level level = ESTestCase.randomFrom(ClusterHealthRequest.Level.values()); |
| 107 | + healthRequest.level(level); |
| 108 | + expectedParams.put("level", level.name().toLowerCase(Locale.ROOT)); |
| 109 | + } else { |
| 110 | + expectedParams.put("level", "cluster"); |
| 111 | + } |
| 112 | + if (ESTestCase.randomBoolean()) { |
| 113 | + Priority priority = ESTestCase.randomFrom(Priority.values()); |
| 114 | + healthRequest.waitForEvents(priority); |
| 115 | + expectedParams.put("wait_for_events", priority.name().toLowerCase(Locale.ROOT)); |
| 116 | + } |
| 117 | + if (ESTestCase.randomBoolean()) { |
| 118 | + ClusterHealthStatus status = ESTestCase.randomFrom(ClusterHealthStatus.values()); |
| 119 | + healthRequest.waitForStatus(status); |
| 120 | + expectedParams.put("wait_for_status", status.name().toLowerCase(Locale.ROOT)); |
| 121 | + } |
| 122 | + if (ESTestCase.randomBoolean()) { |
| 123 | + boolean waitForNoInitializingShards = ESTestCase.randomBoolean(); |
| 124 | + healthRequest.waitForNoInitializingShards(waitForNoInitializingShards); |
| 125 | + if (waitForNoInitializingShards) { |
| 126 | + expectedParams.put("wait_for_no_initializing_shards", Boolean.TRUE.toString()); |
| 127 | + } |
| 128 | + } |
| 129 | + if (ESTestCase.randomBoolean()) { |
| 130 | + boolean waitForNoRelocatingShards = ESTestCase.randomBoolean(); |
| 131 | + healthRequest.waitForNoRelocatingShards(waitForNoRelocatingShards); |
| 132 | + if (waitForNoRelocatingShards) { |
| 133 | + expectedParams.put("wait_for_no_relocating_shards", Boolean.TRUE.toString()); |
| 134 | + } |
| 135 | + } |
| 136 | + String[] indices = ESTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 5); |
| 137 | + healthRequest.indices(indices); |
| 138 | + |
| 139 | + Request request = ClusterRequestConverters.clusterHealth(healthRequest); |
| 140 | + Assert.assertThat(request, CoreMatchers.notNullValue()); |
| 141 | + Assert.assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME)); |
| 142 | + Assert.assertThat(request.getEntity(), nullValue()); |
| 143 | + if (indices != null && indices.length > 0) { |
| 144 | + Assert.assertThat(request.getEndpoint(), equalTo("/_cluster/health/" + String.join(",", indices))); |
| 145 | + } else { |
| 146 | + Assert.assertThat(request.getEndpoint(), equalTo("/_cluster/health")); |
| 147 | + } |
| 148 | + Assert.assertThat(request.getParameters(), equalTo(expectedParams)); |
| 149 | + } |
| 150 | +} |
0 commit comments