]> source.dussan.org Git - sonarqube.git/blob
2bf3faecea05b661355e34d4f3855dc6ed01fb26
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.es.searchrequest;
21
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;
30
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;
34
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);
38
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);
44
45   @Test
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);
49
50     Stream.of(
51       underTest,
52       underTestWithCustomSubAgg)
53       .forEach(t -> {
54         TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, null);
55
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));
61       });
62   }
63
64   @Test
65   public void buildTermsAggregation_adds_custom_order_from_constructor() {
66     String aggName = randomAlphabetic(10);
67     SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
68
69     TermsAggregationBuilder agg = underTestWithCustomsSubAggAndOrder.buildTermsAggregation(aggName, topAggregation, null);
70
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));
74   }
75
76   @Test
77   public void buildTermsAggregation_adds_custom_sub_agg_from_constructor() {
78     String aggName = randomAlphabetic(10);
79     SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
80
81     Stream.of(
82       underTestWithCustomSubAgg,
83       underTestWithCustomsSubAggAndOrder)
84       .forEach(t -> {
85         TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, null);
86
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);
91       });
92   }
93
94   @Test
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);
99
100     Stream.of(
101       underTest,
102       underTestWithCustomSubAgg,
103       underTestWithCustomsSubAggAndOrder)
104       .forEach(t -> {
105         TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, customSize);
106
107         assertThat(agg.getName()).isEqualTo(aggName);
108         assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
109         assertThat(agg.size()).isEqualTo(customSize);
110       });
111   }
112
113   @Test
114   public void buildSelectedItemsAggregation_returns_empty_if_no_selected_item() {
115     String aggName = randomAlphabetic(10);
116     SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
117
118     Stream.of(
119       underTest,
120       underTestWithCustomSubAgg,
121       underTestWithCustomsSubAggAndOrder)
122       .forEach(t -> assertThat(t.buildSelectedItemsAggregation(aggName, topAggregation, new Object[0])).isEmpty());
123   }
124
125   @Test
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();
130
131     TermsAggregationBuilder agg = underTestWithCustomsSubAggAndOrder.buildSelectedItemsAggregation(aggName, topAggregation, selected)
132       .get();
133
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));
137   }
138
139   @Test
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();
144
145     Stream.of(
146       underTestWithCustomSubAgg,
147       underTestWithCustomsSubAggAndOrder)
148       .forEach(t -> {
149         TermsAggregationBuilder agg = t.buildSelectedItemsAggregation(aggName, topAggregation, selected).get();
150
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);
155       });
156   }
157
158   private static String[] randomNonEmptySelected() {
159     return IntStream.range(0, 1 + new Random().nextInt(22))
160       .mapToObj(i -> "selected_" + i)
161       .toArray(String[]::new);
162   }
163
164 }