]> source.dussan.org Git - sonarqube.git/blob
159097cf72f0bcc8733cf31516d9bbb717a8d1a6
[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 org.junit.Test;
23 import org.sonar.server.es.searchrequest.TopAggregationDefinition.NestedFieldFilterScope;
24 import org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope;
25
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;
29
30 public class SimpleFieldFilterScopeTest {
31   @Test
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");
36   }
37
38   @Test
39   public void getFieldName() {
40     String fieldName = randomAlphabetic(12);
41     SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName);
42
43     assertThat(underTest.getFieldName()).isEqualTo(fieldName);
44   }
45
46   @Test
47   public void verify_equals() {
48     String fieldName1 = randomAlphabetic(11);
49     String fieldName2 = randomAlphabetic(12);
50     SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName1);
51
52     assertThat(underTest)
53       .isEqualTo(underTest)
54       .isEqualTo(new SimpleFieldFilterScope(fieldName1))
55       .isNotNull()
56       .isNotEqualTo(new Object())
57       .isNotEqualTo(new SimpleFieldFilterScope(fieldName2))
58       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName1, "foo", "bar"))
59       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, "foo", "bar"));
60   }
61
62   @Test
63   public void verify_hashcode() {
64     String fieldName1 = randomAlphabetic(11);
65     String fieldName2 = randomAlphabetic(12);
66     SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName1);
67
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());
76   }
77
78   @Test
79   public void verify_intersect() {
80     String fieldName1 = randomAlphabetic(11);
81     String fieldName2 = randomAlphabetic(12);
82     SimpleFieldFilterScope underTest = new SimpleFieldFilterScope(fieldName1);
83
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();
89   }
90 }