3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.es.searchrequest;
22 import java.util.Random;
23 import java.util.stream.IntStream;
24 import java.util.stream.Stream;
25 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
26 import org.elasticsearch.search.aggregations.AggregationBuilders;
27 import org.elasticsearch.search.aggregations.BucketOrder;
28 import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
29 import org.junit.Test;
31 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.sonar.server.es.searchrequest.TopAggregationHelperTest.DEFAULT_BUCKET_SIZE;
35 public class SubAggregationHelperTest {
36 private static final BucketOrder ES_BUILTIN_TIE_BREAKER = BucketOrder.key(true);
37 private static final BucketOrder SQ_DEFAULT_BUCKET_ORDER = BucketOrder.count(false);
39 private AbstractAggregationBuilder<?> customSubAgg = AggregationBuilders.sum("foo");
40 private SubAggregationHelper underTest = new SubAggregationHelper();
41 private BucketOrder customOrder = BucketOrder.count(true);
42 private SubAggregationHelper underTestWithCustomSubAgg = new SubAggregationHelper(customSubAgg);
43 private SubAggregationHelper underTestWithCustomsSubAggAndOrder = new SubAggregationHelper(customSubAgg, customOrder);
46 public void buildTermsAggregation_adds_term_subaggregation_with_minDoc_1_and_default_sort() {
47 String aggName = randomAlphabetic(10);
48 SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
52 underTestWithCustomSubAgg)
54 TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, null);
56 assertThat(agg.getName()).isEqualTo(aggName);
57 assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
58 assertThat(agg.size()).isEqualTo(DEFAULT_BUCKET_SIZE);
59 assertThat(agg.minDocCount()).isOne();
60 assertThat(agg.order()).isEqualTo(BucketOrder.compound(SQ_DEFAULT_BUCKET_ORDER, ES_BUILTIN_TIE_BREAKER));
65 public void buildTermsAggregation_adds_custom_order_from_constructor() {
66 String aggName = randomAlphabetic(10);
67 SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
69 TermsAggregationBuilder agg = underTestWithCustomsSubAggAndOrder.buildTermsAggregation(aggName, topAggregation, null);
71 assertThat(agg.getName()).isEqualTo(aggName);
72 assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
73 assertThat(agg.order()).isEqualTo(BucketOrder.compound(customOrder, ES_BUILTIN_TIE_BREAKER));
77 public void buildTermsAggregation_adds_custom_sub_agg_from_constructor() {
78 String aggName = randomAlphabetic(10);
79 SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
82 underTestWithCustomSubAgg,
83 underTestWithCustomsSubAggAndOrder)
85 TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, null);
87 assertThat(agg.getName()).isEqualTo(aggName);
88 assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
89 assertThat(agg.getSubAggregations()).hasSize(1);
90 assertThat(agg.getSubAggregations().iterator().next()).isSameAs(customSubAgg);
95 public void buildTermsAggregation_adds_custom_size_if_TermTopAggregation_specifies_one() {
96 String aggName = randomAlphabetic(10);
97 int customSize = 1 + new Random().nextInt(400);
98 SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
102 underTestWithCustomSubAgg,
103 underTestWithCustomsSubAggAndOrder)
105 TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, customSize);
107 assertThat(agg.getName()).isEqualTo(aggName);
108 assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
109 assertThat(agg.size()).isEqualTo(customSize);
114 public void buildSelectedItemsAggregation_returns_empty_if_no_selected_item() {
115 String aggName = randomAlphabetic(10);
116 SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
120 underTestWithCustomSubAgg,
121 underTestWithCustomsSubAggAndOrder)
122 .forEach(t -> assertThat(t.buildSelectedItemsAggregation(aggName, topAggregation, new Object[0])).isEmpty());
126 public void buildSelectedItemsAggregation_does_not_add_custom_order_from_constructor() {
127 String aggName = randomAlphabetic(10);
128 SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
129 String[] selected = randomNonEmptySelected();
131 TermsAggregationBuilder agg = underTestWithCustomsSubAggAndOrder.buildSelectedItemsAggregation(aggName, topAggregation, selected)
134 assertThat(agg.getName()).isEqualTo(aggName + "_selected");
135 assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
136 assertThat(agg.order()).isEqualTo(BucketOrder.compound(SQ_DEFAULT_BUCKET_ORDER, ES_BUILTIN_TIE_BREAKER));
140 public void buildSelectedItemsAggregation_adds_custom_sub_agg_from_constructor() {
141 String aggName = randomAlphabetic(10);
142 SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
143 String[] selected = randomNonEmptySelected();
146 underTestWithCustomSubAgg,
147 underTestWithCustomsSubAggAndOrder)
149 TermsAggregationBuilder agg = t.buildSelectedItemsAggregation(aggName, topAggregation, selected).get();
151 assertThat(agg.getName()).isEqualTo(aggName + "_selected");
152 assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
153 assertThat(agg.getSubAggregations()).hasSize(1);
154 assertThat(agg.getSubAggregations().iterator().next()).isSameAs(customSubAgg);
158 private static String[] randomNonEmptySelected() {
159 return IntStream.range(0, 1 + new Random().nextInt(22))
160 .mapToObj(i -> "selected_" + i)
161 .toArray(String[]::new);