diff --git a/distribution/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java b/distribution/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java new file mode 100644 index 0000000000000..ce72af26628a1 --- /dev/null +++ b/distribution/integ-test-zip/src/test/java/org/elasticsearch/test/rest/RequestsWithoutContentIT.java @@ -0,0 +1,88 @@ +/* + * 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.test.rest; + +import org.elasticsearch.client.ResponseException; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.containsString; + +public class RequestsWithoutContentIT extends ESRestTestCase { + + public void testIndexMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "PUT", "/idx/type/123")); + assertResponseException(responseException, "request body is required"); + } + + public void testBulkMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "PUT", "/_bulk")); + assertResponseException(responseException, "request body is required"); + } + + public void testPutSettingsMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + "PUT", "/_settings")); + assertResponseException(responseException, "request body is required"); + } + + public void testPutMappingsMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "PUT", "/test_index/test_type/_mapping")); + assertResponseException(responseException, "request body is required"); + } + + public void testPutIndexTemplateMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "PUT" : "POST", "/_template/my_template")); + assertResponseException(responseException, "request body is required"); + } + + public void testMultiSearchMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "GET", "/_msearch")); + assertResponseException(responseException, "request body or source parameter is required"); + } + + public void testPutPipelineMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + "PUT", "/_ingest/pipeline/my_pipeline")); + assertResponseException(responseException, "request body or source parameter is required"); + } + + public void testSimulatePipelineMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate")); + assertResponseException(responseException, "request body or source parameter is required"); + } + + public void testPutScriptMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "PUT", "/_scripts/lang")); + assertResponseException(responseException, "request body is required"); + } + + private static void assertResponseException(ResponseException responseException, String message) { + assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); + assertThat(responseException.getMessage(), containsString(message)); + } +} diff --git a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java index 427a81bdd2184..a4870aa0c1171 100644 --- a/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java +++ b/docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java @@ -21,10 +21,16 @@ import com.carrotsearch.randomizedtesting.annotations.Name; import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; - +import org.elasticsearch.Version; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.http.HttpHost; +import org.elasticsearch.test.rest.yaml.ClientYamlDocsTestClient; import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate; +import org.elasticsearch.test.rest.yaml.ClientYamlTestClient; import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase; +import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec; +import java.io.IOException; import java.util.List; public class DocsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase { @@ -52,5 +58,11 @@ protected void afterIfFailed(List errors) { protected boolean randomizeContentType() { return false; } + + @Override + protected ClientYamlTestClient initClientYamlTestClient(ClientYamlSuiteRestSpec restSpec, RestClient restClient, + List hosts, Version esVersion) throws IOException { + return new ClientYamlDocsTestClient(restSpec, restClient, hosts, esVersion); + } } diff --git a/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/20_crud.yml b/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/20_crud.yml index 10a47bb2b811c..b041e0664bb6c 100644 --- a/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/20_crud.yml +++ b/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/20_crud.yml @@ -203,16 +203,3 @@ teardown: catch: missing ingest.get_pipeline: id: "my_pipeline" - ---- -"missing body": - - - skip: - version: " - 5.99.99" - reason: NPE caused by missing body fixed in 6.0.0 - - - do: - catch: /request body or source parameter is required/ - raw: - method: PUT - path: _ingest/pipeline/my_pipeline diff --git a/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/90_simulate.yml b/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/90_simulate.yml index 747f132f9031f..8b08535c12494 100644 --- a/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/90_simulate.yml +++ b/modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/90_simulate.yml @@ -605,16 +605,3 @@ teardown: - length: { docs.0.processor_results.1: 2 } - match: { docs.0.processor_results.1.tag: "rename-1" } - match: { docs.0.processor_results.1.doc._source.new_status: 200 } - ---- -"missing body": - - - skip: - version: " - 5.99.99" - reason: NPE caused by missing body fixed in 6.0.0 - - - do: - catch: /request body or source parameter is required/ - raw: - method: POST - path: _ingest/pipeline/my_pipeline/_simulate diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateWithoutContentIT.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateWithoutContentIT.java new file mode 100644 index 0000000000000..cbc6adf6be227 --- /dev/null +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateWithoutContentIT.java @@ -0,0 +1,44 @@ +/* + * 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.script.mustache; + +import org.elasticsearch.client.ResponseException; +import org.elasticsearch.test.rest.ESRestTestCase; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.containsString; + +public class SearchTemplateWithoutContentIT extends ESRestTestCase { + + public void testSearchTemplateMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "GET", "/_search/template")); + assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); + assertThat(responseException.getMessage(), containsString("request body or source parameter is required")); + } + + public void testMultiSearchTemplateMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + randomBoolean() ? "POST" : "GET", "/_msearch/template")); + assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); + assertThat(responseException.getMessage(), containsString("request body or source parameter is required")); + } +} diff --git a/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/10_basic.yml b/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/10_basic.yml index 25a7845a4b563..5deabe038906d 100644 --- a/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/10_basic.yml +++ b/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/10_basic.yml @@ -11,16 +11,3 @@ nodes.info: {} - match: { nodes.$master.modules.0.name: lang-mustache } - ---- -"missing body": - - - skip: - version: " - 5.99.99" - reason: NPE caused by missing body fixed in 6.0.0 - - - do: - catch: /request body is required/ - raw: - method: POST - path: _search/template/1 diff --git a/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/30_search_template.yml b/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/30_search_template.yml index f90850c361d06..d2608a48e733e 100644 --- a/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/30_search_template.yml +++ b/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/30_search_template.yml @@ -122,16 +122,3 @@ - match: { hits.total: 1 } - length: { hits.hits: 1 } - length: { profile: 1 } - ---- -"missing body": - - - skip: - version: " - 5.99.99" - reason: NPE caused by missing body fixed in 6.0.0 - - - do: - catch: /request body or source parameter is required/ - raw: - method: POST - path: _search/template diff --git a/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml b/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml index 8e1829955ad05..0d0b16cdbfcf9 100644 --- a/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml +++ b/modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml @@ -156,16 +156,3 @@ setup: - match: { responses.0.hits.total: 2 } - match: { responses.1.hits.total: 1 } - match: { responses.2.hits.total: 1 } - ---- -"missing body": - - - skip: - version: " - 5.99.99" - reason: NPE caused by missing body fixed in 6.0.0 - - - do: - catch: /request body or source parameter is required/ - raw: - method: POST - path: _msearch/template diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexWithoutContentIT.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexWithoutContentIT.java new file mode 100644 index 0000000000000..f580b1400c3bd --- /dev/null +++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexWithoutContentIT.java @@ -0,0 +1,37 @@ +/* + * 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.index.reindex; + +import org.elasticsearch.client.ResponseException; +import org.elasticsearch.test.rest.ESRestTestCase; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.containsString; + +public class ReindexWithoutContentIT extends ESRestTestCase { + + public void testReindexMissingBody() throws IOException { + ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest( + "POST", "/_reindex")); + assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode()); + assertThat(responseException.getMessage(), containsString("request body is required")); + } +} diff --git a/modules/reindex/src/test/resources/rest-api-spec/test/reindex/10_basic.yml b/modules/reindex/src/test/resources/rest-api-spec/test/reindex/10_basic.yml index 247da75e80678..3557cf9bad7f3 100644 --- a/modules/reindex/src/test/resources/rest-api-spec/test/reindex/10_basic.yml +++ b/modules/reindex/src/test/resources/rest-api-spec/test/reindex/10_basic.yml @@ -299,16 +299,3 @@ index: source metric: search - match: {indices.source.total.search.open_contexts: 0} - ---- -"missing body": - - - skip: - version: " - 5.99.99" - reason: NPE caused by missing body fixed in 6.0.0 - - - do: - catch: /request body is required/ - raw: - method: POST - path: _reindex diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/bulk/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/bulk/10_basic.yml index 3eaf8016e1dd0..c6ba03a9aeb8d 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/bulk/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/bulk/10_basic.yml @@ -59,19 +59,6 @@ - match: { count: 2 } ---- -"missing body": - - - skip: - version: " - 5.4.99" - reason: NPE caused by missing body fixed in 5.5.0 - - - do: - catch: /request body is required/ - raw: - method: POST - path: _bulk - --- "empty action": diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.put_script/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.put_script/10_basic.yml deleted file mode 100644 index b0900667fe05c..0000000000000 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.put_script/10_basic.yml +++ /dev/null @@ -1,12 +0,0 @@ ---- -"missing body": - - - skip: - version: " - 5.4.99" - reason: NPE caused by missing body fixed in 5.5.0 - - - do: - catch: /request body is required/ - raw: - method: POST - path: _scripts/lang diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.put_settings/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.put_settings/10_basic.yml index e41f697807eee..9339e5797cf4a 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.put_settings/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/cluster.put_settings/10_basic.yml @@ -74,16 +74,3 @@ include_defaults: true - match: {defaults.node.attr.testattr: "test"} - ---- -"missing body": - - - skip: - version: " - 5.4.99" - reason: NPE caused by missing body fixed in 5.5.0 - - - do: - catch: /request body is required/ - raw: - method: PUT - path: _settings diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/index/10_with_id.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/index/10_with_id.yml index c3d55952b1d26..8ac55ec79f626 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/index/10_with_id.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/index/10_with_id.yml @@ -32,16 +32,3 @@ type: type id: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa body: { foo: bar } - ---- -"missing body": - - - skip: - version: " - 5.4.99" - reason: NPE caused by missing body fixed in 5.5.0 - - - do: - catch: /request body is required/ - raw: - method: POST - path: idx/type/123 diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_mapping/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_mapping/10_basic.yml index d7831a55ac165..1d33f2d31bb15 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_mapping/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_mapping/10_basic.yml @@ -67,16 +67,3 @@ properties: "": type: keyword - ---- -"missing body": - - - skip: - version: " - 5.4.99" - reason: NPE caused by missing body fixed in 5.5.0 - - - do: - catch: /request body is required/ - raw: - method: POST - path: test_index/test_type/_mapping diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_template/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_template/10_basic.yml index baf00208bba7f..01bd7afc582b8 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_template/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.put_template/10_basic.yml @@ -210,16 +210,3 @@ catch: missing indices.get_template: name: "my_template" - ---- -"missing body": - - - skip: - version: " - 5.4.99" - reason: NPE caused by missing body fixed in 5.5.0 - - - do: - catch: /request body is required/ - raw: - method: PUT - path: _template/my_template diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/msearch/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/msearch/10_basic.yml index 64268f19f8de6..536e2bfaf9495 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/msearch/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/msearch/10_basic.yml @@ -61,16 +61,3 @@ setup: - match: { responses.3.error.root_cause.0.reason: "/no.such.index/" } - match: { responses.3.error.root_cause.0.index: index_3 } - match: { responses.4.hits.total: 4 } - ---- -"missing body": - - - skip: - version: " - 5.4.99" - reason: NPE caused by missing body fixed in 5.5.0 - - - do: - catch: /request body or source parameter is required/ - raw: - method: POST - path: _msearch diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlDocsTestClient.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlDocsTestClient.java new file mode 100644 index 0000000000000..8b892a020440e --- /dev/null +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlDocsTestClient.java @@ -0,0 +1,66 @@ +/* + * 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.test.rest.yaml; + +import org.elasticsearch.Version; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.ResponseException; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.http.HttpEntity; +import org.elasticsearch.client.http.HttpHost; +import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * Used to execute REST requests according to the docs snippets that need to be tests. Wraps a + * {@link RestClient} instance used to send the REST requests. Holds the {@link ClientYamlSuiteRestSpec} used to translate api calls into + * REST calls. Supports raw requests besides the usual api calls based on the rest spec. + */ +public final class ClientYamlDocsTestClient extends ClientYamlTestClient { + + public ClientYamlDocsTestClient(ClientYamlSuiteRestSpec restSpec, RestClient restClient, List hosts, Version esVersion) + throws IOException { + super(restSpec, restClient, hosts, esVersion); + } + + public ClientYamlTestResponse callApi(String apiName, Map params, HttpEntity entity, Map headers) + throws IOException { + + if ("raw".equals(apiName)) { + // Raw requests are bit simpler.... + Map queryStringParams = new HashMap<>(params); + String method = Objects.requireNonNull(queryStringParams.remove("method"), "Method must be set to use raw request"); + String path = "/" + Objects.requireNonNull(queryStringParams.remove("path"), "Path must be set to use raw request"); + // And everything else is a url parameter! + try { + Response response = restClient.performRequest(method, path, queryStringParams, entity); + return new ClientYamlTestResponse(response); + } catch (ResponseException e) { + throw new ClientYamlTestResponseException(e); + } + } + return super.callApi(apiName, params, entity, headers); + } +} diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestClient.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestClient.java index 579e573ed390f..d31af19b3f169 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestClient.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestClient.java @@ -19,6 +19,11 @@ package org.elasticsearch.test.rest.yaml; import com.carrotsearch.randomizedtesting.RandomizedTest; +import org.apache.logging.log4j.Logger; +import org.elasticsearch.Version; +import org.elasticsearch.client.Response; +import org.elasticsearch.client.ResponseException; +import org.elasticsearch.client.RestClient; import org.elasticsearch.client.http.Header; import org.elasticsearch.client.http.HttpEntity; import org.elasticsearch.client.http.HttpHost; @@ -26,11 +31,6 @@ import org.elasticsearch.client.http.entity.ContentType; import org.elasticsearch.client.http.message.BasicHeader; import org.elasticsearch.client.http.util.EntityUtils; -import org.apache.logging.log4j.Logger; -import org.elasticsearch.Version; -import org.elasticsearch.client.Response; -import org.elasticsearch.client.ResponseException; -import org.elasticsearch.client.RestClient; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestApi; import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestPath; @@ -42,7 +42,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; /** * Used by {@link ESClientYamlSuiteTestCase} to execute REST requests according to the tests written in yaml suite files. Wraps a @@ -55,7 +54,7 @@ public class ClientYamlTestClient { private static final ContentType YAML_CONTENT_TYPE = ContentType.create("application/yaml"); private final ClientYamlSuiteRestSpec restSpec; - private final RestClient restClient; + protected final RestClient restClient; private final Version esVersion; public ClientYamlTestClient(ClientYamlSuiteRestSpec restSpec, RestClient restClient, List hosts, @@ -76,20 +75,6 @@ public Version getEsVersion() { public ClientYamlTestResponse callApi(String apiName, Map params, HttpEntity entity, Map headers) throws IOException { - if ("raw".equals(apiName)) { - // Raw requests are bit simpler.... - Map queryStringParams = new HashMap<>(params); - String method = Objects.requireNonNull(queryStringParams.remove("method"), "Method must be set to use raw request"); - String path = "/"+ Objects.requireNonNull(queryStringParams.remove("path"), "Path must be set to use raw request"); - // And everything else is a url parameter! - try { - Response response = restClient.performRequest(method, path, queryStringParams, entity); - return new ClientYamlTestResponse(response); - } catch(ResponseException e) { - throw new ClientYamlTestResponseException(e); - } - } - ClientYamlSuiteRestApi restApi = restApi(apiName); //divide params between ones that go within query string and ones that go within path diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponse.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponse.java index a41fd5f58c99a..631e29535575f 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponse.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponse.java @@ -47,7 +47,7 @@ public class ClientYamlTestResponse { private ObjectPath parsedResponse; private String bodyAsString; - ClientYamlTestResponse(Response response) throws IOException { + public ClientYamlTestResponse(Response response) throws IOException { this.response = response; if (response.getEntity() != null) { String contentType = response.getHeader("Content-Type"); diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponseException.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponseException.java index 7d983d480296b..8874570b73ccc 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponseException.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ClientYamlTestResponseException.java @@ -32,7 +32,7 @@ public class ClientYamlTestResponseException extends IOException { private final ClientYamlTestResponse restTestResponse; private final ResponseException responseException; - ClientYamlTestResponseException(ResponseException responseException) throws IOException { + public ClientYamlTestResponseException(ResponseException responseException) throws IOException { super(responseException); this.responseException = responseException; this.restTestResponse = new ClientYamlTestResponse(responseException.getResponse()); diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java index cfad40ea1a702..b46f151fd3806 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/yaml/ESClientYamlSuiteTestCase.java @@ -119,8 +119,7 @@ public void initAndResetContext() throws Exception { throw ex; } } - ClientYamlTestClient clientYamlTestClient = - new ClientYamlTestClient(restSpec, restClient, hosts, esVersion); + ClientYamlTestClient clientYamlTestClient = initClientYamlTestClient(restSpec, restClient, hosts, esVersion); restTestExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, randomizeContentType()); adminExecutionContext = new ClientYamlTestExecutionContext(clientYamlTestClient, false); String[] blacklist = resolvePathsProperty(REST_TESTS_BLACKLIST, null); @@ -139,6 +138,11 @@ public void initAndResetContext() throws Exception { restTestExecutionContext.clear(); } + protected ClientYamlTestClient initClientYamlTestClient(ClientYamlSuiteRestSpec restSpec, RestClient restClient, + List hosts, Version esVersion) throws IOException { + return new ClientYamlTestClient(restSpec, restClient, hosts, esVersion); + } + @Override protected void afterIfFailed(List errors) { // Dump the stash on failure. Instead of dumping it in true json we escape `\n`s so stack traces are easier to read