]> source.dussan.org Git - sonarqube.git/blob
c5948cfc60b2eab12b43faa745275dd5bb0c9f22
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
21
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;
35
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.verifyZeroInteractions;
38 import static org.mockito.Mockito.when;
39
40 public class ShortBranchIssueStatusCopierTest {
41   @Mock
42   private ShortBranchIssuesLoader resolvedShortBranchIssuesLoader;
43   @Mock
44   private IssueLifecycle issueLifecycle;
45   @Mock
46   private Component component;
47
48   private SimpleTracker<DefaultIssue, ShortBranchIssue> tracker = new SimpleTracker<>();
49   private ShortBranchIssueStatusCopier copier;
50
51   @Before
52   public void setUp() {
53     MockitoAnnotations.initMocks(this);
54     copier = new ShortBranchIssueStatusCopier(resolvedShortBranchIssuesLoader, tracker, issueLifecycle);
55   }
56
57   @Test
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));
62
63     verify(resolvedShortBranchIssuesLoader).loadCandidateIssuesForMergingInTargetBranch(component);
64     verifyZeroInteractions(issueLifecycle);
65   }
66
67   @Test
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());
72
73     verify(resolvedShortBranchIssuesLoader).loadCandidateIssuesForMergingInTargetBranch(component);
74     verifyZeroInteractions(issueLifecycle);
75   }
76
77   @Test
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);
81
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());
85   }
86
87   @Test
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);
93
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);
97   }
98
99   private static DefaultIssue createIssue(String key, String ruleKey, String status, @Nullable String resolution) {
100     DefaultIssue issue = new DefaultIssue();
101     issue.setKey(key);
102     issue.setRuleKey(RuleKey.of("repo", ruleKey));
103     issue.setMessage("msg");
104     issue.setLine(1);
105     issue.setStatus(status);
106     issue.setResolution(resolution);
107     return issue;
108   }
109
110   private ShortBranchIssue newShortBranchIssue(DefaultIssue i) {
111     return new ShortBranchIssue(i.line(), i.message(), i.getLineHash(), i.ruleKey(), i.status(), i.resolution());
112   }
113 }