Commit d5d56df8 authored by neelesh thakur's avatar neelesh thakur
Browse files

comments

parent 94d3e161
Pipeline #45744 failed with stages
in 38 minutes and 57 seconds
......@@ -14,6 +14,7 @@
package org.opengroup.osdu.search.provider.azure.provider.impl;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
......@@ -28,6 +29,7 @@ import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
......@@ -45,6 +47,8 @@ import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.search.Point;
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.search.SortOrder;
import org.opengroup.osdu.core.common.model.search.SortQuery;
import org.opengroup.osdu.core.common.model.search.SpatialFilter;
import org.opengroup.osdu.search.config.SearchConfigurationProperties;
import org.opengroup.osdu.search.logging.AuditLogger;
......@@ -64,12 +68,13 @@ import org.opengroup.osdu.search.util.SortParserUtil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class QueryServiceImplTest {
......@@ -380,6 +385,30 @@ public class QueryServiceImplTest {
}
}
@Test(expected = AppException.class)
public void testQueryBase_whenUnsupportedSortRequested_statusBadRequest_throwsException() throws IOException {
String dummySortError = "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead";
ElasticsearchStatusException exception = new ElasticsearchStatusException("blah", RestStatus.BAD_REQUEST, new ElasticsearchException(dummySortError));
doThrow(exception).when(client).search(any(), any(RequestOptions.class));
doReturn(new HashSet<>()).when(fieldMappingTypeService).getFieldTypes(eq(client), eq(fieldName), eq(indexName));
SortQuery sortQuery = new SortQuery();
sortQuery.setField(Collections.singletonList("data.name"));
sortQuery.setOrder(Collections.singletonList(SortOrder.DESC));
when(searchRequest.getSort()).thenReturn(sortQuery);
doReturn(Collections.singletonList(new FieldSortBuilder("data.name").order(org.elasticsearch.search.sort.SortOrder.DESC)))
.when(sortParserUtil).getSortQuery(eq(client), eq(sortQuery), eq(indexName));
try {
sut.queryIndex(searchRequest);
} catch (AppException e) {
int errorCode = 400;
String errorMessage = "Sort is not supported for one or more of the requested fields";
validateAppException(e, errorCode, errorMessage);
throw(e);
}
}
@Test(expected = AppException.class)
public void testQueryBase_whenClientSearchResultsInElasticsearchStatusException_statusServiceUnavailable_throwsException() throws IOException {
ElasticsearchStatusException exception = mock(ElasticsearchStatusException.class);
......
......@@ -20,8 +20,10 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
......@@ -30,12 +32,15 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.TotalHits.Relation;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
......@@ -56,6 +61,7 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -65,10 +71,7 @@ import org.mockito.Spy;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.search.Point;
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.search.SpatialFilter;
import org.opengroup.osdu.core.common.model.search.*;
import org.opengroup.osdu.core.common.model.search.SpatialFilter.ByBoundingBox;
import org.opengroup.osdu.core.common.model.search.SpatialFilter.ByDistance;
import org.opengroup.osdu.core.common.model.search.SpatialFilter.ByGeoPolygon;
......@@ -365,6 +368,34 @@ public class QueryServiceTest {
verifyAcls(topLevelMustClause.get(0), false);
}
@Test(expected = AppException.class)
public void testQueryBase_whenUnsupportedSortRequested_statusBadRequest_throwsException() throws IOException {
String fieldName = "field";
String indexName = "index";
String dummySortError = "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead";
ElasticsearchStatusException exception = new ElasticsearchStatusException("blah", RestStatus.BAD_REQUEST, new ElasticsearchException(dummySortError));
doThrow(exception).when(restHighLevelClient).search(any(), any(RequestOptions.class));
doReturn(new HashSet<>()).when(fieldMappingTypeService).getFieldTypes(eq(restHighLevelClient), eq(fieldName), eq(indexName));
SortQuery sortQuery = new SortQuery();
sortQuery.setField(Collections.singletonList("name"));
sortQuery.setOrder(Collections.singletonList(SortOrder.DESC));
when(searchRequest.getSort()).thenReturn(sortQuery);
when(sortParserUtil.getSortQuery(restHighLevelClient, sortQuery, indexName))
.thenReturn(Collections.singletonList(new FieldSortBuilder("name").order(org.elasticsearch.search.sort.SortOrder.DESC)));
try {
this.sut.makeSearchRequest(searchRequest, restHighLevelClient);
} catch (AppException e) {
int errorCode = 400;
String errorMessage = "Sort is not supported for one or more of the requested fields";
assertEquals(e.getError().getCode(), errorCode);
assertEquals(e.getError().getMessage(), errorMessage);
throw(e);
}
}
@Test
public void should_return_boundingBoxQuery_given_spatialCriteria() throws IOException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment