|
| 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 | + |
| 20 | +package org.elasticsearch.search.aggregations.metrics.weighted_avg; |
| 21 | + |
| 22 | +import org.elasticsearch.common.io.stream.Writeable; |
| 23 | +import org.elasticsearch.common.settings.Settings; |
| 24 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 25 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.elasticsearch.search.SearchModule; |
| 27 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 28 | +import org.elasticsearch.search.aggregations.metrics.WeightedAvgAggregationBuilder; |
| 29 | +import org.elasticsearch.search.aggregations.support.MultiValuesSourceFieldConfig; |
| 30 | +import org.elasticsearch.test.AbstractSerializingTestCase; |
| 31 | +import org.junit.Before; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Collections; |
| 35 | + |
| 36 | +import static org.hamcrest.Matchers.hasSize; |
| 37 | + |
| 38 | +public class WeightedAvgAggregationBuilderTests extends AbstractSerializingTestCase<WeightedAvgAggregationBuilder> { |
| 39 | + String aggregationName; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setupName() { |
| 43 | + aggregationName = randomAlphaOfLength(10); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected NamedXContentRegistry xContentRegistry() { |
| 48 | + SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList()); |
| 49 | + return new NamedXContentRegistry(searchModule.getNamedXContents()); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected WeightedAvgAggregationBuilder doParseInstance(XContentParser parser) throws IOException { |
| 54 | + assertSame(XContentParser.Token.START_OBJECT, parser.nextToken()); |
| 55 | + AggregatorFactories.Builder parsed = AggregatorFactories.parseAggregators(parser); |
| 56 | + assertThat(parsed.getAggregatorFactories(), hasSize(1)); |
| 57 | + assertThat(parsed.getPipelineAggregatorFactories(), hasSize(0)); |
| 58 | + WeightedAvgAggregationBuilder agg = (WeightedAvgAggregationBuilder) parsed.getAggregatorFactories().iterator().next(); |
| 59 | + assertNull(parser.nextToken()); |
| 60 | + assertNotNull(agg); |
| 61 | + return agg; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected WeightedAvgAggregationBuilder createTestInstance() { |
| 66 | + MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build(); |
| 67 | + MultiValuesSourceFieldConfig weightConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("weight_field").build(); |
| 68 | + WeightedAvgAggregationBuilder aggregationBuilder = new WeightedAvgAggregationBuilder(aggregationName) |
| 69 | + .value(valueConfig) |
| 70 | + .weight(weightConfig); |
| 71 | + return aggregationBuilder; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected Writeable.Reader<WeightedAvgAggregationBuilder> instanceReader() { |
| 76 | + return WeightedAvgAggregationBuilder::new; |
| 77 | + } |
| 78 | +} |
0 commit comments