]> source.dussan.org Git - sonarqube.git/blob
efac6c783a325ff1cb29355c1bec948c2a5bac05
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.collect.ImmutableList;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Test;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
28 import org.sonar.ce.task.projectanalysis.filemove.MovedFilesRepository;
29 import org.sonar.core.issue.DefaultIssue;
30 import org.sonar.core.issue.tracking.Input;
31 import org.sonar.db.DbClient;
32
33 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.reset;
37 import static org.mockito.Mockito.verifyZeroInteractions;
38 import static org.mockito.Mockito.when;
39
40 public class ClosedIssuesInputFactoryTest {
41   private ComponentIssuesLoader issuesLoader = mock(ComponentIssuesLoader.class);
42   private DbClient dbClient = mock(DbClient.class);
43   private MovedFilesRepository movedFilesRepository = mock(MovedFilesRepository.class);
44   private ClosedIssuesInputFactory underTest = new ClosedIssuesInputFactory(issuesLoader, dbClient, movedFilesRepository);
45
46   @Test
47   public void underTest_returns_inputFactory_loading_closed_issues_only_when_getIssues_is_called() {
48     String componentUuid = randomAlphanumeric(12);
49     ReportComponent component = ReportComponent.builder(Component.Type.FILE, 1).setUuid(componentUuid).build();
50     when(movedFilesRepository.getOriginalFile(component)).thenReturn(Optional.empty());
51
52     Input<DefaultIssue> input = underTest.create(component);
53
54     verifyZeroInteractions(dbClient, issuesLoader);
55
56     List<DefaultIssue> issues = ImmutableList.of(new DefaultIssue(), new DefaultIssue());
57     when(issuesLoader.loadClosedIssues(componentUuid)).thenReturn(issues);
58
59     assertThat(input.getIssues()).isSameAs(issues);
60   }
61
62   @Test
63   public void underTest_returns_inputFactory_loading_closed_issues_from_moved_component_when_present() {
64     String componentUuid = randomAlphanumeric(12);
65     String originalComponentUuid = randomAlphanumeric(12);
66     ReportComponent component = ReportComponent.builder(Component.Type.FILE, 1).setUuid(componentUuid).build();
67     when(movedFilesRepository.getOriginalFile(component))
68       .thenReturn(Optional.of(new MovedFilesRepository.OriginalFile(originalComponentUuid, randomAlphanumeric(2))));
69
70     Input<DefaultIssue> input = underTest.create(component);
71
72     verifyZeroInteractions(dbClient, issuesLoader);
73
74     List<DefaultIssue> issues = ImmutableList.of();
75     when(issuesLoader.loadClosedIssues(originalComponentUuid)).thenReturn(issues);
76
77     assertThat(input.getIssues()).isSameAs(issues);
78   }
79
80   @Test
81   public void underTest_returns_inputFactory_which_caches_loaded_issues() {
82     String componentUuid = randomAlphanumeric(12);
83     ReportComponent component = ReportComponent.builder(Component.Type.FILE, 1).setUuid(componentUuid).build();
84     when(movedFilesRepository.getOriginalFile(component)).thenReturn(Optional.empty());
85
86     Input<DefaultIssue> input = underTest.create(component);
87
88     verifyZeroInteractions(dbClient, issuesLoader);
89
90     List<DefaultIssue> issues = ImmutableList.of(new DefaultIssue());
91     when(issuesLoader.loadClosedIssues(componentUuid)).thenReturn(issues);
92
93     assertThat(input.getIssues()).isSameAs(issues);
94
95     reset(issuesLoader);
96
97     assertThat(input.getIssues()).isSameAs(issues);
98     verifyZeroInteractions(issuesLoader);
99   }
100 }