|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.query; |
| 10 | + |
| 11 | +import org.opensearch.action.IndicesRequest; |
| 12 | +import org.opensearch.action.search.SearchRequest; |
| 13 | +import org.opensearch.cluster.metadata.IndexNameExpressionResolver; |
| 14 | +import org.opensearch.cluster.service.ClusterService; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | +import org.opensearch.common.util.concurrent.OpenSearchExecutors; |
| 17 | +import org.opensearch.common.util.concurrent.ThreadContext; |
| 18 | +import org.opensearch.plugins.SearchPipelinePlugin; |
| 19 | +import org.opensearch.search.pipeline.PipelinedRequest; |
| 20 | +import org.opensearch.search.pipeline.Processor; |
| 21 | +import org.opensearch.search.pipeline.SearchPhaseResultsProcessor; |
| 22 | +import org.opensearch.search.pipeline.SearchPipelineService; |
| 23 | +import org.opensearch.search.pipeline.SearchRequestProcessor; |
| 24 | +import org.opensearch.search.pipeline.SearchResponseProcessor; |
| 25 | +import org.opensearch.test.OpenSearchTestCase; |
| 26 | +import org.opensearch.threadpool.ThreadPool; |
| 27 | +import org.opensearch.transport.client.Client; |
| 28 | +import org.junit.Before; |
| 29 | + |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.ExecutorService; |
| 33 | + |
| 34 | +import static org.mockito.ArgumentMatchers.anyString; |
| 35 | +import static org.mockito.Mockito.mock; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | + |
| 38 | +public class QueryCoordinatorContextTests extends OpenSearchTestCase { |
| 39 | + |
| 40 | + private IndexNameExpressionResolver indexNameExpressionResolver; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setup() { |
| 44 | + indexNameExpressionResolver = new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)); |
| 45 | + } |
| 46 | + |
| 47 | + public void testGetContextVariables_whenPipelinedSearchRequest_thenReturnVariables() { |
| 48 | + final PipelinedRequest searchRequest = createDummyPipelinedRequest(); |
| 49 | + searchRequest.getPipelineProcessingContext().setAttribute("key", "value"); |
| 50 | + |
| 51 | + final QueryCoordinatorContext queryCoordinatorContext = new QueryCoordinatorContext(mock(QueryRewriteContext.class), searchRequest); |
| 52 | + |
| 53 | + assertEquals(Map.of("key", "value"), queryCoordinatorContext.getContextVariables()); |
| 54 | + } |
| 55 | + |
| 56 | + private PipelinedRequest createDummyPipelinedRequest() { |
| 57 | + final Client client = mock(Client.class); |
| 58 | + final ThreadPool threadPool = mock(ThreadPool.class); |
| 59 | + final ExecutorService executorService = OpenSearchExecutors.newDirectExecutorService(); |
| 60 | + when(threadPool.generic()).thenReturn(executorService); |
| 61 | + when(threadPool.executor(anyString())).thenReturn(executorService); |
| 62 | + final SearchPipelineService searchPipelineService = new SearchPipelineService( |
| 63 | + mock(ClusterService.class), |
| 64 | + threadPool, |
| 65 | + null, |
| 66 | + null, |
| 67 | + null, |
| 68 | + null, |
| 69 | + this.writableRegistry(), |
| 70 | + Collections.singletonList(new SearchPipelinePlugin() { |
| 71 | + @Override |
| 72 | + public Map<String, Processor.Factory<SearchRequestProcessor>> getRequestProcessors(Parameters parameters) { |
| 73 | + return Collections.emptyMap(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Map<String, Processor.Factory<SearchResponseProcessor>> getResponseProcessors(Parameters parameters) { |
| 78 | + return Collections.emptyMap(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public Map<String, Processor.Factory<SearchPhaseResultsProcessor>> getSearchPhaseResultsProcessors(Parameters parameters) { |
| 83 | + return Collections.emptyMap(); |
| 84 | + } |
| 85 | + |
| 86 | + }), |
| 87 | + client |
| 88 | + ); |
| 89 | + final SearchRequest searchRequest = new SearchRequest(); |
| 90 | + return searchPipelineService.resolvePipeline(searchRequest, indexNameExpressionResolver); |
| 91 | + } |
| 92 | + |
| 93 | + public void testGetContextVariables_whenNotPipelinedSearchRequest_thenReturnEmpty() { |
| 94 | + final IndicesRequest searchRequest = mock(IndicesRequest.class); |
| 95 | + |
| 96 | + final QueryCoordinatorContext queryCoordinatorContext = new QueryCoordinatorContext(mock(QueryRewriteContext.class), searchRequest); |
| 97 | + |
| 98 | + assertTrue(queryCoordinatorContext.getContextVariables().isEmpty()); |
| 99 | + } |
| 100 | + |
| 101 | + public void testGetSearchRequest() { |
| 102 | + final IndicesRequest searchRequest = mock(IndicesRequest.class); |
| 103 | + |
| 104 | + final QueryCoordinatorContext queryCoordinatorContext = new QueryCoordinatorContext(mock(QueryRewriteContext.class), searchRequest); |
| 105 | + |
| 106 | + assertEquals(searchRequest, queryCoordinatorContext.getSearchRequest()); |
| 107 | + } |
| 108 | +} |
0 commit comments