]> source.dussan.org Git - sonarqube.git/blob
8a6ad9c11451fa6654aa0cd98e4b55d8deb070f3
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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 org.assertj.core.api.Assertions;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
29 import org.sonar.ce.task.projectanalysis.issue.fixedissues.PullRequestFixedIssueRepositoryImpl;
30 import org.sonar.ce.task.step.TestComputationStepContext;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.component.ComponentDto;
35 import org.sonar.db.component.ProjectData;
36 import org.sonar.db.issue.IssueFixedDto;
37
38 import static org.assertj.core.groups.Tuple.tuple;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
42 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
43
44 public class PersistPullRequestFixedIssueStepIT {
45   @Rule
46   public DbTester db = DbTester.create(System2.INSTANCE);
47
48   @Rule
49   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
50   private PullRequestFixedIssueRepositoryImpl fixedIssueRepository;
51   public PersistPullRequestFixedIssueStep underTest;
52
53   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
54
55   @Before
56   public void before() {
57     fixedIssueRepository = new PullRequestFixedIssueRepositoryImpl();
58     underTest = new PersistPullRequestFixedIssueStep(analysisMetadataHolder, db.getDbClient(), treeRootHolder, fixedIssueRepository);
59     when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
60   }
61
62   @Test
63   public void execute_shouldPersistFixedIssues() {
64     ProjectData projectData = db.components().insertPublicProject();
65     ComponentDto pullRequest = db.components().insertProjectBranch(projectData.getMainBranchComponent());
66
67     fixedIssueRepository.addFixedIssue(new DefaultIssue().setKey("key1"));
68     fixedIssueRepository.addFixedIssue(new DefaultIssue().setKey("key2"));
69     treeRootHolder.setRoot(builder(PROJECT, 1)
70       .setUuid(pullRequest.uuid())
71       .build());
72
73     underTest.execute(new TestComputationStepContext());
74
75     Assertions.assertThat(db.getDbClient().issueFixedDao().selectByPullRequest(db.getSession(), pullRequest.uuid()))
76       .extracting(IssueFixedDto::issueKey, IssueFixedDto::pullRequestUuid)
77       .containsExactly(tuple("key1", pullRequest.uuid()), tuple("key2", pullRequest.uuid()));
78   }
79
80   @Test
81   public void execute_whenFixedIssuesAlreadyExists_shouldKeepExistingFixedIssues() {
82     ProjectData projectData = db.components().insertPublicProject();
83     ComponentDto pullRequest = db.components().insertProjectBranch(projectData.getMainBranchComponent());
84
85     DbSession session = db.getSession();
86     db.getDbClient().issueFixedDao().insert(session, new IssueFixedDto(pullRequest.uuid(), "key1"));
87     db.getDbClient().issueFixedDao().insert(session, new IssueFixedDto(pullRequest.uuid(), "key2"));
88     session.commit();
89
90     fixedIssueRepository.addFixedIssue(new DefaultIssue().setKey("key1"));
91     treeRootHolder.setRoot(builder(PROJECT, 1)
92       .setUuid(pullRequest.uuid())
93       .build());
94
95     underTest.execute(new TestComputationStepContext());
96
97     Assertions.assertThat(db.getDbClient().issueFixedDao().selectByPullRequest(session, pullRequest.uuid()))
98       .extracting(IssueFixedDto::issueKey, IssueFixedDto::pullRequestUuid)
99       .containsExactly(tuple("key1", pullRequest.uuid()));
100   }
101
102   @Test
103   public void execute_whenRepositoryIsEmpty_shouldNotPersistAnyFixedIssues() {
104     ProjectData projectData = db.components().insertPublicProject();
105     ComponentDto pullRequest = db.components().insertProjectBranch(projectData.getMainBranchComponent());
106
107     DbSession session = db.getSession();
108     db.getDbClient().issueFixedDao().insert(session, new IssueFixedDto(pullRequest.uuid(), "key1"));
109     session.commit();
110
111     treeRootHolder.setRoot(builder(PROJECT, 1)
112       .setUuid(pullRequest.uuid())
113       .build());
114
115     underTest.execute(new TestComputationStepContext());
116
117     Assertions.assertThat(db.getDbClient().issueFixedDao().selectByPullRequest(db.getSession(), pullRequest.uuid()))
118       .isEmpty();
119   }
120
121
122 }