3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.TemporaryFolder;
27 import org.sonar.api.issue.Issue;
28 import org.sonar.api.rule.RuleKey;
29 import org.sonar.api.rule.Severity;
30 import org.sonar.api.rules.RuleType;
31 import org.sonar.api.utils.System2;
32 import org.sonar.core.issue.DefaultIssue;
33 import org.sonar.core.issue.DefaultIssueComment;
34 import org.sonar.core.issue.FieldDiffs;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbSession;
37 import org.sonar.db.DbTester;
38 import org.sonar.db.component.ComponentDto;
39 import org.sonar.db.component.ComponentTesting;
40 import org.sonar.db.issue.IssueDto;
41 import org.sonar.db.organization.OrganizationDto;
42 import org.sonar.db.rule.RuleDefinitionDto;
43 import org.sonar.db.rule.RuleTesting;
44 import org.sonar.scanner.protocol.output.ScannerReport;
45 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
46 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
47 import org.sonar.server.computation.task.projectanalysis.issue.IssueCache;
48 import org.sonar.server.computation.task.projectanalysis.issue.RuleRepositoryImpl;
49 import org.sonar.server.computation.task.projectanalysis.issue.UpdateConflictResolver;
50 import org.sonar.server.computation.task.step.ComputationStep;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.when;
56 public class PersistIssuesStepTest extends BaseStepTest {
58 public static final long NOW = 1400000000000L;
61 public TemporaryFolder temp = new TemporaryFolder();
63 public DbTester dbTester = DbTester.create(System2.INSTANCE);
65 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
67 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
68 .setOrganizationUuid("org-1");
70 private DbSession session = dbTester.getSession();
71 private DbClient dbClient = dbTester.getDbClient();
72 private System2 system2;
73 private IssueCache issueCache;
74 private ComputationStep step;
77 protected ComputationStep step() {
82 public void setup() throws Exception {
83 issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
84 system2 = mock(System2.class);
85 when(system2.now()).thenReturn(NOW);
86 reportReader.setMetadata(ScannerReport.Metadata.getDefaultInstance());
88 step = new PersistIssuesStep(dbClient, system2, new UpdateConflictResolver(), new RuleRepositoryImpl(dbClient, analysisMetadataHolder), issueCache);
92 public void tearDown() {
97 public void insert_copied_issue() {
98 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
99 dbTester.rules().insert(rule);
100 OrganizationDto organizationDto = dbTester.organizations().insert();
101 ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
102 dbClient.componentDao().insert(session, project);
103 ComponentDto file = ComponentTesting.newFileDto(project, null);
104 dbClient.componentDao().insert(session, file);
107 issueCache.newAppender().append(new DefaultIssue()
109 .setType(RuleType.CODE_SMELL)
110 .setRuleKey(rule.getKey())
111 .setComponentUuid(file.uuid())
112 .setProjectUuid(project.uuid())
113 .setSeverity(Severity.BLOCKER)
114 .setStatus(Issue.STATUS_OPEN)
117 .setType(RuleType.BUG)).close();
121 IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
122 assertThat(result.getKey()).isEqualTo("ISSUE");
123 assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
124 assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
125 assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
126 assertThat(result.getSeverity()).isEqualTo(Severity.BLOCKER);
127 assertThat(result.getStatus()).isEqualTo(Issue.STATUS_OPEN);
128 assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
132 public void insert_new_issue() {
133 RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
134 dbTester.rules().insert(rule);
135 OrganizationDto organizationDto = dbTester.organizations().insert();
136 ComponentDto project = ComponentTesting.newPrivateProjectDto(organizationDto);
137 dbClient.componentDao().insert(session, project);
138 ComponentDto file = ComponentTesting.newFileDto(project, null);
139 dbClient.componentDao().insert(session, file);
142 issueCache.newAppender().append(new DefaultIssue()
144 .setType(RuleType.CODE_SMELL)
145 .setRuleKey(rule.getKey())
146 .setComponentUuid(file.uuid())
147 .setProjectUuid(project.uuid())
148 .setSeverity(Severity.BLOCKER)
149 .setStatus(Issue.STATUS_OPEN)
151 .setType(RuleType.BUG)).close();
155 IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
156 assertThat(result.getKey()).isEqualTo("ISSUE");
157 assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
158 assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
159 assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
160 assertThat(result.getSeverity()).isEqualTo(Severity.BLOCKER);
161 assertThat(result.getStatus()).isEqualTo(Issue.STATUS_OPEN);
162 assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
166 public void close_issue() {
167 dbTester.prepareDbUnit(getClass(), "shared.xml");
169 issueCache.newAppender().append(new DefaultIssue()
171 .setType(RuleType.CODE_SMELL)
172 .setRuleKey(RuleKey.of("xoo", "S01"))
173 .setComponentUuid("COMPONENT")
174 .setProjectUuid("PROJECT")
175 .setSeverity(Severity.BLOCKER)
176 .setStatus(Issue.STATUS_CLOSED)
177 .setResolution(Issue.RESOLUTION_FIXED)
180 .setChanged(true)).close();
184 dbTester.assertDbUnit(getClass(), "close_issue-result.xml", "issues");
188 public void add_comment() {
189 dbTester.prepareDbUnit(getClass(), "shared.xml");
191 issueCache.newAppender().append(new DefaultIssue()
193 .setType(RuleType.CODE_SMELL)
194 .setRuleKey(RuleKey.of("xoo", "S01"))
195 .setComponentUuid("COMPONENT")
196 .setProjectUuid("PROJECT")
197 .setSeverity(Severity.BLOCKER)
198 .setStatus(Issue.STATUS_CLOSED)
199 .setResolution(Issue.RESOLUTION_FIXED)
202 .addComment(new DefaultIssueComment()
204 .setIssueKey("ISSUE")
205 .setUserLogin("john")
206 .setMarkdownText("Some text")
212 dbTester.assertDbUnit(getClass(), "add_comment-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
216 public void add_change() {
217 dbTester.prepareDbUnit(getClass(), "shared.xml");
219 issueCache.newAppender().append(new DefaultIssue()
221 .setType(RuleType.CODE_SMELL)
222 .setRuleKey(RuleKey.of("xoo", "S01"))
223 .setComponentUuid("COMPONENT")
224 .setProjectUuid("PROJECT")
225 .setSeverity(Severity.BLOCKER)
226 .setStatus(Issue.STATUS_CLOSED)
227 .setResolution(Issue.RESOLUTION_FIXED)
230 .setCurrentChange(new FieldDiffs()
231 .setIssueKey("ISSUE")
232 .setUserLogin("john")
233 .setDiff("technicalDebt", null, 1L)))
238 dbTester.assertDbUnit(getClass(), "add_change-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");