Skip to content

Commit

Permalink
Define constants for REST requests endpoints in tests (#37610)
Browse files Browse the repository at this point in the history
(cherry picked from commit 90ae556)
  • Loading branch information
astefan committed Jan 22, 2019
1 parent 3a6e984 commit 94d28bf
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 12 deletions.
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(1)", "long", JDBCType.BIGINT, 20)));
expected.put("rows", singletonList(singletonList(count)));

Request request = new Request("POST", "/_xpack/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", "/_xpack/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", "/_xpack/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 @@ -50,6 +50,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 @@ -311,7 +313,7 @@ private Map<String, Object> runSql(String mode, String sql, String suffix) throw
}

private Map<String, Object> runSql(HttpEntity sql, String suffix) throws IOException {
Request request = new Request("POST", "/_xpack/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 @@ -642,7 +644,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", "/_xpack/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 @@ -660,7 +662,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", "/_xpack/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", "/_xpack/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", "/_xpack/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", "/_xpack/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 [/_xpack/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
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public final class Protocol {
*/
public static final String CLEAR_CURSOR_REST_ENDPOINT = "/_xpack/sql/close";
public static final String SQL_QUERY_REST_ENDPOINT = "/_xpack/sql";
public static final String SQL_TRANSLATE_REST_ENDPOINT = "/_xpack/sql/translate";
public static final String SQL_STATS_REST_ENDPOINT = "/_xpack/sql/stats";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.sql.action.SqlTranslateAction;
import org.elasticsearch.xpack.sql.action.SqlTranslateRequest;
import org.elasticsearch.xpack.sql.proto.Protocol;

import java.io.IOException;

Expand All @@ -26,8 +27,8 @@
public class RestSqlTranslateAction extends BaseRestHandler {
public RestSqlTranslateAction(Settings settings, RestController controller) {
super(settings);
controller.registerHandler(GET, "/_xpack/sql/translate", this);
controller.registerHandler(POST, "/_xpack/sql/translate", this);
controller.registerHandler(GET, Protocol.SQL_TRANSLATE_REST_ENDPOINT, this);
controller.registerHandler(POST, Protocol.SQL_TRANSLATE_REST_ENDPOINT, this);
}

@Override
Expand Down

0 comments on commit 94d28bf

Please sign in to comment.