]> source.dussan.org Git - sonarqube.git/blob
9716ed4dbca7aac112f0c22efdfbd65bac117727
[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.Collections;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.core.issue.DefaultIssue;
29 import org.sonar.core.issue.ShortBranchIssue;
30 import org.sonar.core.issue.tracking.SimpleTracker;
31 import org.sonar.server.computation.task.projectanalysis.component.Component;
32
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.verifyZeroInteractions;
35 import static org.mockito.Mockito.when;
36
37 public class IssueStatusCopierTest {
38   @Mock
39   private ResolvedShortBranchIssuesLoader resolvedShortBranchIssuesLoader;
40   @Mock
41   private IssueLifecycle issueLifecycle;
42   @Mock
43   private Component component;
44
45   private SimpleTracker<DefaultIssue, ShortBranchIssue> tracker = new SimpleTracker<>();
46   private IssueStatusCopier copier;
47
48   @Before
49   public void setUp() {
50     MockitoAnnotations.initMocks(this);
51     copier = new IssueStatusCopier(resolvedShortBranchIssuesLoader, tracker, issueLifecycle);
52   }
53
54   @Test
55   public void do_nothing_if_no_match() {
56     when(resolvedShortBranchIssuesLoader.create(component)).thenReturn(Collections.emptyList());
57     DefaultIssue i = createIssue("issue1", "rule1");
58     copier.updateStatus(component, Collections.singleton(i));
59
60     verify(resolvedShortBranchIssuesLoader).create(component);
61     verifyZeroInteractions(issueLifecycle);
62   }
63
64   @Test
65   public void do_nothing_if_no_new_issue() {
66     DefaultIssue i = createIssue("issue1", "rule1");
67     when(resolvedShortBranchIssuesLoader.create(component)).thenReturn(Collections.singleton(newShortBranchIssue(i)));
68     copier.updateStatus(component, Collections.emptyList());
69
70     verify(resolvedShortBranchIssuesLoader).create(component);
71     verifyZeroInteractions(issueLifecycle);
72   }
73
74   @Test
75   public void update_status_on_matches() {
76     ShortBranchIssue shortBranchIssue = newShortBranchIssue(createIssue("issue1", "rule1"));
77     DefaultIssue newIssue = createIssue("issue2", "rule1");
78
79     when(resolvedShortBranchIssuesLoader.create(component)).thenReturn(Collections.singleton(shortBranchIssue));
80     copier.updateStatus(component, Collections.singleton(newIssue));
81     verify(issueLifecycle).copyResolution(newIssue, shortBranchIssue.getStatus(), shortBranchIssue.getResolution());
82   }
83
84   private static DefaultIssue createIssue(String key, String ruleKey) {
85     DefaultIssue issue = new DefaultIssue();
86     issue.setKey(key);
87     issue.setRuleKey(RuleKey.of("repo", ruleKey));
88     issue.setMessage("msg");
89     issue.setLine(1);
90     return issue;
91   }
92
93   private ShortBranchIssue newShortBranchIssue(DefaultIssue i) {
94     return new ShortBranchIssue(i.line(), i.message(), i.getLineHash(), i.ruleKey(), i.status(), i.resolution());
95   }
96 }