|
@@ -15,6 +15,7 @@ import org.elasticsearch.client.Response;
|
|
|
import org.elasticsearch.client.ResponseException;
|
|
|
import org.elasticsearch.common.CheckedSupplier;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
+import org.elasticsearch.common.bytes.BytesArray;
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
|
|
import org.elasticsearch.common.io.Streams;
|
|
|
import org.elasticsearch.common.xcontent.XContentHelper;
|
|
@@ -414,6 +415,85 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void testPrettyPrintingEnabled() throws IOException {
|
|
|
+ boolean columnar = randomBoolean();
|
|
|
+ String expected = "";
|
|
|
+ if (columnar) {
|
|
|
+ expected = "{\n" +
|
|
|
+ " \"columns\" : [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"name\" : \"test1\",\n" +
|
|
|
+ " \"type\" : \"text\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ],\n" +
|
|
|
+ " \"values\" : [\n" +
|
|
|
+ " [\n" +
|
|
|
+ " \"test1\",\n" +
|
|
|
+ " \"test2\"\n" +
|
|
|
+ " ]\n" +
|
|
|
+ " ]\n" +
|
|
|
+ "}\n";
|
|
|
+ } else {
|
|
|
+ expected = "{\n" +
|
|
|
+ " \"columns\" : [\n" +
|
|
|
+ " {\n" +
|
|
|
+ " \"name\" : \"test1\",\n" +
|
|
|
+ " \"type\" : \"text\"\n" +
|
|
|
+ " }\n" +
|
|
|
+ " ],\n" +
|
|
|
+ " \"rows\" : [\n" +
|
|
|
+ " [\n" +
|
|
|
+ " \"test1\"\n" +
|
|
|
+ " ],\n" +
|
|
|
+ " [\n" +
|
|
|
+ " \"test2\"\n" +
|
|
|
+ " ]\n" +
|
|
|
+ " ]\n" +
|
|
|
+ "}\n";
|
|
|
+ }
|
|
|
+ executeAndAssertPrettyPrinting(expected, "true", columnar);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testPrettyPrintingDisabled() throws IOException {
|
|
|
+ boolean columnar = randomBoolean();
|
|
|
+ String expected = "";
|
|
|
+ if (columnar) {
|
|
|
+ expected = "{\"columns\":[{\"name\":\"test1\",\"type\":\"text\"}],\"values\":[[\"test1\",\"test2\"]]}";
|
|
|
+ } else {
|
|
|
+ expected = "{\"columns\":[{\"name\":\"test1\",\"type\":\"text\"}],\"rows\":[[\"test1\"],[\"test2\"]]}";
|
|
|
+ }
|
|
|
+ executeAndAssertPrettyPrinting(expected, randomFrom("false", null), columnar);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void executeAndAssertPrettyPrinting(String expectedJson, String prettyParameter, boolean columnar)
|
|
|
+ throws IOException {
|
|
|
+ index("{\"test1\":\"test1\"}",
|
|
|
+ "{\"test1\":\"test2\"}");
|
|
|
+
|
|
|
+ Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT);
|
|
|
+ if (prettyParameter != null) {
|
|
|
+ request.addParameter("pretty", prettyParameter);
|
|
|
+ }
|
|
|
+ if (randomBoolean()) {
|
|
|
+ // We default to JSON but we force it randomly for extra coverage
|
|
|
+ request.addParameter("format", "json");
|
|
|
+ }
|
|
|
+ if (randomBoolean()) {
|
|
|
+ // JSON is the default but randomly set it sometime for extra coverage
|
|
|
+ RequestOptions.Builder options = request.getOptions().toBuilder();
|
|
|
+ options.addHeader("Accept", randomFrom("*/*", "application/json"));
|
|
|
+ request.setOptions(options);
|
|
|
+ }
|
|
|
+ request.setEntity(new StringEntity("{\"query\":\"SELECT * FROM test\"" + mode("plain") + columnarParameter(columnar) + "}",
|
|
|
+ ContentType.APPLICATION_JSON));
|
|
|
+
|
|
|
+ Response response = client().performRequest(request);
|
|
|
+ try (InputStream content = response.getEntity().getContent()) {
|
|
|
+ String actualJson = new BytesArray(content.readAllBytes()).utf8ToString();
|
|
|
+ assertEquals(expectedJson, actualJson);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void testBasicTranslateQuery() throws IOException {
|
|
|
index("{\"test\":\"test\"}", "{\"test\":\"test\"}");
|
|
|
|