From 873ecee88c1c47ffb0d9667e230b64a2f343e74c Mon Sep 17 00:00:00 2001 From: Lantao Jin Date: Thu, 4 Jul 2024 22:07:04 +0800 Subject: [PATCH] address coverage Signed-off-by: Lantao Jin --- .../opensearch/sql/legacy/plugin/RestSqlAction.java | 2 +- .../legacy/unittest/utils/QueryDataAnonymizerTest.java | 2 +- .../org/opensearch/sql/sql/domain/SQLQueryRequest.java | 3 ++- .../opensearch/sql/sql/domain/SQLQueryRequestTest.java | 10 ++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java index 11a6cdba7e..3a47af2700 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSqlAction.java @@ -167,7 +167,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli LOG.info( "[{}] Request {} is not supported and falling back to old SQL engine", QueryContext.getRequestId(), - newSqlRequest.toAnonymousString(QueryDataAnonymizer::anonymizeData)); + newSqlRequest.toString(QueryDataAnonymizer::anonymizeData)); LOG.info("Request Query: {}", QueryDataAnonymizer.anonymizeData(sqlRequest.getSql())); QueryAction queryAction = explainRequest(client, sqlRequest, format); executeSqlRequest(request, queryAction, client, restChannel); diff --git a/legacy/src/test/java/org/opensearch/sql/legacy/unittest/utils/QueryDataAnonymizerTest.java b/legacy/src/test/java/org/opensearch/sql/legacy/unittest/utils/QueryDataAnonymizerTest.java index 6f96bc39a3..be17d828a1 100644 --- a/legacy/src/test/java/org/opensearch/sql/legacy/unittest/utils/QueryDataAnonymizerTest.java +++ b/legacy/src/test/java/org/opensearch/sql/legacy/unittest/utils/QueryDataAnonymizerTest.java @@ -101,7 +101,7 @@ public void test_to_anonymous_string_of_SQLQueryRequest() { + "FROM accounts a JOIN employees e"; SQLQueryRequest request = new SQLQueryRequest(null, query, "/_plugins/_sql", Map.of("pretty", "true"), null); - String actualQuery = request.toAnonymousString(QueryDataAnonymizer::anonymizeData); + String actualQuery = request.toString(QueryDataAnonymizer::anonymizeData); String expectedQuery = "SQLQueryRequest(query=( SELECT identifier, identifier, identifier, identifier, identifier" + " FROM table a JOIN table e ), path=/_plugins/_sql, format=jdbc," diff --git a/sql/src/main/java/org/opensearch/sql/sql/domain/SQLQueryRequest.java b/sql/src/main/java/org/opensearch/sql/sql/domain/SQLQueryRequest.java index ac028b9856..1169afb121 100644 --- a/sql/src/main/java/org/opensearch/sql/sql/domain/SQLQueryRequest.java +++ b/sql/src/main/java/org/opensearch/sql/sql/domain/SQLQueryRequest.java @@ -150,7 +150,8 @@ private boolean shouldSanitize(Map params) { return true; } - public String toAnonymousString(Function anonymizer) { + /** A new toString() with anonymizer parameter to anonymize its query statement. */ + public String toString(Function anonymizer) { return "SQLQueryRequest(" + "query=" + anonymizer.apply(getQuery()) diff --git a/sql/src/test/java/org/opensearch/sql/sql/domain/SQLQueryRequestTest.java b/sql/src/test/java/org/opensearch/sql/sql/domain/SQLQueryRequestTest.java index 2b64b13b35..fc1434301e 100644 --- a/sql/src/test/java/org/opensearch/sql/sql/domain/SQLQueryRequestTest.java +++ b/sql/src/test/java/org/opensearch/sql/sql/domain/SQLQueryRequestTest.java @@ -222,6 +222,16 @@ public void should_support_raw_format() { assertTrue(csvRequest.isSupported()); } + @Test + public void test_to_string_with_anonymizer() { + SQLQueryRequest request = SQLQueryRequestBuilder.request("SELECT 1").build(); + String actual = request.toString(s -> "***"); + String expected = + "SQLQueryRequest(query=***, path=_plugins/_sql, format=jdbc, params={}, sanitize=true," + + " cursor=Optional.empty)"; + assertEquals(expected, actual); + } + /** SQL query request build helper to improve test data setup readability. */ private static class SQLQueryRequestBuilder { private String jsonContent;