]> source.dussan.org Git - sonarqube.git/blob
342ffe9c14c822ed380a6f4bdb926c11c55ae938
[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.ce.task.projectanalysis.step;
21
22 import java.util.Set;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
27 import org.sonar.ce.task.projectanalysis.component.Component;
28 import org.sonar.ce.task.projectanalysis.component.FileStatuses;
29 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
30 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
31 import org.sonar.ce.task.projectanalysis.component.ViewsComponent;
32 import org.sonar.ce.task.step.ComputationStep;
33 import org.sonar.ce.task.step.TestComputationStepContext;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.component.BranchDao;
37 import org.sonar.server.es.ProjectIndexer;
38
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.when;
42 import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
43 import static org.sonar.ce.task.projectanalysis.component.Component.Type.VIEW;
44
45 public class IndexAnalysisStepIT extends BaseStepTest {
46
47   private static final String PROJECT_KEY = "PROJECT_KEY";
48   private static final String PROJECT_UUID = "PROJECT_UUID";
49
50   @Rule
51   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
52   @Rule
53   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
54
55   private final DbClient dbClient = mock(DbClient.class);
56
57   private final FileStatuses fileStatuses = mock(FileStatuses.class);
58
59   private final ProjectIndexer projectIndexer = mock(ProjectIndexer.class);
60
61   private final DbSession dbSession = mock(DbSession.class);
62
63   private final BranchDao branchDao = mock(BranchDao.class);
64
65   private final IndexAnalysisStep underTest = new IndexAnalysisStep(treeRootHolder, fileStatuses, dbClient, projectIndexer);
66
67   private TestComputationStepContext testComputationStepContext;
68
69   @Before
70   public void init() {
71     testComputationStepContext = new TestComputationStepContext();
72
73     when(dbClient.openSession(false)).thenReturn(dbSession);
74     when(dbClient.branchDao()).thenReturn(branchDao);
75   }
76
77   @Test
78   public void call_indexByProjectUuid_of_indexer_for_project() {
79     Component project = ReportComponent.builder(PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
80     treeRootHolder.setRoot(project);
81
82     underTest.execute(testComputationStepContext);
83
84     verify(projectIndexer).indexOnAnalysis(PROJECT_UUID, Set.of());
85   }
86
87   @Test
88   public void call_indexByProjectUuid_of_indexer_for_view() {
89     Component view = ViewsComponent.builder(VIEW, PROJECT_KEY).setUuid(PROJECT_UUID).build();
90     treeRootHolder.setRoot(view);
91
92     underTest.execute(testComputationStepContext);
93
94     verify(projectIndexer).indexOnAnalysis(PROJECT_UUID, Set.of());
95   }
96
97   @Test
98   public void execute_whenMarkAsUnchangedFlagActivated_shouldCallIndexOnAnalysisWithChangedComponents() {
99     Component project = ReportComponent.builder(PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
100     treeRootHolder.setRoot(project);
101     Set<String> anyUuids = Set.of("any-uuid");
102     when(fileStatuses.getFileUuidsMarkedAsUnchanged()).thenReturn(anyUuids);
103
104     underTest.execute(testComputationStepContext);
105
106     verify(projectIndexer).indexOnAnalysis(PROJECT_UUID, anyUuids);
107   }
108
109   @Test
110   public void execute_whenBranchIsNeedIssueSync_shouldReindexEverything() {
111     Component project = ReportComponent.builder(PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).build();
112     treeRootHolder.setRoot(project);
113     when(branchDao.isBranchNeedIssueSync(dbSession, PROJECT_UUID)).thenReturn(true);
114
115     underTest.execute(testComputationStepContext);
116
117     verify(projectIndexer).indexOnAnalysis(PROJECT_UUID);
118   }
119
120   @Override
121   protected ComputationStep step() {
122     return underTest;
123   }
124 }