3 * Copyright (C) 2009-2023 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.ce.task.projectanalysis.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.DbSession;
30 import org.sonar.db.issue.IssueDao;
31 import org.sonar.db.issue.IssueDto;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.verify;
37 import static org.sonar.api.issue.Issue.STATUS_OPEN;
38 import static org.sonar.api.rules.RuleType.CODE_SMELL;
40 public class UpdateConflictResolverTest {
43 public void should_reload_issue_and_resolve_conflict() {
44 DefaultIssue issue = new DefaultIssue()
47 .setRuleKey(RuleKey.of("java", "AvoidCycles"))
49 .setComponentUuid("U2")
51 .setStatus(STATUS_OPEN);
53 // Issue as seen and changed by end-user
54 IssueDao issueDao = mock(IssueDao.class);
55 DbSession dbSession = mock(DbSession.class);
56 IssueDto issueDto = new IssueDto()
59 .setRuleUuid("uuid-10")
60 .setRuleKey("java", "AvoidCycles")
62 .setComponentUuid("U2")
64 .setStatus(STATUS_OPEN)
66 // field changed by user
67 .setAssigneeUuid("arthur-uuid");
69 new UpdateConflictResolver().resolve(issue, issueDto, issueDao, dbSession);
71 ArgumentCaptor<IssueDto> argument = ArgumentCaptor.forClass(IssueDto.class);
72 verify(issueDao).update(any(), argument.capture());
73 IssueDto updatedIssue = argument.getValue();
74 assertThat(updatedIssue.getKee()).isEqualTo("ABCDE");
75 assertThat(updatedIssue.getAssigneeUuid()).isEqualTo("arthur-uuid");
79 public void should_keep_changes_made_by_user() {
80 DefaultIssue issue = new DefaultIssue()
82 .setRuleKey(RuleKey.of("java", "AvoidCycles"))
83 .setComponentKey("struts:org.apache.struts.Action")
86 // Before starting scan
87 issue.setAssigneeUuid(null);
88 issue.setCreationDate(DateUtils.parseDate("2012-01-01"));
89 issue.setUpdateDate(DateUtils.parseDate("2012-02-02"));
93 issue.setSeverity(Severity.BLOCKER);
94 issue.setManualSeverity(false);
95 issue.setAuthorLogin("simon");
96 issue.setChecksum("CHECKSUM-ABCDE");
97 issue.setResolution(null);
98 issue.setStatus(Issue.STATUS_REOPENED);
100 // Issue as seen and changed by end-user
101 IssueDto dbIssue = new IssueDto()
103 .setRuleUuid("uuid-10")
104 .setRuleKey("java", "AvoidCycles")
105 .setComponentUuid("100")
106 .setComponentKey("struts:org.apache.struts.Action")
108 .setResolution(Issue.RESOLUTION_FALSE_POSITIVE)
109 .setStatus(Issue.STATUS_RESOLVED)
110 .setAssigneeUuid("arthur")
111 .setSeverity(Severity.MAJOR)
112 .setManualSeverity(false);
114 new UpdateConflictResolver().mergeFields(dbIssue, issue);
116 assertThat(issue.key()).isEqualTo("ABCDE");
117 assertThat(issue.componentKey()).isEqualTo("struts:org.apache.struts.Action");
120 assertThat(issue.line()).isEqualTo(200);
121 assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
122 assertThat(issue.manualSeverity()).isFalse();
125 assertThat(issue.assignee()).isEqualTo("arthur");
126 assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE);
127 assertThat(issue.status()).isEqualTo(Issue.STATUS_RESOLVED);
131 public void severity_changed_by_user_should_be_kept() {
132 DefaultIssue issue = new DefaultIssue()
134 .setRuleKey(RuleKey.of("java", "AvoidCycles"))
135 .setComponentKey("struts:org.apache.struts.Action")
137 .setStatus(STATUS_OPEN);
140 issue.setSeverity(Severity.BLOCKER);
141 issue.setManualSeverity(false);
143 // Issue as seen and changed by end-user
144 IssueDto dbIssue = new IssueDto()
146 .setStatus(STATUS_OPEN)
147 .setSeverity(Severity.INFO)
148 .setManualSeverity(true);
150 new UpdateConflictResolver().mergeFields(dbIssue, issue);
152 assertThat(issue.severity()).isEqualTo(Severity.INFO);
153 assertThat(issue.manualSeverity()).isTrue();