|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.search.lookup; |
| 20 | + |
| 21 | +import org.elasticsearch.index.fielddata.AtomicFieldData; |
| 22 | +import org.elasticsearch.index.fielddata.IndexFieldData; |
| 23 | +import org.elasticsearch.index.fielddata.ScriptDocValues; |
| 24 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 25 | +import org.elasticsearch.index.mapper.MapperService; |
| 26 | +import org.elasticsearch.test.ESTestCase; |
| 27 | +import org.junit.Before; |
| 28 | + |
| 29 | +import static org.mockito.AdditionalAnswers.returnsFirstArg; |
| 30 | +import static org.mockito.Matchers.anyObject; |
| 31 | +import static org.mockito.Mockito.doReturn; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | +import static org.mockito.Mockito.when; |
| 34 | + |
| 35 | +public class LeafDocLookupTests extends ESTestCase { |
| 36 | + private ScriptDocValues<?> docValues; |
| 37 | + private LeafDocLookup docLookup; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() throws Exception { |
| 41 | + super.setUp(); |
| 42 | + |
| 43 | + MappedFieldType fieldType = mock(MappedFieldType.class); |
| 44 | + when(fieldType.name()).thenReturn("field"); |
| 45 | + when(fieldType.valueForDisplay(anyObject())).then(returnsFirstArg()); |
| 46 | + |
| 47 | + MapperService mapperService = mock(MapperService.class); |
| 48 | + when(mapperService.fullName("field")).thenReturn(fieldType); |
| 49 | + when(mapperService.fullName("alias")).thenReturn(fieldType); |
| 50 | + |
| 51 | + docValues = mock(ScriptDocValues.class); |
| 52 | + |
| 53 | + AtomicFieldData atomicFieldData = mock(AtomicFieldData.class); |
| 54 | + doReturn(docValues).when(atomicFieldData).getScriptValues(); |
| 55 | + |
| 56 | + IndexFieldData<?> fieldData = mock(IndexFieldData.class); |
| 57 | + when(fieldData.getFieldName()).thenReturn("field"); |
| 58 | + doReturn(atomicFieldData).when(fieldData).load(anyObject()); |
| 59 | + |
| 60 | + docLookup = new LeafDocLookup(mapperService, |
| 61 | + ignored -> fieldData, |
| 62 | + new String[] { "type" }, |
| 63 | + null); |
| 64 | + } |
| 65 | + |
| 66 | + public void testBasicLookup() { |
| 67 | + ScriptDocValues<?> fetchedDocValues = docLookup.get("field"); |
| 68 | + assertEquals(docValues, fetchedDocValues); |
| 69 | + } |
| 70 | + |
| 71 | + public void testLookupWithFieldAlias() { |
| 72 | + ScriptDocValues<?> fetchedDocValues = docLookup.get("alias"); |
| 73 | + assertEquals(docValues, fetchedDocValues); |
| 74 | + } |
| 75 | +} |
0 commit comments