]> source.dussan.org Git - sonarqube.git/blob
e0d40c64aa4d0e7931e2a7e97876600655155ced
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.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.issue.IssueDto;
30 import org.sonar.db.issue.IssueMapper;
31
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;
36
37 public class UpdateConflictResolverTest {
38
39   @Test
40   public void should_reload_issue_and_resolve_conflict() {
41     DefaultIssue issue = new DefaultIssue()
42       .setKey("ABCDE")
43       .setRuleKey(RuleKey.of("squid", "AvoidCycles"))
44       .setComponentKey("struts:org.apache.struts.Action")
45       .setNew(false)
46       .setStatus(Issue.STATUS_OPEN);
47
48     // Issue as seen and changed by end-user
49     IssueMapper mapper = mock(IssueMapper.class);
50     when(mapper.selectByKey("ABCDE")).thenReturn(
51       new IssueDto()
52         .setKee("ABCDE")
53         .setRuleId(10)
54         .setRuleKey("squid", "AvoidCycles")
55         .setComponentKey("struts:org.apache.struts.Action")
56         .setLine(10)
57         .setStatus(Issue.STATUS_OPEN)
58
59         // field changed by user
60         .setAssignee("arthur")
61       );
62
63     new UpdateConflictResolver().resolve(issue, mapper);
64
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");
70   }
71
72   @Test
73   public void should_keep_changes_made_by_user() {
74     DefaultIssue issue = new DefaultIssue()
75       .setKey("ABCDE")
76       .setRuleKey(RuleKey.of("squid", "AvoidCycles"))
77       .setComponentKey("struts:org.apache.struts.Action")
78       .setNew(false);
79
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"));
85
86     // Changed by scan
87     issue.setLine(200);
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);
94
95     // Issue as seen and changed by end-user
96     IssueDto dbIssue = new IssueDto()
97       .setKee("ABCDE")
98       .setRuleId(10)
99       .setRuleKey("squid", "AvoidCycles")
100       .setComponentUuid("100")
101       .setComponentKey("struts:org.apache.struts.Action")
102       .setLine(10)
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);
109
110     new UpdateConflictResolver().mergeFields(dbIssue, issue);
111
112     assertThat(issue.key()).isEqualTo("ABCDE");
113     assertThat(issue.componentKey()).isEqualTo("struts:org.apache.struts.Action");
114
115     // Scan wins on :
116     assertThat(issue.line()).isEqualTo(200);
117     assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
118     assertThat(issue.manualSeverity()).isFalse();
119
120     // User wins on :
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");
125   }
126
127   @Test
128   public void severity_changed_by_user_should_be_kept() {
129     DefaultIssue issue = new DefaultIssue()
130       .setKey("ABCDE")
131       .setRuleKey(RuleKey.of("squid", "AvoidCycles"))
132       .setComponentKey("struts:org.apache.struts.Action")
133       .setNew(false)
134       .setStatus(Issue.STATUS_OPEN);
135
136     // Changed by scan
137     issue.setSeverity(Severity.BLOCKER);
138     issue.setManualSeverity(false);
139
140     // Issue as seen and changed by end-user
141     IssueDto dbIssue = new IssueDto()
142       .setKee("ABCDE")
143       .setStatus(Issue.STATUS_OPEN)
144       .setSeverity(Severity.INFO)
145       .setManualSeverity(true);
146
147     new UpdateConflictResolver().mergeFields(dbIssue, issue);
148
149     assertThat(issue.severity()).isEqualTo(Severity.INFO);
150     assertThat(issue.manualSeverity()).isTrue();
151   }
152 }