]> source.dussan.org Git - sonarqube.git/blob
386bf4a223158a3eaa88ff32c2fb544ea88408a4
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.ce.task.projectanalysis.issue;
21
22 import com.google.common.base.Optional;
23 import java.util.Date;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.mockito.ArgumentCaptor;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
31 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
32 import org.sonar.core.issue.DefaultIssue;
33 import org.sonar.core.issue.IssueChangeContext;
34 import org.sonar.server.issue.IssueFieldsSetter;
35
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.ArgumentMatchers.any;
38 import static org.mockito.ArgumentMatchers.eq;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoMoreInteractions;
42 import static org.mockito.Mockito.verifyZeroInteractions;
43 import static org.mockito.Mockito.when;
44
45 public class MovedIssueVisitorTest {
46   private static final long ANALYSIS_DATE = 894521;
47   private static final String FILE_UUID = "file uuid";
48   private static final Component FILE = ReportComponent.builder(Component.Type.FILE, 1)
49     .setKey("key_1")
50     .setPublicKey("public_key_1")
51     .setUuid(FILE_UUID)
52     .build();
53
54   @org.junit.Rule
55   public ExpectedException expectedException = ExpectedException.none();
56   @org.junit.Rule
57   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
58
59   private MovedFilesRepository movedFilesRepository = mock(MovedFilesRepository.class);
60   private MovedIssueVisitor underTest = new MovedIssueVisitor(analysisMetadataHolder, movedFilesRepository, new IssueFieldsSetter());
61
62   @Before
63   public void setUp() throws Exception {
64     analysisMetadataHolder.setAnalysisDate(ANALYSIS_DATE);
65     when(movedFilesRepository.getOriginalFile(any(Component.class)))
66       .thenReturn(Optional.absent());
67   }
68
69   @Test
70   public void onIssue_does_not_alter_issue_if_component_is_not_a_file() {
71     DefaultIssue issue = mock(DefaultIssue.class);
72     underTest.onIssue(ReportComponent.builder(Component.Type.DIRECTORY, 1).build(), issue);
73
74     verifyZeroInteractions(issue);
75   }
76
77   @Test
78   public void onIssue_does_not_alter_issue_if_component_file_but_issue_has_the_same_component_uuid() {
79     DefaultIssue issue = mockIssue(FILE_UUID);
80     underTest.onIssue(FILE, issue);
81
82     verify(issue).componentUuid();
83     verifyNoMoreInteractions(issue);
84   }
85
86   @Test
87   public void onIssue_throws_ISE_if_issue_has_different_component_uuid_but_component_has_no_original_file() {
88     DefaultIssue issue = mockIssue("other component uuid");
89     when(issue.toString()).thenReturn("[bad issue, bad!]");
90
91     expectedException.expect(IllegalStateException.class);
92     expectedException.expectMessage("Issue [bad issue, bad!] for component ReportComponent{ref=1, key='key_1', type=FILE} " +
93       "has a different component key but no original file exist in MovedFilesRepository");
94
95     underTest.onIssue(FILE, issue);
96   }
97
98   @Test
99   public void onIssue_throws_ISE_if_issue_has_different_component_uuid_from_component_but_it_is_not_the_one_of_original_file() {
100     DefaultIssue issue = mockIssue("other component uuid");
101     when(issue.toString()).thenReturn("[bad issue, bad!]");
102     when(movedFilesRepository.getOriginalFile(FILE))
103       .thenReturn(Optional.of(new MovedFilesRepository.OriginalFile(6451, "original uuid", "original key")));
104
105     expectedException.expect(IllegalStateException.class);
106     expectedException.expectMessage("Issue [bad issue, bad!] doesn't belong to file original uuid registered as original " +
107       "file of current file ReportComponent{ref=1, key='key_1', type=FILE}");
108
109     underTest.onIssue(FILE, issue);
110   }
111
112   @Test
113   public void onIssue_update_component_and_module_fields_to_component_and_flag_issue_has_changed() {
114     MovedFilesRepository.OriginalFile originalFile = new MovedFilesRepository.OriginalFile(6451, "original uuid", "original key");
115     DefaultIssue issue = mockIssue(originalFile.getUuid());
116     when(movedFilesRepository.getOriginalFile(FILE))
117       .thenReturn(Optional.of(originalFile));
118
119     underTest.onIssue(FILE, issue);
120
121     verify(issue).setComponentUuid(FILE.getUuid());
122     verify(issue).setComponentKey(FILE.getKey());
123     verify(issue).setModuleUuid(null);
124     verify(issue).setModuleUuidPath(null);
125     verify(issue).setChanged(true);
126     ArgumentCaptor<IssueChangeContext> issueChangeContextCaptor = ArgumentCaptor.forClass(IssueChangeContext.class);
127     verify(issue).setFieldChange(issueChangeContextCaptor.capture(), eq("file"), eq(originalFile.getUuid()), eq(FILE.getUuid()));
128     assertThat(issueChangeContextCaptor.getValue().date()).isEqualTo(new Date(ANALYSIS_DATE));
129     assertThat(issueChangeContextCaptor.getValue().userUuid()).isNull();
130     assertThat(issueChangeContextCaptor.getValue().scan()).isFalse();
131   }
132
133   private DefaultIssue mockIssue(String fileUuid) {
134     DefaultIssue issue = mock(DefaultIssue.class);
135     when(issue.componentUuid()).thenReturn(fileUuid);
136     return issue;
137   }
138 }