3 * Copyright (C) 2009-2023 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.mockito.ArgumentCaptor;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.issue.IssueDao;
33 import org.sonar.db.issue.IssueDto;
34 import org.sonar.db.issue.IssueQueryParams;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.mockito.ArgumentMatchers.any;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.times;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
43 public class PullActionIssuesRetrieverTest {
45 private final DbClient dbClient = mock(DbClient.class);
46 private final String branchUuid = "master-branch-uuid";
47 private final List<String> languages = List.of("java");
48 private final List<String> ruleRepositories = List.of("js-security", "java");
49 private final Long defaultChangedSince = 1_000_000L;
51 private final IssueQueryParams queryParams = new IssueQueryParams(branchUuid, languages, ruleRepositories, null,
52 false, defaultChangedSince);
53 private final IssueDao issueDao = mock(IssueDao.class);
56 public void before() {
57 when(dbClient.issueDao()).thenReturn(issueDao);
61 public void processIssuesByBatch_givenNoIssuesReturnedByDatabase_noIssuesConsumed() {
62 var pullActionIssuesRetriever = new PullActionIssuesRetriever(dbClient, queryParams);
63 when(issueDao.selectByBranch(any(), any(), any()))
64 .thenReturn(List.of());
65 List<IssueDto> returnedDtos = new ArrayList<>();
66 Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
68 pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), Set.of(), listConsumer);
70 assertThat(returnedDtos).isEmpty();
74 public void processIssuesByBatch_givenThousandOneIssuesReturnedByDatabase_thousandOneIssuesConsumed() {
75 var pullActionIssuesRetriever = new PullActionIssuesRetriever(dbClient, queryParams);
76 List<IssueDto> thousandIssues = IntStream.rangeClosed(1, 1000).mapToObj(i -> new IssueDto().setKee(Integer.toString(i))).collect(Collectors.toList());
77 IssueDto singleIssue = new IssueDto().setKee("kee");
78 when(issueDao.selectByBranch(any(), any(), any()))
79 .thenReturn(thousandIssues)
80 .thenReturn(List.of(singleIssue));
81 List<IssueDto> returnedDtos = new ArrayList<>();
82 Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
84 Set<String> thousandIssueUuidsSnapshot = thousandIssues.stream().map(IssueDto::getKee).collect(Collectors.toSet());
85 thousandIssueUuidsSnapshot.add(singleIssue.getKee());
86 pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), thousandIssueUuidsSnapshot, listConsumer);
88 ArgumentCaptor<Set<String>> uuidsCaptor = ArgumentCaptor.forClass(Set.class);
89 verify(issueDao, times(2)).selectByBranch(any(), uuidsCaptor.capture(), any());
90 List<Set<String>> capturedSets = uuidsCaptor.getAllValues();
91 assertThat(capturedSets.get(0)).hasSize(1000);
92 assertThat(capturedSets.get(1)).hasSize(1);
94 assertThat(returnedDtos).hasSize(1001);
98 public void processIssuesByBatch_correctly_processes_all_issues_regardless_of_creation_timestamp() {
99 var pullActionIssuesRetriever = new PullActionIssuesRetriever(dbClient, queryParams);
100 List<IssueDto> issuesWithSameCreationTimestamp = IntStream.rangeClosed(1, 100).mapToObj(i -> new IssueDto()
101 .setKee(Integer.toString(i)).setCreatedAt(100L)).collect(Collectors.toList());
102 when(issueDao.selectByBranch(any(), any(), any()))
103 .thenReturn(issuesWithSameCreationTimestamp);
104 List<IssueDto> returnedDtos = new ArrayList<>();
105 Consumer<List<IssueDto>> listConsumer = returnedDtos::addAll;
107 Set<String> issueKeysSnapshot = issuesWithSameCreationTimestamp.stream().map(IssueDto::getKee).collect(Collectors.toSet());
108 pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), issueKeysSnapshot, listConsumer);
110 assertThat(returnedDtos).hasSize(100);