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_new_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)
116 .setType(RuleType.BUG)).close();
120 IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
121 assertThat(result.getKey()).isEqualTo("ISSUE");
122 assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
123 assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
124 assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
125 assertThat(result.getSeverity()).isEqualTo(Severity.BLOCKER);
126 assertThat(result.getStatus()).isEqualTo(Issue.STATUS_OPEN);
127 assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
131 public void close_issue() {
132 dbTester.prepareDbUnit(getClass(), "shared.xml");
134 issueCache.newAppender().append(new DefaultIssue()
136 .setType(RuleType.CODE_SMELL)
137 .setRuleKey(RuleKey.of("xoo", "S01"))
138 .setComponentUuid("COMPONENT")
139 .setProjectUuid("PROJECT")
140 .setSeverity(Severity.BLOCKER)
141 .setStatus(Issue.STATUS_CLOSED)
142 .setResolution(Issue.RESOLUTION_FIXED)
145 .setChanged(true)).close();
149 dbTester.assertDbUnit(getClass(), "close_issue-result.xml", "issues");
153 public void add_comment() {
154 dbTester.prepareDbUnit(getClass(), "shared.xml");
156 issueCache.newAppender().append(new DefaultIssue()
158 .setType(RuleType.CODE_SMELL)
159 .setRuleKey(RuleKey.of("xoo", "S01"))
160 .setComponentUuid("COMPONENT")
161 .setProjectUuid("PROJECT")
162 .setSeverity(Severity.BLOCKER)
163 .setStatus(Issue.STATUS_CLOSED)
164 .setResolution(Issue.RESOLUTION_FIXED)
167 .addComment(new DefaultIssueComment()
169 .setIssueKey("ISSUE")
170 .setUserLogin("john")
171 .setMarkdownText("Some text")
177 dbTester.assertDbUnit(getClass(), "add_comment-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
181 public void add_change() {
182 dbTester.prepareDbUnit(getClass(), "shared.xml");
184 issueCache.newAppender().append(new DefaultIssue()
186 .setType(RuleType.CODE_SMELL)
187 .setRuleKey(RuleKey.of("xoo", "S01"))
188 .setComponentUuid("COMPONENT")
189 .setProjectUuid("PROJECT")
190 .setSeverity(Severity.BLOCKER)
191 .setStatus(Issue.STATUS_CLOSED)
192 .setResolution(Issue.RESOLUTION_FIXED)
195 .setCurrentChange(new FieldDiffs()
196 .setIssueKey("ISSUE")
197 .setUserLogin("john")
198 .setDiff("technicalDebt", null, 1L)))
203 dbTester.assertDbUnit(getClass(), "add_change-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");