]> source.dussan.org Git - sonarqube.git/blob
19ebc222006dd6c50d49d25ddf39cd20f62feb5a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.ce.task.projectanalysis.issue;
21
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;
32
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;
39
40 public class UpdateConflictResolverTest {
41
42   @Test
43   public void should_reload_issue_and_resolve_conflict() {
44     DefaultIssue issue = new DefaultIssue()
45       .setKey("ABCDE")
46       .setType(CODE_SMELL)
47       .setRuleKey(RuleKey.of("java", "AvoidCycles"))
48       .setProjectUuid("U1")
49       .setComponentUuid("U2")
50       .setNew(false)
51       .setStatus(STATUS_OPEN);
52
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()
57       .setKee("ABCDE")
58       .setType(CODE_SMELL)
59       .setRuleUuid("uuid-10")
60       .setRuleKey("java", "AvoidCycles")
61       .setProjectUuid("U1")
62       .setComponentUuid("U2")
63       .setLine(10)
64       .setStatus(STATUS_OPEN)
65
66       // field changed by user
67       .setAssigneeUuid("arthur-uuid");
68
69     new UpdateConflictResolver().resolve(issue, issueDto, issueDao, dbSession);
70
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");
76   }
77
78   @Test
79   public void should_keep_changes_made_by_user() {
80     DefaultIssue issue = new DefaultIssue()
81       .setKey("ABCDE")
82       .setRuleKey(RuleKey.of("java", "AvoidCycles"))
83       .setComponentKey("struts:org.apache.struts.Action")
84       .setNew(false);
85
86     // Before starting scan
87     issue.setAssigneeUuid(null);
88     issue.setCreationDate(DateUtils.parseDate("2012-01-01"));
89     issue.setUpdateDate(DateUtils.parseDate("2012-02-02"));
90
91     // Changed by scan
92     issue.setLine(200);
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);
99
100     // Issue as seen and changed by end-user
101     IssueDto dbIssue = new IssueDto()
102       .setKee("ABCDE")
103       .setRuleUuid("uuid-10")
104       .setRuleKey("java", "AvoidCycles")
105       .setComponentUuid("100")
106       .setComponentKey("struts:org.apache.struts.Action")
107       .setLine(10)
108       .setResolution(Issue.RESOLUTION_FALSE_POSITIVE)
109       .setStatus(Issue.STATUS_RESOLVED)
110       .setAssigneeUuid("arthur")
111       .setSeverity(Severity.MAJOR)
112       .setManualSeverity(false);
113
114     new UpdateConflictResolver().mergeFields(dbIssue, issue);
115
116     assertThat(issue.key()).isEqualTo("ABCDE");
117     assertThat(issue.componentKey()).isEqualTo("struts:org.apache.struts.Action");
118
119     // Scan wins on :
120     assertThat(issue.line()).isEqualTo(200);
121     assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
122     assertThat(issue.manualSeverity()).isFalse();
123
124     // User wins on :
125     assertThat(issue.assignee()).isEqualTo("arthur");
126     assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE);
127     assertThat(issue.status()).isEqualTo(Issue.STATUS_RESOLVED);
128   }
129
130   @Test
131   public void severity_changed_by_user_should_be_kept() {
132     DefaultIssue issue = new DefaultIssue()
133       .setKey("ABCDE")
134       .setRuleKey(RuleKey.of("java", "AvoidCycles"))
135       .setComponentKey("struts:org.apache.struts.Action")
136       .setNew(false)
137       .setStatus(STATUS_OPEN);
138
139     // Changed by scan
140     issue.setSeverity(Severity.BLOCKER);
141     issue.setManualSeverity(false);
142
143     // Issue as seen and changed by end-user
144     IssueDto dbIssue = new IssueDto()
145       .setKee("ABCDE")
146       .setStatus(STATUS_OPEN)
147       .setSeverity(Severity.INFO)
148       .setManualSeverity(true);
149
150     new UpdateConflictResolver().mergeFields(dbIssue, issue);
151
152     assertThat(issue.severity()).isEqualTo(Severity.INFO);
153     assertThat(issue.manualSeverity()).isTrue();
154   }
155 }