Skip to content

Commit

Permalink
Add Elastic 7 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eskabetxe authored and martint committed Apr 13, 2020
1 parent 840d589 commit 1c71e2e
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 98 deletions.
Expand Up @@ -13,6 +13,7 @@
*/
package io.prestosql.elasticsearch;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.HostAndPort;
Expand All @@ -23,8 +24,6 @@
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.intellij.lang.annotations.Language;
Expand All @@ -44,17 +43,23 @@
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestElasticsearchIntegrationSmokeTest
public abstract class BaseElasticsearchSmokeTest
extends AbstractTestIntegrationSmokeTest
{
private final String elasticVersion;
private ElasticsearchServer elasticsearch;
private RestHighLevelClient client;

BaseElasticsearchSmokeTest(String elasticVersion)
{
this.elasticVersion = elasticVersion;
}

@Override
protected QueryRunner createQueryRunner()
throws Exception
{
elasticsearch = new ElasticsearchServer();
elasticsearch = new ElasticsearchServer(elasticVersion);

HostAndPort address = elasticsearch.getAddress();
client = new RestHighLevelClient(RestClient.builder(new HttpHost(address.getHost(), address.getPort())));
Expand Down Expand Up @@ -144,10 +149,9 @@ public void testArrayFields()
{
String indexName = "test_arrays";

@Language("JSON")
String mapping = "" +
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"_meta\": {" +
" \"presto\": {" +
" \"a\": {" +
Expand Down Expand Up @@ -222,8 +226,6 @@ public void testArrayFields()
" \"type\": \"long\"" +
" }" +
" }" +
" }" +
" }" +
"}";

createIndex(indexName, mapping);
Expand Down Expand Up @@ -322,28 +324,25 @@ public void testDataTypes()
{
String indexName = "types";

String mapping = "" +
@Language("JSON")
String mappings = "" +
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"properties\": {" +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
" }" +
" \"properties\": { " +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
"}";

createIndex(indexName, mapping);
createIndex(indexName, mappings);

index(indexName, ImmutableMap.<String, Object>builder()
.put("boolean_column", true)
Expand Down Expand Up @@ -388,28 +387,25 @@ public void testFilters()
{
String indexName = "filter_pushdown";

String mapping = "" +
@Language("JSON")
String mappings = "" +
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"properties\": {" +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
" }" +
" \"properties\": { " +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
"}";

createIndex(indexName, mapping);
createIndex(indexName, mappings);

index(indexName, ImmutableMap.<String, Object>builder()
.put("boolean_column", true)
Expand Down Expand Up @@ -525,32 +521,29 @@ public void testDataTypesNested()
{
String indexName = "types_nested";

String mapping = "" +
@Language("JSON")
String properties = "" +
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"properties\":{" +
" \"field\": {" +
" \"properties\": {" +
" \"field\": {" +
" \"properties\": {" +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
" }" +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
" }" +
" }" +
"}";

createIndex(indexName, mapping);
createIndex(indexName, properties);

index(indexName, ImmutableMap.of(
"field",
Expand Down Expand Up @@ -597,33 +590,30 @@ public void testNestedTypeDataTypesNested()
{
String indexName = "nested_type_nested";

String mapping = "" +
@Language("JSON")
String mappings = "" +
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"properties\":{" +
" \"nested_field\": {" +
" \"type\":\"nested\"," +
" \"properties\": {" +
" \"nested_field\": {" +
" \"type\":\"nested\"," +
" \"properties\": {" +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
" }" +
" \"boolean_column\": { \"type\": \"boolean\" }," +
" \"float_column\": { \"type\": \"float\" }," +
" \"double_column\": { \"type\": \"double\" }," +
" \"integer_column\": { \"type\": \"integer\" }," +
" \"long_column\": { \"type\": \"long\" }," +
" \"keyword_column\": { \"type\": \"keyword\" }," +
" \"text_column\": { \"type\": \"text\" }," +
" \"binary_column\": { \"type\": \"binary\" }," +
" \"timestamp_column\": { \"type\": \"date\" }," +
" \"ipv4_column\": { \"type\": \"ip\" }," +
" \"ipv6_column\": { \"type\": \"ip\" }" +
" }" +
" }" +
" }" +
"}";

createIndex(indexName, mapping);
createIndex(indexName, mappings);

index(indexName, ImmutableMap.of(
"nested_field",
Expand Down Expand Up @@ -700,17 +690,14 @@ public void testNumericKeyword()
throws IOException
{
String indexName = "numeric_keyword";
@Language("JSON") String mapping = "" +
@Language("JSON")
String properties = "" +
"{" +
" \"mappings\": {" +
" \"doc\": {" +
" \"properties\": {" +
" \"numeric_keyword\": { \"type\": \"keyword\" }" +
" }" +
" }" +
" \"properties\":{" +
" \"numeric_keyword\": { \"type\": \"keyword\" }" +
" }" +
"}";
createIndex(indexName, mapping);
createIndex(indexName, properties);
index(indexName, ImmutableMap.<String, Object>builder()
.put("numeric_keyword", 20)
.build());
Expand Down Expand Up @@ -752,12 +739,15 @@ public void testMultiIndexAlias()
"SELECT (SELECT count(*) FROM region) + (SELECT count(*) FROM nation)");
}

protected abstract String indexEndpoint(String index, String docId);

private void index(String index, Map<String, Object> document)
throws IOException
{
client.index(new IndexRequest(index, "doc")
.source(document)
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE));
String json = new ObjectMapper().writeValueAsString(document);
String endpoint = format("%s?refresh", indexEndpoint(index, String.valueOf(System.nanoTime())));
client.getLowLevelClient()
.performRequest("PUT", endpoint, ImmutableMap.of(), new NStringEntity(json, ContentType.APPLICATION_JSON));
}

private void addAlias(String index, String alias)
Expand All @@ -769,11 +759,14 @@ private void addAlias(String index, String alias)
refreshIndex(alias);
}

private void createIndex(String indexName, @Language("JSON") String mapping)
protected abstract String indexMapping(@Language("JSON") String properties);

private void createIndex(String indexName, @Language("JSON") String properties)
throws IOException
{
String mappings = indexMapping(properties);
client.getLowLevelClient()
.performRequest("PUT", "/" + indexName, ImmutableMap.of(), new NStringEntity(mapping, ContentType.APPLICATION_JSON));
.performRequest("PUT", "/" + indexName, ImmutableMap.of(), new NStringEntity(mappings, ContentType.APPLICATION_JSON));
}

private void refreshIndex(String index)
Expand Down
Expand Up @@ -20,9 +20,9 @@ public class ElasticsearchServer
{
private final ElasticsearchContainer container;

public ElasticsearchServer()
public ElasticsearchServer(String version)
{
container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:6.0.0");
container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + version);
container.start();
}

Expand Down
@@ -0,0 +1,39 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.elasticsearch;

import static java.lang.String.format;

public class TestElasticsearch6IntegrationSmokeTest
extends BaseElasticsearchSmokeTest
{
public TestElasticsearch6IntegrationSmokeTest()
{
super("6.0.0");
}

@Override
protected String indexEndpoint(String index, String docId)
{
return format("/%s/doc/%s", index, docId);
}

@Override
protected String indexMapping(String properties)
{
return "{\"mappings\": " +
" {\"doc\": " + properties + "}" +
"}";
}
}

0 comments on commit 1c71e2e

Please sign in to comment.