From 76c0fd5e2736fe9f2a479f1ea1be4a2a3467ad15 Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Wed, 21 Jun 2023 13:12:31 -0700 Subject: [PATCH 1/7] specify expected format in formatter and pass to RestSQLQueryAction Signed-off-by: Matthew Wells --- .../opensearch/sql/legacy/plugin/RestSQLQueryAction.java | 8 ++++---- .../protocol/response/format/FlatResponseFormatter.java | 4 ++++ .../protocol/response/format/JsonResponseFormatter.java | 4 ++++ .../sql/protocol/response/format/ResponseFormatter.java | 2 ++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java index 37cbba4adfd..f32fd45d4eb 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java @@ -149,7 +149,7 @@ public void onResponse(ExplainResponse response) { protected Object buildJsonObject(ExplainResponse response) { return response; } - }.format(response)); + }.format(response), "application/json; charset=UTF-8"); } @Override @@ -180,7 +180,7 @@ private ResponseListener createQueryResponseListener( public void onResponse(QueryResponse response) { sendResponse(channel, OK, formatter.format(new QueryResult(response.getSchema(), response.getResults(), - response.getCursor()))); + response.getCursor())), formatter.getFormat()); } @Override @@ -190,9 +190,9 @@ public void onFailure(Exception e) { }; } - private void sendResponse(RestChannel channel, RestStatus status, String content) { + private void sendResponse(RestChannel channel, RestStatus status, String content, String contentType) { channel.sendResponse(new BytesRestResponse( - status, "application/json; charset=UTF-8", content)); + status, contentType, content)); } private static void logAndPublishMetrics(Exception e) { diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java index d4d00c45356..07dd420d48b 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java @@ -30,6 +30,10 @@ public FlatResponseFormatter(String seperator, boolean sanitize) { this.sanitize = sanitize; } + public String getFormat() { + return "plain/text; charset=UTF-8"; + } + @Override public String format(QueryResult response) { FlatResult result = new FlatResult(response, sanitize); diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java index 03e060925de..bfe8ab991ac 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java @@ -47,6 +47,10 @@ public String format(Throwable t) { (style == PRETTY) ? prettyFormat(t) : compactFormat(t)); } + public String getFormat() { + return "application/json; charset=UTF-8"; + } + /** * Build JSON object to generate response json string. * diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java index 4618a4f80d2..13e06035f2a 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java @@ -27,4 +27,6 @@ public interface ResponseFormatter { */ String format(Throwable t); + String getFormat(); + } From 51e5719121a9a4e749919b917230aaab699230d9 Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Wed, 21 Jun 2023 15:31:42 -0700 Subject: [PATCH 2/7] Added unit tests, updated code, changed local variables to constants Signed-off-by: Matthew Wells --- .../sql/legacy/plugin/RestSQLQueryAction.java | 11 ++++++---- .../format/FlatResponseFormatter.java | 6 ++++-- .../format/JsonResponseFormatter.java | 6 ++++-- .../response/format/ResponseFormatter.java | 7 ++++++- .../format/CommandResponseFormatterTest.java | 6 ++++++ .../format/CsvResponseFormatterTest.java | 5 +++++ .../format/RawResponseFormatterTest.java | 21 ++++++++++++------- .../VisualizationResponseFormatterTest.java | 6 ++++++ 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java index f32fd45d4eb..57b25a42ee5 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java @@ -53,6 +53,8 @@ public class RestSQLQueryAction extends BaseRestHandler { private final Injector injector; + private final String DEFAULT_FORMAT = "application/json; charset=UTF-8"; + /** * Constructor of RestSQLQueryAction. */ @@ -141,15 +143,16 @@ public void onFailure(Exception e) { private ResponseListener createExplainResponseListener( RestChannel channel, BiConsumer errorHandler) { - return new ResponseListener() { + return new ResponseListener<>() { @Override public void onResponse(ExplainResponse response) { - sendResponse(channel, OK, new JsonResponseFormatter(PRETTY) { + JsonResponseFormatter formatter = new JsonResponseFormatter<>(PRETTY) { @Override protected Object buildJsonObject(ExplainResponse response) { return response; } - }.format(response), "application/json; charset=UTF-8"); + }; + sendResponse(channel, OK, formatter.format(response), formatter.contentType()); } @Override @@ -180,7 +183,7 @@ private ResponseListener createQueryResponseListener( public void onResponse(QueryResponse response) { sendResponse(channel, OK, formatter.format(new QueryResult(response.getSchema(), response.getResults(), - response.getCursor())), formatter.getFormat()); + response.getCursor())), formatter.contentType()); } @Override diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java index 07dd420d48b..0575647dad3 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/FlatResponseFormatter.java @@ -23,6 +23,8 @@ public abstract class FlatResponseFormatter implements ResponseFormatter SENSITIVE_CHAR = ImmutableSet.of("=", "+", "-", "@"); + public static final String CONTENT_TYPE = "plain/text; charset=UTF-8"; + private boolean sanitize = false; public FlatResponseFormatter(String seperator, boolean sanitize) { @@ -30,8 +32,8 @@ public FlatResponseFormatter(String seperator, boolean sanitize) { this.sanitize = sanitize; } - public String getFormat() { - return "plain/text; charset=UTF-8"; + public String contentType() { + return CONTENT_TYPE; } @Override diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java index bfe8ab991ac..810a7d0c2d5 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/JsonResponseFormatter.java @@ -36,6 +36,8 @@ public enum Style { */ private final Style style; + public static final String CONTENT_TYPE = "application/json; charset=UTF-8"; + @Override public String format(R response) { return jsonify(buildJsonObject(response)); @@ -47,8 +49,8 @@ public String format(Throwable t) { (style == PRETTY) ? prettyFormat(t) : compactFormat(t)); } - public String getFormat() { - return "application/json; charset=UTF-8"; + public String contentType() { + return CONTENT_TYPE; } /** diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java index 13e06035f2a..4e997a8799e 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java @@ -27,6 +27,11 @@ public interface ResponseFormatter { */ String format(Throwable t); - String getFormat(); + /** + * Getter for the content type of the response + * + * @return string + */ + String contentType(); } diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java index a3052324fe5..89ee357753d 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java @@ -56,4 +56,10 @@ public void formats_error_as_default_formatter() { assertEquals(new JdbcResponseFormatter(PRETTY).format(exception), new CommandResponseFormatter().format(exception)); } + + @Test + void testContentType() { + var formatter = new CommandResponseFormatter(); + assertEquals(formatter.contentType(), "application/json; charset=UTF-8"); + } } diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java index 7008b51fa60..54d1330dc9c 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java @@ -130,4 +130,9 @@ void replaceNullValues() { assertEquals(format(expected), formatter.format(response)); } + @Test + void testContentType() { + assertEquals(formatter.contentType(), "plain/text; charset=UTF-8"); + } + } diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java index 24b5a4431dd..7e1da1630b8 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java @@ -27,7 +27,7 @@ * Unit test for {@link FlatResponseFormatter}. */ public class RawResponseFormatterTest { - private FlatResponseFormatter rawFormater = new RawResponseFormatter(); + private FlatResponseFormatter rawFormatter = new RawResponseFormatter(); @Test void formatResponse() { @@ -38,7 +38,7 @@ void formatResponse() { tupleValue(ImmutableMap.of("name", "John", "age", 20)), tupleValue(ImmutableMap.of("name", "Smith", "age", 30)))); String expected = "name|age%nJohn|20%nSmith|30"; - assertEquals(format(expected), rawFormater.format(response)); + assertEquals(format(expected), rawFormatter.format(response)); } @Test @@ -53,7 +53,7 @@ void sanitizeHeaders() { "=firstname", "John", "+lastname", "Smith", "-city", "Seattle", "@age", 20)))); String expected = "=firstname|+lastname|-city|@age%n" + "John|Smith|Seattle|20"; - assertEquals(format(expected), rawFormater.format(response)); + assertEquals(format(expected), rawFormatter.format(response)); } @Test @@ -74,7 +74,7 @@ void sanitizeData() { + "-Seattle%n" + "@Seattle%n" + "Seattle="; - assertEquals(format(expected), rawFormater.format(response)); + assertEquals(format(expected), rawFormatter.format(response)); } @Test @@ -86,7 +86,7 @@ void quoteIfRequired() { tupleValue(ImmutableMap.of("na|me", "John|Smith", "||age", "30|||")))); String expected = "\"na|me\"|\"||age\"%n" + "\"John|Smith\"|\"30|||\""; - assertEquals(format(expected), rawFormater.format(response)); + assertEquals(format(expected), rawFormatter.format(response)); } @Test @@ -94,7 +94,7 @@ void formatError() { Throwable t = new RuntimeException("This is an exception"); String expected = "{\n \"type\": \"RuntimeException\",\n \"reason\": \"This is an exception\"\n}"; - assertEquals(expected, rawFormater.format(t)); + assertEquals(expected, rawFormatter.format(t)); } @Test @@ -121,7 +121,7 @@ void senstiveCharater() { String expected = "city%n" + "@Seattle%n" + "++Seattle"; - assertEquals(format(expected), rawFormater.format(response)); + assertEquals(format(expected), rawFormatter.format(response)); } @Test @@ -153,7 +153,12 @@ void replaceNullValues() { + "John|Seattle%n" + "|Seattle%n" + "John|"; - assertEquals(format(expected), rawFormater.format(response)); + assertEquals(format(expected), rawFormatter.format(response)); + } + + @Test + void testContentType() { + assertEquals(rawFormatter.contentType(), "plain/text; charset=UTF-8"); } } diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java index 88a26aeb6bc..06d5eea59a0 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java @@ -187,4 +187,10 @@ private static void assertJsonEquals(String expected, String actual) { JsonParser.parseString(expected), JsonParser.parseString(actual)); } + + @Test + void testContentType() { + var formatter = new CommandResponseFormatter(); + assertEquals(formatter.contentType(), "application/json; charset=UTF-8"); + } } From 6237601195c4fdb67cf90a3b2c69d59452328db3 Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Thu, 22 Jun 2023 09:08:37 -0700 Subject: [PATCH 3/7] removed line of unneeded code Signed-off-by: Matthew Wells --- .../org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java index 57b25a42ee5..bbc1c293bec 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java @@ -53,8 +53,6 @@ public class RestSQLQueryAction extends BaseRestHandler { private final Injector injector; - private final String DEFAULT_FORMAT = "application/json; charset=UTF-8"; - /** * Constructor of RestSQLQueryAction. */ From 04fce29c7411a4204664269431202bafe3bf2015 Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Thu, 22 Jun 2023 11:35:38 -0700 Subject: [PATCH 4/7] fixed missing period in javadoc Signed-off-by: Matthew Wells --- .../sql/protocol/response/format/ResponseFormatter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java index 4e997a8799e..9be915e28cd 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java @@ -28,7 +28,7 @@ public interface ResponseFormatter { String format(Throwable t); /** - * Getter for the content type of the response + * Getter for the content type of the response. * * @return string */ From 1fce3446613b66bee8476b87f8d8a5f5da7bb66e Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Fri, 23 Jun 2023 09:12:56 -0700 Subject: [PATCH 5/7] improved tests, updated comment for clarity Signed-off-by: Matthew Wells --- .../sql/protocol/response/format/ResponseFormatter.java | 2 +- .../protocol/response/format/CommandResponseFormatterTest.java | 3 ++- .../sql/protocol/response/format/CsvResponseFormatterTest.java | 3 ++- .../sql/protocol/response/format/RawResponseFormatterTest.java | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java index 9be915e28cd..6d9cc093c58 100644 --- a/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java +++ b/protocol/src/main/java/org/opensearch/sql/protocol/response/format/ResponseFormatter.java @@ -28,7 +28,7 @@ public interface ResponseFormatter { String format(Throwable t); /** - * Getter for the content type of the response. + * Getter for the content type header of the response. * * @return string */ diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java index 89ee357753d..85efbab369f 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CommandResponseFormatterTest.java @@ -10,6 +10,7 @@ import static org.opensearch.sql.data.model.ExprValueUtils.tupleValue; import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; import static org.opensearch.sql.data.type.ExprCoreType.STRING; +import static org.opensearch.sql.protocol.response.format.JsonResponseFormatter.CONTENT_TYPE; import static org.opensearch.sql.protocol.response.format.JsonResponseFormatter.Style.PRETTY; import com.google.common.collect.ImmutableList; @@ -60,6 +61,6 @@ public void formats_error_as_default_formatter() { @Test void testContentType() { var formatter = new CommandResponseFormatter(); - assertEquals(formatter.contentType(), "application/json; charset=UTF-8"); + assertEquals(formatter.contentType(), CONTENT_TYPE); } } diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java index 54d1330dc9c..82b4f372b35 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/CsvResponseFormatterTest.java @@ -14,6 +14,7 @@ import static org.opensearch.sql.data.model.ExprValueUtils.tupleValue; import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; import static org.opensearch.sql.data.type.ExprCoreType.STRING; +import static org.opensearch.sql.protocol.response.format.FlatResponseFormatter.CONTENT_TYPE; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -132,7 +133,7 @@ void replaceNullValues() { @Test void testContentType() { - assertEquals(formatter.contentType(), "plain/text; charset=UTF-8"); + assertEquals(formatter.contentType(), CONTENT_TYPE); } } diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java index 7e1da1630b8..b33a4f216a0 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/RawResponseFormatterTest.java @@ -14,6 +14,7 @@ import static org.opensearch.sql.data.model.ExprValueUtils.tupleValue; import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; import static org.opensearch.sql.data.type.ExprCoreType.STRING; +import static org.opensearch.sql.protocol.response.format.FlatResponseFormatter.CONTENT_TYPE; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -158,7 +159,7 @@ void replaceNullValues() { @Test void testContentType() { - assertEquals(rawFormatter.contentType(), "plain/text; charset=UTF-8"); + assertEquals(rawFormatter.contentType(), CONTENT_TYPE); } } From b14c2460ff4e00507481064c12fd3031bf4c3d96 Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Fri, 23 Jun 2023 14:15:56 -0700 Subject: [PATCH 6/7] added integration tests Signed-off-by: Matthew Wells --- .../org/opensearch/sql/sql/CsvFormatIT.java | 16 ++++++++++++++++ .../sql/sql/SimpleQueryStringIT.java | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java index 782e2e22b51..9856489c59f 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java @@ -10,7 +10,10 @@ import java.io.IOException; import java.util.Locale; + import org.junit.Test; +import org.opensearch.client.Request; +import org.opensearch.client.Response; import org.opensearch.sql.common.utils.StringUtils; import org.opensearch.sql.legacy.SQLIntegTestCase; @@ -49,4 +52,17 @@ public void escapeSanitizeTest() { + "\",Elinor\",\"Ratliff,,,\"%n"), result); } + + @Test + public void contentHeaderTest() throws IOException { + String query = String.format(Locale.ROOT, "SELECT firstname, lastname FROM %s", TEST_INDEX_BANK_CSV_SANITIZE); + String requestBody = makeRequest(query); + + Request sqlRequest = new Request("POST", "/_plugins/_sql?format=csv"); + sqlRequest.setJsonEntity(requestBody); + + Response response = client().performRequest(sqlRequest); + + assertEquals(response.getEntity().getContentType(), "plain/text; charset=UTF-8"); + } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java index 6efd2769466..3b46bb2c994 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java @@ -5,11 +5,16 @@ package org.opensearch.sql.sql; +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_CSV_SANITIZE; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BEER; import java.io.IOException; +import java.util.Locale; + import org.json.JSONObject; import org.junit.Test; +import org.opensearch.client.Request; +import org.opensearch.client.Response; import org.opensearch.sql.legacy.SQLIntegTestCase; public class SimpleQueryStringIT extends SQLIntegTestCase { @@ -61,4 +66,18 @@ public void verify_wildcard_test() throws IOException { var result = new JSONObject(executeQuery(query, "jdbc")); assertEquals(10, result.getInt("total")); } + + @Test + public void contentHeaderTest() throws IOException { + String query = "SELECT Id FROM " + TEST_INDEX_BEER + + " WHERE simple_query_string([\\\"Tags\\\" ^ 1.5, Title, 'Body' 4.2], 'taste')"; + String requestBody = makeRequest(query); + + Request sqlRequest = new Request("POST", "/_plugins/_sql"); + sqlRequest.setJsonEntity(requestBody); + + Response response = client().performRequest(sqlRequest); + + assertEquals(response.getEntity().getContentType(), "application/json; charset=UTF-8"); + } } From 29e0cda00fbac4e45b73bbcc0d04c91b7e551780 Mon Sep 17 00:00:00 2001 From: Matthew Wells Date: Mon, 26 Jun 2023 08:59:30 -0700 Subject: [PATCH 7/7] updated and added tests Signed-off-by: Matthew Wells --- .../java/org/opensearch/sql/sql/CsvFormatIT.java | 3 ++- .../java/org/opensearch/sql/sql/RawFormatIT.java | 16 ++++++++++++++++ .../opensearch/sql/sql/SimpleQueryStringIT.java | 3 ++- .../VisualizationResponseFormatterTest.java | 3 ++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java index 9856489c59f..aa2737cbacc 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/CsvFormatIT.java @@ -7,6 +7,7 @@ package org.opensearch.sql.sql; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_CSV_SANITIZE; +import static org.opensearch.sql.protocol.response.format.FlatResponseFormatter.CONTENT_TYPE; import java.io.IOException; import java.util.Locale; @@ -63,6 +64,6 @@ public void contentHeaderTest() throws IOException { Response response = client().performRequest(sqlRequest); - assertEquals(response.getEntity().getContentType(), "plain/text; charset=UTF-8"); + assertEquals(response.getEntity().getContentType(), CONTENT_TYPE); } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java index 8cba86647c8..b040b971368 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/RawFormatIT.java @@ -6,11 +6,15 @@ package org.opensearch.sql.sql; +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_CSV_SANITIZE; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_RAW_SANITIZE; +import static org.opensearch.sql.protocol.response.format.FlatResponseFormatter.CONTENT_TYPE; import java.io.IOException; import java.util.Locale; import org.junit.Test; +import org.opensearch.client.Request; +import org.opensearch.client.Response; import org.opensearch.sql.common.utils.StringUtils; import org.opensearch.sql.legacy.SQLIntegTestCase; @@ -35,4 +39,16 @@ public void rawFormatWithPipeFieldTest() { result); } + @Test + public void contentHeaderTest() throws IOException { + String query = String.format(Locale.ROOT, "SELECT firstname, lastname FROM %s", TEST_INDEX_BANK_RAW_SANITIZE); + String requestBody = makeRequest(query); + + Request sqlRequest = new Request("POST", "/_plugins/_sql?format=raw"); + sqlRequest.setJsonEntity(requestBody); + + Response response = client().performRequest(sqlRequest); + + assertEquals(response.getEntity().getContentType(), CONTENT_TYPE); + } } diff --git a/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java b/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java index 3b46bb2c994..efd23dfdd41 100644 --- a/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java +++ b/integ-test/src/test/java/org/opensearch/sql/sql/SimpleQueryStringIT.java @@ -7,6 +7,7 @@ import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK_CSV_SANITIZE; import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BEER; +import static org.opensearch.sql.protocol.response.format.JsonResponseFormatter.CONTENT_TYPE; import java.io.IOException; import java.util.Locale; @@ -78,6 +79,6 @@ public void contentHeaderTest() throws IOException { Response response = client().performRequest(sqlRequest); - assertEquals(response.getEntity().getContentType(), "application/json; charset=UTF-8"); + assertEquals(response.getEntity().getContentType(), CONTENT_TYPE); } } diff --git a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java index 06d5eea59a0..f501a53d64c 100644 --- a/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java +++ b/protocol/src/test/java/org/opensearch/sql/protocol/response/format/VisualizationResponseFormatterTest.java @@ -11,6 +11,7 @@ import static org.opensearch.sql.data.model.ExprValueUtils.tupleValue; import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; import static org.opensearch.sql.data.type.ExprCoreType.STRING; +import static org.opensearch.sql.protocol.response.format.JsonResponseFormatter.CONTENT_TYPE; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -191,6 +192,6 @@ private static void assertJsonEquals(String expected, String actual) { @Test void testContentType() { var formatter = new CommandResponseFormatter(); - assertEquals(formatter.contentType(), "application/json; charset=UTF-8"); + assertEquals(formatter.contentType(), CONTENT_TYPE); } }