3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.issue;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.sonar.api.issue.Issue;
25 import org.sonar.api.rule.RuleKey;
26 import org.sonar.api.rule.Severity;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.core.issue.DefaultIssue;
29 import org.sonar.db.issue.IssueDto;
30 import org.sonar.db.issue.IssueMapper;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
37 public class UpdateConflictResolverTest {
40 public void should_reload_issue_and_resolve_conflict() {
41 DefaultIssue issue = new DefaultIssue()
43 .setRuleKey(RuleKey.of("squid", "AvoidCycles"))
44 .setComponentKey("struts:org.apache.struts.Action")
46 .setStatus(Issue.STATUS_OPEN);
48 // Issue as seen and changed by end-user
49 IssueMapper mapper = mock(IssueMapper.class);
50 when(mapper.selectByKey("ABCDE")).thenReturn(
54 .setRuleKey("squid", "AvoidCycles")
55 .setComponentKey("struts:org.apache.struts.Action")
57 .setStatus(Issue.STATUS_OPEN)
59 // field changed by user
60 .setAssignee("arthur")
63 new UpdateConflictResolver().resolve(issue, mapper);
65 ArgumentCaptor<IssueDto> argument = ArgumentCaptor.forClass(IssueDto.class);
66 verify(mapper).update(argument.capture());
67 IssueDto updatedIssue = argument.getValue();
68 assertThat(updatedIssue.getKee()).isEqualTo("ABCDE");
69 assertThat(updatedIssue.getAssignee()).isEqualTo("arthur");
73 public void should_keep_changes_made_by_user() {
74 DefaultIssue issue = new DefaultIssue()
76 .setRuleKey(RuleKey.of("squid", "AvoidCycles"))
77 .setComponentKey("struts:org.apache.struts.Action")
80 // Before starting scan
81 issue.setAssignee(null);
82 issue.setActionPlanKey("PLAN-1");
83 issue.setCreationDate(DateUtils.parseDate("2012-01-01"));
84 issue.setUpdateDate(DateUtils.parseDate("2012-02-02"));
88 issue.setSeverity(Severity.BLOCKER);
89 issue.setManualSeverity(false);
90 issue.setAuthorLogin("simon");
91 issue.setChecksum("CHECKSUM-ABCDE");
92 issue.setResolution(null);
93 issue.setStatus(Issue.STATUS_REOPENED);
95 // Issue as seen and changed by end-user
96 IssueDto dbIssue = new IssueDto()
99 .setRuleKey("squid", "AvoidCycles")
100 .setComponentUuid("100")
101 .setComponentKey("struts:org.apache.struts.Action")
103 .setResolution(Issue.RESOLUTION_FALSE_POSITIVE)
104 .setStatus(Issue.STATUS_RESOLVED)
105 .setAssignee("arthur")
106 .setActionPlanKey("PLAN-2")
107 .setSeverity(Severity.MAJOR)
108 .setManualSeverity(false);
110 new UpdateConflictResolver().mergeFields(dbIssue, issue);
112 assertThat(issue.key()).isEqualTo("ABCDE");
113 assertThat(issue.componentKey()).isEqualTo("struts:org.apache.struts.Action");
116 assertThat(issue.line()).isEqualTo(200);
117 assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
118 assertThat(issue.manualSeverity()).isFalse();
121 assertThat(issue.assignee()).isEqualTo("arthur");
122 assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE);
123 assertThat(issue.status()).isEqualTo(Issue.STATUS_RESOLVED);
124 assertThat(issue.actionPlanKey()).isEqualTo("PLAN-2");
128 public void severity_changed_by_user_should_be_kept() {
129 DefaultIssue issue = new DefaultIssue()
131 .setRuleKey(RuleKey.of("squid", "AvoidCycles"))
132 .setComponentKey("struts:org.apache.struts.Action")
134 .setStatus(Issue.STATUS_OPEN);
137 issue.setSeverity(Severity.BLOCKER);
138 issue.setManualSeverity(false);
140 // Issue as seen and changed by end-user
141 IssueDto dbIssue = new IssueDto()
143 .setStatus(Issue.STATUS_OPEN)
144 .setSeverity(Severity.INFO)
145 .setManualSeverity(true);
147 new UpdateConflictResolver().mergeFields(dbIssue, issue);
149 assertThat(issue.severity()).isEqualTo(Severity.INFO);
150 assertThat(issue.manualSeverity()).isTrue();