/
IssueQueryTest.java
178 lines (168 loc) · 6.76 KB
/
IssueQueryTest.java
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
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.server.issue;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.util.Date;
import org.junit.Test;
import org.sonar.api.issue.Issue;
import org.sonar.api.rule.Severity;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.server.issue.IssueQuery.PeriodStart;
import static com.google.common.collect.Lists.newArrayList;
import static org.apache.commons.lang.math.RandomUtils.nextInt;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
public class IssueQueryTest {
@Test
public void build_query() {
RuleDefinitionDto rule = new RuleDefinitionDto().setId(nextInt(1000));
PeriodStart filterDate = new IssueQuery.PeriodStart(new Date(10_000_000_000L), false);
IssueQuery query = IssueQuery.builder()
.issueKeys(newArrayList("ABCDE"))
.severities(newArrayList(Severity.BLOCKER))
.statuses(Lists.newArrayList(Issue.STATUS_RESOLVED))
.resolutions(newArrayList(Issue.RESOLUTION_FALSE_POSITIVE))
.projectUuids(newArrayList("PROJECT"))
.componentUuids(newArrayList("org/struts/Action.java"))
.moduleUuids(newArrayList("org.struts:core"))
.rules(newArrayList(rule))
.assignees(newArrayList("gargantua"))
.languages(newArrayList("xoo"))
.tags(newArrayList("tag1", "tag2"))
.types(newArrayList("RELIABILITY", "SECURITY"))
.organizationUuid("orga")
.branchUuid("my_branch")
.createdAfterByProjectUuids(ImmutableMap.of("PROJECT", filterDate))
.assigned(true)
.createdAfter(new Date())
.createdBefore(new Date())
.createdAt(new Date())
.resolved(true)
.sort(IssueQuery.SORT_BY_ASSIGNEE)
.asc(true)
.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.projectUuids()).containsOnly("PROJECT");
assertThat(query.componentUuids()).containsOnly("org/struts/Action.java");
assertThat(query.moduleUuids()).containsOnly("org.struts:core");
assertThat(query.assignees()).containsOnly("gargantua");
assertThat(query.languages()).containsOnly("xoo");
assertThat(query.tags()).containsOnly("tag1", "tag2");
assertThat(query.types()).containsOnly("RELIABILITY", "SECURITY");
assertThat(query.organizationUuid()).isEqualTo("orga");
assertThat(query.branchUuid()).isEqualTo("my_branch");
assertThat(query.createdAfterByProjectUuids()).containsOnly(entry("PROJECT", filterDate));
assertThat(query.assigned()).isTrue();
assertThat(query.rules()).containsOnly(rule);
assertThat(query.createdAfter()).isNotNull();
assertThat(query.createdBefore()).isNotNull();
assertThat(query.createdAt()).isNotNull();
assertThat(query.resolved()).isTrue();
assertThat(query.sort()).isEqualTo(IssueQuery.SORT_BY_ASSIGNEE);
assertThat(query.asc()).isTrue();
}
@Test
public void build_query_without_dates() {
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() {
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() {
IssueQuery query = IssueQuery.builder()
.issueKeys(null)
.projectUuids(null)
.componentUuids(null)
.moduleUuids(null)
.statuses(null)
.assignees(null)
.resolutions(null)
.rules(null)
.severities(null)
.languages(null)
.tags(null)
.types(null)
.createdAfterByProjectUuids(null)
.build();
assertThat(query.issueKeys()).isEmpty();
assertThat(query.projectUuids()).isEmpty();
assertThat(query.componentUuids()).isEmpty();
assertThat(query.moduleUuids()).isEmpty();
assertThat(query.statuses()).isEmpty();
assertThat(query.assignees()).isEmpty();
assertThat(query.resolutions()).isEmpty();
assertThat(query.rules()).isEmpty();
assertThat(query.severities()).isEmpty();
assertThat(query.languages()).isEmpty();
assertThat(query.tags()).isEmpty();
assertThat(query.types()).isEmpty();
assertThat(query.createdAfterByProjectUuids()).isEmpty();
}
@Test
public void test_default_query() {
IssueQuery query = IssueQuery.builder().build();
assertThat(query.issueKeys()).isEmpty();
assertThat(query.projectUuids()).isEmpty();
assertThat(query.componentUuids()).isEmpty();
assertThat(query.moduleUuids()).isEmpty();
assertThat(query.statuses()).isEmpty();
assertThat(query.assignees()).isEmpty();
assertThat(query.resolutions()).isEmpty();
assertThat(query.rules()).isEmpty();
assertThat(query.severities()).isEmpty();
assertThat(query.languages()).isEmpty();
assertThat(query.tags()).isEmpty();
assertThat(query.types()).isEmpty();
assertThat(query.organizationUuid()).isNull();
assertThat(query.branchUuid()).isNull();
assertThat(query.assigned()).isNull();
assertThat(query.createdAfter()).isNull();
assertThat(query.createdBefore()).isNull();
assertThat(query.resolved()).isNull();
assertThat(query.sort()).isNull();
assertThat(query.createdAfterByProjectUuids()).isEmpty();
}
@Test
public void should_accept_null_sort() {
IssueQuery query = IssueQuery.builder().sort(null).build();
assertThat(query.sort()).isNull();
}
}