]> source.dussan.org Git - sonarqube.git/blob
d3f1b4737a29ed29d995de660b5c26cc5db4e25f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.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;
35
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;
42
43 public class PullActionIssuesRetrieverTest {
44
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;
50
51   private final IssueQueryParams queryParams = new IssueQueryParams(branchUuid, languages, ruleRepositories, null,
52     false, defaultChangedSince);
53   private final IssueDao issueDao = mock(IssueDao.class);
54
55   @Before
56   public void before() {
57     when(dbClient.issueDao()).thenReturn(issueDao);
58   }
59
60   @Test
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;
67
68     pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), Set.of(), listConsumer);
69
70     assertThat(returnedDtos).isEmpty();
71   }
72
73   @Test
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;
83
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);
87
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);
93
94     assertThat(returnedDtos).hasSize(1001);
95   }
96
97   @Test
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;
106
107     Set<String> issueKeysSnapshot = issuesWithSameCreationTimestamp.stream().map(IssueDto::getKee).collect(Collectors.toSet());
108     pullActionIssuesRetriever.processIssuesByBatch(dbClient.openSession(true), issueKeysSnapshot, listConsumer);
109
110     assertThat(returnedDtos).hasSize(100);
111   }
112 }