/
ServerIssueStorageTest.java
203 lines (166 loc) · 6.04 KB
/
ServerIssueStorageTest.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
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
/*
* 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.server.issue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.issue.internal.DefaultIssueComment;
import org.sonar.api.issue.internal.IssueChangeContext;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.Duration;
import org.sonar.api.utils.System2;
import org.sonar.core.persistence.AbstractDaoTestCase;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.resource.ResourceDao;
import org.sonar.server.component.db.ComponentDao;
import org.sonar.server.db.DbClient;
import org.sonar.server.issue.db.IssueDao;
import org.sonar.server.issue.index.IssueIndexer;
import java.util.Collection;
import java.util.Date;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ServerIssueStorageTest extends AbstractDaoTestCase {
DbClient dbClient;
DbSession session;
ServerIssueStorage storage;
@Before
public void setupDbClient() {
System2 system = mock(System2.class);
when(system.now()).thenReturn(2000000000L);
dbClient = new DbClient(getDatabase(), getMyBatis(),
new ComponentDao(system),
new IssueDao(getMyBatis()),
new ResourceDao(getMyBatis(), system));
session = dbClient.openSession(false);
storage = new ServerIssueStorage(getMyBatis(), new FakeRuleFinder(), dbClient, mock(IssueIndexer.class));
}
@After
public void tearDown() throws Exception {
session.close();
}
@Test
public void load_component_id_from_db() throws Exception {
setupData("load_component_id_from_db");
session.commit();
long componentId = storage.component(session, new DefaultIssue().setComponentKey("struts:Action")).getId();
assertThat(componentId).isEqualTo(100);
}
@Test
public void load_project_id_from_db() throws Exception {
setupData("load_project_id_from_db");
session.commit();
long projectId = storage.project(session, new DefaultIssue().setProjectKey("struts")).getId();
assertThat(projectId).isEqualTo(1);
}
@Test
public void should_insert_new_issues() throws Exception {
setupData("should_insert_new_issues");
DefaultIssueComment comment = DefaultIssueComment.create("ABCDE", "emmerik", "the comment");
// override generated key
comment.setKey("FGHIJ");
Date date = DateUtils.parseDate("2013-05-18");
DefaultIssue issue = new DefaultIssue()
.setKey("ABCDE")
.setNew(true)
.setRuleKey(RuleKey.of("squid", "AvoidCycle"))
.setProjectKey("struts")
.setLine(5000)
.setDebt(Duration.create(10L))
.setReporter("emmerik")
.setResolution("OPEN")
.setStatus("OPEN")
.setSeverity("BLOCKER")
.setAttribute("foo", "bar")
.addComment(comment)
.setCreationDate(date)
.setUpdateDate(date)
.setCloseDate(date)
.setComponentKey("struts:Action");
storage.save(issue);
checkTables("should_insert_new_issues", new String[] {"id", "created_at", "updated_at", "issue_change_creation_date"}, "issues", "issue_changes");
}
@Test
public void should_update_issues() throws Exception {
setupData("should_update_issues");
IssueChangeContext context = IssueChangeContext.createUser(new Date(), "emmerik");
DefaultIssueComment comment = DefaultIssueComment.create("ABCDE", "emmerik", "the comment");
// override generated key
comment.setKey("FGHIJ");
Date date = DateUtils.parseDate("2013-05-18");
DefaultIssue issue = new DefaultIssue()
.setKey("ABCDE")
.setNew(false)
.setChanged(true)
// updated fields
.setLine(5000)
.setProjectUuid("CDEF")
.setDebt(Duration.create(10L))
.setChecksum("FFFFF")
.setAuthorLogin("simon")
.setAssignee("loic")
.setFieldChange(context, "severity", "INFO", "BLOCKER")
.setReporter("emmerik")
.setResolution("FIXED")
.setStatus("RESOLVED")
.setSeverity("BLOCKER")
.setAttribute("foo", "bar")
.addComment(comment)
.setCreationDate(date)
.setUpdateDate(date)
.setCloseDate(date)
// unmodifiable fields
.setRuleKey(RuleKey.of("xxx", "unknown"))
.setComponentKey("struts:Action")
.setProjectKey("struts");
storage.save(issue);
checkTables("should_update_issues", new String[] {"id", "created_at", "updated_at", "issue_change_creation_date"}, "issues", "issue_changes");
}
static class FakeRuleFinder implements RuleFinder {
@Override
public Rule findById(int ruleId) {
return null;
}
@Override
public Rule findByKey(String repositoryKey, String key) {
return null;
}
@Override
public Rule findByKey(RuleKey key) {
Rule rule = Rule.create().setRepositoryKey(key.repository()).setKey(key.rule());
rule.setId(200);
return rule;
}
@Override
public Rule find(RuleQuery query) {
return null;
}
@Override
public Collection<Rule> findAll(RuleQuery query) {
return null;
}
}
}