]> source.dussan.org Git - sonarqube.git/blob
460f4e572447e1fe4231c32946694f743cbe4dbd
[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.server.issue.ws.pull;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.function.Consumer;
26 import java.util.stream.Collectors;
27 import java.util.stream.IntStream;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.issue.IssueDao;
32 import org.sonar.db.issue.IssueDto;
33 import org.sonar.db.issue.IssueQueryParams;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.ArgumentMatchers.anyInt;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40
41 public class PullActionIssuesRetrieverTest {
42
43   private final DbClient dbClient = mock(DbClient.class);
44   private final String projectUuid = "default-project-uuid";
45   private final String branchName = "master";
46   private final List<String> languages = List.of("java");
47   private final List<String> ruleRepositories = List.of("js-security", "java");
48   private final Long defaultChangedSince = 1_000_000L;
49
50   private final IssueQueryParams queryParams = new IssueQueryParams(projectUuid, branchName, languages, ruleRepositories, false,
51     defaultChangedSince);
52   private final IssueDao issueDao = mock(IssueDao.class);
53
54   @Before
55   public void before() {
56     when(dbClient.issueDao()).thenReturn(issueDao);
57   }
58
59   @Test
60   public void processIssuesByBatch_givenNoIssuesReturnedByDatabase_noIssuesConsumed() {
61     var pullActionIssuesRetriever = new PullActionIssuesRetriever(dbClient, queryParams);
62     when(issueDao.selectByBranch(any(), any(), anyInt()))
63       .thenReturn(List.of());
64     List<IssueDto> returnedDtos = new ArrayList<>();
65     Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
66
67     pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), Set.of(), listConsumer);
68
69     assertThat(returnedDtos).isEmpty();
70   }
71
72   @Test
73   public void processIssuesByBatch_givenThousandOneIssuesReturnedByDatabase_thousandOneIssuesConsumed() {
74     var pullActionIssuesRetriever = new PullActionIssuesRetriever(dbClient, queryParams);
75     List<IssueDto> thousandIssues = IntStream.rangeClosed(1, 1000).mapToObj(i -> new IssueDto().setKee(Integer.toString(i))).collect(Collectors.toList());
76     IssueDto singleIssue = new IssueDto().setKee("kee");
77     when(issueDao.selectByBranch(any(), any(), anyInt()))
78       .thenReturn(thousandIssues)
79       .thenReturn(List.of(singleIssue));
80     List<IssueDto> returnedDtos = new ArrayList<>();
81     Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
82
83     Set<String> thousandIssueUuidsSnapshot = thousandIssues.stream().map(IssueDto::getKee).collect(Collectors.toSet());
84     thousandIssueUuidsSnapshot.add(singleIssue.getKee());
85     pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), thousandIssueUuidsSnapshot, listConsumer);
86
87     assertThat(returnedDtos).hasSize(1001);
88   }
89
90   @Test
91   public void processIssuesByBatch_filter_out_duplicate_issue_entries() {
92     var pullActionIssuesRetriever = new PullActionIssuesRetriever(dbClient, queryParams);
93     IssueDto issue1 = new IssueDto().setKee("kee1");
94     IssueDto issue2 = new IssueDto().setKee("kee2");
95     List<IssueDto> issues = List.of(issue1, issue1, issue1, issue2);
96     when(issueDao.selectByBranch(any(), any(), anyInt()))
97       .thenReturn(issues);
98     List<IssueDto> returnedDtos = new ArrayList<>();
99     Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
100
101     Set<String> thousandIssueKeysSnapshot = issues.stream().map(IssueDto::getKee).collect(Collectors.toSet());
102     pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), thousandIssueKeysSnapshot, listConsumer);
103
104     assertThat(returnedDtos)
105       .hasSize(2)
106       .containsExactlyInAnyOrder(issue1, issue2);
107   }
108
109   @Test
110   public void processIssuesByBatch_correctly_processes_all_issues_regardless_of_creation_timestamp() {
111     var pullActionIssuesRetriever = new PullActionIssuesRetriever(dbClient, queryParams);
112     List<IssueDto> issuesWithSameCreationTimestamp = IntStream.rangeClosed(1, 100).mapToObj(i -> new IssueDto().setKee(Integer.toString(i)).setCreatedAt(100L)).collect(Collectors.toList());
113     when(issueDao.selectByBranch(any(), any(), anyInt()))
114       .thenReturn(issuesWithSameCreationTimestamp);
115     List<IssueDto> returnedDtos = new ArrayList<>();
116     Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
117
118     Set<String> issueKeysSnapshot = issuesWithSameCreationTimestamp.stream().map(IssueDto::getKee).collect(Collectors.toSet());
119     pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), issueKeysSnapshot, listConsumer);
120
121     assertThat(returnedDtos).hasSize(100);
122   }
123 }