Skip to content

Commit 90ae556

Browse files
authored
Define constants for REST requests endpoints in tests (elastic#37610)
1 parent 7394892 commit 90ae556

7 files changed

Lines changed: 22 additions & 11 deletions

File tree

x-pack/plugin/sql/qa/multi-node/src/test/java/org/elasticsearch/xpack/sql/qa/multi_node/RestSqlMultinodeIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo;
2929
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
3030
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.randomMode;
31+
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.SQL_QUERY_REST_ENDPOINT;
3132

3233
/**
3334
* Tests specific to multiple nodes.
@@ -111,7 +112,7 @@ private void assertCount(RestClient client, int count) throws IOException {
111112
expected.put("columns", singletonList(columnInfo(mode, "COUNT(*)", "long", JDBCType.BIGINT, 20)));
112113
expected.put("rows", singletonList(singletonList(count)));
113114

114-
Request request = new Request("POST", "/_sql");
115+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
115116
request.setJsonEntity("{\"query\": \"SELECT COUNT(*) FROM test\"" + mode(mode) + "}");
116117
Map<String, Object> actual = responseToMap(client.performRequest(request));
117118

x-pack/plugin/sql/qa/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/RestSqlSecurityIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo;
3333
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
3434
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.randomMode;
35+
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.SQL_QUERY_REST_ENDPOINT;
3536
import static org.hamcrest.Matchers.containsString;
3637
import static org.hamcrest.Matchers.empty;
3738
import static org.hamcrest.Matchers.equalTo;
@@ -180,7 +181,7 @@ private static Map<String, Object> runSql(@Nullable String asUser, String mode,
180181
}
181182

182183
private static Map<String, Object> runSql(@Nullable String asUser, HttpEntity entity) throws IOException {
183-
Request request = new Request("POST", "/_sql");
184+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
184185
if (asUser != null) {
185186
RequestOptions.Builder options = request.getOptions().toBuilder();
186187
options.addHeader("es-security-runas-user", asUser);

x-pack/plugin/sql/qa/security/src/test/java/org/elasticsearch/xpack/sql/qa/security/UserFunctionIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo;
3838
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
3939
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.randomMode;
40+
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.SQL_QUERY_REST_ENDPOINT;
4041

4142
public class UserFunctionIT extends ESRestTestCase {
4243

@@ -178,7 +179,7 @@ private Map<String, Object> runSql(String asUser, String mode, String sql) throw
178179
}
179180

180181
private Map<String, Object> runSql(String asUser, HttpEntity entity) throws IOException {
181-
Request request = new Request("POST", "/_sql");
182+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
182183
if (asUser != null) {
183184
RequestOptions.Builder options = request.getOptions().toBuilder();
184185
options.addHeader("es-security-runas-user", asUser);

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/SqlProtocolTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Locale;
2929
import java.util.Map;
3030

31+
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
3132
import static org.elasticsearch.xpack.sql.proto.RequestInfo.CLIENT_IDS;
3233
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
3334

@@ -134,7 +135,7 @@ private void assertQuery(String sql, String columnName, String columnType, Objec
134135
}
135136

136137
private Map<String, Object> runSql(String mode, String sql) throws IOException {
137-
Request request = new Request("POST", "/_sql");
138+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
138139
String requestContent = "{\"query\":\"" + sql + "\"" + mode(mode) + "}";
139140
String format = randomFrom(XContentType.values()).name().toLowerCase(Locale.ROOT);
140141

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
* user rather than to the JDBC driver or CLI.
5050
*/
5151
public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTestCase {
52+
53+
public static String SQL_QUERY_REST_ENDPOINT = org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
5254
/**
5355
* Builds that map that is returned in the header for each column.
5456
*/
@@ -309,7 +311,7 @@ private Map<String, Object> runSql(String mode, String sql, String suffix) throw
309311
}
310312

311313
protected Map<String, Object> runSql(HttpEntity sql, String suffix) throws IOException {
312-
Request request = new Request("POST", "/_sql" + suffix);
314+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT + suffix);
313315
request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
314316
request.addParameter("pretty", "true"); // Improves error reporting readability
315317
if (randomBoolean()) {
@@ -640,7 +642,7 @@ private Tuple<String, String> runSqlAsText(String sql, String accept) throws IOE
640642
* rather than the {@code format} parameter.
641643
*/
642644
private Tuple<String, String> runSqlAsText(String suffix, HttpEntity entity, String accept) throws IOException {
643-
Request request = new Request("POST", "/_sql" + suffix);
645+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT + suffix);
644646
request.addParameter("error_trace", "true");
645647
request.setEntity(entity);
646648
RequestOptions.Builder options = request.getOptions().toBuilder();
@@ -658,7 +660,7 @@ private Tuple<String, String> runSqlAsText(String suffix, HttpEntity entity, Str
658660
* rather than an {@code Accept} header.
659661
*/
660662
private Tuple<String, String> runSqlAsTextFormat(String sql, String format) throws IOException {
661-
Request request = new Request("POST", "/_sql");
663+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
662664
request.addParameter("error_trace", "true");
663665
request.addParameter("format", format);
664666
request.setJsonEntity("{\"query\":\"" + sql + "\"}");

x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlUsageTestCase.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import java.util.Locale;
2727
import java.util.Map;
2828

29+
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
30+
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_STATS_REST_ENDPOINT;
31+
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_TRANSLATE_REST_ENDPOINT;
2932
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
3033

3134
public abstract class RestSqlUsageTestCase extends ESRestTestCase {
@@ -226,7 +229,7 @@ private void index(List<IndexDocument> docs) throws IOException {
226229
}
227230

228231
private Map<String, Object> getStats() throws UnsupportedOperationException, IOException {
229-
Request request = new Request("GET", "/_sql/stats");
232+
Request request = new Request("GET", SQL_STATS_REST_ENDPOINT);
230233
Map<String, Object> responseAsMap;
231234
try (InputStream content = client().performRequest(request).getEntity().getContent()) {
232235
responseAsMap = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
@@ -236,7 +239,7 @@ private Map<String, Object> getStats() throws UnsupportedOperationException, IOE
236239
}
237240

238241
private void runTranslate(String sql) throws IOException {
239-
Request request = new Request("POST", "/_sql/translate");
242+
Request request = new Request("POST", SQL_TRANSLATE_REST_ENDPOINT);
240243
if (randomBoolean()) {
241244
// We default to JSON but we force it randomly for extra coverage
242245
request.addParameter("format", "json");
@@ -276,7 +279,7 @@ private void assertTranslateQueryMetric(int expected, Map<String, Object> respon
276279
}
277280

278281
private void runSql(String mode, String restClient, String sql) throws IOException {
279-
Request request = new Request("POST", "/_sql");
282+
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
280283
request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
281284
request.addParameter("pretty", "true"); // Improves error reporting readability
282285
if (randomBoolean()) {

x-pack/plugin/sql/sql-client/src/main/java/org/elasticsearch/xpack/sql/client/JreHttpUrlConnection.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.sql.rowset.serial.SerialException;
3636

3737
import static java.util.Collections.emptyMap;
38+
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
3839

3940
/**
4041
* Low-level http client using the built-in {@link HttpURLConnection}.
@@ -47,7 +48,8 @@ public class JreHttpUrlConnection implements Closeable {
4748
* error.
4849
*/
4950
public static final String SQL_STATE_BAD_SERVER = "bad_server";
50-
private static final String SQL_NOT_AVAILABLE_ERROR_MESSAGE = "request [/_sql] contains unrecognized parameter: [mode]";
51+
private static final String SQL_NOT_AVAILABLE_ERROR_MESSAGE = "request [" + SQL_QUERY_REST_ENDPOINT
52+
+ "] contains unrecognized parameter: [mode]";
5153

5254
public static <R> R http(String path, String query, ConnectionConfiguration cfg, Function<JreHttpUrlConnection, R> handler) {
5355
final URI uriPath = cfg.baseUri().resolve(path); // update path if needed

0 commit comments

Comments
 (0)