Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define constants for REST requests endpoints in tests #37610

Merged
merged 1 commit into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.randomMode;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.SQL_QUERY_REST_ENDPOINT;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.randomMode;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.SQL_QUERY_REST_ENDPOINT;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -180,7 +181,7 @@ private static Map<String, Object> runSql(@Nullable String asUser, String mode,
}

private static Map<String, Object> runSql(@Nullable String asUser, HttpEntity entity) throws IOException {
Request request = new Request("POST", "/_sql");
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
if (asUser != null) {
RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader("es-security-runas-user", asUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.columnInfo;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.randomMode;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.SQL_QUERY_REST_ENDPOINT;

public class UserFunctionIT extends ESRestTestCase {

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

private Map<String, Object> runSql(String asUser, HttpEntity entity) throws IOException {
Request request = new Request("POST", "/_sql");
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
if (asUser != null) {
RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader("es-security-runas-user", asUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Locale;
import java.util.Map;

import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
import static org.elasticsearch.xpack.sql.proto.RequestInfo.CLIENT_IDS;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;

Expand Down Expand Up @@ -134,7 +135,7 @@ private void assertQuery(String sql, String columnName, String columnType, Objec
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
* user rather than to the JDBC driver or CLI.
*/
public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTestCase {

public static String SQL_QUERY_REST_ENDPOINT = org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
/**
* Builds that map that is returned in the header for each column.
*/
Expand Down Expand Up @@ -309,7 +311,7 @@ private Map<String, Object> runSql(String mode, String sql, String suffix) throw
}

protected Map<String, Object> runSql(HttpEntity sql, String suffix) throws IOException {
Request request = new Request("POST", "/_sql" + suffix);
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT + suffix);
request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
request.addParameter("pretty", "true"); // Improves error reporting readability
if (randomBoolean()) {
Expand Down Expand Up @@ -640,7 +642,7 @@ private Tuple<String, String> runSqlAsText(String sql, String accept) throws IOE
* rather than the {@code format} parameter.
*/
private Tuple<String, String> runSqlAsText(String suffix, HttpEntity entity, String accept) throws IOException {
Request request = new Request("POST", "/_sql" + suffix);
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT + suffix);
request.addParameter("error_trace", "true");
request.setEntity(entity);
RequestOptions.Builder options = request.getOptions().toBuilder();
Expand All @@ -658,7 +660,7 @@ private Tuple<String, String> runSqlAsText(String suffix, HttpEntity entity, Str
* rather than an {@code Accept} header.
*/
private Tuple<String, String> runSqlAsTextFormat(String sql, String format) throws IOException {
Request request = new Request("POST", "/_sql");
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
request.addParameter("error_trace", "true");
request.addParameter("format", format);
request.setJsonEntity("{\"query\":\"" + sql + "\"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Locale;
import java.util.Map;

import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_STATS_REST_ENDPOINT;
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_TRANSLATE_REST_ENDPOINT;
import static org.elasticsearch.xpack.sql.qa.rest.RestSqlTestCase.mode;

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

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

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

private void runSql(String mode, String restClient, String sql) throws IOException {
Request request = new Request("POST", "/_sql");
Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
request.addParameter("pretty", "true"); // Improves error reporting readability
if (randomBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javax.sql.rowset.serial.SerialException;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.xpack.sql.proto.Protocol.SQL_QUERY_REST_ENDPOINT;

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

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