3 * Copyright (C) 2009-2022 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.ce.task.projectanalysis.issue;
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;
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;
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);
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());
52 Input<DefaultIssue> input = underTest.create(component);
54 verifyZeroInteractions(dbClient, issuesLoader);
56 List<DefaultIssue> issues = ImmutableList.of(new DefaultIssue(), new DefaultIssue());
57 when(issuesLoader.loadClosedIssues(componentUuid)).thenReturn(issues);
59 assertThat(input.getIssues()).isSameAs(issues);
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))));
70 Input<DefaultIssue> input = underTest.create(component);
72 verifyZeroInteractions(dbClient, issuesLoader);
74 List<DefaultIssue> issues = ImmutableList.of();
75 when(issuesLoader.loadClosedIssues(originalComponentUuid)).thenReturn(issues);
77 assertThat(input.getIssues()).isSameAs(issues);
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());
86 Input<DefaultIssue> input = underTest.create(component);
88 verifyZeroInteractions(dbClient, issuesLoader);
90 List<DefaultIssue> issues = ImmutableList.of(new DefaultIssue());
91 when(issuesLoader.loadClosedIssues(componentUuid)).thenReturn(issues);
93 assertThat(input.getIssues()).isSameAs(issues);
97 assertThat(input.getIssues()).isSameAs(issues);
98 verifyZeroInteractions(issuesLoader);