]> source.dussan.org Git - sonarqube.git/blob
94e1450b1dfe3d1cd981c58c68e4eaf6d885e82d
[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.lang.RandomStringUtils.randomAlphabetic;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.assertj.core.api.Assertions.assertThatThrownBy;
29
30 public class NestedFieldFilterScopeTest {
31   @Test
32   public void constructor_fails_with_NPE_if_fieldName_is_null() {
33     String nestedFieldName = randomAlphabetic(11);
34     String value = randomAlphabetic(12);
35
36     assertThatThrownBy(() -> new NestedFieldFilterScope<>(null, nestedFieldName, value))
37       .isInstanceOf(NullPointerException.class)
38       .hasMessage("fieldName can't be null");
39   }
40
41   @Test
42   public void constructor_fails_with_NPE_if_nestedFieldName_is_null() {
43     String fieldName = randomAlphabetic(10);
44     String value = randomAlphabetic(12);
45
46     assertThatThrownBy(() -> new NestedFieldFilterScope<>(fieldName, null, value))
47       .isInstanceOf(NullPointerException.class)
48       .hasMessage("nestedFieldName can't be null");
49   }
50
51   @Test
52   public void constructor_fails_with_NPE_if_value_is_null() {
53     String fieldName = randomAlphabetic(10);
54     String nestedFieldName = randomAlphabetic(11);
55
56     assertThatThrownBy(() -> new NestedFieldFilterScope<>(fieldName, nestedFieldName, null))
57       .isInstanceOf(NullPointerException.class)
58       .hasMessage("value can't be null");
59   }
60
61   @Test
62   public void verify_getters() {
63     String fieldName = randomAlphabetic(10);
64     String nestedFieldName = randomAlphabetic(11);
65     Object value = new Object();
66
67     NestedFieldFilterScope<Object> underTest = new NestedFieldFilterScope<>(fieldName, nestedFieldName, value);
68
69     assertThat(underTest.getFieldName()).isEqualTo(fieldName);
70     assertThat(underTest.getNestedFieldName()).isEqualTo(nestedFieldName);
71     assertThat(underTest.getNestedFieldValue()).isSameAs(value);
72   }
73
74   @Test
75   public void verify_equals() {
76     String fieldName = randomAlphabetic(10);
77     String nestedFieldName = randomAlphabetic(11);
78     Object value = new Object();
79     String fieldName2 = randomAlphabetic(12);
80     String nestedFieldName2 = randomAlphabetic(13);
81     Object value2 = new Object();
82     NestedFieldFilterScope<Object> underTest = new NestedFieldFilterScope<>(fieldName, nestedFieldName, value);
83
84     assertThat(underTest)
85       .isEqualTo(underTest)
86       .isEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName, value))
87       .isNotNull()
88       .isNotEqualTo(new Object())
89       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName, value))
90       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName2, value))
91       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName, value2))
92       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName2, value))
93       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName2, value2))
94       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName, value2))
95       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName2, value2))
96       .isNotEqualTo(new SimpleFieldFilterScope(fieldName))
97       .isNotEqualTo(new SimpleFieldFilterScope(fieldName2));
98   }
99
100   @Test
101   public void verify_hashcode() {
102     String fieldName = randomAlphabetic(10);
103     String nestedFieldName = randomAlphabetic(11);
104     Object value = new Object();
105     String fieldName2 = randomAlphabetic(12);
106     String nestedFieldName2 = randomAlphabetic(13);
107     Object value2 = new Object();
108     NestedFieldFilterScope<Object> underTest = new NestedFieldFilterScope<>(fieldName, nestedFieldName, value);
109
110     assertThat(underTest.hashCode())
111       .isEqualTo(underTest.hashCode())
112       .isEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName, value).hashCode());
113     assertThat(underTest.hashCode())
114       .isNotEqualTo(new Object().hashCode())
115       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName, value).hashCode())
116       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName2, value).hashCode())
117       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName, value2).hashCode())
118       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName2, value).hashCode())
119       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName, nestedFieldName2, value2).hashCode())
120       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName, value2).hashCode())
121       .isNotEqualTo(new NestedFieldFilterScope<>(fieldName2, nestedFieldName2, value2).hashCode())
122       .isNotEqualTo(new SimpleFieldFilterScope(fieldName).hashCode())
123       .isNotEqualTo(new SimpleFieldFilterScope(fieldName2)).hashCode();
124   }
125
126   @Test
127   public void verify_intersect() {
128     String fieldName = randomAlphabetic(10);
129     String nestedFieldName = randomAlphabetic(11);
130     Object value = new Object();
131     String fieldName2 = randomAlphabetic(12);
132     String nestedFieldName2 = randomAlphabetic(13);
133     Object value2 = new Object();
134     NestedFieldFilterScope<Object> underTest = new NestedFieldFilterScope<>(fieldName, nestedFieldName, value);
135
136     assertThat(underTest.intersect(underTest)).isTrue();
137     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName, nestedFieldName, value))).isTrue();
138     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName2, nestedFieldName, value))).isFalse();
139     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName, nestedFieldName2, value))).isFalse();
140     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName, nestedFieldName, value2))).isFalse();
141     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName2, nestedFieldName2, value))).isFalse();
142     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName, nestedFieldName2, value2))).isFalse();
143     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName2, nestedFieldName, value2))).isFalse();
144     assertThat(underTest.intersect(new NestedFieldFilterScope<>(fieldName2, nestedFieldName2, value2))).isFalse();
145     assertThat(underTest.intersect(new SimpleFieldFilterScope(fieldName))).isFalse();
146     assertThat(underTest.intersect(new SimpleFieldFilterScope(fieldName2))).isFalse();
147   }
148 }