]> source.dussan.org Git - sonarqube.git/blob
d244d44cb6a3469cf4df0a9b11f248358f083bf0
[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 com.google.common.collect.ImmutableMap;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import javax.annotation.Nullable;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.sonar.api.issue.Issue;
33 import org.sonar.api.rule.RuleKey;
34 import org.sonar.core.issue.DefaultIssue;
35 import org.sonar.core.issue.ShortBranchIssue;
36 import org.sonar.core.issue.tracking.SimpleTracker;
37 import org.sonar.server.computation.task.projectanalysis.component.Component;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Matchers.anyListOf;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.verifyZeroInteractions;
43 import static org.mockito.Mockito.when;
44
45 public class ShortBranchIssueStatusCopierTest {
46   @Mock
47   private ShortBranchIssuesLoader resolvedShortBranchIssuesLoader;
48   @Mock
49   private IssueLifecycle issueLifecycle;
50   @Mock
51   private Component component;
52
53   private SimpleTracker<DefaultIssue, ShortBranchIssue> tracker = new SimpleTracker<>();
54   private ShortBranchIssueStatusCopier copier;
55
56   @Before
57   public void setUp() {
58     MockitoAnnotations.initMocks(this);
59     copier = new ShortBranchIssueStatusCopier(resolvedShortBranchIssuesLoader, tracker, issueLifecycle);
60   }
61
62   @Test
63   public void do_nothing_if_no_match() {
64     when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Collections.emptyList());
65     DefaultIssue i = createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null);
66     copier.updateStatus(component, Collections.singleton(i));
67
68     verify(resolvedShortBranchIssuesLoader).loadCandidateIssuesForMergingInTargetBranch(component);
69     verifyZeroInteractions(issueLifecycle);
70   }
71
72   @Test
73   public void do_nothing_if_no_new_issue() {
74     DefaultIssue i = createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null);
75     when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Collections.singleton(newShortBranchIssue(i)));
76     copier.updateStatus(component, Collections.emptyList());
77
78     verify(resolvedShortBranchIssuesLoader).loadCandidateIssuesForMergingInTargetBranch(component);
79     verifyZeroInteractions(issueLifecycle);
80   }
81
82   @Test
83   public void update_status_on_matches() {
84     DefaultIssue issue1 = createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null);
85     ShortBranchIssue shortBranchIssue = newShortBranchIssue(issue1);
86     DefaultIssue newIssue = createIssue("issue2", "rule1", Issue.STATUS_OPEN, null);
87
88     when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Collections.singleton(shortBranchIssue));
89     when(resolvedShortBranchIssuesLoader.loadDefaultIssuesWithChanges(anyListOf(ShortBranchIssue.class))).thenReturn(ImmutableMap.of(shortBranchIssue, issue1));
90     copier.updateStatus(component, Collections.singleton(newIssue));
91     ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class);
92     verify(resolvedShortBranchIssuesLoader).loadDefaultIssuesWithChanges(captor.capture());
93     assertThat(captor.getValue()).containsOnly(shortBranchIssue);
94     verify(issueLifecycle).mergeIssueFromShortLivingBranch(newIssue, issue1);
95   }
96
97   @Test
98   public void prefer_resolved_issues() {
99     ShortBranchIssue shortBranchIssue1 = newShortBranchIssue(createIssue("issue1", "rule1", Issue.STATUS_CONFIRMED, null));
100     ShortBranchIssue shortBranchIssue2 = newShortBranchIssue(createIssue("issue2", "rule1", Issue.STATUS_CONFIRMED, null));
101     DefaultIssue issue3 = createIssue("issue3", "rule1", Issue.STATUS_RESOLVED, Issue.RESOLUTION_FALSE_POSITIVE);
102     ShortBranchIssue shortBranchIssue3 = newShortBranchIssue(issue3);
103     DefaultIssue newIssue = createIssue("newIssue", "rule1", Issue.STATUS_OPEN, null);
104
105     when(resolvedShortBranchIssuesLoader.loadCandidateIssuesForMergingInTargetBranch(component)).thenReturn(Arrays.asList(shortBranchIssue1, shortBranchIssue2, shortBranchIssue3));
106     when(resolvedShortBranchIssuesLoader.loadDefaultIssuesWithChanges(anyListOf(ShortBranchIssue.class))).thenReturn(ImmutableMap.of(shortBranchIssue3, issue3));
107     copier.updateStatus(component, Collections.singleton(newIssue));
108     verify(issueLifecycle).mergeIssueFromShortLivingBranch(newIssue, issue3);
109   }
110
111   private static DefaultIssue createIssue(String key, String ruleKey, String status, @Nullable String resolution) {
112     DefaultIssue issue = new DefaultIssue();
113     issue.setKey(key);
114     issue.setRuleKey(RuleKey.of("repo", ruleKey));
115     issue.setMessage("msg");
116     issue.setLine(1);
117     issue.setStatus(status);
118     issue.setResolution(resolution);
119     return issue;
120   }
121
122   private ShortBranchIssue newShortBranchIssue(DefaultIssue i) {
123     return new ShortBranchIssue(i.key(), i.line(), i.message(), i.getLineHash(), i.ruleKey(), i.status());
124   }
125 }