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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact 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.db.issue;
import java.util.List;
import org.apache.ibatis.executor.result.DefaultResultHandler;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.RowNotFoundException;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.test.DbTests;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
@Category(DbTests.class)
public class IssueDaoTest {
public static final RuleDto RULE = RuleTesting.newXooX1();
public static final ComponentDto PROJECT = ComponentTesting.newProjectDto();
public static final ComponentDto FILE = ComponentTesting.newFileDto(PROJECT);
public static final String ISSUE_KEY1 = "I1";
public static final String ISSUE_KEY2 = "I2";
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
IssueDao underTest = dbTester.getDbClient().issueDao();
@Test
public void select_non_closed_issues_by_module() {
dbTester.prepareDbUnit(getClass(), "shared.xml", "should_select_non_closed_issues_by_module.xml");
// 400 is a non-root module, we should find 2 issues from classes and one on itself
DefaultResultHandler handler = new DefaultResultHandler();
underTest.selectNonClosedIssuesByModule(400, handler);
assertThat(handler.getResultList()).hasSize(3);
IssueDto issue = (IssueDto) handler.getResultList().get(0);
assertThat(issue.getRuleRepo()).isNotNull();
assertThat(issue.getRule()).isNotNull();
assertThat(issue.getComponentKey()).isNotNull();
assertThat(issue.getProjectKey()).isEqualTo("struts");
// 399 is the root module, we should only find 1 issue on itself
handler = new DefaultResultHandler();
underTest.selectNonClosedIssuesByModule(399, handler);
assertThat(handler.getResultList()).hasSize(1);
issue = (IssueDto) handler.getResultList().get(0);
assertThat(issue.getComponentKey()).isEqualTo("struts");
assertThat(issue.getProjectKey()).isEqualTo("struts");
}
/**
* SONAR-5218
*/
@Test
public void select_non_closed_issues_by_module_on_removed_project() {
// All issues are linked on a project that is not existing anymore
dbTester.prepareDbUnit(getClass(), "shared.xml", "should_select_non_closed_issues_by_module_on_removed_project.xml");
// 400 is a non-root module, we should find 2 issues from classes and one on itself
DefaultResultHandler handler = new DefaultResultHandler();
underTest.selectNonClosedIssuesByModule(400, handler);
assertThat(handler.getResultList()).hasSize(3);
IssueDto issue = (IssueDto) handler.getResultList().get(0);
assertThat(issue.getRuleRepo()).isNotNull();
assertThat(issue.getRule()).isNotNull();
assertThat(issue.getComponentKey()).isNotNull();
assertThat(issue.getProjectKey()).isNull();
}
@Test
public void selectByKeyOrFail() {
prepareTables();
IssueDto issue = underTest.selectOrFailByKey(dbTester.getSession(), ISSUE_KEY1);
assertThat(issue.getKee()).isEqualTo(ISSUE_KEY1);
assertThat(issue.getId()).isGreaterThan(0L);
assertThat(issue.getComponentUuid()).isEqualTo(FILE.uuid());
assertThat(issue.getProjectUuid()).isEqualTo(PROJECT.uuid());
assertThat(issue.getRuleId()).isEqualTo(RULE.getId());
assertThat(issue.getLanguage()).isEqualTo(RULE.getLanguage());
assertThat(issue.getSeverity()).isEqualTo("BLOCKER");
assertThat(issue.getType()).isEqualTo(2);
assertThat(issue.isManualSeverity()).isFalse();
assertThat(issue.getMessage()).isEqualTo("the message");
assertThat(issue.getLine()).isEqualTo(500);
assertThat(issue.getEffortToFix()).isEqualTo(3.14);
assertThat(issue.getStatus()).isEqualTo("RESOLVED");
assertThat(issue.getResolution()).isEqualTo("FIXED");
assertThat(issue.getChecksum()).isEqualTo("123456789");
assertThat(issue.getAuthorLogin()).isEqualTo("morgan");
assertThat(issue.getReporter()).isEqualTo("emmerik");
assertThat(issue.getAssignee()).isEqualTo("karadoc");
assertThat(issue.getIssueAttributes()).isEqualTo("JIRA=FOO-1234");
assertThat(issue.getIssueCreationDate()).isNotNull();
assertThat(issue.getIssueUpdateDate()).isNotNull();
assertThat(issue.getIssueCloseDate()).isNotNull();
assertThat(issue.getCreatedAt()).isEqualTo(1_440_000_000_000L);
assertThat(issue.getUpdatedAt()).isEqualTo(1_440_000_000_000L);
assertThat(issue.getRuleRepo()).isEqualTo(RULE.getRepositoryKey());
assertThat(issue.getRule()).isEqualTo(RULE.getRuleKey());
assertThat(issue.getComponentKey()).isEqualTo(FILE.key());
assertThat(issue.getProjectKey()).isEqualTo(PROJECT.key());
assertThat(issue.getLocations()).isNull();
assertThat(issue.parseLocations()).isNull();
}
@Test
public void selectByKeyOrFail_fails_if_key_not_found() {
thrown.expect(RowNotFoundException.class);
thrown.expectMessage("Issue with key 'DOES_NOT_EXIST' does not exist");
prepareTables();
underTest.selectOrFailByKey(dbTester.getSession(), "DOES_NOT_EXIST");
}
@Test
public void selectByKeys() {
// contains I1 and I2
prepareTables();
List<IssueDto> issues = underTest.selectByKeys(dbTester.getSession(), asList("I1", "I2", "I3"));
// results are not ordered, so do not use "containsExactly"
assertThat(issues).extracting("key").containsOnly("I1", "I2");
}
@Test
public void selectByOrderedKeys() {
// contains I1 and I2
prepareTables();
Iterable<IssueDto> issues = underTest.selectByOrderedKeys(dbTester.getSession(), asList("I1", "I2", "I3"));
assertThat(issues).extracting("key").containsExactly("I1", "I2");
issues = underTest.selectByOrderedKeys(dbTester.getSession(), asList("I2", "I3", "I1"));
assertThat(issues).extracting("key").containsExactly("I2", "I1");
}
@Test
public void selectByActionPlan() {
dbTester.prepareDbUnit(getClass(), "shared.xml", "find_by_action_plan.xml");
List<IssueDto> issues = underTest.selectByActionPlan(dbTester.getSession(), "AP-1");
assertThat(issues).hasSize(1);
IssueDto issue = issues.get(0);
assertThat(issue.getKee()).isEqualTo("ABCDE");
assertThat(issue.getActionPlanKey()).isEqualTo("AP-1");
assertThat(issue.getComponentUuid()).isEqualTo("CDEF");
assertThat(issue.getProjectUuid()).isEqualTo("ABCD");
assertThat(issue.getRuleId()).isEqualTo(500);
assertThat(issue.getLanguage()).isEqualTo("java");
assertThat(issue.getSeverity()).isEqualTo("BLOCKER");
assertThat(issue.isManualSeverity()).isFalse();
assertThat(issue.getMessage()).isNull();
assertThat(issue.getLine()).isEqualTo(200);
assertThat(issue.getEffortToFix()).isEqualTo(4.2);
assertThat(issue.getStatus()).isEqualTo("OPEN");
assertThat(issue.getResolution()).isEqualTo("FIXED");
assertThat(issue.getChecksum()).isEqualTo("XXX");
assertThat(issue.getAuthorLogin()).isEqualTo("karadoc");
assertThat(issue.getReporter()).isEqualTo("arthur");
assertThat(issue.getAssignee()).isEqualTo("perceval");
assertThat(issue.getIssueAttributes()).isEqualTo("JIRA=FOO-1234");
assertThat(issue.getIssueCreationDate()).isNotNull();
assertThat(issue.getIssueUpdateDate()).isNotNull();
assertThat(issue.getIssueCloseDate()).isNotNull();
assertThat(issue.getCreatedAt()).isNotNull();
assertThat(issue.getUpdatedAt()).isNotNull();
assertThat(issue.getRuleRepo()).isEqualTo("squid");
assertThat(issue.getRule()).isEqualTo("AvoidCycle");
assertThat(issue.getComponentKey()).isEqualTo("Action.java");
assertThat(issue.getProjectKey()).isEqualTo("struts");
}
private static IssueDto newIssueDto(String key) {
IssueDto dto = new IssueDto();
dto.setComponent(new ComponentDto().setKey("struts:Action").setId(123L).setUuid("component-uuid"));
dto.setProject(new ComponentDto().setKey("struts").setId(100L).setUuid("project-uuid"));
dto.setRule(RuleTesting.newDto(RuleKey.of("squid", "S001")).setId(200));
dto.setKee(key);
dto.setType(2);
dto.setLine(500);
dto.setEffortToFix(3.14);
dto.setDebt(10L);
dto.setResolution("FIXED");
dto.setStatus("RESOLVED");
dto.setSeverity("BLOCKER");
dto.setReporter("emmerik");
dto.setAuthorLogin("morgan");
dto.setAssignee("karadoc");
dto.setActionPlanKey("current_sprint");
dto.setIssueAttributes("JIRA=FOO-1234");
dto.setChecksum("123456789");
dto.setMessage("the message");
dto.setCreatedAt(1_440_000_000_000L);
dto.setUpdatedAt(1_440_000_000_000L);
dto.setIssueCreationTime(1_450_000_000_000L);
dto.setIssueUpdateTime(1_450_000_000_000L);
dto.setIssueCloseTime(1_450_000_000_000L);
return dto;
}
private void prepareTables() {
dbTester.getDbClient().ruleDao().insert(dbTester.getSession(), RULE);
dbTester.getDbClient().componentDao().insert(dbTester.getSession(), PROJECT, FILE);
underTest.insert(dbTester.getSession(), newIssueDto(ISSUE_KEY1)
.setMessage("the message")
.setRuleId(RULE.getId())
.setComponentUuid(FILE.uuid())
.setProjectUuid(PROJECT.uuid()));
underTest.insert(dbTester.getSession(), newIssueDto(ISSUE_KEY2)
.setRuleId(RULE.getId())
.setComponentUuid(FILE.uuid())
.setProjectUuid(PROJECT.uuid()));
dbTester.getSession().commit();
}
}
|