SQL: Use request flavored methods in tests (#30345)

Modifies the SQL tests to use the new `Request` object flavored methods
introduced onto the `RestClient` in #29623. We'd like to remove the old
methods eventually so we should stop using them.
This commit is contained in:
Nik Everett 2018-05-11 13:10:01 -04:00 committed by GitHub
parent 15df911e41
commit 069fec83a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 165 additions and 150 deletions

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.qa.sql.multinode;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -53,7 +54,7 @@ public class RestSqlMultinodeIT extends ESRestTestCase {
String firstHostName = null; String firstHostName = null;
String match = firstHost.getHostName() + ":" + firstHost.getPort(); String match = firstHost.getHostName() + ":" + firstHost.getPort();
Map<String, Object> nodesInfo = responseToMap(client().performRequest("GET", "/_nodes")); Map<String, Object> nodesInfo = responseToMap(client().performRequest(new Request("GET", "/_nodes")));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> nodes = (Map<String, Object>) nodesInfo.get("nodes"); Map<String, Object> nodes = (Map<String, Object>) nodesInfo.get("nodes");
for (Map.Entry<String, Object> node : nodes.entrySet()) { for (Map.Entry<String, Object> node : nodes.entrySet()) {
@ -74,7 +75,9 @@ public class RestSqlMultinodeIT extends ESRestTestCase {
} }
index.endObject(); index.endObject();
index.endObject(); index.endObject();
client().performRequest("PUT", "/test", emptyMap(), new StringEntity(Strings.toString(index), ContentType.APPLICATION_JSON)); Request request = new Request("PUT", "/test");
request.setJsonEntity(Strings.toString(index));
client().performRequest(request);
int documents = between(10, 100); int documents = between(10, 100);
createTestData(documents); createTestData(documents);
@ -84,6 +87,9 @@ public class RestSqlMultinodeIT extends ESRestTestCase {
} }
private void createTestData(int documents) throws UnsupportedCharsetException, IOException { private void createTestData(int documents) throws UnsupportedCharsetException, IOException {
Request request = new Request("PUT", "/test/test/_bulk");
request.addParameter("refresh", "true");
StringBuilder bulk = new StringBuilder(); StringBuilder bulk = new StringBuilder();
for (int i = 0; i < documents; i++) { for (int i = 0; i < documents; i++) {
int a = 3 * i; int a = 3 * i;
@ -92,8 +98,9 @@ public class RestSqlMultinodeIT extends ESRestTestCase {
bulk.append("{\"index\":{\"_id\":\"" + i + "\"}\n"); bulk.append("{\"index\":{\"_id\":\"" + i + "\"}\n");
bulk.append("{\"a\": " + a + ", \"b\": " + b + ", \"c\": " + c + "}\n"); bulk.append("{\"a\": " + a + ", \"b\": " + b + ", \"c\": " + c + "}\n");
} }
client().performRequest("PUT", "/test/test/_bulk", singletonMap("refresh", "true"), request.setJsonEntity(bulk.toString());
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
client().performRequest(request);
} }
private Map<String, Object> responseToMap(Response response) throws IOException { private Map<String, Object> responseToMap(Response response) throws IOException {
@ -108,14 +115,12 @@ public class RestSqlMultinodeIT extends ESRestTestCase {
expected.put("columns", singletonList(columnInfo(mode, "COUNT(1)", "long", JDBCType.BIGINT, 20))); expected.put("columns", singletonList(columnInfo(mode, "COUNT(1)", "long", JDBCType.BIGINT, 20)));
expected.put("rows", singletonList(singletonList(count))); expected.put("rows", singletonList(singletonList(count)));
Map<String, String> params = new TreeMap<>(); Request request = new Request("POST", "/_xpack/sql");
params.put("format", "json"); // JSON is easier to parse then a table if (false == mode.isEmpty()) {
if (Strings.hasText(mode)) { request.addParameter("mode", mode);
params.put("mode", mode); // JDBC or PLAIN mode
} }
request.setJsonEntity("{\"query\": \"SELECT COUNT(*) FROM test\"}");
Map<String, Object> actual = responseToMap(client.performRequest("POST", "/_xpack/sql", params, Map<String, Object> actual = responseToMap(client.performRequest(request));
new StringEntity("{\"query\": \"SELECT COUNT(*) FROM test\"}", ContentType.APPLICATION_JSON)));
if (false == expected.equals(actual)) { if (false == expected.equals(actual)) {
NotEqualMessageBuilder message = new NotEqualMessageBuilder(); NotEqualMessageBuilder message = new NotEqualMessageBuilder();

View File

@ -10,6 +10,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
@ -176,14 +177,15 @@ public class RestSqlSecurityIT extends SqlSecurityTestCase {
} }
private static Map<String, Object> runSql(@Nullable String asUser, String mode, HttpEntity entity) throws IOException { private static Map<String, Object> runSql(@Nullable String asUser, String mode, HttpEntity entity) throws IOException {
Map<String, String> params = new TreeMap<>(); Request request = new Request("POST", "/_xpack/sql");
params.put("format", "json"); // JSON is easier to parse then a table if (false == mode.isEmpty()) {
if (Strings.hasText(mode)) { request.addParameter("mode", mode);
params.put("mode", mode); // JDBC or PLAIN mode
} }
Header[] headers = asUser == null ? new Header[0] : new Header[] {new BasicHeader("es-security-runas-user", asUser)}; if (asUser != null) {
Response response = client().performRequest("POST", "/_xpack/sql", params, entity, headers); request.setHeaders(new BasicHeader("es-security-runas-user", asUser));
return toMap(response); }
request.setEntity(entity);
return toMap(client().performRequest(request));
} }
private static void assertResponse(Map<String, Object> expected, Map<String, Object> actual) { private static void assertResponse(Map<String, Object> expected, Map<String, Object> actual) {

View File

@ -11,6 +11,7 @@ import org.apache.lucene.util.SuppressForbidden;
import org.elasticsearch.SpecialPermission; import org.elasticsearch.SpecialPermission;
import org.elasticsearch.action.admin.indices.get.GetIndexAction; import org.elasticsearch.action.admin.indices.get.GetIndexAction;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -41,7 +42,6 @@ import java.util.TreeMap;
import java.util.function.Function; import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap; import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
@ -135,6 +135,9 @@ public abstract class SqlSecurityTestCase extends ESRestTestCase {
* write the test data once. */ * write the test data once. */
return; return;
} }
Request request = new Request("PUT", "/_bulk");
request.addParameter("refresh", "true");
StringBuilder bulk = new StringBuilder(); StringBuilder bulk = new StringBuilder();
bulk.append("{\"index\":{\"_index\": \"test\", \"_type\": \"doc\", \"_id\":\"1\"}\n"); bulk.append("{\"index\":{\"_index\": \"test\", \"_type\": \"doc\", \"_id\":\"1\"}\n");
bulk.append("{\"a\": 1, \"b\": 2, \"c\": 3}\n"); bulk.append("{\"a\": 1, \"b\": 2, \"c\": 3}\n");
@ -142,8 +145,8 @@ public abstract class SqlSecurityTestCase extends ESRestTestCase {
bulk.append("{\"a\": 4, \"b\": 5, \"c\": 6}\n"); bulk.append("{\"a\": 4, \"b\": 5, \"c\": 6}\n");
bulk.append("{\"index\":{\"_index\": \"bort\", \"_type\": \"doc\", \"_id\":\"1\"}\n"); bulk.append("{\"index\":{\"_index\": \"bort\", \"_type\": \"doc\", \"_id\":\"1\"}\n");
bulk.append("{\"a\": \"test\"}\n"); bulk.append("{\"a\": \"test\"}\n");
client().performRequest("PUT", "/_bulk", singletonMap("refresh", "true"), request.setJsonEntity(bulk.toString());
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); client().performRequest(request);
oneTimeSetup = true; oneTimeSetup = true;
} }
@ -173,7 +176,7 @@ public abstract class SqlSecurityTestCase extends ESRestTestCase {
@AfterClass @AfterClass
public static void wipeIndicesAfterTests() throws IOException { public static void wipeIndicesAfterTests() throws IOException {
try { try {
adminClient().performRequest("DELETE", "*"); adminClient().performRequest(new Request("DELETE", "*"));
} catch (ResponseException e) { } catch (ResponseException e) {
// 404 here just means we had no indexes // 404 here just means we had no indexes
if (e.getResponse().getStatusLine().getStatusCode() != 404) { if (e.getResponse().getStatusLine().getStatusCode() != 404) {
@ -472,13 +475,15 @@ public abstract class SqlSecurityTestCase extends ESRestTestCase {
} }
protected static void createUser(String name, String role) throws IOException { protected static void createUser(String name, String role) throws IOException {
XContentBuilder user = JsonXContent.contentBuilder().prettyPrint().startObject(); { Request request = new Request("PUT", "/_xpack/security/user/" + name);
XContentBuilder user = JsonXContent.contentBuilder().prettyPrint();
user.startObject(); {
user.field("password", "testpass"); user.field("password", "testpass");
user.field("roles", role); user.field("roles", role);
} }
user.endObject(); user.endObject();
client().performRequest("PUT", "/_xpack/security/user/" + name, emptyMap(), request.setJsonEntity(Strings.toString(user));
new StringEntity(Strings.toString(user), ContentType.APPLICATION_JSON)); client().performRequest(request);
} }
protected AuditLogAsserter createAuditLogAsserter() { protected AuditLogAsserter createAuditLogAsserter() {

View File

@ -5,9 +5,9 @@
*/ */
package org.elasticsearch.xpack.qa.sql.cli; package org.elasticsearch.xpack.qa.sql.cli;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -19,7 +19,6 @@ import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import static java.util.Collections.singletonMap;
import static org.elasticsearch.xpack.qa.sql.rest.RestSqlTestCase.assertNoSearchContexts; import static org.elasticsearch.xpack.qa.sql.rest.RestSqlTestCase.assertNoSearchContexts;
public abstract class CliIntegrationTestCase extends ESRestTestCase { public abstract class CliIntegrationTestCase extends ESRestTestCase {
@ -60,11 +59,13 @@ public abstract class CliIntegrationTestCase extends ESRestTestCase {
} }
protected void index(String index, CheckedConsumer<XContentBuilder, IOException> body) throws IOException { protected void index(String index, CheckedConsumer<XContentBuilder, IOException> body) throws IOException {
Request request = new Request("PUT", "/" + index + "/doc/1");
request.addParameter("refresh", "true");
XContentBuilder builder = JsonXContent.contentBuilder().startObject(); XContentBuilder builder = JsonXContent.contentBuilder().startObject();
body.accept(builder); body.accept(builder);
builder.endObject(); builder.endObject();
HttpEntity doc = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); request.setJsonEntity(Strings.toString(builder));
client().performRequest("PUT", "/" + index + "/doc/1", singletonMap("refresh", "true"), doc); client().performRequest(request);
} }
public String command(String command) throws IOException { public String command(String command) throws IOException {

View File

@ -8,8 +8,7 @@ package org.elasticsearch.xpack.qa.sql.cli;
import java.io.IOException; import java.io.IOException;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
@ -41,7 +40,9 @@ public abstract class ErrorsTestCase extends CliIntegrationTestCase implements o
@Override @Override
public void testSelectFromIndexWithoutTypes() throws Exception { public void testSelectFromIndexWithoutTypes() throws Exception {
// Create an index without any types // Create an index without any types
client().performRequest("PUT", "/test", emptyMap(), new StringEntity("{}", ContentType.APPLICATION_JSON)); Request request = new Request("PUT", "/test");
request.setJsonEntity("{}");
client().performRequest(request);
assertFoundOneProblem(command("SELECT * FROM test")); assertFoundOneProblem(command("SELECT * FROM test"));
assertEquals("line 1:15: [test] doesn't have any types so it is incompatible with sql" + END, readLine()); assertEquals("line 1:15: [test] doesn't have any types so it is incompatible with sql" + END, readLine());

View File

@ -7,10 +7,10 @@ package org.elasticsearch.xpack.qa.sql.cli;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import java.io.IOException; import java.io.IOException;
import static java.util.Collections.singletonMap;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
/** /**
@ -18,13 +18,16 @@ import static org.hamcrest.Matchers.containsString;
*/ */
public abstract class FetchSizeTestCase extends CliIntegrationTestCase { public abstract class FetchSizeTestCase extends CliIntegrationTestCase {
public void testSelect() throws IOException { public void testSelect() throws IOException {
Request request = new Request("PUT", "/test/doc/_bulk");
request.addParameter("refresh", "true");
StringBuilder bulk = new StringBuilder(); StringBuilder bulk = new StringBuilder();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
bulk.append("{\"index\":{}}\n"); bulk.append("{\"index\":{}}\n");
bulk.append("{\"test_field\":" + i + "}\n"); bulk.append("{\"test_field\":" + i + "}\n");
} }
client().performRequest("PUT", "/test/doc/_bulk", singletonMap("refresh", "true"), request.setJsonEntity(bulk.toString());
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); client().performRequest(request);
assertEquals("[?1l>[?1000l[?2004lfetch size set to [90m4[0m", command("fetch size = 4")); assertEquals("[?1l>[?1000l[?2004lfetch size set to [90m4[0m", command("fetch size = 4"));
assertEquals("[?1l>[?1000l[?2004lfetch separator set to \"[90m -- fetch sep -- [0m\"", assertEquals("[?1l>[?1000l[?2004lfetch separator set to \"[90m -- fetch sep -- [0m\"",
command("fetch separator = \" -- fetch sep -- \"")); command("fetch separator = \" -- fetch sep -- \""));

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.qa.sql.jdbc;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.CheckedBiConsumer; import org.elasticsearch.common.CheckedBiConsumer;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
@ -55,6 +56,7 @@ public class DataLoader {
.endObject(); .endObject();
} }
protected static void loadDatasetIntoEs(RestClient client, String index) throws Exception { protected static void loadDatasetIntoEs(RestClient client, String index) throws Exception {
Request request = new Request("PUT", "/" + index);
XContentBuilder createIndex = JsonXContent.contentBuilder().startObject(); XContentBuilder createIndex = JsonXContent.contentBuilder().startObject();
createIndex.startObject("settings"); createIndex.startObject("settings");
{ {
@ -91,11 +93,9 @@ public class DataLoader {
createIndex.endObject(); createIndex.endObject();
} }
createIndex.endObject().endObject(); createIndex.endObject().endObject();
request.setJsonEntity(Strings.toString(createIndex));
client.performRequest("PUT", "/" + index, emptyMap(), new StringEntity(Strings.toString(createIndex), client.performRequest(request);
ContentType.APPLICATION_JSON));
Map<String, String> deps = new LinkedHashMap<>(); Map<String, String> deps = new LinkedHashMap<>();
csvToLines("departments", (titles, fields) -> deps.put(fields.get(0), fields.get(1))); csvToLines("departments", (titles, fields) -> deps.put(fields.get(0), fields.get(1)));
@ -119,6 +119,8 @@ public class DataLoader {
list.add(dep); list.add(dep);
}); });
request = new Request("POST", "/" + index + "/emp/_bulk");
request.addParameter("refresh", "true");
StringBuilder bulk = new StringBuilder(); StringBuilder bulk = new StringBuilder();
csvToLines("employees", (titles, fields) -> { csvToLines("employees", (titles, fields) -> {
bulk.append("{\"index\":{}}\n"); bulk.append("{\"index\":{}}\n");
@ -146,17 +148,16 @@ public class DataLoader {
bulk.setLength(bulk.length() - 1); bulk.setLength(bulk.length() - 1);
bulk.append("]"); bulk.append("]");
} }
bulk.append("}\n"); bulk.append("}\n");
}); });
request.setJsonEntity(bulk.toString());
client.performRequest("POST", "/" + index + "/emp/_bulk", singletonMap("refresh", "true"), client.performRequest(request);
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
} }
protected static void makeAlias(RestClient client, String aliasName, String... indices) throws Exception { protected static void makeAlias(RestClient client, String aliasName, String... indices) throws Exception {
for (String index : indices) { for (String index : indices) {
client.performRequest("POST", "/" + index + "/_alias/" + aliasName); client.performRequest(new Request("POST", "/" + index + "/_alias/" + aliasName));
} }
} }

View File

@ -9,8 +9,7 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import static java.util.Collections.emptyMap;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
@ -37,7 +36,9 @@ public class ErrorsTestCase extends JdbcIntegrationTestCase implements org.elast
@Override @Override
public void testSelectFromIndexWithoutTypes() throws Exception { public void testSelectFromIndexWithoutTypes() throws Exception {
// Create an index without any types // Create an index without any types
client().performRequest("PUT", "/test", emptyMap(), new StringEntity("{}", ContentType.APPLICATION_JSON)); Request request = new Request("PUT", "/test");
request.setJsonEntity("{}");
client().performRequest(request);
try (Connection c = esJdbc()) { try (Connection c = esJdbc()) {
SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FROM test").executeQuery()); SQLException e = expectThrows(SQLException.class, () -> c.prepareStatement("SELECT * FROM test").executeQuery());

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.qa.sql.jdbc;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.elasticsearch.client.Request;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
@ -15,7 +16,6 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import static java.util.Collections.singletonMap;
import static org.elasticsearch.xpack.qa.sql.rest.RestSqlTestCase.assertNoSearchContexts; import static org.elasticsearch.xpack.qa.sql.rest.RestSqlTestCase.assertNoSearchContexts;
/** /**
@ -25,13 +25,15 @@ import static org.elasticsearch.xpack.qa.sql.rest.RestSqlTestCase.assertNoSearch
public class FetchSizeTestCase extends JdbcIntegrationTestCase { public class FetchSizeTestCase extends JdbcIntegrationTestCase {
@Before @Before
public void createTestIndex() throws IOException { public void createTestIndex() throws IOException {
Request request = new Request("PUT", "/test/doc/_bulk");
request.addParameter("refresh", "true");
StringBuilder bulk = new StringBuilder(); StringBuilder bulk = new StringBuilder();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
bulk.append("{\"index\":{}}\n"); bulk.append("{\"index\":{}}\n");
bulk.append("{\"test_field\":" + i + "}\n"); bulk.append("{\"test_field\":" + i + "}\n");
} }
client().performRequest("PUT", "/test/doc/_bulk", singletonMap("refresh", "true"), request.setJsonEntity(bulk.toString());
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); client().performRequest(request);
} }
/** /**

View File

@ -9,6 +9,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.CheckedConsumer;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -85,16 +86,18 @@ public abstract class JdbcIntegrationTestCase extends ESRestTestCase {
} }
public static void index(String index, CheckedConsumer<XContentBuilder, IOException> body) throws IOException { public static void index(String index, CheckedConsumer<XContentBuilder, IOException> body) throws IOException {
Request request = new Request("PUT", "/" + index + "/doc/1");
request.addParameter("refresh", "true");
XContentBuilder builder = JsonXContent.contentBuilder().startObject(); XContentBuilder builder = JsonXContent.contentBuilder().startObject();
body.accept(builder); body.accept(builder);
builder.endObject(); builder.endObject();
HttpEntity doc = new StringEntity(Strings.toString(builder), ContentType.APPLICATION_JSON); request.setJsonEntity(Strings.toString(builder));
client().performRequest("PUT", "/" + index + "/doc/1", singletonMap("refresh", "true"), doc); client().performRequest(request);
} }
protected String clusterName() { protected String clusterName() {
try { try {
String response = EntityUtils.toString(client().performRequest("GET", "/").getEntity()); String response = EntityUtils.toString(client().performRequest(new Request("GET", "/")).getEntity());
return XContentHelper.convertToMap(JsonXContent.jsonXContent, response, false).get("cluster_name").toString(); return XContentHelper.convertToMap(JsonXContent.jsonXContent, response, false).get("cluster_name").toString();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.qa.sql.jdbc; package org.elasticsearch.xpack.qa.sql.jdbc;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.SuppressForbidden;
@ -49,7 +50,7 @@ public abstract class SpecBaseIntegrationTestCase extends JdbcIntegrationTestCas
@Before @Before
public void setupTestDataIfNeeded() throws Exception { public void setupTestDataIfNeeded() throws Exception {
if (client().performRequest("HEAD", "/test_emp").getStatusLine().getStatusCode() == 404) { if (client().performRequest(new Request("HEAD", "/test_emp")).getStatusLine().getStatusCode() == 404) {
DataLoader.loadDatasetIntoEs(client()); DataLoader.loadDatasetIntoEs(client());
} }
} }
@ -62,7 +63,7 @@ public abstract class SpecBaseIntegrationTestCase extends JdbcIntegrationTestCas
@AfterClass @AfterClass
public static void wipeTestData() throws IOException { public static void wipeTestData() throws IOException {
try { try {
adminClient().performRequest("DELETE", "/*"); adminClient().performRequest(new Request("DELETE", "/*"));
} catch (ResponseException e) { } catch (ResponseException e) {
// 404 here just means we had no indexes // 404 here just means we had no indexes
if (e.getResponse().getStatusLine().getStatusCode() != 404) { if (e.getResponse().getStatusLine().getStatusCode() != 404) {

View File

@ -12,6 +12,7 @@ import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response; import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException; import org.elasticsearch.client.ResponseException;
import org.elasticsearch.common.CheckedSupplier; import org.elasticsearch.common.CheckedSupplier;
@ -74,16 +75,19 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
} }
public void testNextPage() throws IOException { public void testNextPage() throws IOException {
Request request = new Request("POST", "/test/test/_bulk");
request.addParameter("refresh", "true");
String mode = randomMode(); String mode = randomMode();
StringBuilder bulk = new StringBuilder(); StringBuilder bulk = new StringBuilder();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
bulk.append("{\"index\":{\"_id\":\"" + i + "\"}}\n"); bulk.append("{\"index\":{\"_id\":\"" + i + "\"}}\n");
bulk.append("{\"text\":\"text" + i + "\", \"number\":" + i + "}\n"); bulk.append("{\"text\":\"text" + i + "\", \"number\":" + i + "}\n");
} }
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"), request.setJsonEntity(bulk.toString());
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); client().performRequest(request);
String request = "{\"query\":\"" String sqlRequest =
"{\"query\":\""
+ " SELECT text, number, SQRT(number) AS s, SCORE()" + " SELECT text, number, SQRT(number) AS s, SCORE()"
+ " FROM test" + " FROM test"
+ " ORDER BY number, SCORE()\", " + " ORDER BY number, SCORE()\", "
@ -94,7 +98,7 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
for (int i = 0; i < 20; i += 2) { for (int i = 0; i < 20; i += 2) {
Map<String, Object> response; Map<String, Object> response;
if (i == 0) { if (i == 0) {
response = runSql(mode, new StringEntity(request, ContentType.APPLICATION_JSON)); response = runSql(mode, new StringEntity(sqlRequest, ContentType.APPLICATION_JSON));
} else { } else {
response = runSql(mode, new StringEntity("{\"cursor\":\"" + cursor + "\"}", response = runSql(mode, new StringEntity("{\"cursor\":\"" + cursor + "\"}",
ContentType.APPLICATION_JSON)); ContentType.APPLICATION_JSON));
@ -138,12 +142,14 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
} }
public void testScoreWithFieldNamedScore() throws IOException { public void testScoreWithFieldNamedScore() throws IOException {
Request request = new Request("POST", "/test/test/_bulk");
request.addParameter("refresh", "true");
String mode = randomMode(); String mode = randomMode();
StringBuilder bulk = new StringBuilder(); StringBuilder bulk = new StringBuilder();
bulk.append("{\"index\":{\"_id\":\"1\"}}\n"); bulk.append("{\"index\":{\"_id\":\"1\"}}\n");
bulk.append("{\"name\":\"test\", \"score\":10}\n"); bulk.append("{\"name\":\"test\", \"score\":10}\n");
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"), request.setJsonEntity(bulk.toString());
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON)); client().performRequest(request);
Map<String, Object> expected = new HashMap<>(); Map<String, Object> expected = new HashMap<>();
expected.put("columns", Arrays.asList( expected.put("columns", Arrays.asList(
@ -209,7 +215,9 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
@Override @Override
public void testSelectFromIndexWithoutTypes() throws Exception { public void testSelectFromIndexWithoutTypes() throws Exception {
// Create an index without any types // Create an index without any types
client().performRequest("PUT", "/test", emptyMap(), new StringEntity("{}", ContentType.APPLICATION_JSON)); Request request = new Request("PUT", "/test");
request.setJsonEntity("{}");
client().performRequest(request);
String mode = randomFrom("jdbc", "plain"); String mode = randomFrom("jdbc", "plain");
expectBadRequest(() -> runSql(mode, "SELECT * FROM test"), expectBadRequest(() -> runSql(mode, "SELECT * FROM test"),
containsString("1:15: [test] doesn't have any types so it is incompatible with sql")); containsString("1:15: [test] doesn't have any types so it is incompatible with sql"));
@ -229,24 +237,9 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
containsString("1:8: Unknown function [missing]")); containsString("1:8: Unknown function [missing]"));
} }
private void index(String... docs) throws IOException {
StringBuilder bulk = new StringBuilder();
for (String doc : docs) {
bulk.append("{\"index\":{}\n");
bulk.append(doc + "\n");
}
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
}
@Override @Override
public void testSelectProjectScoreInAggContext() throws Exception { public void testSelectProjectScoreInAggContext() throws Exception {
StringBuilder bulk = new StringBuilder(); index("{\"foo\":1}");
bulk.append("{\"index\":{\"_id\":\"1\"}}\n");
bulk.append("{\"foo\":1}\n");
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
expectBadRequest(() -> runSql(randomMode(), expectBadRequest(() -> runSql(randomMode(),
" SELECT foo, SCORE(), COUNT(*)" " SELECT foo, SCORE(), COUNT(*)"
+ " FROM test" + " FROM test"
@ -256,12 +249,7 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
@Override @Override
public void testSelectOrderByScoreInAggContext() throws Exception { public void testSelectOrderByScoreInAggContext() throws Exception {
StringBuilder bulk = new StringBuilder(); index("{\"foo\":1}");
bulk.append("{\"index\":{\"_id\":\"1\"}}\n");
bulk.append("{\"foo\":1}\n");
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
expectBadRequest(() -> runSql(randomMode(), expectBadRequest(() -> runSql(randomMode(),
" SELECT foo, COUNT(*)" " SELECT foo, COUNT(*)"
+ " FROM test" + " FROM test"
@ -272,36 +260,21 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
@Override @Override
public void testSelectGroupByScore() throws Exception { public void testSelectGroupByScore() throws Exception {
StringBuilder bulk = new StringBuilder(); index("{\"foo\":1}");
bulk.append("{\"index\":{\"_id\":\"1\"}}\n");
bulk.append("{\"foo\":1}\n");
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
expectBadRequest(() -> runSql(randomMode(), "SELECT COUNT(*) FROM test GROUP BY SCORE()"), expectBadRequest(() -> runSql(randomMode(), "SELECT COUNT(*) FROM test GROUP BY SCORE()"),
containsString("Cannot use [SCORE()] for grouping")); containsString("Cannot use [SCORE()] for grouping"));
} }
@Override @Override
public void testSelectScoreSubField() throws Exception { public void testSelectScoreSubField() throws Exception {
StringBuilder bulk = new StringBuilder(); index("{\"foo\":1}");
bulk.append("{\"index\":{\"_id\":\"1\"}}\n");
bulk.append("{\"foo\":1}\n");
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
expectBadRequest(() -> runSql(randomMode(), "SELECT SCORE().bar FROM test"), expectBadRequest(() -> runSql(randomMode(), "SELECT SCORE().bar FROM test"),
containsString("line 1:15: extraneous input '.' expecting {<EOF>, ','")); containsString("line 1:15: extraneous input '.' expecting {<EOF>, ','"));
} }
@Override @Override
public void testSelectScoreInScalar() throws Exception { public void testSelectScoreInScalar() throws Exception {
StringBuilder bulk = new StringBuilder(); index("{\"foo\":1}");
bulk.append("{\"index\":{\"_id\":\"1\"}}\n");
bulk.append("{\"foo\":1}\n");
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
expectBadRequest(() -> runSql(randomMode(), "SELECT SIN(SCORE()) FROM test"), expectBadRequest(() -> runSql(randomMode(), "SELECT SIN(SCORE()) FROM test"),
containsString("line 1:12: [SCORE()] cannot be an argument to a function")); containsString("line 1:12: [SCORE()] cannot be an argument to a function"));
} }
@ -340,37 +313,32 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
} }
private Map<String, Object> runSql(String mode, HttpEntity sql, String suffix) throws IOException { private Map<String, Object> runSql(String mode, HttpEntity sql, String suffix) throws IOException {
Map<String, String> params = new TreeMap<>(); Request request = new Request("POST", "/_xpack/sql" + suffix);
params.put("error_trace", "true"); // Helps with debugging in case something crazy happens on the server. request.addParameter("error_trace", "true"); // Helps with debugging in case something crazy happens on the server.
params.put("pretty", "true"); // Improves error reporting readability request.addParameter("pretty", "true"); // Improves error reporting readability
if (randomBoolean()) { if (randomBoolean()) {
// We default to JSON but we force it randomly for extra coverage // We default to JSON but we force it randomly for extra coverage
params.put("format", "json"); request.addParameter("format", "json");
} }
if (Strings.hasText(mode)) { if (false == mode.isEmpty()) {
params.put("mode", mode); // JDBC or PLAIN mode request.addParameter("mode", mode); // JDBC or PLAIN mode
} }
Header[] headers = randomFrom( request.setHeaders(randomFrom(
new Header[] {}, new Header[] {},
new Header[] {new BasicHeader("Accept", "*/*")}, new Header[] {new BasicHeader("Accept", "*/*")},
new Header[] {new BasicHeader("Accpet", "application/json")}); new Header[] {new BasicHeader("Accpet", "application/json")}));
Response response = client().performRequest("POST", "/_xpack/sql" + suffix, params, sql); request.setEntity(sql);
Response response = client().performRequest(request);
try (InputStream content = response.getEntity().getContent()) { try (InputStream content = response.getEntity().getContent()) {
return XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false); return XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
} }
} }
public void testBasicTranslateQuery() throws IOException { public void testBasicTranslateQuery() throws IOException {
StringBuilder bulk = new StringBuilder(); index("{\"test\":\"test\"}", "{\"test\":\"test\"}");
bulk.append("{\"index\":{\"_id\":\"1\"}}\n");
bulk.append("{\"test\":\"test\"}\n");
bulk.append("{\"index\":{\"_id\":\"2\"}}\n");
bulk.append("{\"test\":\"test\"}\n");
client().performRequest("POST", "/test_translate/test/_bulk", singletonMap("refresh", "true"),
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
Map<String, Object> response = runSql(randomMode(), "SELECT * FROM test_translate", "/translate/"); Map<String, Object> response = runSql(randomMode(), "SELECT * FROM test", "/translate/");
assertEquals(response.get("size"), 1000); assertEquals(1000, response.get("size"));
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> source = (Map<String, Object>) response.get("_source"); Map<String, Object> source = (Map<String, Object>) response.get("_source");
assertNotNull(source); assertNotNull(source);
@ -459,13 +427,12 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
} }
public void testNextPageText() throws IOException { public void testNextPageText() throws IOException {
StringBuilder bulk = new StringBuilder(); int size = 20;
for (int i = 0; i < 20; i++) { String[] docs = new String[size];
bulk.append("{\"index\":{\"_id\":\"" + i + "\"}}\n"); for (int i = 0; i < size; i++) {
bulk.append("{\"text\":\"text" + i + "\", \"number\":" + i + "}\n"); docs[i] = "{\"text\":\"text" + i + "\", \"number\":" + i + "}\n";
} }
client().performRequest("POST", "/test/test/_bulk", singletonMap("refresh", "true"), index(docs);
new StringEntity(bulk.toString(), ContentType.APPLICATION_JSON));
String request = "{\"query\":\"SELECT text, number, number + 5 AS sum FROM test ORDER BY number\", \"fetch_size\":2}"; String request = "{\"query\":\"SELECT text, number, number + 5 AS sum FROM test ORDER BY number\", \"fetch_size\":2}";
@ -563,23 +530,33 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
return runSqlAsText("", new StringEntity("{\"query\":\"" + sql + "\"}", ContentType.APPLICATION_JSON), accept); return runSqlAsText("", new StringEntity("{\"query\":\"" + sql + "\"}", ContentType.APPLICATION_JSON), accept);
} }
/**
* Run SQL as text using the {@code Accept} header to specify the format
* rather than the {@code format} parameter.
*/
private Tuple<String, String> runSqlAsText(String suffix, HttpEntity entity, String accept) throws IOException { private Tuple<String, String> runSqlAsText(String suffix, HttpEntity entity, String accept) throws IOException {
Response response = client().performRequest("POST", "/_xpack/sql" + suffix, singletonMap("error_trace", "true"), Request request = new Request("POST", "/_xpack/sql" + suffix);
entity, new BasicHeader("Accept", accept)); request.addParameter("error_trace", "true");
request.setEntity(entity);
request.setHeaders(new BasicHeader("Accept", accept));
Response response = client().performRequest(request);
return new Tuple<>( return new Tuple<>(
Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)), Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)),
response.getHeader("Cursor") response.getHeader("Cursor")
); );
} }
/**
* Run SQL as text using the {@code format} parameter to specify the format
* rather than an {@code Accept} header.
*/
private Tuple<String, String> runSqlAsTextFormat(String sql, String format) throws IOException { private Tuple<String, String> runSqlAsTextFormat(String sql, String format) throws IOException {
StringEntity entity = new StringEntity("{\"query\":\"" + sql + "\"}", ContentType.APPLICATION_JSON); Request request = new Request("POST", "/_xpack/sql");
request.addParameter("error_trace", "true");
request.addParameter("format", format);
request.setJsonEntity("{\"query\":\"" + sql + "\"}");
Map<String, String> params = new HashMap<>(); Response response = client().performRequest(request);
params.put("error_trace", "true");
params.put("format", format);
Response response = client().performRequest("POST", "/_xpack/sql", params, entity);
return new Tuple<>( return new Tuple<>(
Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)), Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)),
response.getHeader("Cursor") response.getHeader("Cursor")
@ -595,23 +572,14 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
} }
public static int getNumberOfSearchContexts(String index) throws IOException { public static int getNumberOfSearchContexts(String index) throws IOException {
Response response = client().performRequest("GET", "/_stats/search"); return getOpenContexts(searchStats(), index);
Map<String, Object> stats;
try (InputStream content = response.getEntity().getContent()) {
stats = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
}
return getOpenContexts(stats, index);
} }
public static void assertNoSearchContexts() throws IOException { public static void assertNoSearchContexts() throws IOException {
Response response = client().performRequest("GET", "/_stats/search"); Map<String, Object> stats = searchStats();
Map<String, Object> stats;
try (InputStream content = response.getEntity().getContent()) {
stats = XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> indexStats = (Map<String, Object>) stats.get("indices"); Map<String, Object> indicesStats = (Map<String, Object>) stats.get("indices");
for (String index : indexStats.keySet()) { for (String index : indicesStats.keySet()) {
if (index.startsWith(".") == false) { // We are not interested in internal indices if (index.startsWith(".") == false) { // We are not interested in internal indices
assertEquals(index + " should have no search contexts", 0, getOpenContexts(stats, index)); assertEquals(index + " should have no search contexts", 0, getOpenContexts(stats, index));
} }
@ -619,12 +587,34 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static int getOpenContexts(Map<String, Object> indexStats, String index) { private static int getOpenContexts(Map<String, Object> stats, String index) {
return (int) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) stats = (Map<String, Object>) stats.get("indices");
indexStats.get("indices")).get(index)).get("total")).get("search")).get("open_contexts"); stats = (Map<String, Object>) stats.get(index);
stats = (Map<String, Object>) stats.get("total");
stats = (Map<String, Object>) stats.get("search");
return (Integer) stats.get("open_contexts");
}
private static Map<String, Object> searchStats() throws IOException {
Response response = client().performRequest(new Request("GET", "/_stats/search"));
try (InputStream content = response.getEntity().getContent()) {
return XContentHelper.convertToMap(JsonXContent.jsonXContent, content, false);
}
} }
public static String randomMode() { public static String randomMode() {
return randomFrom("", "jdbc", "plain"); return randomFrom("", "jdbc", "plain");
} }
private void index(String... docs) throws IOException {
Request request = new Request("POST", "/test/test/_bulk");
request.addParameter("refresh", "true");
StringBuilder bulk = new StringBuilder();
for (String doc : docs) {
bulk.append("{\"index\":{}\n");
bulk.append(doc + "\n");
}
request.setJsonEntity(bulk.toString());
client().performRequest(request);
}
} }