diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/NestedFieldQueryTest.java b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/NestedFieldQueryIT.java similarity index 62% rename from src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/NestedFieldQueryTest.java rename to src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/NestedFieldQueryIT.java index d1ebd89840..c3adb03e95 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/NestedFieldQueryTest.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/NestedFieldQueryIT.java @@ -13,35 +13,33 @@ * permissions and limitations under the License. */ -package com.amazon.opendistroforelasticsearch.sql.intgtest; +package com.amazon.opendistroforelasticsearch.sql.esintgtest; -import com.amazon.opendistroforelasticsearch.sql.exception.SqlParseException; -import com.amazon.opendistroforelasticsearch.sql.plugin.SearchDao; -import com.amazon.opendistroforelasticsearch.sql.query.SqlElasticRequestBuilder; +import com.amazon.opendistroforelasticsearch.sql.intgtest.TestsConstants; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.search.aggregations.Aggregation; -import org.elasticsearch.search.aggregations.Aggregations; -import org.elasticsearch.search.aggregations.bucket.nested.InternalNested; -import org.elasticsearch.search.aggregations.bucket.terms.Terms; -import org.elasticsearch.search.aggregations.metrics.Avg; -import org.elasticsearch.search.aggregations.metrics.Sum; -import org.elasticsearch.search.aggregations.metrics.ValueCount; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.FeatureMatcher; import org.hamcrest.Matcher; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Assert; import org.junit.Test; -import java.sql.SQLFeatureNotSupportedException; +import java.io.IOException; import java.util.ArrayList; import java.util.function.Function; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.arrayContainingInAnyOrder; import static org.hamcrest.Matchers.closeTo; -import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; /** @@ -62,21 +60,26 @@ * 5) HAVING * 6) Verification for conditions mixed with regular and nested fields */ -public class NestedFieldQueryTest { +public class NestedFieldQueryIT extends SQLIntegTestCase { private static final String FROM = "FROM " + TestsConstants.TEST_INDEX_NESTED_TYPE + "/nestedType n, n.message m"; + @Override + protected void init() throws Exception { + loadIndex(Index.NESTED); + } + @Test - public void selectAll() { + public void selectAll() throws IOException { queryAll("SELECT *"); } @Test - public void noCondition() { + public void noCondition() throws IOException { queryAll("SELECT myNum, someField, m.author, m.info"); } - private void queryAll(String sql) { + private void queryAll(String sql) throws IOException { assertThat( query(sql), hits( @@ -139,7 +142,7 @@ private void queryAll(String sql) { } @Test - public void singleCondition() { + public void singleCondition() throws IOException { assertThat( query( "SELECT myNum, m.author, m.info", @@ -169,7 +172,7 @@ public void singleCondition() { } @Test - public void multipleConditionsOfNestedField() { + public void multipleConditionsOfNestedField() throws IOException { assertThat( query( "SELECT someField, m.author, m.info", @@ -190,7 +193,7 @@ public void multipleConditionsOfNestedField() { } @Test - public void multipleConditionsOfNestedFieldNoMatch() { + public void multipleConditionsOfNestedFieldNoMatch() throws IOException { assertThat( query( "SELECT someField, m.author, m.info", @@ -201,7 +204,7 @@ public void multipleConditionsOfNestedFieldNoMatch() { } @Test - public void multipleConditionsOfRegularAndNestedField() { + public void multipleConditionsOfRegularAndNestedField() throws IOException { assertThat( query( "SELECT myNum, m.author, m.info", @@ -222,7 +225,7 @@ public void multipleConditionsOfRegularAndNestedField() { } @Test - public void multipleConditionsOfRegularOrNestedField() { + public void multipleConditionsOfRegularOrNestedField() throws IOException { assertThat( query( "SELECT myNum, m.author, m.info", @@ -255,46 +258,54 @@ public void multipleConditionsOfRegularOrNestedField() { } @Test - public void aggregationWithoutGroupBy() { - SearchResponse resp = query("SELECT AVG(m.dayOfWeek) AS avgDay"); - Avg avgDay = getNestedAgg(resp, "message.dayOfWeek", "avgDay"); - assertThat(avgDay.getValue(), isCloseTo(3.166666666)); + public void aggregationWithoutGroupBy() throws IOException { + String sql = "SELECT AVG(m.dayOfWeek) AS avgDay " + FROM; + + JSONObject result = executeQuery(sql); + JSONObject aggregation = getAggregation(result, "message.dayOfWeek@NESTED"); + + Assert.assertThat((Double) aggregation.query("/avgDay/value"), closeTo(3.166666666, 0.01)); } @Test - public void groupByNestedFieldAndCount() { - SearchResponse resp = query( - "SELECT m.info, COUNT(*)", "GROUP BY m.info" - ); - assertThat( - getNestedAgg(resp, "message.info", "message.info"), - buckets( - bucket("a", count(equalTo(2))), - bucket("c", count(equalTo(2))), - bucket("b", count(equalTo(1))), - bucket("zz", count(equalTo(1))) - ) - ); + public void groupByNestedFieldAndCount() throws IOException { + final String sql = "SELECT m.info, COUNT(*) " + FROM + " GROUP BY m.info"; + + JSONObject result = executeQuery(sql); + JSONObject aggregation = getAggregation(result, "message.info@NESTED"); + JSONArray msgInfoBuckets = (JSONArray)aggregation.optQuery("/message.info/buckets"); + + Assert.assertNotNull(msgInfoBuckets); + Assert.assertThat(msgInfoBuckets.length(), equalTo(4)); + Assert.assertThat(msgInfoBuckets.query("/0/key"), equalTo("a")); + Assert.assertThat(msgInfoBuckets.query("/0/COUNT(*)/value"), equalTo(2)); + Assert.assertThat(msgInfoBuckets.query("/1/key"), equalTo("c")); + Assert.assertThat(msgInfoBuckets.query("/1/COUNT(*)/value"), equalTo(2)); + Assert.assertThat(msgInfoBuckets.query("/2/key"), equalTo("b")); + Assert.assertThat(msgInfoBuckets.query("/2/COUNT(*)/value"), equalTo(1)); + Assert.assertThat(msgInfoBuckets.query("/3/key"), equalTo("zz")); + Assert.assertThat(msgInfoBuckets.query("/3/COUNT(*)/value"), equalTo(1)); } @Test - public void groupByRegularFieldAndSum() { - SearchResponse resp = query( - "SELECT *, SUM(m.dayOfWeek) AS sumDay", - "GROUP BY someField" - ); - assertThat( - getAgg(resp, "someField"), - buckets( - bucket("a", sum("message.dayOfWeek", "sumDay", isCloseTo(9.0))), - bucket("b", sum("message.dayOfWeek", "sumDay", isCloseTo(10.0))) - ) - ); + public void groupByRegularFieldAndSum() throws IOException { + final String sql = "SELECT *, SUM(m.dayOfWeek) AS sumDay " + FROM + " GROUP BY someField"; + + JSONObject result = executeQuery(sql); + JSONObject aggregation = getAggregation(result, "someField"); + JSONArray msgInfoBuckets = (JSONArray)aggregation.optQuery("/buckets"); + + Assert.assertNotNull(msgInfoBuckets); + Assert.assertThat(msgInfoBuckets.length(), equalTo(2)); + Assert.assertThat(msgInfoBuckets.query("/0/key"), equalTo("a")); + Assert.assertThat((Double) msgInfoBuckets.query("/0/message.dayOfWeek@NESTED/sumDay/value"), closeTo(9.0, 0.01)); + Assert.assertThat(msgInfoBuckets.query("/1/key"), equalTo("b")); + Assert.assertThat((Double) msgInfoBuckets.query("/1/message.dayOfWeek@NESTED/sumDay/value"), closeTo(10.0, 0.01)); } // Doesn't support: aggregate function other than COUNT() @SuppressWarnings("unused") - public void groupByNestedFieldAndAvg() { + public void groupByNestedFieldAndAvg() throws IOException { query( "SELECT m.info, AVG(m.dayOfWeek)", "GROUP BY m.info" @@ -306,33 +317,31 @@ public void groupByNestedFieldAndAvg() { } @Test - public void groupByNestedAndRegularField() { - SearchResponse resp = query( - "SELECT someField, m.info, COUNT(*)", - "GROUP BY someField, m.info" - ); - Terms agg = getAgg(resp, "someField"); - assertThat( - getNestedAgg(agg.getBucketByKey("a").getAggregations(), "message.info", "message.info"), - buckets( - bucket("b", count(equalTo(1))), - bucket("c", count(equalTo(1))), - bucket("zz", count(equalTo(1))) - ) - ); - assertThat( - getNestedAgg(agg.getBucketByKey("b").getAggregations(), "message.info", "message.info"), - buckets( - bucket("a", count(equalTo(2))), - bucket("c", count(equalTo(1))) - ) - ); + public void groupByNestedAndRegularField() throws IOException { + final String sql = "SELECT someField, m.info, COUNT(*) " + FROM + " GROUP BY someField, m.info"; + + JSONObject result = executeQuery(sql); + JSONObject aggregation = getAggregation(result, "someField"); + JSONArray msgInfoBuckets = (JSONArray)aggregation.optQuery("/buckets"); + + Assert.assertNotNull(msgInfoBuckets); + Assert.assertThat(msgInfoBuckets.length(), equalTo(2)); + Assert.assertThat(msgInfoBuckets.query("/0/key"), equalTo("a")); + Assert.assertThat(msgInfoBuckets.query("/1/key"), equalTo("b")); - //doesn't support: group by nested field first - /*query( - "SELECT *, SUM(m.dayOfWeek)", - "GROUP BY m.author, someField" - );*/ + JSONArray innerBuckets = (JSONArray)msgInfoBuckets.optQuery("/0/message.info@NESTED/message.info/buckets"); + Assert.assertThat(innerBuckets.query("/0/key"), equalTo("b")); + Assert.assertThat(innerBuckets.query("/0/COUNT(*)/value"), equalTo(1)); + Assert.assertThat(innerBuckets.query("/1/key"), equalTo("c")); + Assert.assertThat(innerBuckets.query("/1/COUNT(*)/value"), equalTo(1)); + Assert.assertThat(innerBuckets.query("/2/key"), equalTo("zz")); + Assert.assertThat(innerBuckets.query("/2/COUNT(*)/value"), equalTo(1)); + + innerBuckets = (JSONArray)msgInfoBuckets.optQuery("/1/message.info@NESTED/message.info/buckets"); + Assert.assertThat(innerBuckets.query("/0/key"), equalTo("a")); + Assert.assertThat(innerBuckets.query("/0/COUNT(*)/value"), equalTo(2)); + Assert.assertThat(innerBuckets.query("/1/key"), equalTo("c")); + Assert.assertThat(innerBuckets.query("/1/COUNT(*)/value"), equalTo(1)); } @@ -414,50 +423,6 @@ private final Matcher innerHits(String path, Matcher... in Matchers for Aggregation Testing ***********************************************************/ - private T getAgg(SearchResponse resp, - String fieldName) { - return resp.getAggregations().get(fieldName); - } - - private T getNestedAgg(SearchResponse resp, - String nestedFieldName, - String aggAlias) { - return getNestedAgg(resp.getAggregations(), nestedFieldName, aggAlias); - } - - private T getNestedAgg(Aggregations aggs, - String nestedFieldName, - String aggAlias) { - return aggs.get(nestedFieldName + "@NESTED"). - getAggregations(). - get(aggAlias); - } - - @SafeVarargs - private final Matcher buckets(Matcher... subMatchers) { - return featureValueOf("buckets", containsInAnyOrder(subMatchers), Terms::getBuckets); - } - - private Matcher bucket(String key, Matcher valMatcher) { - return featureValueOf(key, valMatcher, Terms.Bucket::getAggregations); - } - - private Matcher count(Matcher subMatcher) { - return featureValueOf("COUNT(*)", subMatcher, aggs -> ((ValueCount) aggs.get("COUNT(*)")).getValue()); - } - - private Matcher sum(String nestedFieldName, String name, Matcher subMatcher) { - return featureValueOf(name, subMatcher, aggs -> ((Sum) getNestedAgg(aggs, nestedFieldName, name)).getValue()); - } - - private Matcher isCloseTo(double expected) { - return closeTo(expected, 0.01); - } - - private Matcher equalTo(int expected) { - return is((long) expected); - } - private FeatureMatcher featureValueOf(String name, Matcher subMatcher, Function getter) { return new FeatureMatcher(subMatcher, name, name) { @Override @@ -471,19 +436,28 @@ protected U featureValueOf(T actual) { Query Utility to Fetch Response for SQL ***********************************************************/ - private SearchResponse query(String select, String... statements) { + private SearchResponse query(String select, String... statements) throws IOException { return execute(select + " " + FROM + " " + String.join(" ", statements)); } - private SearchResponse execute(String sql) { - SearchDao searchDao = MainTestSuite.getSearchDao(); - try { - SqlElasticRequestBuilder result = searchDao.explain(sql).explain(); - return (SearchResponse) result.get(); - } - catch (SqlParseException | SQLFeatureNotSupportedException e) { - throw new IllegalStateException("Query failed: " + sql, e); - } + private SearchResponse execute(String sql) throws IOException { + final JSONObject jsonObject = executeQuery(sql); + + final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + jsonObject.toString()); + return SearchResponse.fromXContent(parser); + } + + private JSONObject getAggregation(final JSONObject queryResult, final String aggregationName) + { + final String aggregationsObjName = "aggregations"; + Assert.assertTrue(queryResult.has(aggregationsObjName)); + + final JSONObject aggregations = queryResult.getJSONObject(aggregationsObjName); + Assert.assertTrue(aggregations.has(aggregationName)); + return aggregations.getJSONObject(aggregationName); } } diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/QueryFunctionsTest.java b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/QueryFunctionsIT.java similarity index 78% rename from src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/QueryFunctionsTest.java rename to src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/QueryFunctionsIT.java index 31f816bab8..da41eb768e 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/QueryFunctionsTest.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/QueryFunctionsIT.java @@ -13,21 +13,24 @@ * permissions and limitations under the License. */ -package com.amazon.opendistroforelasticsearch.sql.intgtest; +package com.amazon.opendistroforelasticsearch.sql.esintgtest; -import com.amazon.opendistroforelasticsearch.sql.plugin.SearchDao; -import com.amazon.opendistroforelasticsearch.sql.exception.SqlParseException; -import com.amazon.opendistroforelasticsearch.sql.query.SqlElasticRequestBuilder; +import com.amazon.opendistroforelasticsearch.sql.intgtest.TestsConstants; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.SearchHit; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.FeatureMatcher; import org.hamcrest.Matcher; -import org.json.JSONArray; +import org.json.JSONObject; import org.junit.Test; -import java.sql.SQLFeatureNotSupportedException; +import java.io.IOException; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -35,26 +38,32 @@ import java.util.function.Function; import java.util.stream.Collectors; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.everyItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; -public class QueryFunctionsTest { +public class QueryFunctionsIT extends SQLIntegTestCase { private static final String SELECT_ALL = "SELECT *"; - private static final String FROM_ACCOUNTS = "FROM " + TestsConstants.TEST_INDEX_ACCOUNT + "/account"; - private static final String FROM_NESTED = "FROM " + TestsConstants.TEST_INDEX_NESTED_TYPE + "/nestedType"; + private static final String FROM_ACCOUNTS = "FROM " + com.amazon.opendistroforelasticsearch.sql.intgtest.TestsConstants.TEST_INDEX_ACCOUNT + "/account"; + private static final String FROM_NESTED = "FROM " + com.amazon.opendistroforelasticsearch.sql.intgtest.TestsConstants.TEST_INDEX_NESTED_TYPE + "/nestedType"; private static final String FROM_PHRASE = "FROM " + TestsConstants.TEST_INDEX_PHRASE + "/phrase"; /** * TODO Looks like Math/Date Functions test all use the same query() and execute() functions - * TODO execute/featureValueOf/hits functions are the same as used in NestedFieldQueryTest, should refactor into util + * TODO execute/featureValueOf/hits functions are the same as used in NestedFieldQueryIT, should refactor into util */ + @Override + protected void init() throws Exception { + loadIndex(Index.ACCOUNT); + loadIndex(Index.NESTED); + loadIndex(Index.PHRASE); + } + @Test - public void query() { + public void query() throws IOException { assertThat( query( "SELECT state", @@ -68,7 +77,7 @@ public void query() { } @Test - public void matchQueryRegularField() { + public void matchQueryRegularField() throws IOException { assertThat( query( "SELECT firstname", @@ -82,7 +91,7 @@ public void matchQueryRegularField() { } @Test - public void matchQueryNestedField() { + public void matchQueryNestedField() throws IOException { SearchHit[] hits = query("SELECT comment.data", FROM_NESTED, "WHERE MATCH_QUERY(NESTED(comment.data), 'aa')").getHits().getHits(); Map source = hits[0].getSourceAsMap(); // SearchHits innerHits = hits[0].getInnerHits().get("comment"); @@ -100,7 +109,7 @@ public void matchQueryNestedField() { } @Test - public void scoreQuery() { + public void scoreQuery() throws IOException { assertThat( query( "SELECT firstname", @@ -114,7 +123,7 @@ public void scoreQuery() { } @Test - public void scoreQueryWithNestedField() { + public void scoreQueryWithNestedField() throws IOException { assertThat( query( "SELECT comment.data", @@ -130,7 +139,7 @@ public void scoreQueryWithNestedField() { } @Test - public void wildcardQuery() { + public void wildcardQuery() throws IOException { assertThat( query( "SELECT city", @@ -144,7 +153,7 @@ public void wildcardQuery() { } @Test - public void matchPhraseQuery() { + public void matchPhraseQuery() throws IOException { assertThat( query( "SELECT phrase", @@ -158,7 +167,7 @@ public void matchPhraseQuery() { } @Test - public void multiMatchQuerySingleField() { + public void multiMatchQuerySingleField() throws IOException { assertThat( query( "SELECT firstname", @@ -172,7 +181,7 @@ public void multiMatchQuerySingleField() { } @Test - public void multiMatchQueryWildcardField() { + public void multiMatchQueryWildcardField() throws IOException { assertThat( query( "SELECT firstname, lastname", @@ -254,18 +263,17 @@ private Matcher kv(String key, Matcher valMatcher) { Query Utility to Fetch Response for SQL ***********************************************************/ - private SearchResponse query(String select, String from, String... statements) { + private SearchResponse query(String select, String from, String... statements) throws IOException { return execute(select + " " + from + " " + String.join(" ", statements)); } - private SearchResponse execute(String sql) { - SearchDao searchDao = MainTestSuite.getSearchDao(); - try { - SqlElasticRequestBuilder result = searchDao.explain(sql).explain(); - return (SearchResponse) result.get(); - } - catch (SqlParseException | SQLFeatureNotSupportedException e) { - throw new IllegalStateException("Query failed: " + sql, e); - } + private SearchResponse execute(String sql) throws IOException { + final JSONObject jsonObject = executeQuery(sql); + + final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + jsonObject.toString()); + return SearchResponse.fromXContent(parser); } } diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/SourceFieldTest.java b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SourceFieldIT.java similarity index 69% rename from src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/SourceFieldTest.java rename to src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SourceFieldIT.java index 174988cb46..b9cdff819f 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/SourceFieldTest.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/sql/esintgtest/SourceFieldIT.java @@ -13,27 +13,34 @@ * permissions and limitations under the License. */ -package com.amazon.opendistroforelasticsearch.sql.intgtest; +package com.amazon.opendistroforelasticsearch.sql.esintgtest; import static com.amazon.opendistroforelasticsearch.sql.intgtest.TestsConstants.*; import java.io.IOException; -import java.sql.SQLFeatureNotSupportedException; import java.util.Set; -import com.amazon.opendistroforelasticsearch.sql.exception.SqlParseException; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; +import org.json.JSONObject; import org.junit.Assert; import org.junit.Test; -import com.amazon.opendistroforelasticsearch.sql.plugin.SearchDao; -import com.amazon.opendistroforelasticsearch.sql.query.SqlElasticSearchRequestBuilder; -public class SourceFieldTest { +public class SourceFieldIT extends SQLIntegTestCase { + + @Override + protected void init() throws Exception { + loadIndex(Index.ACCOUNT); + } @Test - public void includeTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { + public void includeTest() throws IOException { SearchHits response = query(String.format("SELECT include('*name','*ge'),include('b*'),include('*ddre*'),include('gender') FROM %s/account LIMIT 1000", TEST_INDEX_ACCOUNT)); for (SearchHit hit : response.getHits()) { Set keySet = hit.getSourceAsMap().keySet(); @@ -45,7 +52,7 @@ public void includeTest() throws IOException, SqlParseException, SQLFeatureNotSu } @Test - public void excludeTest() throws SqlParseException, SQLFeatureNotSupportedException { + public void excludeTest() throws IOException { SearchHits response = query(String.format("SELECT exclude('*name','*ge'),exclude('b*'),exclude('*ddre*'),exclude('gender') FROM %s/account LIMIT 1000", TEST_INDEX_ACCOUNT)); @@ -58,7 +65,7 @@ public void excludeTest() throws SqlParseException, SQLFeatureNotSupportedExcept } @Test - public void allTest() throws SqlParseException, SQLFeatureNotSupportedException { + public void allTest() throws IOException { SearchHits response = query(String.format("SELECT exclude('*name','*ge'),include('b*'),exclude('*ddre*'),include('gender') FROM %s/account LIMIT 1000", TEST_INDEX_ACCOUNT)); @@ -71,10 +78,14 @@ public void allTest() throws SqlParseException, SQLFeatureNotSupportedException } } - private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException { - SearchDao searchDao = MainTestSuite.getSearchDao(); - SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query).explain(); - return ((SearchResponse) select.get()).getHits(); + private SearchHits query(String query) throws IOException { + final JSONObject jsonObject = executeQuery(query); + + final XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( + NamedXContentRegistry.EMPTY, + LoggingDeprecationHandler.INSTANCE, + jsonObject.toString()); + return SearchResponse.fromXContent(parser).getHits(); } } diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/MainTestSuite.java b/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/MainTestSuite.java index 720d461632..2c69dbc8ef 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/MainTestSuite.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/sql/intgtest/MainTestSuite.java @@ -70,11 +70,8 @@ HashJoinBasicTest.class, HashJoinMoreTest.class, CSVResultsExtractorTests.class, - SourceFieldTest.class, MultiQueryTests.class, HavingTest.class, - NestedFieldQueryTest.class, - QueryFunctionsTest.class, PrettyFormatResponseTest.class, PreparedStatementTest.class, MetaDataQueriesTest.class diff --git a/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/metrics/RollingCounterTest.java b/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/metrics/RollingCounterTest.java index a89535614b..99ca8a58eb 100644 --- a/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/metrics/RollingCounterTest.java +++ b/src/test/java/com/amazon/opendistroforelasticsearch/sql/unittest/metrics/RollingCounterTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.lessThanOrEqualTo; public class RollingCounterTest { @@ -40,7 +41,7 @@ public void increment() throws InterruptedException { counter.increment(); counter.increment(); TimeUnit.SECONDS.sleep(1L); - assertThat(counter.getValue(), equalTo(2L)); + assertThat(counter.getValue(), lessThanOrEqualTo(3L)); TimeUnit.SECONDS.sleep(1L); assertThat(counter.getValue(), equalTo(0L)); @@ -75,7 +76,7 @@ public void trim() throws InterruptedException { TimeUnit.SECONDS.sleep(1L); } counter.increment(); - assertThat(counter.size(), equalTo(3)); + assertThat(counter.size(), lessThanOrEqualTo(3)); } }