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 org.junit.Test;
23 import org.sonar.server.es.searchrequest.TopAggregationDefinition.NestedFieldFilterScope;
24 import org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope;
26 import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.assertj.core.api.Assertions.assertThatThrownBy;
30 public class SimpleFieldFilterScopeTest {
32 public void constructor_fails_with_NPE_if_fieldName_is_null() {
33 assertThatThrownBy(() -> new SimpleFieldFilterScope(null))
34 .isInstanceOf(NullPointerException.class)
35 .hasMessage("fieldName can't be null");
39 public void getFieldName() {
40 String fieldName = randomAlphabetic(12);
41 SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName);
43 assertThat(underTest.getFieldName()).isEqualTo(fieldName);
47 public void verify_equals() {
48 String fieldName1 = randomAlphabetic(11);
49 String fieldName2 = randomAlphabetic(12);
50 SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName1);
54 .isEqualTo(new SimpleFieldFilterScope(fieldName1))
56 .isNotEqualTo(new Object())
57 .isNotEqualTo(new SimpleFieldFilterScope(fieldName2))
58 .isNotEqualTo(new NestedFieldFilterScope<>(fieldName1, "foo", "bar"))
59 .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, "foo", "bar"));
63 public void verify_hashcode() {
64 String fieldName1 = randomAlphabetic(11);
65 String fieldName2 = randomAlphabetic(12);
66 SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName1);
68 assertThat(underTest.hashCode())
69 .isEqualTo(underTest.hashCode())
70 .isEqualTo(new SimpleFieldFilterScope(fieldName1).hashCode());
71 assertThat(underTest.hashCode())
72 .isNotEqualTo(new Object().hashCode())
73 .isNotEqualTo(new SimpleFieldFilterScope(fieldName2).hashCode())
74 .isNotEqualTo(new NestedFieldFilterScope<>(fieldName1, "foo", "bar").hashCode())
75 .isNotEqualTo(new NestedFieldFilterScope<>(fieldName1, "foo", "bar").hashCode());
79 public void verify_intersect() {
80 String fieldName1 = randomAlphabetic(11);
81 String fieldName2 = randomAlphabetic(12);
82 SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName1);
84 assertThat(underTest.intersect(underTest)).isTrue();
85 assertThat(underTest.intersect(new SimpleFieldFilterScope(fieldName1))).isTrue();
86 assertThat(underTest.intersect(new SimpleFieldFilterScope(fieldName2))).isFalse();
87 assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName1, "foo", "bar"))).isTrue();
88 assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName2, "foo", "bar"))).isFalse();