]> source.dussan.org Git - sonarqube.git/blob
bbb198d92ab63a16c8d189df5e5dc6f0f25b6e6b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.step;
21
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;
51
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.when;
55
56 public class PersistIssuesStepTest extends BaseStepTest {
57
58   public static final long NOW = 1400000000000L;
59
60   @Rule
61   public TemporaryFolder temp = new TemporaryFolder();
62   @Rule
63   public DbTester dbTester = DbTester.create(System2.INSTANCE);
64   @Rule
65   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
66   @Rule
67   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
68     .setOrganizationUuid("org-1");
69
70   private DbSession session = dbTester.getSession();
71   private DbClient dbClient = dbTester.getDbClient();
72   private System2 system2;
73   private IssueCache issueCache;
74   private ComputationStep step;
75
76   @Override
77   protected ComputationStep step() {
78     return step;
79   }
80
81   @Before
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());
87
88     step = new PersistIssuesStep(dbClient, system2, new UpdateConflictResolver(), new RuleRepositoryImpl(dbClient, analysisMetadataHolder), issueCache);
89   }
90
91   @After
92   public void tearDown() {
93     session.close();
94   }
95
96   @Test
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);
105     session.commit();
106
107     issueCache.newAppender().append(new DefaultIssue()
108       .setKey("ISSUE")
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)
115       .setNew(false)
116       .setCopied(true)
117       .setType(RuleType.BUG)).close();
118
119     step.execute();
120
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());
129   }
130
131   @Test
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);
140     session.commit();
141
142     issueCache.newAppender().append(new DefaultIssue()
143       .setKey("ISSUE")
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)
150       .setNew(true)
151       .setType(RuleType.BUG)).close();
152
153     step.execute();
154
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());
163   }
164
165   @Test
166   public void close_issue() {
167     dbTester.prepareDbUnit(getClass(), "shared.xml");
168
169     issueCache.newAppender().append(new DefaultIssue()
170       .setKey("ISSUE")
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)
178       .setSelectedAt(NOW)
179       .setNew(false)
180       .setChanged(true)).close();
181
182     step.execute();
183
184     dbTester.assertDbUnit(getClass(), "close_issue-result.xml", "issues");
185   }
186
187   @Test
188   public void add_comment() {
189     dbTester.prepareDbUnit(getClass(), "shared.xml");
190
191     issueCache.newAppender().append(new DefaultIssue()
192       .setKey("ISSUE")
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)
200       .setNew(false)
201       .setChanged(true)
202       .addComment(new DefaultIssueComment()
203         .setKey("COMMENT")
204         .setIssueKey("ISSUE")
205         .setUserLogin("john")
206         .setMarkdownText("Some text")
207         .setNew(true)))
208       .close();
209
210     step.execute();
211
212     dbTester.assertDbUnit(getClass(), "add_comment-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
213   }
214
215   @Test
216   public void add_change() {
217     dbTester.prepareDbUnit(getClass(), "shared.xml");
218
219     issueCache.newAppender().append(new DefaultIssue()
220       .setKey("ISSUE")
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)
228       .setNew(false)
229       .setChanged(true)
230       .setCurrentChange(new FieldDiffs()
231         .setIssueKey("ISSUE")
232         .setUserLogin("john")
233         .setDiff("technicalDebt", null, 1L)))
234       .close();
235
236     step.execute();
237
238     dbTester.assertDbUnit(getClass(), "add_change-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
239   }
240
241 }