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 com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.util.List;
26 import java.util.Random;
28 import java.util.stream.Collectors;
29 import java.util.stream.IntStream;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
33 import static java.util.Collections.emptyList;
34 import static java.util.Collections.singletonList;
35 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
39 @RunWith(DataProviderRunner.class)
40 public class NestedFieldTopAggregationDefinitionTest {
42 public static final Random RANDOM = new Random();
45 @UseDataProvider("notOneLevelDeepPaths")
46 public void constructor_supports_nestedFieldPath_only_one_level_deep(String unsupportedPath) {
47 String value = randomAlphabetic(7);
48 boolean sticky = RANDOM.nextBoolean();
50 assertThatThrownBy(() -> new NestedFieldTopAggregationDefinition<>(unsupportedPath, value, sticky))
51 .isInstanceOf(IllegalArgumentException.class)
52 .hasMessage("Field path should have only one dot: " + unsupportedPath);
56 public static Object[][] notOneLevelDeepPaths() {
57 return new Object[][] {
69 @UseDataProvider("emptyFieldNames")
70 public void constructor_fails_with_IAE_if_empty_field_name(String unsupportedPath, List<String> expectedParsedFieldNames) {
71 String value = randomAlphabetic(7);
73 assertThatThrownBy(() -> new NestedFieldTopAggregationDefinition<>(unsupportedPath, value, true))
74 .isInstanceOf(IllegalArgumentException.class)
75 .hasMessage("field path \"" + unsupportedPath + "\" should have exactly 2 non empty field names, got: " + expectedParsedFieldNames);
79 public static Object[][] emptyFieldNames() {
80 String str1 = randomAlphabetic(6);
81 return new Object[][] {
84 {str1 + ".", singletonList(str1)},
85 {str1 + ". ", singletonList(str1)},
86 {"." + str1, singletonList(str1)},
87 {" . " + str1, singletonList(str1)}
92 public void constructor_parses_nested_field_path() {
93 String fieldName = randomAlphabetic(5);
94 String nestedFieldName = randomAlphabetic(6);
95 String value = randomAlphabetic(7);
96 boolean sticky = RANDOM.nextBoolean();
97 NestedFieldTopAggregationDefinition<String> underTest = new NestedFieldTopAggregationDefinition<>(fieldName + "." + nestedFieldName, value, sticky);
99 assertThat(underTest.getFilterScope().getFieldName()).isEqualTo(fieldName);
100 assertThat(underTest.getFilterScope().getNestedFieldName()).isEqualTo(nestedFieldName);
101 assertThat(underTest.getFilterScope().getNestedFieldValue()).isEqualTo(value);
102 assertThat(underTest.isSticky()).isEqualTo(sticky);
106 public void constructor_fails_with_NPE_if_nestedFieldPath_is_null() {
107 String value = randomAlphabetic(7);
108 boolean sticky = RANDOM.nextBoolean();
110 assertThatThrownBy(() -> new NestedFieldTopAggregationDefinition<>(null, value, sticky))
111 .isInstanceOf(NullPointerException.class)
112 .hasMessage("nestedFieldPath can't be null");
116 public void constructor_fails_with_NPE_if_value_is_null() {
117 String value = randomAlphabetic(7);
118 boolean sticky = RANDOM.nextBoolean();
120 assertThatThrownBy(() -> new NestedFieldTopAggregationDefinition<>(value, null, sticky))
121 .isInstanceOf(NullPointerException.class)
122 .hasMessage("value can't be null");
126 public void getFilterScope_always_returns_the_same_instance() {
127 String fieldName = randomAlphabetic(5);
128 String nestedFieldName = randomAlphabetic(6);
129 String value = randomAlphabetic(7);
130 boolean sticky = RANDOM.nextBoolean();
131 NestedFieldTopAggregationDefinition<String> underTest = new NestedFieldTopAggregationDefinition<>(fieldName + "." + nestedFieldName, value, sticky);
133 Set<TopAggregationDefinition.FilterScope> filterScopes = IntStream.range(0, 2 + RANDOM.nextInt(200))
134 .mapToObj(i -> underTest.getFilterScope())
135 .collect(Collectors.toSet());
137 assertThat(filterScopes).hasSize(1);