3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
22 import java.util.Arrays;
23 import java.util.Collections;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.sonar.api.issue.Issue;
30 import org.sonar.api.rule.RuleKey;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.core.issue.ShortBranchIssue;
33 import org.sonar.core.issue.tracking.SimpleTracker;
34 import org.sonar.server.computation.task.projectanalysis.component.Component;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.verifyZeroInteractions;
38 import static org.mockito.Mockito.when;
40 public class ShortBranchIssueStatusCopierTest {
42 private ShortBranchIssuesLoader resolvedShortBranchIssuesLoader;
44 private IssueLifecycle issueLifecycle;
46 private Component component;
48 private SimpleTracker<DefaultIssue, ShortBranchIssue> tracker = new SimpleTracker<>();
49 private ShortBranchIssueStatusCopier copier;
53 MockitoAnnotations.initMocks(this);
54 copier = new ShortBranchIssueStatusCopier(resolvedShortBranchIssuesLoader, tracker, issueLifecycle);
58 public void do_nothing_if_no_match() {
59 when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Collections.emptyList());
60 DefaultIssue i = createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null);
61 copier.updateStatus(component, Collections.singleton(i));
63 verify(resolvedShortBranchIssuesLoader).loadCandidateIssuesForMergingInTargetBranch(component);
64 verifyZeroInteractions(issueLifecycle);
68 public void do_nothing_if_no_new_issue() {
69 DefaultIssue i = createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null);
70 when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Collections.singleton(newShortBranchIssue(i)));
71 copier.updateStatus(component, Collections.emptyList());
73 verify(resolvedShortBranchIssuesLoader).loadCandidateIssuesForMergingInTargetBranch(component);
74 verifyZeroInteractions(issueLifecycle);
78 public void update_status_on_matches() {
79 ShortBranchIssue shortBranchIssue = newShortBranchIssue(createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null));
80 DefaultIssue newIssue = createIssue("issue2", "rule1", Issue.STATUS_OPEN, null);
82 when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Collections.singleton(shortBranchIssue));
83 copier.updateStatus(component, Collections.singleton(newIssue));
84 verify(issueLifecycle).copyResolution(newIssue, shortBranchIssue.getStatus(), shortBranchIssue.getResolution());
88 public void prefer_resolved_issues() {
89 ShortBranchIssue shortBranchIssue1 = newShortBranchIssue(createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null));
90 ShortBranchIssue shortBranchIssue2 = newShortBranchIssue(createIssue("issue2", "rule1", Issue.STATUS_CONFIRMED, null));
91 ShortBranchIssue shortBranchIssue3 = newShortBranchIssue(createIssue("issue3", "rule1", Issue.STATUS_RESOLVED, Issue.RESOLUTION_FALSE_POSITIVE));
92 DefaultIssue newIssue = createIssue("newIssue", "rule1", Issue.STATUS_OPEN, null);
94 when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Arrays.asList(shortBranchIssue1, shortBranchIssue2, shortBranchIssue3));
95 copier.updateStatus(component, Collections.singleton(newIssue));
96 verify(issueLifecycle).copyResolution(newIssue, Issue.STATUS_RESOLVED, Issue.RESOLUTION_FALSE_POSITIVE);
99 private static DefaultIssue createIssue(String key, String ruleKey, String status, @Nullable String resolution) {
100 DefaultIssue issue = new DefaultIssue();
102 issue.setRuleKey(RuleKey.of("repo", ruleKey));
103 issue.setMessage("msg");
105 issue.setStatus(status);
106 issue.setResolution(resolution);
110 private ShortBranchIssue newShortBranchIssue(DefaultIssue i) {
111 return new ShortBranchIssue(i.line(), i.message(), i.getLineHash(), i.ruleKey(), i.status(), i.resolution());