]> source.dussan.org Git - sonarqube.git/blob
2dd7b1448856c198be406c4c34bfbf8353d88f5e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.ArrayList;
23 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
28 import org.sonar.ce.task.projectanalysis.component.Component;
29 import org.sonar.ce.task.projectanalysis.component.FileStatusesImpl;
30 import org.sonar.ce.task.projectanalysis.component.PreviousSourceHashRepositoryImpl;
31 import org.sonar.ce.task.projectanalysis.component.ReportComponent;
32 import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
33 import org.sonar.ce.task.step.TestComputationStepContext;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.component.ComponentTesting;
37 import org.sonar.db.source.FileHashesDto;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
43
44 public class LoadFileHashesAndStatusStepIT {
45   @Rule
46   public DbTester db = DbTester.create();
47   @Rule
48   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
49   public PreviousSourceHashRepositoryImpl previousFileHashesRepository = new PreviousSourceHashRepositoryImpl();
50   public FileStatusesImpl fileStatuses = mock(FileStatusesImpl.class);
51   public AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
52
53   private final LoadFileHashesAndStatusStep underTest = new LoadFileHashesAndStatusStep(db.getDbClient(), previousFileHashesRepository, fileStatuses,
54     db.getDbClient().fileSourceDao(), treeRootHolder);
55
56   @Before
57   public void before() {
58     when(analysisMetadataHolder.isFirstAnalysis()).thenReturn(false);
59   }
60
61   @Test
62   public void initialized_file_statuses() {
63     Component project = ReportComponent.builder(Component.Type.PROJECT, 1, "project").build();
64     treeRootHolder.setRoot(project);
65     underTest.execute(new TestComputationStepContext());
66     verify(fileStatuses).initialize();
67   }
68
69   @Test
70   public void loads_file_hashes_for_project_branch() {
71     ComponentDto project1 = db.components().insertPublicProject().getMainBranchComponent();
72     ComponentDto project2 = db.components().insertPublicProject().getMainBranchComponent();
73
74     ComponentDto dbFile1 = db.components().insertComponent(ComponentTesting.newFileDto(project1));
75     ComponentDto dbFile2 = db.components().insertComponent(ComponentTesting.newFileDto(project1));
76
77     insertFileSources(dbFile1, dbFile2);
78
79     Component reportFile1 = ReportComponent.builder(Component.Type.FILE, 2, dbFile1.getKey()).setUuid(dbFile1.uuid()).build();
80     Component reportFile2 = ReportComponent.builder(Component.Type.FILE, 3, dbFile2.getKey()).setUuid(dbFile2.uuid()).build();
81     Component reportFile3 = ReportComponent.builder(Component.Type.FILE, 4, dbFile2.getKey()).build();
82
83     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1, project1.getKey()).setUuid(project1.uuid())
84       .addChildren(reportFile1, reportFile2, reportFile3).build());
85     underTest.execute(new TestComputationStepContext());
86
87     assertThat(previousFileHashesRepository.getMap()).hasSize(2);
88     assertThat(previousFileHashesRepository.getDbFile(reportFile1).get())
89       .extracting(FileHashesDto::getSrcHash, FileHashesDto::getRevision, FileHashesDto::getDataHash)
90       .containsOnly("srcHash" + dbFile1.getKey(), "revision" + dbFile1.getKey(), "dataHash" + dbFile1.getKey());
91     assertThat(previousFileHashesRepository.getDbFile(reportFile2).get())
92       .extracting(FileHashesDto::getSrcHash, FileHashesDto::getRevision, FileHashesDto::getDataHash)
93       .containsOnly("srcHash" + dbFile2.getKey(), "revision" + dbFile2.getKey(), "dataHash" + dbFile2.getKey());
94     assertThat(previousFileHashesRepository.getDbFile(reportFile3)).isEmpty();
95   }
96
97   @Test
98   public void loads_high_number_of_files() {
99     ComponentDto project1 = db.components().insertPublicProject().getMainBranchComponent();
100     List<Component> files = new ArrayList<>(2000);
101
102     for (int i = 0; i < 2000; i++) {
103       ComponentDto dbFile = db.components().insertComponent(ComponentTesting.newFileDto(project1));
104       insertFileSources(dbFile);
105       files.add(ReportComponent.builder(Component.Type.FILE, 2, dbFile.getKey()).setUuid(dbFile.uuid()).build());
106     }
107
108     treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1, project1.getKey()).setUuid(project1.uuid())
109       .addChildren(files.toArray(Component[]::new)).build());
110     underTest.execute(new TestComputationStepContext());
111
112     assertThat(previousFileHashesRepository.getMap()).hasSize(2000);
113     for (Component file : files) {
114       assertThat(previousFileHashesRepository.getDbFile(file)).isPresent();
115     }
116   }
117
118   private void insertFileSources(ComponentDto... files) {
119     for (ComponentDto file : files) {
120       db.fileSources().insertFileSource(file, f -> f
121         .setSrcHash("srcHash" + file.getKey())
122         .setRevision("revision" + file.getKey())
123         .setDataHash("dataHash" + file.getKey()));
124     }
125   }
126 }