From e7cffa5e9fd3c2eb177eb57e86a55c13006c47ad Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Wed, 9 Mar 2016 11:37:15 -0500 Subject: [PATCH] simplify mocking field type in SuggestionBuilderTests --- .../AbstractSuggestionBuilderTestCase.java | 35 +++++++++---------- .../CompletionSuggesterBuilderTests.java | 28 ++++++--------- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java index 2c310b04870..bfbac27a8ca 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java @@ -213,20 +213,12 @@ public abstract class AbstractSuggestionBuilderTestCase mockMapperServiceAndSuggestionBuilder( - IndexSettings idxSettings, AnalysisService mockAnalysisService, SB suggestBuilder) { - final MapperService mapperService = new MapperService(idxSettings, mockAnalysisService, null, - new IndicesModule().getMapperRegistry(), null) { - @Override - public MappedFieldType fullName(String fullName) { - StringFieldType type = new StringFieldType(); - if (randomBoolean()) { - type.setSearchAnalyzer(new NamedAnalyzer("foo", new WhitespaceAnalyzer())); - } - return type; - } - }; - return new Tuple<>(mapperService, suggestBuilder); + protected Tuple randomFieldTypeAndSuggestionBuilder() { + StringFieldType type = new StringFieldType(); + if (randomBoolean()) { + type.setSearchAnalyzer(new NamedAnalyzer("foo", new WhitespaceAnalyzer())); + } + return new Tuple<>(type, randomTestBuilder()); } /** @@ -246,12 +238,17 @@ public abstract class AbstractSuggestionBuilderTestCase mapperServiceSBTuple = - mockMapperServiceAndSuggestionBuilder(idxSettings, mockAnalysisService, suggestionBuilder); - suggestionBuilder = mapperServiceSBTuple.v2(); + final Tuple mappedFieldTypeSBTuple = randomFieldTypeAndSuggestionBuilder(); + final MapperService mapperService = new MapperService(idxSettings, mockAnalysisService, null, + new IndicesModule().getMapperRegistry(), null) { + @Override + public MappedFieldType fullName(String fullName) { + return mappedFieldTypeSBTuple.v1(); + } + }; + SB suggestionBuilder = mappedFieldTypeSBTuple.v2(); QueryShardContext mockShardContext = new QueryShardContext(idxSettings, - null, null, mapperServiceSBTuple.v1(), null, scriptService, null) { + null, null, mapperService, null, scriptService, null) { @Override public MappedFieldType fieldMapper(String name) { StringFieldMapper.Builder builder = new StringFieldMapper.Builder(name); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java b/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java index 137030e4c15..1a8efbb748d 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/completion/CompletionSuggesterBuilderTests.java @@ -122,25 +122,17 @@ public class CompletionSuggesterBuilderTests extends AbstractSuggestionBuilderTe } @Override - protected Tuple mockMapperServiceAndSuggestionBuilder( - IndexSettings idxSettings, AnalysisService mockAnalysisService, CompletionSuggestionBuilder suggestBuilder) { + protected Tuple randomFieldTypeAndSuggestionBuilder() { final BuilderAndInfo builderAndInfo = randomSuggestionBuilderWithContextInfo(); - final MapperService mapperService = new MapperService(idxSettings, mockAnalysisService, null, - new IndicesModule().getMapperRegistry(), null) { - @Override - public MappedFieldType fullName(String fullName) { - CompletionFieldMapper.CompletionFieldType type = new CompletionFieldMapper.CompletionFieldType(); - List contextMappings = builderAndInfo.catContexts.stream() - .map(catContext -> new CategoryContextMapping.Builder(catContext).build()) - .collect(Collectors.toList()); - contextMappings.addAll(builderAndInfo.geoContexts.stream() - .map(geoContext -> new GeoContextMapping.Builder(geoContext).build()) - .collect(Collectors.toList())); - type.setContextMappings(new ContextMappings(contextMappings)); - return type; - } - }; - return new Tuple<>(mapperService, builderAndInfo.builder); + CompletionFieldMapper.CompletionFieldType type = new CompletionFieldMapper.CompletionFieldType(); + List contextMappings = builderAndInfo.catContexts.stream() + .map(catContext -> new CategoryContextMapping.Builder(catContext).build()) + .collect(Collectors.toList()); + contextMappings.addAll(builderAndInfo.geoContexts.stream() + .map(geoContext -> new GeoContextMapping.Builder(geoContext).build()) + .collect(Collectors.toList())); + type.setContextMappings(new ContextMappings(contextMappings)); + return new Tuple<>(type, builderAndInfo.builder); } @Override