diff options
Diffstat (limited to 'core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java')
-rw-r--r-- | core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java new file mode 100644 index 0000000000..f78700d4a1 --- /dev/null +++ b/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java @@ -0,0 +1,83 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you 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.elasticsearch.index.query; + +import org.elasticsearch.Version; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.Index; +import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.core.StringFieldMapper; +import org.elasticsearch.test.ESTestCase; + +import java.util.Collections; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.sameInstance; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class QueryShardContextTests extends ESTestCase { + + public void testFailIfFieldMappingNotFound() { + IndexMetaData.Builder indexMetadata = new IndexMetaData.Builder("index"); + indexMetadata.settings(Settings.builder().put("index.version.created", Version.CURRENT) + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 1) + ); + IndexSettings indexSettings = new IndexSettings(indexMetadata.build(), Settings.EMPTY); + MapperService mapperService = mock(MapperService.class); + when(mapperService.getIndexSettings()).thenReturn(indexSettings); + QueryShardContext context = new QueryShardContext( + indexSettings, null, null, null, mapperService, null, null, null + ); + + context.setAllowUnmappedFields(false); + MappedFieldType fieldType = new StringFieldMapper.StringFieldType(); + MappedFieldType result = context.failIfFieldMappingNotFound("name", fieldType); + assertThat(result, sameInstance(fieldType)); + try { + context.failIfFieldMappingNotFound("name", null); + fail("exception expected"); + } catch (QueryShardException e) { + assertThat(e.getMessage(), equalTo("No field mapping can be found for the field with name [name]")); + } + + context.setAllowUnmappedFields(true); + result = context.failIfFieldMappingNotFound("name", fieldType); + assertThat(result, sameInstance(fieldType)); + result = context.failIfFieldMappingNotFound("name", null); + assertThat(result, nullValue()); + + context.setAllowUnmappedFields(false); + context.setMapUnmappedFieldAsString(true); + result = context.failIfFieldMappingNotFound("name", fieldType); + assertThat(result, sameInstance(fieldType)); + result = context.failIfFieldMappingNotFound("name", null); + assertThat(result, notNullValue()); + assertThat(result, instanceOf(StringFieldMapper.StringFieldType.class)); + assertThat(result.name(), equalTo("name")); + } + +} |