1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.issue;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.web.UserRole;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.Fail.fail;
public class IssueQueryTest {
@Test
public void build_query() throws Exception {
IssueQuery query = IssueQuery.builder()
.issueKeys(newArrayList("ABCDE"))
.severities(newArrayList(Severity.BLOCKER))
.statuses(newArrayList(Issue.STATUS_RESOLVED))
.resolutions(newArrayList(Issue.RESOLUTION_FALSE_POSITIVE))
.components(newArrayList("org/struts/Action.java"))
.componentRoots(newArrayList("org.struts:core"))
.rules(newArrayList(RuleKey.of("squid", "AvoidCycle")))
.actionPlans(newArrayList("AP1", "AP2"))
.reporters(newArrayList("crunky"))
.assignees(newArrayList("gargantua"))
.languages(newArrayList("xoo"))
.assigned(true)
.hideRules(true)
.createdAfter(new Date())
.createdBefore(new Date())
.createdAt(new Date())
.planned(true)
.resolved(true)
.sort(IssueQuery.SORT_BY_ASSIGNEE)
.asc(true)
.pageSize(10)
.pageIndex(2)
.requiredRole(UserRole.USER)
.build();
assertThat(query.issueKeys()).containsOnly("ABCDE");
assertThat(query.severities()).containsOnly(Severity.BLOCKER);
assertThat(query.statuses()).containsOnly(Issue.STATUS_RESOLVED);
assertThat(query.resolutions()).containsOnly(Issue.RESOLUTION_FALSE_POSITIVE);
assertThat(query.components()).containsOnly("org/struts/Action.java");
assertThat(query.componentRoots()).containsOnly("org.struts:core");
assertThat(query.reporters()).containsOnly("crunky");
assertThat(query.assignees()).containsOnly("gargantua");
assertThat(query.languages()).containsOnly("xoo");
assertThat(query.assigned()).isTrue();
assertThat(query.hideRules()).isTrue();
assertThat(query.rules()).containsOnly(RuleKey.of("squid", "AvoidCycle"));
assertThat(query.actionPlans()).containsOnly("AP1", "AP2");
assertThat(query.createdAfter()).isNotNull();
assertThat(query.createdBefore()).isNotNull();
assertThat(query.createdAt()).isNotNull();
assertThat(query.planned()).isTrue();
assertThat(query.resolved()).isTrue();
assertThat(query.sort()).isEqualTo(IssueQuery.SORT_BY_ASSIGNEE);
assertThat(query.asc()).isTrue();
assertThat(query.pageSize()).isEqualTo(10);
assertThat(query.pageIndex()).isEqualTo(2);
assertThat(query.requiredRole()).isEqualTo(UserRole.USER);
}
@Test
public void build_query_without_dates() throws Exception {
IssueQuery query = IssueQuery.builder()
.issueKeys(newArrayList("ABCDE"))
.createdAfter(null)
.createdBefore(null)
.createdAt(null)
.build();
assertThat(query.issueKeys()).containsOnly("ABCDE");
assertThat(query.createdAfter()).isNull();
assertThat(query.createdBefore()).isNull();
assertThat(query.createdAt()).isNull();
}
@Test
public void throw_exception_if_sort_is_not_valid() throws Exception {
try {
IssueQuery.builder()
.sort("UNKNOWN")
.build();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("Bad sort field: UNKNOWN");
}
}
@Test
public void collection_params_should_not_be_null_but_empty() throws Exception {
IssueQuery query = IssueQuery.builder()
.issueKeys(null)
.components(null)
.componentRoots(null)
.statuses(null)
.actionPlans(null)
.assignees(null)
.reporters(null)
.resolutions(null)
.rules(null)
.severities(null)
.languages(null)
.build();
assertThat(query.issueKeys()).isEmpty();
assertThat(query.components()).isEmpty();
assertThat(query.componentRoots()).isEmpty();
assertThat(query.statuses()).isEmpty();
assertThat(query.actionPlans()).isEmpty();
assertThat(query.assignees()).isEmpty();
assertThat(query.reporters()).isEmpty();
assertThat(query.resolutions()).isEmpty();
assertThat(query.rules()).isEmpty();
assertThat(query.severities()).isEmpty();
assertThat(query.languages()).isEmpty();
}
@Test
public void test_default_query() throws Exception {
IssueQuery query = IssueQuery.builder().build();
assertThat(query.issueKeys()).isEmpty();
assertThat(query.components()).isEmpty();
assertThat(query.componentRoots()).isEmpty();
assertThat(query.statuses()).isEmpty();
assertThat(query.actionPlans()).isEmpty();
assertThat(query.assignees()).isEmpty();
assertThat(query.reporters()).isEmpty();
assertThat(query.resolutions()).isEmpty();
assertThat(query.rules()).isEmpty();
assertThat(query.severities()).isEmpty();
assertThat(query.languages()).isEmpty();
assertThat(query.assigned()).isNull();
assertThat(query.createdAfter()).isNull();
assertThat(query.createdBefore()).isNull();
assertThat(query.planned()).isNull();
assertThat(query.resolved()).isNull();
assertThat(query.sort()).isNull();
assertThat(query.pageSize()).isEqualTo(100);
assertThat(query.pageIndex()).isEqualTo(1);
assertThat(query.requiredRole()).isEqualTo(UserRole.USER);
assertThat(query.maxResults()).isEqualTo(IssueQuery.MAX_RESULTS);
}
@Test
public void use_max_page_size_if_negative() throws Exception {
IssueQuery query = IssueQuery.builder().pageSize(0).build();
assertThat(query.pageSize()).isEqualTo(IssueQuery.MAX_PAGE_SIZE);
query = IssueQuery.builder().pageSize(-1).build();
assertThat(query.pageSize()).isEqualTo(IssueQuery.MAX_PAGE_SIZE);
}
@Test
public void test_default_page_index_and_size() throws Exception {
IssueQuery query = IssueQuery.builder().build();
assertThat(query.pageSize()).isEqualTo(IssueQuery.DEFAULT_PAGE_SIZE);
assertThat(query.pageIndex()).isEqualTo(IssueQuery.DEFAULT_PAGE_INDEX);
}
@Test
public void reset_to_max_page_size() throws Exception {
IssueQuery query = IssueQuery.builder()
.pageSize(IssueQuery.MAX_PAGE_SIZE + 100)
.build();
assertThat(query.pageSize()).isEqualTo(IssueQuery.MAX_PAGE_SIZE);
}
@Test
public void could_disable_paging_on_single_component() throws Exception {
IssueQuery query = IssueQuery.builder().components(Arrays.asList("Action.java")).build();
assertThat(query.pageSize()).isGreaterThan(IssueQuery.MAX_PAGE_SIZE);
}
@Test
public void page_index_should_be_positive() throws Exception {
try {
IssueQuery.builder()
.pageIndex(0)
.build();
fail();
} catch (Exception e) {
assertThat(e).hasMessage("Page index must be greater than 0 (got 0)").isInstanceOf(IllegalArgumentException.class);
}
}
@Test
public void number_of_issue_keys_should_be_limited() throws Exception {
List<String> issueKeys = newArrayList();
for (int i=0; i<IssueQuery.MAX_PAGE_SIZE+1; i++) {
issueKeys.add("issue-key-"+ i);
}
try {
IssueQuery.builder()
.issueKeys(issueKeys)
.build();
fail();
} catch (Exception e) {
assertThat(e).hasMessage("Number of issue keys must be less than 500 (got 501)").isInstanceOf(IllegalArgumentException.class);
}
}
@Test
public void should_accept_null_sort() throws Exception {
IssueQuery query = IssueQuery.builder().sort(null).build();
assertThat(query.sort()).isNull();
}
}
|