]> source.dussan.org Git - sonarqube.git/blob
33cfdb245d152afa253635d796c2d1fc9d5f6b21
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.scm;
21
22 import com.google.common.collect.ImmutableList;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Optional;
27 import javax.annotation.Nullable;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.log.LogTester;
32 import org.sonar.ce.task.projectanalysis.analysis.Analysis;
33 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
34 import org.sonar.ce.task.projectanalysis.analysis.Branch;
35 import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
36 import org.sonar.ce.task.projectanalysis.component.Component;
37 import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
38 import org.sonar.core.hash.SourceHashComputer;
39 import org.sonar.db.DbTester;
40 import org.sonar.db.component.BranchType;
41 import org.sonar.db.protobuf.DbFileSources;
42 import org.sonar.db.source.FileSourceDto;
43
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
47 import static org.sonar.api.utils.log.LoggerLevel.TRACE;
48 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
49
50 public class ScmInfoDbLoaderTest {
51   static final int FILE_REF = 1;
52   static final Component FILE = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").build();
53   static final long DATE_1 = 123456789L;
54   static final long DATE_2 = 1234567810L;
55
56   static Analysis baseProjectAnalysis = new Analysis.Builder()
57     .setId(1)
58     .setUuid("uuid_1")
59     .setCreatedAt(123456789L)
60     .build();
61
62   @Rule
63   public LogTester logTester = new LogTester();
64   @Rule
65   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
66   @Rule
67   public DbTester dbTester = DbTester.create(System2.INSTANCE);
68   @Rule
69   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
70
71   private Branch branch = mock(Branch.class);
72   private ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
73
74   private ScmInfoDbLoader underTest = new ScmInfoDbLoader(analysisMetadataHolder, dbTester.getDbClient(), referenceBranchComponentUuids);
75
76   @Test
77   public void returns_ScmInfo_from_DB() {
78     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
79     analysisMetadataHolder.setBranch(null);
80
81     String hash = computeSourceHash(1);
82     addFileSourceInDb("henry", DATE_1, "rev-1", hash);
83
84     DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
85     assertThat(scmInfo.getAllChangesets()).hasSize(1);
86     assertThat(scmInfo.fileHash()).isEqualTo(hash);
87
88     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'FILE_UUID'");
89   }
90
91   @Test
92   public void read_from_reference_branch_if_no_base() {
93     analysisMetadataHolder.setBaseAnalysis(null);
94     analysisMetadataHolder.setBranch(branch);
95
96     String referenceFileUuid = "referenceFileUuid";
97     String hash = computeSourceHash(1);
98
99     when(referenceBranchComponentUuids.getComponentUuid(FILE.getDbKey())).thenReturn(referenceFileUuid);
100     addFileSourceInDb("henry", DATE_1, "rev-1", hash, referenceFileUuid);
101
102     DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
103     assertThat(scmInfo.getAllChangesets()).hasSize(1);
104     assertThat(scmInfo.fileHash()).isEqualTo(hash);
105     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'referenceFileUuid'");
106   }
107
108   @Test
109   public void read_from_target_if_pullrequest() {
110     Branch branch = mock(Branch.class);
111     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
112     analysisMetadataHolder.setBaseAnalysis(null);
113     analysisMetadataHolder.setBranch(branch);
114
115     String targetBranchFileUuid = "targetBranchFileUuid";
116     String hash = computeSourceHash(1);
117
118     when(referenceBranchComponentUuids.getComponentUuid(FILE.getDbKey())).thenReturn(targetBranchFileUuid);
119     addFileSourceInDb("henry", DATE_1, "rev-1", hash, targetBranchFileUuid);
120
121     DbScmInfo scmInfo = underTest.getScmInfo(FILE).get();
122     assertThat(scmInfo.getAllChangesets()).hasSize(1);
123     assertThat(scmInfo.fileHash()).isEqualTo(hash);
124     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'targetBranchFileUuid'");
125   }
126
127   @Test
128   public void return_empty_if_no_dto_available() {
129     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
130     analysisMetadataHolder.setBranch(null);
131
132     Optional<DbScmInfo> scmInfo = underTest.getScmInfo(FILE);
133
134     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from DB for file 'FILE_UUID'");
135     assertThat(scmInfo).isEmpty();
136   }
137
138   @Test
139   public void do_not_read_from_db_on_first_analysis_if_there_is_no_reference_branch() {
140     Branch branch = mock(Branch.class);
141     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
142     analysisMetadataHolder.setBaseAnalysis(null);
143     analysisMetadataHolder.setBranch(branch);
144
145     assertThat(underTest.getScmInfo(FILE)).isEmpty();
146     assertThat(logTester.logs(TRACE)).isEmpty();
147   }
148
149   private static List<String> generateLines(int lineCount) {
150     ImmutableList.Builder<String> builder = ImmutableList.builder();
151     for (int i = 0; i < lineCount; i++) {
152       builder.add("line " + i);
153     }
154     return builder.build();
155   }
156
157   private static String computeSourceHash(int lineCount) {
158     SourceHashComputer sourceHashComputer = new SourceHashComputer();
159     Iterator<String> lines = generateLines(lineCount).iterator();
160     while (lines.hasNext()) {
161       sourceHashComputer.addLine(lines.next(), lines.hasNext());
162     }
163     return sourceHashComputer.getHash();
164   }
165
166   private void addFileSourceInDb(@Nullable String author, @Nullable Long date, @Nullable String revision, String srcHash) {
167     addFileSourceInDb(author, date, revision, srcHash, FILE.getUuid());
168   }
169
170   private void addFileSourceInDb(@Nullable String author, @Nullable Long date, @Nullable String revision, String srcHash, String fileUuid) {
171     DbFileSources.Data.Builder fileDataBuilder = DbFileSources.Data.newBuilder();
172     DbFileSources.Line.Builder builder = fileDataBuilder.addLinesBuilder()
173       .setLine(1);
174     if (author != null) {
175       builder.setScmAuthor(author);
176     }
177     if (date != null) {
178       builder.setScmDate(date);
179     }
180     if (revision != null) {
181       builder.setScmRevision(revision);
182     }
183     dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), new FileSourceDto()
184       .setLineHashes(Collections.singletonList("lineHash"))
185       .setFileUuid(fileUuid)
186       .setProjectUuid("PROJECT_UUID")
187       .setSourceData(fileDataBuilder.build())
188       .setSrcHash(srcHash));
189     dbTester.commit();
190   }
191
192 }