]> source.dussan.org Git - sonarqube.git/blob
71ab05babebd00cad98bc7d9f9860564a05d2e6e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.ce.task.projectanalysis.step;
21
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.List;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.TemporaryFolder;
30 import org.mockito.ArgumentCaptor;
31 import org.sonar.api.rule.RuleKey;
32 import org.sonar.api.rules.RuleType;
33 import org.sonar.api.utils.System2;
34 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
35 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
36 import org.sonar.ce.task.projectanalysis.issue.AdHocRuleCreator;
37 import org.sonar.ce.task.projectanalysis.issue.IssueCache;
38 import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryImpl;
39 import org.sonar.ce.task.projectanalysis.issue.UpdateConflictResolver;
40 import org.sonar.ce.task.projectanalysis.util.cache.DiskCache;
41 import org.sonar.ce.task.step.ComputationStep;
42 import org.sonar.ce.task.step.TestComputationStepContext;
43 import org.sonar.core.issue.DefaultIssue;
44 import org.sonar.core.issue.DefaultIssueComment;
45 import org.sonar.core.issue.FieldDiffs;
46 import org.sonar.db.DbClient;
47 import org.sonar.db.DbSession;
48 import org.sonar.db.DbTester;
49 import org.sonar.db.component.ComponentDto;
50 import org.sonar.db.issue.IssueChangeDto;
51 import org.sonar.db.issue.IssueDto;
52 import org.sonar.db.issue.IssueMapper;
53 import org.sonar.db.organization.OrganizationDto;
54 import org.sonar.db.rule.RuleDefinitionDto;
55 import org.sonar.db.rule.RuleTesting;
56 import org.sonar.scanner.protocol.output.ScannerReport;
57 import org.sonar.server.issue.IssueStorage;
58
59 import static java.util.Collections.singletonList;
60 import static org.assertj.core.api.Assertions.assertThat;
61 import static org.assertj.core.data.MapEntry.entry;
62 import static org.mockito.ArgumentMatchers.any;
63 import static org.mockito.ArgumentMatchers.eq;
64 import static org.mockito.Mockito.mock;
65 import static org.mockito.Mockito.verify;
66 import static org.mockito.Mockito.when;
67 import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
68 import static org.sonar.api.issue.Issue.STATUS_CLOSED;
69 import static org.sonar.api.issue.Issue.STATUS_OPEN;
70 import static org.sonar.api.rule.Severity.BLOCKER;
71 import static org.sonar.db.component.ComponentTesting.newFileDto;
72
73 public class PersistIssuesStepTest extends BaseStepTest {
74
75   private static final long NOW = 1_400_000_000_000L;
76
77   @Rule
78   public TemporaryFolder temp = new TemporaryFolder();
79   @Rule
80   public DbTester db = DbTester.create(System2.INSTANCE);
81   @Rule
82   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
83   @Rule
84   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
85     .setOrganizationUuid("org-1", "qg-uuid-1");
86
87   private System2 system2 = mock(System2.class);
88   private DbSession session = db.getSession();
89   private DbClient dbClient = db.getDbClient();
90   private UpdateConflictResolver conflictResolver = mock(UpdateConflictResolver.class);
91   private IssueCache issueCache;
92   private ComputationStep underTest;
93
94   private AdHocRuleCreator adHocRuleCreator = mock(AdHocRuleCreator.class);
95
96   @Override
97   protected ComputationStep step() {
98     return underTest;
99   }
100
101   @Before
102   public void setup() throws Exception {
103     issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
104     reportReader.setMetadata(ScannerReport.Metadata.getDefaultInstance());
105
106     underTest = new PersistIssuesStep(dbClient, system2, conflictResolver, new RuleRepositoryImpl(adHocRuleCreator, dbClient, analysisMetadataHolder), issueCache,
107       new IssueStorage());
108   }
109
110   @After
111   public void tearDown() {
112     session.close();
113   }
114
115   @Test
116   public void insert_copied_issue() {
117     RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
118     db.rules().insert(rule);
119     OrganizationDto organizationDto = db.organizations().insert();
120     ComponentDto project = db.components().insertPrivateProject(organizationDto);
121     ComponentDto file = db.components().insertComponent(newFileDto(project, null));
122     when(system2.now()).thenReturn(NOW);
123
124     issueCache.newAppender().append(new DefaultIssue()
125       .setKey("ISSUE")
126       .setType(RuleType.CODE_SMELL)
127       .setRuleKey(rule.getKey())
128       .setComponentUuid(file.uuid())
129       .setProjectUuid(project.uuid())
130       .setSeverity(BLOCKER)
131       .setStatus(STATUS_OPEN)
132       .setNew(false)
133       .setCopied(true)
134       .setType(RuleType.BUG)
135       .setSelectedAt(NOW)
136       .addComment(new DefaultIssueComment()
137         .setKey("COMMENT")
138         .setIssueKey("ISSUE")
139         .setUserUuid("john_uuid")
140         .setMarkdownText("Some text")
141         .setCreatedAt(new Date(NOW))
142         .setNew(true))
143       .setCurrentChange(
144         new FieldDiffs()
145           .setIssueKey("ISSUE")
146           .setUserUuid("john_uuid")
147           .setDiff("technicalDebt", null, 1L)
148           .setCreationDate(new Date(NOW))))
149       .close();
150
151     TestComputationStepContext context = new TestComputationStepContext();
152     underTest.execute(context);
153
154     IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
155     assertThat(result.getKey()).isEqualTo("ISSUE");
156     assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
157     assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
158     assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
159     assertThat(result.getSeverity()).isEqualTo(BLOCKER);
160     assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
161     assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
162
163     List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE"));
164     assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE);
165     assertThat(context.getStatistics().getAll()).containsOnly(
166       entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"), entry("untouched", "0"));
167   }
168
169   @Test
170   public void insert_merged_issue() {
171     RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
172     db.rules().insert(rule);
173     OrganizationDto organizationDto = db.organizations().insert();
174     ComponentDto project = db.components().insertPrivateProject(organizationDto);
175     ComponentDto file = db.components().insertComponent(newFileDto(project, null));
176     when(system2.now()).thenReturn(NOW);
177
178     issueCache.newAppender().append(new DefaultIssue()
179       .setKey("ISSUE")
180       .setType(RuleType.CODE_SMELL)
181       .setRuleKey(rule.getKey())
182       .setComponentUuid(file.uuid())
183       .setProjectUuid(project.uuid())
184       .setSeverity(BLOCKER)
185       .setStatus(STATUS_OPEN)
186       .setNew(true)
187       .setCopied(true)
188       .setType(RuleType.BUG)
189       .setSelectedAt(NOW)
190       .addComment(new DefaultIssueComment()
191         .setKey("COMMENT")
192         .setIssueKey("ISSUE")
193         .setUserUuid("john_uuid")
194         .setMarkdownText("Some text")
195         .setCreatedAt(new Date(NOW))
196         .setNew(true))
197       .setCurrentChange(new FieldDiffs()
198         .setIssueKey("ISSUE")
199         .setUserUuid("john_uuid")
200         .setDiff("technicalDebt", null, 1L)
201         .setCreationDate(new Date(NOW))))
202       .close();
203
204     TestComputationStepContext context = new TestComputationStepContext();
205     underTest.execute(context);
206
207     IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
208     assertThat(result.getKey()).isEqualTo("ISSUE");
209     assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
210     assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
211     assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
212     assertThat(result.getSeverity()).isEqualTo(BLOCKER);
213     assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
214     assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
215
216     List<IssueChangeDto> changes = dbClient.issueChangeDao().selectByIssueKeys(session, Arrays.asList("ISSUE"));
217     assertThat(changes).extracting(IssueChangeDto::getChangeType).containsExactly(IssueChangeDto.TYPE_COMMENT, IssueChangeDto.TYPE_FIELD_CHANGE);
218     assertThat(context.getStatistics().getAll()).containsOnly(
219       entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"), entry("untouched", "0"));
220   }
221
222   @Test
223   public void update_conflicting_issue() {
224     RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
225     db.rules().insert(rule);
226     OrganizationDto organizationDto = db.organizations().insert();
227     ComponentDto project = db.components().insertPrivateProject(organizationDto);
228     ComponentDto file = db.components().insertComponent(newFileDto(project, null));
229     IssueDto issue = db.issues().insert(rule, project, file,
230       i -> i.setStatus(STATUS_OPEN)
231         .setResolution(null)
232         .setCreatedAt(NOW - 1_000_000_000L)
233         // simulate the issue has been updated after the analysis ran
234         .setUpdatedAt(NOW + 1_000_000_000L));
235     issue = dbClient.issueDao().selectByKey(db.getSession(), issue.getKey()).get();
236     DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
237     when(system2.now()).thenReturn(NOW);
238
239     DefaultIssue defaultIssue = issue.toDefaultIssue()
240       .setStatus(STATUS_CLOSED)
241       .setResolution(RESOLUTION_FIXED)
242       .setSelectedAt(NOW)
243       .setNew(false)
244       .setChanged(true);
245     issueCacheAppender.append(defaultIssue).close();
246
247     TestComputationStepContext context = new TestComputationStepContext();
248     underTest.execute(context);
249
250     ArgumentCaptor<IssueDto> issueDtoCaptor = ArgumentCaptor.forClass(IssueDto.class);
251     verify(conflictResolver).resolve(eq(defaultIssue), issueDtoCaptor.capture(), any(IssueMapper.class));
252     assertThat(issueDtoCaptor.getValue().getId()).isEqualTo(issue.getId());
253     assertThat(context.getStatistics().getAll()).containsOnly(
254       entry("inserts", "0"), entry("updates", "1"), entry("merged", "1"), entry("untouched", "0"));
255
256   }
257
258   @Test
259   public void insert_new_issue() {
260     RuleDefinitionDto rule = RuleTesting.newRule(RuleKey.of("xoo", "S01"));
261     db.rules().insert(rule);
262     OrganizationDto organizationDto = db.organizations().insert();
263     ComponentDto project = db.components().insertPrivateProject(organizationDto);
264     ComponentDto file = db.components().insertComponent(newFileDto(project, null));
265     session.commit();
266
267     issueCache.newAppender().append(new DefaultIssue()
268       .setKey("ISSUE")
269       .setType(RuleType.CODE_SMELL)
270       .setRuleKey(rule.getKey())
271       .setComponentUuid(file.uuid())
272       .setProjectUuid(project.uuid())
273       .setSeverity(BLOCKER)
274       .setStatus(STATUS_OPEN)
275       .setNew(true)
276       .setType(RuleType.BUG)).close();
277
278     TestComputationStepContext context = new TestComputationStepContext();
279     underTest.execute(context);
280
281     IssueDto result = dbClient.issueDao().selectOrFailByKey(session, "ISSUE");
282     assertThat(result.getKey()).isEqualTo("ISSUE");
283     assertThat(result.getRuleKey()).isEqualTo(rule.getKey());
284     assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
285     assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
286     assertThat(result.getSeverity()).isEqualTo(BLOCKER);
287     assertThat(result.getStatus()).isEqualTo(STATUS_OPEN);
288     assertThat(result.getType()).isEqualTo(RuleType.BUG.getDbConstant());
289     assertThat(context.getStatistics().getAll()).containsOnly(
290       entry("inserts", "1"), entry("updates", "0"), entry("merged", "0"), entry("untouched", "0"));
291   }
292
293   @Test
294   public void close_issue() {
295     ComponentDto project = db.components().insertPrivateProject();
296     ComponentDto file = db.components().insertComponent(newFileDto(project));
297     RuleDefinitionDto rule = db.rules().insert();
298     IssueDto issue = db.issues().insert(rule, project, file,
299       i -> i.setStatus(STATUS_OPEN)
300         .setResolution(null)
301         .setCreatedAt(NOW - 1_000_000_000L)
302         .setUpdatedAt(NOW - 1_000_000_000L));
303     DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
304
305     issueCacheAppender.append(
306       issue.toDefaultIssue()
307         .setStatus(STATUS_CLOSED)
308         .setResolution(RESOLUTION_FIXED)
309         .setSelectedAt(NOW)
310         .setNew(false)
311         .setChanged(true))
312       .close();
313
314     TestComputationStepContext context = new TestComputationStepContext();
315     underTest.execute(context);
316
317     IssueDto issueReloaded = db.getDbClient().issueDao().selectByKey(db.getSession(), issue.getKey()).get();
318     assertThat(issueReloaded.getStatus()).isEqualTo(STATUS_CLOSED);
319     assertThat(issueReloaded.getResolution()).isEqualTo(RESOLUTION_FIXED);
320     assertThat(context.getStatistics().getAll()).containsOnly(
321       entry("inserts", "0"), entry("updates", "1"), entry("merged", "0"), entry("untouched", "0"));
322   }
323
324   @Test
325   public void add_comment() {
326     ComponentDto project = db.components().insertPrivateProject();
327     ComponentDto file = db.components().insertComponent(newFileDto(project));
328     RuleDefinitionDto rule = db.rules().insert();
329     IssueDto issue = db.issues().insert(rule, project, file,
330       i -> i.setStatus(STATUS_OPEN)
331         .setResolution(null)
332         .setCreatedAt(NOW - 1_000_000_000L)
333         .setUpdatedAt(NOW - 1_000_000_000L));
334     DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
335
336     issueCacheAppender.append(
337       issue.toDefaultIssue()
338         .setStatus(STATUS_CLOSED)
339         .setResolution(RESOLUTION_FIXED)
340         .setSelectedAt(NOW)
341         .setNew(false)
342         .setChanged(true)
343         .addComment(new DefaultIssueComment()
344           .setKey("COMMENT")
345           .setIssueKey(issue.getKey())
346           .setUserUuid("john_uuid")
347           .setMarkdownText("Some text")
348           .setCreatedAt(new Date(NOW))
349           .setNew(true)))
350       .close();
351
352     TestComputationStepContext context = new TestComputationStepContext();
353     underTest.execute(context);
354
355     IssueChangeDto issueChangeDto = db.getDbClient().issueChangeDao().selectByIssueKeys(db.getSession(), singletonList(issue.getKey())).get(0);
356     assertThat(issueChangeDto)
357       .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserUuid, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey,
358         IssueChangeDto::getIssueChangeCreationDate)
359       .containsOnly(IssueChangeDto.TYPE_COMMENT, "john_uuid", "Some text", issue.getKey(), NOW);
360     assertThat(context.getStatistics().getAll()).containsOnly(
361       entry("inserts", "0"), entry("updates", "1"), entry("merged", "0"), entry("untouched", "0"));
362   }
363
364   @Test
365   public void add_change() {
366     ComponentDto project = db.components().insertPrivateProject();
367     ComponentDto file = db.components().insertComponent(newFileDto(project));
368     RuleDefinitionDto rule = db.rules().insert();
369     IssueDto issue = db.issues().insert(rule, project, file,
370       i -> i.setStatus(STATUS_OPEN)
371         .setResolution(null)
372         .setCreatedAt(NOW - 1_000_000_000L)
373         .setUpdatedAt(NOW - 1_000_000_000L));
374     DiskCache<DefaultIssue>.DiskAppender issueCacheAppender = issueCache.newAppender();
375
376     issueCacheAppender.append(
377       issue.toDefaultIssue()
378         .setStatus(STATUS_CLOSED)
379         .setResolution(RESOLUTION_FIXED)
380         .setSelectedAt(NOW)
381         .setNew(false)
382         .setChanged(true)
383         .setCurrentChange(new FieldDiffs()
384           .setIssueKey("ISSUE")
385           .setUserUuid("john_uuid")
386           .setDiff("technicalDebt", null, 1L)
387           .setCreationDate(new Date(NOW))))
388       .close();
389
390     TestComputationStepContext context = new TestComputationStepContext();
391     underTest.execute(context);
392
393     IssueChangeDto issueChangeDto = db.getDbClient().issueChangeDao().selectByIssueKeys(db.getSession(), singletonList(issue.getKey())).get(0);
394     assertThat(issueChangeDto)
395       .extracting(IssueChangeDto::getChangeType, IssueChangeDto::getUserUuid, IssueChangeDto::getChangeData, IssueChangeDto::getIssueKey,
396         IssueChangeDto::getIssueChangeCreationDate)
397       .containsOnly(IssueChangeDto.TYPE_FIELD_CHANGE, "john_uuid", "technicalDebt=1", issue.getKey(), NOW);
398     assertThat(context.getStatistics().getAll()).containsOnly(
399       entry("inserts", "0"), entry("updates", "1"), entry("merged", "0"), entry("untouched", "0"));
400   }
401
402 }