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.issue;
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Date;
24 import org.junit.Test;
25 import org.sonar.api.utils.Duration;
26 import org.sonar.core.issue.DefaultIssue;
27 import org.sonar.core.issue.IssueChangeContext;
28 import org.sonar.db.protobuf.DbCommons;
29 import org.sonar.db.protobuf.DbIssues;
30 import org.sonar.server.issue.IssueFieldsSetter;
31 import org.sonar.server.issue.workflow.IssueWorkflow;
33 import static com.google.common.collect.Lists.newArrayList;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.never;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.verifyZeroInteractions;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
41 import static org.sonar.api.issue.Issue.STATUS_CLOSED;
42 import static org.sonar.api.issue.Issue.STATUS_OPEN;
43 import static org.sonar.api.rule.Severity.BLOCKER;
44 import static org.sonar.api.utils.DateUtils.parseDate;
46 public class IssueLifecycleTest {
48 static final Date DEFAULT_DATE = new Date();
50 static final Duration DEFAULT_DURATION = Duration.create(10);
52 IssueChangeContext issueChangeContext = IssueChangeContext.createUser(DEFAULT_DATE, "julien");
54 IssueWorkflow workflow = mock(IssueWorkflow.class);
56 IssueFieldsSetter updater = mock(IssueFieldsSetter.class);
58 DebtCalculator debtCalculator = mock(DebtCalculator.class);
60 IssueLifecycle underTest = new IssueLifecycle(issueChangeContext, workflow, updater, debtCalculator);
63 public void initNewOpenIssue() throws Exception {
64 DefaultIssue issue = new DefaultIssue();
65 when(debtCalculator.calculate(issue)).thenReturn(DEFAULT_DURATION);
67 underTest.initNewOpenIssue(issue);
69 assertThat(issue.key()).isNotNull();
70 assertThat(issue.creationDate()).isNotNull();
71 assertThat(issue.updateDate()).isNotNull();
72 assertThat(issue.status()).isEqualTo(STATUS_OPEN);
73 assertThat(issue.debt()).isEqualTo(DEFAULT_DURATION);
74 assertThat(issue.isNew()).isTrue();
75 assertThat(issue.isCopied()).isFalse();
79 public void copyResolution() {
80 DefaultIssue issue = new DefaultIssue();
81 underTest.copyResolution(issue, "status", "resolution");
82 assertThat(issue.resolution()).isEqualTo("resolution");
83 assertThat(issue.status()).isEqualTo("status");
87 public void copiedIssue() throws Exception {
88 DefaultIssue raw = new DefaultIssue()
91 .setCreationDate(parseDate("2015-10-01"))
92 .setUpdateDate(parseDate("2015-10-02"))
93 .setCloseDate(parseDate("2015-10-03"));
95 DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
96 .setTextRange(DbCommons.TextRange.newBuilder()
101 DefaultIssue base = new DefaultIssue()
103 .setCreationDate(parseDate("2015-01-01"))
104 .setUpdateDate(parseDate("2015-01-02"))
105 .setCloseDate(parseDate("2015-01-03"))
106 .setResolution(RESOLUTION_FIXED)
107 .setStatus(STATUS_CLOSED)
108 .setSeverity(BLOCKER)
109 .setAssignee("base assignee")
110 .setAuthorLogin("base author")
111 .setTags(newArrayList("base tag"))
112 .setOnDisabledRule(true)
113 .setSelectedAt(1000L)
115 .setMessage("message")
117 .setEffort(Duration.create(15L))
118 .setManualSeverity(false)
119 .setLocations(issueLocations);
121 when(debtCalculator.calculate(raw)).thenReturn(DEFAULT_DURATION);
123 underTest.copyExistingOpenIssue(raw, base);
125 assertThat(raw.isNew()).isFalse();
126 assertThat(raw.isCopied()).isTrue();
127 assertThat(raw.key()).isNotNull();
128 assertThat(raw.key()).isNotEqualTo(base.key());
129 assertThat(raw.creationDate()).isEqualTo(base.creationDate());
130 assertThat(raw.updateDate()).isEqualTo(base.updateDate());
131 assertThat(raw.closeDate()).isEqualTo(base.closeDate());
132 assertThat(raw.resolution()).isEqualTo(RESOLUTION_FIXED);
133 assertThat(raw.status()).isEqualTo(STATUS_CLOSED);
134 assertThat(raw.assignee()).isEqualTo("base assignee");
135 assertThat(raw.authorLogin()).isEqualTo("base author");
136 assertThat(raw.tags()).containsOnly("base tag");
137 assertThat(raw.debt()).isEqualTo(DEFAULT_DURATION);
138 assertThat(raw.isOnDisabledRule()).isTrue();
139 assertThat(raw.selectedAt()).isEqualTo(1000L);
141 verifyZeroInteractions(updater);
145 public void doAutomaticTransition() throws Exception {
146 DefaultIssue issue = new DefaultIssue();
148 underTest.doAutomaticTransition(issue);
150 verify(workflow).doAutomaticTransition(issue, issueChangeContext);
154 public void mergeExistingOpenIssue() throws Exception {
155 DefaultIssue raw = new DefaultIssue()
158 .setCreationDate(parseDate("2015-10-01"))
159 .setUpdateDate(parseDate("2015-10-02"))
160 .setCloseDate(parseDate("2015-10-03"));
162 DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
163 .setTextRange(DbCommons.TextRange.newBuilder()
168 DefaultIssue base = new DefaultIssue()
170 .setCreationDate(parseDate("2015-01-01"))
171 .setUpdateDate(parseDate("2015-01-02"))
172 .setCloseDate(parseDate("2015-01-03"))
173 .setResolution(RESOLUTION_FIXED)
174 .setStatus(STATUS_CLOSED)
175 .setSeverity(BLOCKER)
176 .setAssignee("base assignee")
177 .setAuthorLogin("base author")
178 .setTags(newArrayList("base tag"))
179 .setOnDisabledRule(true)
180 .setSelectedAt(1000L)
182 .setMessage("message")
184 .setEffort(Duration.create(15L))
185 .setManualSeverity(false)
186 .setLocations(issueLocations);
188 when(debtCalculator.calculate(raw)).thenReturn(DEFAULT_DURATION);
190 underTest.mergeExistingOpenIssue(raw, base);
192 assertThat(raw.isNew()).isFalse();
193 assertThat(raw.key()).isEqualTo("BASE_KEY");
194 assertThat(raw.creationDate()).isEqualTo(base.creationDate());
195 assertThat(raw.updateDate()).isEqualTo(base.updateDate());
196 assertThat(raw.closeDate()).isEqualTo(base.closeDate());
197 assertThat(raw.resolution()).isEqualTo(RESOLUTION_FIXED);
198 assertThat(raw.status()).isEqualTo(STATUS_CLOSED);
199 assertThat(raw.assignee()).isEqualTo("base assignee");
200 assertThat(raw.authorLogin()).isEqualTo("base author");
201 assertThat(raw.tags()).containsOnly("base tag");
202 assertThat(raw.debt()).isEqualTo(DEFAULT_DURATION);
203 assertThat(raw.isOnDisabledRule()).isTrue();
204 assertThat(raw.selectedAt()).isEqualTo(1000L);
206 verify(updater).setPastSeverity(raw, BLOCKER, issueChangeContext);
207 verify(updater).setPastLine(raw, 10);
208 verify(updater).setPastMessage(raw, "message", issueChangeContext);
209 verify(updater).setPastEffort(raw, Duration.create(15L), issueChangeContext);
210 verify(updater).setPastLocations(raw, issueLocations);
214 public void mergeExistingOpenIssue_with_manual_severity() throws Exception {
215 DefaultIssue raw = new DefaultIssue()
218 DefaultIssue base = new DefaultIssue()
220 .setResolution(RESOLUTION_FIXED)
221 .setStatus(STATUS_CLOSED)
222 .setSeverity(BLOCKER)
223 .setManualSeverity(true);
225 underTest.mergeExistingOpenIssue(raw, base);
227 assertThat(raw.manualSeverity()).isTrue();
228 assertThat(raw.severity()).isEqualTo(BLOCKER);
230 verify(updater, never()).setPastSeverity(raw, BLOCKER, issueChangeContext);
234 public void mergeExistingOpenIssue_with_attributes() throws Exception {
235 DefaultIssue raw = new DefaultIssue()
238 DefaultIssue base = new DefaultIssue()
240 .setResolution(RESOLUTION_FIXED)
241 .setStatus(STATUS_CLOSED)
242 .setSeverity(BLOCKER)
243 .setAttributes(ImmutableMap.of("JIRA", "SONAR-01"));
245 underTest.mergeExistingOpenIssue(raw, base);
247 assertThat(raw.attributes()).containsEntry("JIRA", "SONAR-01");