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.ce.task.projectanalysis.step;
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;
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;
45 public class IndexAnalysisStepIT extends BaseStepTest {
47 private static final String PROJECT_KEY = "PROJECT_KEY";
48 private static final String PROJECT_UUID = "PROJECT_UUID";
51 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
53 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
55 private final DbClient dbClient = mock(DbClient.class);
57 private final FileStatuses fileStatuses = mock(FileStatuses.class);
59 private final ProjectIndexer projectIndexer = mock(ProjectIndexer.class);
61 private final DbSession dbSession = mock(DbSession.class);
63 private final BranchDao branchDao = mock(BranchDao.class);
65 private final IndexAnalysisStep underTest = new IndexAnalysisStep(treeRootHolder, fileStatuses, dbClient, projectIndexer);
67 private TestComputationStepContext testComputationStepContext;
71 testComputationStepContext = new TestComputationStepContext();
73 when(dbClient.openSession(false)).thenReturn(dbSession);
74 when(dbClient.branchDao()).thenReturn(branchDao);
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);
82 underTest.execute(testComputationStepContext);
84 verify(projectIndexer).indexOnAnalysis(PROJECT_UUID, Set.of());
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);
92 underTest.execute(testComputationStepContext);
94 verify(projectIndexer).indexOnAnalysis(PROJECT_UUID, Set.of());
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);
104 underTest.execute(testComputationStepContext);
106 verify(projectIndexer).indexOnAnalysis(PROJECT_UUID, anyUuids);
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);
115 underTest.execute(testComputationStepContext);
117 verify(projectIndexer).indexOnAnalysis(PROJECT_UUID);
121 protected ComputationStep step() {