]> source.dussan.org Git - sonarqube.git/blob
1612e0d333c9592f29893bfe1243be22e1344fae
[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_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);
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(true)
116       .setType(RuleType.BUG)).close();
117
118     step.execute();
119
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());
128   }
129
130   @Test
131   public void close_issue() {
132     dbTester.prepareDbUnit(getClass(), "shared.xml");
133
134     issueCache.newAppender().append(new DefaultIssue()
135       .setKey("ISSUE")
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)
143       .setSelectedAt(NOW)
144       .setNew(false)
145       .setChanged(true)).close();
146
147     step.execute();
148
149     dbTester.assertDbUnit(getClass(), "close_issue-result.xml", "issues");
150   }
151
152   @Test
153   public void add_comment() {
154     dbTester.prepareDbUnit(getClass(), "shared.xml");
155
156     issueCache.newAppender().append(new DefaultIssue()
157       .setKey("ISSUE")
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)
165       .setNew(false)
166       .setChanged(true)
167       .addComment(new DefaultIssueComment()
168         .setKey("COMMENT")
169         .setIssueKey("ISSUE")
170         .setUserLogin("john")
171         .setMarkdownText("Some text")
172         .setNew(true)))
173       .close();
174
175     step.execute();
176
177     dbTester.assertDbUnit(getClass(), "add_comment-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
178   }
179
180   @Test
181   public void add_change() {
182     dbTester.prepareDbUnit(getClass(), "shared.xml");
183
184     issueCache.newAppender().append(new DefaultIssue()
185       .setKey("ISSUE")
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)
193       .setNew(false)
194       .setChanged(true)
195       .setCurrentChange(new FieldDiffs()
196         .setIssueKey("ISSUE")
197         .setUserLogin("john")
198         .setDiff("technicalDebt", null, 1L)))
199       .close();
200
201     step.execute();
202
203     dbTester.assertDbUnit(getClass(), "add_change-result.xml", new String[] {"id", "created_at", "updated_at"}, "issue_changes");
204   }
205
206 }