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.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;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.verifyZeroInteractions;
35 import static org.mockito.Mockito.when;
37 public class IssueStatusCopierTest {
39 private ResolvedShortBranchIssuesLoader resolvedShortBranchIssuesLoader;
41 private IssueLifecycle issueLifecycle;
43 private Component component;
45 private SimpleTracker<DefaultIssue, ShortBranchIssue> tracker = new SimpleTracker<>();
46 private IssueStatusCopier copier;
50 MockitoAnnotations.initMocks(this);
51 copier = new IssueStatusCopier(resolvedShortBranchIssuesLoader, tracker, issueLifecycle);
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));
60 verify(resolvedShortBranchIssuesLoader).create(component);
61 verifyZeroInteractions(issueLifecycle);
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());
70 verify(resolvedShortBranchIssuesLoader).create(component);
71 verifyZeroInteractions(issueLifecycle);
75 public void update_status_on_matches() {
76 ShortBranchIssue shortBranchIssue = newShortBranchIssue(createIssue("issue1", "rule1"));
77 DefaultIssue newIssue = createIssue("issue2", "rule1");
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());
84 private static DefaultIssue createIssue(String key, String ruleKey) {
85 DefaultIssue issue = new DefaultIssue();
87 issue.setRuleKey(RuleKey.of("repo", ruleKey));
88 issue.setMessage("msg");
93 private ShortBranchIssue newShortBranchIssue(DefaultIssue i) {
94 return new ShortBranchIssue(i.line(), i.message(), i.getLineHash(), i.ruleKey(), i.status(), i.resolution());