|
6 | 6 | package org.elasticsearch.xpack.sql.qa.rest; |
7 | 7 |
|
8 | 8 | import com.fasterxml.jackson.core.io.JsonStringEncoder; |
| 9 | + |
9 | 10 | import org.apache.http.HttpEntity; |
10 | 11 | import org.apache.http.entity.ContentType; |
11 | 12 | import org.apache.http.entity.StringEntity; |
|
25 | 26 | import org.elasticsearch.xpack.sql.qa.ErrorsTestCase; |
26 | 27 | import org.hamcrest.Matcher; |
27 | 28 |
|
| 29 | +import java.io.ByteArrayOutputStream; |
28 | 30 | import java.io.IOException; |
29 | 31 | import java.io.InputStream; |
30 | 32 | import java.io.InputStreamReader; |
@@ -414,6 +416,91 @@ protected Map<String, Object> runSql(HttpEntity sql, String suffix) throws IOExc |
414 | 416 | } |
415 | 417 | } |
416 | 418 |
|
| 419 | + public void testPrettyPrintingEnabled() throws IOException { |
| 420 | + boolean columnar = randomBoolean(); |
| 421 | + String expected = ""; |
| 422 | + if (columnar) { |
| 423 | + expected = "{\n" + |
| 424 | + " \"columns\" : [\n" + |
| 425 | + " {\n" + |
| 426 | + " \"name\" : \"test1\",\n" + |
| 427 | + " \"type\" : \"text\"\n" + |
| 428 | + " }\n" + |
| 429 | + " ],\n" + |
| 430 | + " \"values\" : [\n" + |
| 431 | + " [\n" + |
| 432 | + " \"test1\",\n" + |
| 433 | + " \"test2\"\n" + |
| 434 | + " ]\n" + |
| 435 | + " ]\n" + |
| 436 | + "}\n"; |
| 437 | + } else { |
| 438 | + expected = "{\n" + |
| 439 | + " \"columns\" : [\n" + |
| 440 | + " {\n" + |
| 441 | + " \"name\" : \"test1\",\n" + |
| 442 | + " \"type\" : \"text\"\n" + |
| 443 | + " }\n" + |
| 444 | + " ],\n" + |
| 445 | + " \"rows\" : [\n" + |
| 446 | + " [\n" + |
| 447 | + " \"test1\"\n" + |
| 448 | + " ],\n" + |
| 449 | + " [\n" + |
| 450 | + " \"test2\"\n" + |
| 451 | + " ]\n" + |
| 452 | + " ]\n" + |
| 453 | + "}\n"; |
| 454 | + } |
| 455 | + executeAndAssertPrettyPrinting(expected, "true", columnar); |
| 456 | + } |
| 457 | + |
| 458 | + public void testPrettyPrintingDisabled() throws IOException { |
| 459 | + boolean columnar = randomBoolean(); |
| 460 | + String expected = ""; |
| 461 | + if (columnar) { |
| 462 | + expected = "{\"columns\":[{\"name\":\"test1\",\"type\":\"text\"}],\"values\":[[\"test1\",\"test2\"]]}"; |
| 463 | + } else { |
| 464 | + expected = "{\"columns\":[{\"name\":\"test1\",\"type\":\"text\"}],\"rows\":[[\"test1\"],[\"test2\"]]}"; |
| 465 | + } |
| 466 | + executeAndAssertPrettyPrinting(expected, randomFrom("false", null), columnar); |
| 467 | + } |
| 468 | + |
| 469 | + private void executeAndAssertPrettyPrinting(String expectedJson, String prettyParameter, boolean columnar) |
| 470 | + throws IOException { |
| 471 | + index("{\"test1\":\"test1\"}", |
| 472 | + "{\"test1\":\"test2\"}"); |
| 473 | + |
| 474 | + Request request = new Request("POST", SQL_QUERY_REST_ENDPOINT); |
| 475 | + if (prettyParameter != null) { |
| 476 | + request.addParameter("pretty", prettyParameter); |
| 477 | + } |
| 478 | + if (randomBoolean()) { |
| 479 | + // We default to JSON but we force it randomly for extra coverage |
| 480 | + request.addParameter("format", "json"); |
| 481 | + } |
| 482 | + if (randomBoolean()) { |
| 483 | + // JSON is the default but randomly set it sometime for extra coverage |
| 484 | + RequestOptions.Builder options = request.getOptions().toBuilder(); |
| 485 | + options.addHeader("Accept", randomFrom("*/*", "application/json")); |
| 486 | + request.setOptions(options); |
| 487 | + } |
| 488 | + request.setEntity(new StringEntity("{\"query\":\"SELECT * FROM test\"" + mode("plain") + columnarParameter(columnar) + "}", |
| 489 | + ContentType.APPLICATION_JSON)); |
| 490 | + |
| 491 | + Response response = client().performRequest(request); |
| 492 | + try (InputStream content = response.getEntity().getContent()) { |
| 493 | + ByteArrayOutputStream result = new ByteArrayOutputStream(); |
| 494 | + byte[] buffer = new byte[1024]; |
| 495 | + int length; |
| 496 | + while ((length = content.read(buffer)) != -1) { |
| 497 | + result.write(buffer, 0, length); |
| 498 | + } |
| 499 | + String actualJson = result.toString("UTF-8"); |
| 500 | + assertEquals(expectedJson, actualJson); |
| 501 | + } |
| 502 | + } |
| 503 | + |
417 | 504 | public void testBasicTranslateQuery() throws IOException { |
418 | 505 | index("{\"test\":\"test\"}", "{\"test\":\"test\"}"); |
419 | 506 |
|
|
0 commit comments