Commit 667d5c84 authored by Riabokon Stanislav(EPAM)[GCP]'s avatar Riabokon Stanislav(EPAM)[GCP]
Browse files

Added Elastic 7.

Fixed unit tests.
parent d0299179
......@@ -35,7 +35,7 @@
<skip.integration.tests>true</skip.integration.tests>
<jackson.version>2.11.2</jackson.version>
<resteasy.version>3.12.0.Final</resteasy.version>
<elasticsearch.version>6.8.1</elasticsearch.version>
<elasticsearch.version>7.8.1</elasticsearch.version>
<snakeyaml.version>1.26</snakeyaml.version>
<commons-codec.version>1.14</commons-codec.version>
<hibernate-validator.version>6.1.5.Final</hibernate-validator.version>
......
......@@ -74,7 +74,7 @@ public class QueryServiceAwsImpl extends QueryBase implements IQueryService {
List<AggregationResponse> aggregations = getAggregationFromSearchResponse(searchResponse);
QueryResponse queryResponse = QueryResponse.getEmptyResponse();
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setAggregations(aggregations);
queryResponse.setResults(results);
......@@ -94,7 +94,7 @@ public class QueryServiceAwsImpl extends QueryBase implements IQueryService {
sourceBuilder.from(searchRequest.getFrom());
if (StringUtils.isNotEmpty(searchRequest.getAggregateBy())) {
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME, ValueType.STRING);
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME);
termsAggregationBuilder.field(searchRequest.getAggregateBy());
termsAggregationBuilder.size(configurationProperties.getAggregationSize());
sourceBuilder.aggregation(termsAggregationBuilder);
......
......@@ -84,7 +84,7 @@ public class ScrollQueryServiceAwsImpl extends QueryBase implements IScrollQuery
SearchResponse searchScrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
List<Map<String, Object>> results = getHitsFromSearchResponse(searchScrollResponse);
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setResults(results);
queryResponse.setCursor(this.refreshCursorCache(searchScrollResponse.getScrollId(), dpsHeaders.getUserEmail()));
......@@ -121,7 +121,7 @@ public class ScrollQueryServiceAwsImpl extends QueryBase implements IScrollQuery
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.totalCount(searchResponse.getHits().getTotalHits())
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
return CursorQueryResponse.getEmptyResponse();
......
......@@ -40,7 +40,6 @@ public class ElasticClientHandlerAws extends ElasticClientHandler {
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, protocolScheme));
builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(REST_CLIENT_CONNECT_TIMEOUT)
.setSocketTimeout(REST_CLIENT_SOCKET_TIMEOUT));
builder.setMaxRetryTimeoutMillis(REST_CLIENT_RETRY_TIMEOUT);
if(isLocalHost(host)) {
builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setSSLHostnameVerifier((s, sslSession) -> true));
......
......@@ -44,13 +44,13 @@ public class FieldMappingTypeService {
request.fields(fieldName);
if (!Strings.isNullOrEmpty(indexPattern)) request.indices(indexPattern);
GetFieldMappingsResponse response = restClient.indices().getFieldMapping(request, RequestOptions.DEFAULT);
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mappings = response.mappings();
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>>> mappings = response.mappings();
for (Map.Entry<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> indexMapping : mappings.entrySet()) {
for (Map.Entry<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>>> indexMapping : mappings.entrySet()) {
if (indexMapping.getValue().isEmpty()) continue;
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>> typeMapping = indexMapping.getValue();
Map<String, GetFieldMappingsResponse.FieldMappingMetaData> fieldMapping = typeMapping.values().iterator().next();
GetFieldMappingsResponse.FieldMappingMetaData fieldMappingMetaData = fieldMapping.get(fieldName);
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> typeMapping = indexMapping.getValue();
Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMapping = typeMapping.values().iterator().next();
GetFieldMappingsResponse.FieldMappingMetadata fieldMappingMetaData = fieldMapping.get(fieldName);
if (fieldMappingMetaData == null) continue;
Map<String, Object> mapping = fieldMappingMetaData.sourceAsMap();
LinkedHashMap<String, Object> typeMap = (LinkedHashMap<String, Object>) mapping.get(fieldLeafNodeLabel);
......
......@@ -68,7 +68,7 @@ public class QueryServiceImpl extends QueryBase implements IQueryService {
List<AggregationResponse> aggregations = getAggregationFromSearchResponse(searchResponse);
QueryResponse queryResponse = QueryResponse.getEmptyResponse();
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setAggregations(aggregations);
queryResponse.setResults(results);
......@@ -89,7 +89,7 @@ public class QueryServiceImpl extends QueryBase implements IQueryService {
// aggregation
if (!Strings.isNullOrEmpty(searchRequest.getAggregateBy())) {
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME, ValueType.STRING);
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME);
termsAggregationBuilder.field(searchRequest.getAggregateBy());
termsAggregationBuilder.size(configurationProperties.getAggregationSize());
sourceBuilder.aggregation(termsAggregationBuilder);
......
......@@ -85,7 +85,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
SearchResponse searchScrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
List<Map<String, Object>> results = getHitsFromSearchResponse(searchScrollResponse);
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setResults(results);
queryResponse.setCursor(this.refreshCursorCache(searchScrollResponse.getScrollId(), dpsHeaders.getUserEmail()));
......@@ -118,7 +118,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.totalCount(searchResponse.getHits().getTotalHits())
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
return CursorQueryResponse.getEmptyResponse();
......
......@@ -44,13 +44,13 @@ public class FieldMappingTypeService {
request.fields(fieldName);
if (!Strings.isNullOrEmpty(indexPattern)) request.indices(indexPattern);
GetFieldMappingsResponse response = restClient.indices().getFieldMapping(request, RequestOptions.DEFAULT);
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mappings = response.mappings();
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>>> mappings = response.mappings();
for (Map.Entry<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> indexMapping : mappings.entrySet()) {
for (Map.Entry<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>>> indexMapping : mappings.entrySet()) {
if (indexMapping.getValue().isEmpty()) continue;
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>> typeMapping = indexMapping.getValue();
Map<String, GetFieldMappingsResponse.FieldMappingMetaData> fieldMapping = typeMapping.values().iterator().next();
GetFieldMappingsResponse.FieldMappingMetaData fieldMappingMetaData = fieldMapping.get(fieldName);
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> typeMapping = indexMapping.getValue();
Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMapping = typeMapping.values().iterator().next();
GetFieldMappingsResponse.FieldMappingMetadata fieldMappingMetaData = fieldMapping.get(fieldName);
if (fieldMappingMetaData == null) continue;
Map<String, Object> mapping = fieldMappingMetaData.sourceAsMap();
LinkedHashMap<String, Object> typeMap = (LinkedHashMap<String, Object>) mapping.get(fieldLeafNodeLabel);
......
......@@ -78,7 +78,7 @@ public class QueryServiceImpl extends QueryBase implements IQueryService {
List<AggregationResponse> aggregations = getAggregationFromSearchResponse(searchResponse);
QueryResponse queryResponse = QueryResponse.getEmptyResponse();
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setAggregations(aggregations);
queryResponse.setResults(results);
......@@ -101,7 +101,7 @@ public class QueryServiceImpl extends QueryBase implements IQueryService {
// aggregation: only make it available in pre demo for now
if (configurationProperties.isPreDemo() && !Strings.isNullOrEmpty(searchRequest.getAggregateBy())) {
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME, ValueType.STRING);
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME);
termsAggregationBuilder.field(searchRequest.getAggregateBy());
termsAggregationBuilder.size(configurationProperties.getAggregationSize());
sourceBuilder.aggregation(termsAggregationBuilder);
......
......@@ -90,7 +90,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
SearchResponse searchScrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
List<Map<String, Object>> results = getHitsFromSearchResponse(searchScrollResponse);
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setResults(results);
queryResponse.setCursor(this.refreshCursorCache(searchScrollResponse.getScrollId(), dpsHeaders.getUserEmail()));
......@@ -123,7 +123,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.totalCount(searchResponse.getHits().getTotalHits())
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
return CursorQueryResponse.getEmptyResponse();
......
......@@ -126,17 +126,14 @@
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.6.2</version>
</dependency>
<dependency>
<groupId>org.locationtech.spatial4j</groupId>
......
......@@ -76,7 +76,7 @@ public class QueryServiceImpl extends QueryBase implements IQueryService {
List<AggregationResponse> aggregations = getAggregationFromSearchResponse(searchResponse);
QueryResponse queryResponse = QueryResponse.getEmptyResponse();
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setAggregations(aggregations);
queryResponse.setResults(results);
......@@ -97,7 +97,7 @@ public class QueryServiceImpl extends QueryBase implements IQueryService {
// aggregation: only make it available in pre demo for now
if (isEnvironmentPreDemo() && !Strings.isNullOrEmpty(searchRequest.getAggregateBy())) {
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME, ValueType.STRING);
TermsAggregationBuilder termsAggregationBuilder = new TermsAggregationBuilder(AGGREGATION_NAME);
termsAggregationBuilder.field(searchRequest.getAggregateBy());
termsAggregationBuilder.size(searchConfigurationProperties.getAggregationSize());
sourceBuilder.aggregation(termsAggregationBuilder);
......
......@@ -84,7 +84,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
SearchResponse searchScrollResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
List<Map<String, Object>> results = getHitsFromSearchResponse(searchScrollResponse);
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits());
queryResponse.setTotalCount(searchScrollResponse.getHits().getTotalHits().value);
if (results != null) {
queryResponse.setResults(results);
queryResponse.setCursor(this.refreshCursorCache(searchScrollResponse.getScrollId(), dpsHeaders.getUserEmail()));
......@@ -117,7 +117,7 @@ public class ScrollQueryServiceImpl extends QueryBase implements IScrollQuerySer
return CursorQueryResponse.builder()
.cursor(refreshCursorCache(searchResponse.getScrollId(), dpsHeaders.getUserEmail()))
.results(results)
.totalCount(searchResponse.getHits().getTotalHits())
.totalCount(searchResponse.getHits().getTotalHits().value)
.build();
}
return CursorQueryResponse.getEmptyResponse();
......
......@@ -15,10 +15,12 @@
package org.opengroup.osdu.search.provider.gcp.service;
import com.google.common.base.Strings;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsRequest;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import java.util.Map.Entry;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;
import org.elasticsearch.client.indices.GetFieldMappingsResponse.FieldMappingMetadata;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.search.Preconditions;
import org.opengroup.osdu.search.provider.gcp.cache.FieldTypeMappingCache;
......@@ -57,13 +59,13 @@ public class FieldMappingTypeService {
request.fields(fieldName);
if (!Strings.isNullOrEmpty(indexPattern)) request.indices(indexPattern);
GetFieldMappingsResponse response = restClient.indices().getFieldMapping(request, RequestOptions.DEFAULT);
Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mappings = response.mappings();
Map<String, Map<String, FieldMappingMetadata>> mappings = response.mappings();
for (Map.Entry<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> indexMapping : mappings.entrySet()) {
for (Entry<String, Map<String, FieldMappingMetadata>> indexMapping: mappings.entrySet()) {
if (indexMapping.getValue().isEmpty()) continue;
Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>> typeMapping = indexMapping.getValue();
Map<String, GetFieldMappingsResponse.FieldMappingMetaData> fieldMapping = typeMapping.values().iterator().next();
GetFieldMappingsResponse.FieldMappingMetaData fieldMappingMetaData = fieldMapping.get(fieldName);
Map<String, FieldMappingMetadata> typeMapping = indexMapping.getValue();
FieldMappingMetadata fieldMappingMetaData = typeMapping.values().iterator().next();
if (fieldMappingMetaData == null) continue;
Map<String, Object> mapping = fieldMappingMetaData.sourceAsMap();
LinkedHashMap<String, Object> typeMap = (LinkedHashMap<String, Object>) mapping.get(fieldLeafNodeLabel);
......
// Copyright 2017-2019, Schlumberger
//
// 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.
/*
* Copyright 2020 Google LLC
* Copyright 2020 EPAM Systems, Inc
*
* 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 org.opengroup.osdu.search.provider.gcp.provider.impl;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.search.ClusterSettings;
import org.opengroup.osdu.core.common.provider.interfaces.IElasticRepository;
import org.opengroup.osdu.core.common.model.search.CcsQueryRequest;
import org.opengroup.osdu.core.common.model.search.CcsQueryResponse;
import org.opengroup.osdu.core.common.model.search.QueryRequest;
import org.opengroup.osdu.core.common.model.search.QueryResponse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.search.CcsQueryRequest;
import org.opengroup.osdu.core.common.model.search.CcsQueryResponse;
import org.opengroup.osdu.core.common.model.search.ClusterSettings;
import org.opengroup.osdu.core.common.model.search.QueryRequest;
import org.opengroup.osdu.core.common.model.search.QueryResponse;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.core.common.provider.interfaces.IElasticRepository;
import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory;
import org.opengroup.osdu.search.config.SearchConfigurationProperties;
import org.opengroup.osdu.search.provider.interfaces.IQueryService;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({SearchConfigurationProperties.class})
public class CcsQueryServiceTest {
@InjectMocks
private CcsQueryServiceImpl ccsQueryService;
@Mock
private SearchConfigurationProperties searchConfigurationProperties;
@Mock
private IElasticRepository elasticRepository;
@Mock
private DpsHeaders dpsHeaders;
@Mock
private ITenantFactory tenantStorageFactory;
@Mock
private IQueryService queryService;
@Before
public void setup() {
Map<String, String> headersMap = new HashMap<>();
headersMap.put(DpsHeaders.ACCOUNT_ID, "tenant1,common");
headersMap.put(DpsHeaders.AUTHORIZATION, "Bearer smth");
when(dpsHeaders.getHeaders()).thenReturn(headersMap);
when(dpsHeaders.getPartitionIdWithFallbackToAccountId()).thenReturn("tenant1,common");
when(tenantStorageFactory.getTenantInfo(any())).thenReturn(new TenantInfo());
}
@Test
public void should_ReturnResultForTwoTenants_When_DataFromTwoTenantsRequested() throws Exception {
QueryResponse tenantQueryResponse = createTenantQueryResponse();
QueryResponse commonTenantQueryResponse = createCommonTenantQueryResponse();
when(elasticRepository.getElasticClusterSettings(any(TenantInfo.class))).thenReturn(new ClusterSettings("", 42, ""));
when(queryService.queryIndex(any(QueryRequest.class), any(ClusterSettings.class))).thenReturn(createTenantQueryResponse())
.thenReturn(createCommonTenantQueryResponse());
when(searchConfigurationProperties.isSmartSearchCcsDisabled()).thenReturn(false);
CcsQueryResponse ccsQueryResponse = this.ccsQueryService.makeRequest(new CcsQueryRequest());
verify(queryService, times(2)).queryIndex(any(QueryRequest.class), any(ClusterSettings.class));
assertNotNull(ccsQueryResponse);
assertEquals(tenantQueryResponse.getTotalCount() + commonTenantQueryResponse.getTotalCount(), ccsQueryResponse.getTotalCount());
assertEquals(tenantQueryResponse.getResults().size() + commonTenantQueryResponse.getResults().size(), ccsQueryResponse.getResults().size());
assertEquals("tenant1:foobar:baz:0.1", ccsQueryResponse.getResults().get(0).get("kind"));
assertEquals("tenant1:foobar:baz:0.1", ccsQueryResponse.getResults().get(0).get("kind"));
assertEquals("tenant1:foobar:baz", ccsQueryResponse.getResults().get(0).get("id"));
assertEquals("tenant1:foobar:baz:42", ccsQueryResponse.getResults().get(4).get("kind"));
}
private QueryResponse createTenantQueryResponse() {
QueryResponse queryResponse = new QueryResponse();
queryResponse.setTotalCount(42);
List<Map<String, Object>> results = new ArrayList<>();
Map<String, Object> result1 = new HashMap<>();
result1.put("kind", "tenant1:foobar:baz:0.1");
result1.put("id", "tenant1:foobar:baz");
results.add(result1);
Map<String, Object> result2 = new HashMap<>();
result2.put("kind", "tenant1:foobar:baz:0.2");
results.add(result2);
Map<String, Object> result3 = new HashMap<>();
result3.put("kind", "tenant1:foobar:baz:42");
results.add(result3);
queryResponse.setResults(results);
return queryResponse;
}
private QueryResponse createCommonTenantQueryResponse() {
QueryResponse queryResponse = new QueryResponse();
queryResponse.setTotalCount(2);
List<Map<String, Object>> results = new ArrayList<>();
Map<String, Object> result1 = new HashMap<>();
result1.put("kind", "common:foobar:baz:0.1");
results.add(result1);
Map<String, Object> result2 = new HashMap<>();
result2.put("kind", "common:foobar:baz:0.2");
results.add(result2);
queryResponse.setResults(results);
return queryResponse;
}
@InjectMocks
private CcsQueryServiceImpl ccsQueryService;
@Mock
private SearchConfigurationProperties searchConfigurationProperties;
@Mock
private IElasticRepository elasticRepository;
@Mock
private DpsHeaders dpsHeaders;
@Mock
private ITenantFactory tenantStorageFactory;
@Mock
private IQueryService queryService;
@Before
public void setup() {
Map<String, String> headersMap = new HashMap<>();
headersMap.put(DpsHeaders.ACCOUNT_ID, "tenant1,common");
headersMap.put(DpsHeaders.AUTHORIZATION, "Bearer smth");
when(dpsHeaders.getHeaders()).thenReturn(headersMap);
when(dpsHeaders.getPartitionIdWithFallbackToAccountId()).thenReturn("tenant1,common");
when(tenantStorageFactory.getTenantInfo(any())).thenReturn(new TenantInfo());
}
@Test
public void should_ReturnResultForTwoTenants_When_DataFromTwoTenantsRequested() throws Exception {
QueryResponse tenantQueryResponse = createTenantQueryResponse();
QueryResponse commonTenantQueryResponse = createCommonTenantQueryResponse();
when(elasticRepository.getElasticClusterSettings(any(TenantInfo.class)))
.thenReturn(new ClusterSettings("", 42, ""));
when(queryService.queryIndex(any(QueryRequest.class), any(ClusterSettings.class)))
.thenReturn(createTenantQueryResponse())
.thenReturn(createCommonTenantQueryResponse());
when(searchConfigurationProperties.isSmartSearchCcsDisabled()).thenReturn(false);
CcsQueryResponse ccsQueryResponse = this.ccsQueryService.makeRequest(new CcsQueryRequest());
verify(queryService, times(2)).queryIndex(any(QueryRequest.class), any(ClusterSettings.class));
assertNotNull(ccsQueryResponse);
assertEquals(tenantQueryResponse.getTotalCount() + commonTenantQueryResponse.getTotalCount(),
ccsQueryResponse.getTotalCount());
assertEquals(
tenantQueryResponse.getResults().size() + commonTenantQueryResponse.getResults().size(),
ccsQueryResponse.getResults().size());
assertEquals("tenant1:foobar:baz:0.1", ccsQueryResponse.getResults().get(0).get("kind"));
assertEquals("tenant1:foobar:baz:0.1", ccsQueryResponse.getResults().get(0).get("kind"));
assertEquals("tenant1:foobar:baz", ccsQueryResponse.getResults().get(0).get("id"));
assertEquals("tenant1:foobar:baz:42", ccsQueryResponse.getResults().get(4).get("kind"));
}
private QueryResponse createTenantQueryResponse() {
QueryResponse queryResponse = new QueryResponse();
queryResponse.setTotalCount(42);
List<Map<String, Object>> results = new ArrayList<>();
Map<String, Object> result1 = new HashMap<>();
result1.put("kind", "tenant1:foobar:baz:0.1");
result1.put("id", "tenant1:foobar:baz");
results.add(result1);
Map<String, Object> result2 = new HashMap<>();
result2.put("kind", "tenant1:foobar:baz:0.2");
results.add(result2);
Map<String, Object> result3 = new HashMap<>();
result3.put("kind", "tenant1:foobar:baz:42");
results.add(result3);
queryResponse.setResults(results);
return queryResponse;
}
private QueryResponse createCommonTenantQueryResponse() {
QueryResponse queryResponse = new QueryResponse();
queryResponse.setTotalCount(2);
List<Map<String, Object>> results = new ArrayList<>();
Map<String, Object> result1 = new HashMap<>();
result1.put("kind", "common:foobar:baz:0.1");
results.add(result1);
Map<String, Object> result2 = new HashMap<>();
result2.put("kind", "common:foobar:baz:0.2");
results.add(result2);
queryResponse.setResults(results);
return queryResponse;
}
}
......@@ -16,10 +16,12 @@ package org.opengroup.osdu.search.provider.gcp.service;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.elasticsearch.action.admin.indices.mapping.get.GetFieldMappingsResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetFieldMappingsRequest;
import org.elasticsearch.client.indices.GetFieldMappingsResponse;