]> source.dussan.org Git - sonarqube.git/blob
bdd6a058bffd57ae48afb9e813196c7d5552e0dc
[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.issue;
21
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;
32
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;
45
46 public class IssueLifecycleTest {
47
48   static final Date DEFAULT_DATE = new Date();
49
50   static final Duration DEFAULT_DURATION = Duration.create(10);
51
52   IssueChangeContext issueChangeContext = IssueChangeContext.createUser(DEFAULT_DATE, "julien");
53
54   IssueWorkflow workflow = mock(IssueWorkflow.class);
55
56   IssueFieldsSetter updater = mock(IssueFieldsSetter.class);
57
58   DebtCalculator debtCalculator = mock(DebtCalculator.class);
59
60   IssueLifecycle underTest = new IssueLifecycle(issueChangeContext, workflow, updater, debtCalculator);
61
62   @Test
63   public void initNewOpenIssue() throws Exception {
64     DefaultIssue issue = new DefaultIssue();
65     when(debtCalculator.calculate(issue)).thenReturn(DEFAULT_DURATION);
66
67     underTest.initNewOpenIssue(issue);
68
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();
76   }
77
78   @Test
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");
84   }
85
86   @Test
87   public void copiedIssue() throws Exception {
88     DefaultIssue raw = new DefaultIssue()
89       .setNew(true)
90       .setKey("RAW_KEY")
91       .setCreationDate(parseDate("2015-10-01"))
92       .setUpdateDate(parseDate("2015-10-02"))
93       .setCloseDate(parseDate("2015-10-03"));
94
95     DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
96       .setTextRange(DbCommons.TextRange.newBuilder()
97         .setStartLine(10)
98         .setEndLine(12)
99         .build())
100       .build();
101     DefaultIssue base = new DefaultIssue()
102       .setKey("BASE_KEY")
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)
114       .setLine(10)
115       .setMessage("message")
116       .setGap(15d)
117       .setEffort(Duration.create(15L))
118       .setManualSeverity(false)
119       .setLocations(issueLocations);
120
121     when(debtCalculator.calculate(raw)).thenReturn(DEFAULT_DURATION);
122
123     underTest.copyExistingOpenIssue(raw, base);
124
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);
140
141     verifyZeroInteractions(updater);
142   }
143
144   @Test
145   public void doAutomaticTransition() throws Exception {
146     DefaultIssue issue = new DefaultIssue();
147
148     underTest.doAutomaticTransition(issue);
149
150     verify(workflow).doAutomaticTransition(issue, issueChangeContext);
151   }
152
153   @Test
154   public void mergeExistingOpenIssue() throws Exception {
155     DefaultIssue raw = new DefaultIssue()
156       .setNew(true)
157       .setKey("RAW_KEY")
158       .setCreationDate(parseDate("2015-10-01"))
159       .setUpdateDate(parseDate("2015-10-02"))
160       .setCloseDate(parseDate("2015-10-03"));
161
162     DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
163       .setTextRange(DbCommons.TextRange.newBuilder()
164         .setStartLine(10)
165         .setEndLine(12)
166         .build())
167       .build();
168     DefaultIssue base = new DefaultIssue()
169       .setKey("BASE_KEY")
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)
181       .setLine(10)
182       .setMessage("message")
183       .setGap(15d)
184       .setEffort(Duration.create(15L))
185       .setManualSeverity(false)
186       .setLocations(issueLocations);
187
188     when(debtCalculator.calculate(raw)).thenReturn(DEFAULT_DURATION);
189
190     underTest.mergeExistingOpenIssue(raw, base);
191
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);
205
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);
211   }
212
213   @Test
214   public void mergeExistingOpenIssue_with_manual_severity() throws Exception {
215     DefaultIssue raw = new DefaultIssue()
216       .setNew(true)
217       .setKey("RAW_KEY");
218     DefaultIssue base = new DefaultIssue()
219       .setKey("BASE_KEY")
220       .setResolution(RESOLUTION_FIXED)
221       .setStatus(STATUS_CLOSED)
222       .setSeverity(BLOCKER)
223       .setManualSeverity(true);
224
225     underTest.mergeExistingOpenIssue(raw, base);
226
227     assertThat(raw.manualSeverity()).isTrue();
228     assertThat(raw.severity()).isEqualTo(BLOCKER);
229
230     verify(updater, never()).setPastSeverity(raw, BLOCKER, issueChangeContext);
231   }
232
233   @Test
234   public void mergeExistingOpenIssue_with_attributes() throws Exception {
235     DefaultIssue raw = new DefaultIssue()
236       .setNew(true)
237       .setKey("RAW_KEY");
238     DefaultIssue base = new DefaultIssue()
239       .setKey("BASE_KEY")
240       .setResolution(RESOLUTION_FIXED)
241       .setStatus(STATUS_CLOSED)
242       .setSeverity(BLOCKER)
243       .setAttributes(ImmutableMap.of("JIRA", "SONAR-01"));
244
245     underTest.mergeExistingOpenIssue(raw, base);
246
247     assertThat(raw.attributes()).containsEntry("JIRA", "SONAR-01");
248   }
249 }