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.server.issue.ws.pull;
22 import java.util.ArrayList;
23 import java.util.List;
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;
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;
41 public class PullActionIssuesRetrieverTest {
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;
50 private final IssueQueryParams queryParams = new IssueQueryParams(projectUuid, branchName, languages, ruleRepositories, false,
52 private final IssueDao issueDao = mock(IssueDao.class);
55 public void before() {
56 when(dbClient.issueDao()).thenReturn(issueDao);
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;
67 pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), Set.of(), listConsumer);
69 assertThat(returnedDtos).isEmpty();
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;
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);
87 assertThat(returnedDtos).hasSize(1001);
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()))
98 List<IssueDto> returnedDtos = new ArrayList<>();
99 Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
101 Set<String> thousandIssueKeysSnapshot = issues.stream().map(IssueDto::getKee).collect(Collectors.toSet());
102 pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), thousandIssueKeysSnapshot, listConsumer);
104 assertThat(returnedDtos)
106 .containsExactlyInAnyOrder(issue1, issue2);
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;
118 Set<String> issueKeysSnapshot = issuesWithSameCreationTimestamp.stream().map(IssueDto::getKee).collect(Collectors.toSet());
119 pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), issueKeysSnapshot, listConsumer);
121 assertThat(returnedDtos).hasSize(100);