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;
24 import java.util.stream.Collectors;
25 import java.util.stream.IntStream;
26 import org.apache.commons.lang.RandomStringUtils;
27 import org.junit.Test;
29 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 public class SimpleFieldTopAggregationDefinitionTest {
34 private static final Random RANDOM = new Random();
38 public void fieldName_cannot_be_null() {
39 boolean sticky = RANDOM.nextBoolean();
41 assertThatThrownBy(() -> new SimpleFieldTopAggregationDefinition(null, sticky))
42 .isInstanceOf(NullPointerException.class)
43 .hasMessage("fieldName can't be null");
47 public void getters() {
48 String fieldName = RandomStringUtils.randomAlphabetic(12);
49 boolean sticky = new Random().nextBoolean();
50 SimpleFieldTopAggregationDefinition underTest = new SimpleFieldTopAggregationDefinition(fieldName, sticky);
52 assertThat(underTest.getFilterScope().getFieldName()).isEqualTo(fieldName);
53 assertThat(underTest.isSticky()).isEqualTo(sticky);
57 public void getFilterScope_always_returns_the_same_instance() {
58 String fieldName = randomAlphabetic(12);
59 boolean sticky = RANDOM.nextBoolean();
60 SimpleFieldTopAggregationDefinition underTest = new SimpleFieldTopAggregationDefinition(fieldName, sticky);
62 Set<TopAggregationDefinition.FilterScope> filterScopes = IntStream.range(0, 2 + RANDOM.nextInt(200))
63 .mapToObj(i -> underTest.getFilterScope())
64 .collect(Collectors.toSet());
66 assertThat(filterScopes).hasSize(1);