|
| 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.search.fetch.subphase; |
| 10 | + |
| 11 | +import org.opensearch.index.query.QueryShardContext; |
| 12 | +import org.opensearch.search.fetch.FetchContext; |
| 13 | +import org.opensearch.search.internal.SearchContext; |
| 14 | +import org.opensearch.search.lookup.SearchLookup; |
| 15 | +import org.opensearch.test.OpenSearchTestCase; |
| 16 | + |
| 17 | +import static org.mockito.Mockito.mock; |
| 18 | +import static org.mockito.Mockito.when; |
| 19 | + |
| 20 | +public class ScriptFieldsPhaseTests extends OpenSearchTestCase { |
| 21 | + |
| 22 | + /* |
| 23 | + Returns mock search context reused across test methods |
| 24 | + */ |
| 25 | + private SearchContext getMockSearchContext(final boolean hasScriptFields) { |
| 26 | + final QueryShardContext queryShardContext = mock(QueryShardContext.class); |
| 27 | + when(queryShardContext.newFetchLookup()).thenReturn(mock(SearchLookup.class)); |
| 28 | + |
| 29 | + final SearchContext searchContext = mock(SearchContext.class); |
| 30 | + when(searchContext.hasScriptFields()).thenReturn(hasScriptFields); |
| 31 | + when(searchContext.getQueryShardContext()).thenReturn(queryShardContext); |
| 32 | + |
| 33 | + return searchContext; |
| 34 | + } |
| 35 | + |
| 36 | + /* |
| 37 | + Validates that ScriptFieldsPhase processor is not initialized when no script fields |
| 38 | + */ |
| 39 | + public void testScriptFieldsNull() { |
| 40 | + assertNull(new ScriptFieldsPhase().getProcessor(new FetchContext(getMockSearchContext(false)))); |
| 41 | + } |
| 42 | + |
| 43 | + /* |
| 44 | + Validates that ScriptFieldsPhase processor is initialized when script fields are present |
| 45 | + */ |
| 46 | + public void testScriptFieldsNonNull() { |
| 47 | + final SearchContext searchContext = getMockSearchContext(true); |
| 48 | + when(searchContext.scriptFields()).thenReturn(new ScriptFieldsContext()); |
| 49 | + |
| 50 | + assertNotNull(new ScriptFieldsPhase().getProcessor(new FetchContext(searchContext))); |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments