3 * Copyright (C) 2009-2022 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.analysis;
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.util.Arrays;
26 import java.util.Optional;
27 import java.util.stream.Stream;
28 import javax.annotation.Nullable;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.sonar.ce.task.projectanalysis.component.DefaultBranchImpl;
32 import org.sonar.core.platform.PlatformEditionProvider;
33 import org.sonar.db.component.BranchType;
34 import org.sonar.server.project.Project;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40 import static org.sonar.core.platform.EditionProvider.Edition;
41 import static org.sonar.db.component.BranchDto.DEFAULT_PROJECT_MAIN_BRANCH_NAME;
42 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
44 @RunWith(DataProviderRunner.class)
45 public class AnalysisMetadataHolderImplTest {
47 private static final Analysis baseProjectAnalysis = new Analysis.Builder()
49 .setCreatedAt(123456789L)
51 private static final long SOME_DATE = 10000000L;
53 private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
54 private final AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
57 public void setUuid_throws_NPE_is_parameter_is_null() {
58 assertThatThrownBy(() -> underTest.setUuid(null))
59 .isInstanceOf(NullPointerException.class)
60 .hasMessage("Analysis uuid can't be null");
64 public void setUuid_throws_ISE_if_called_twice() {
65 underTest.setUuid("org1");
67 assertThatThrownBy(() -> underTest.setUuid("org1"))
68 .isInstanceOf(IllegalStateException.class)
69 .hasMessage("Analysis uuid has already been set");
73 public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() {
75 underTest.setAnalysisDate(SOME_DATE);
77 assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
81 public void get_new_code_reference_branch() {
83 String newCodeReferenceBranch = "newCodeReferenceBranch";
84 underTest.setNewCodeReferenceBranch(newCodeReferenceBranch);
86 assertThat(underTest.getNewCodeReferenceBranch()).hasValue(newCodeReferenceBranch);
90 public void get_new_code_reference_branch_return_empty_when_holder_is_not_initialized() {
92 assertThat(underTest.getNewCodeReferenceBranch()).isEmpty();
96 public void set_new_code_reference_branch_throws_ISE_when_called_twice() {
98 String newCodeReferenceBranch = "newCodeReferenceBranch";
99 underTest.setNewCodeReferenceBranch(newCodeReferenceBranch);
101 assertThatThrownBy(() -> underTest.setNewCodeReferenceBranch(newCodeReferenceBranch))
102 .isInstanceOf(IllegalStateException.class)
103 .hasMessage("newCodeReferenceBranch has already been set");
107 public void getAnalysisDate_throws_ISE_when_holder_is_not_initialized() {
108 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getAnalysisDate())
109 .isInstanceOf(IllegalStateException.class)
110 .hasMessage("Analysis date has not been set");
114 public void setAnalysisDate_throws_ISE_when_called_twice() {
115 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
116 underTest.setAnalysisDate(SOME_DATE);
118 assertThatThrownBy(() -> underTest.setAnalysisDate(SOME_DATE))
119 .isInstanceOf(IllegalStateException.class)
120 .hasMessage("Analysis date has already been set");
124 public void hasAnalysisDateBeenSet_returns_false_when_holder_is_not_initialized() {
125 assertThat(new AnalysisMetadataHolderImpl(editionProvider).hasAnalysisDateBeenSet()).isFalse();
129 public void hasAnalysisDateBeenSet_returns_true_when_holder_date_is_set() {
130 AnalysisMetadataHolderImpl holder = new AnalysisMetadataHolderImpl(editionProvider);
131 holder.setAnalysisDate(46532);
132 assertThat(holder.hasAnalysisDateBeenSet()).isTrue();
136 public void isFirstAnalysis_return_true() {
137 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
139 underTest.setBaseAnalysis(null);
140 assertThat(underTest.isFirstAnalysis()).isTrue();
144 public void isFirstAnalysis_return_false() {
145 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
147 underTest.setBaseAnalysis(baseProjectAnalysis);
148 assertThat(underTest.isFirstAnalysis()).isFalse();
152 public void isFirstAnalysis_throws_ISE_when_base_project_snapshot_is_not_set() {
153 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).isFirstAnalysis())
154 .isInstanceOf(IllegalStateException.class)
155 .hasMessage("Base project snapshot has not been set");
159 public void baseProjectSnapshot_throws_ISE_when_base_project_snapshot_is_not_set() {
160 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getBaseAnalysis())
161 .isInstanceOf(IllegalStateException.class)
162 .hasMessage("Base project snapshot has not been set");
166 public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
167 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
168 underTest.setBaseAnalysis(baseProjectAnalysis);
170 assertThatThrownBy(() -> underTest.setBaseAnalysis(baseProjectAnalysis))
171 .isInstanceOf(IllegalStateException.class)
172 .hasMessage("Base project snapshot has already been set");
176 public void isCrossProjectDuplicationEnabled_return_true() {
177 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
179 underTest.setCrossProjectDuplicationEnabled(true);
181 assertThat(underTest.isCrossProjectDuplicationEnabled()).isTrue();
185 public void isCrossProjectDuplicationEnabled_return_false() {
186 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
188 underTest.setCrossProjectDuplicationEnabled(false);
190 assertThat(underTest.isCrossProjectDuplicationEnabled()).isFalse();
194 public void isCrossProjectDuplicationEnabled_throws_ISE_when_holder_is_not_initialized() {
195 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).isCrossProjectDuplicationEnabled())
196 .isInstanceOf(IllegalStateException.class)
197 .hasMessage("Cross project duplication flag has not been set");
201 public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
202 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
203 underTest.setCrossProjectDuplicationEnabled(true);
205 assertThatThrownBy(() -> underTest.setCrossProjectDuplicationEnabled(false))
206 .isInstanceOf(IllegalStateException.class)
207 .hasMessage("Cross project duplication flag has already been set");
211 public void set_branch() {
212 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
214 underTest.setBranch(new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME));
216 assertThat(underTest.getBranch().getName()).isEqualTo("main");
220 public void getBranch_throws_ISE_when_holder_is_not_initialized() {
221 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getBranch())
222 .isInstanceOf(IllegalStateException.class)
223 .hasMessage("Branch has not been set");
227 public void setBranch_throws_ISE_when_called_twice() {
228 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
229 underTest.setBranch(new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME));
231 assertThatThrownBy(() -> underTest.setBranch(new DefaultBranchImpl("main")))
232 .isInstanceOf(IllegalStateException.class)
233 .hasMessage("Branch has already been set");
237 @UseDataProvider("anyEditionIncludingNone")
238 public void setBranch_does_not_fail_if_main_branch_on_any_edition(@Nullable Edition edition) {
239 when(editionProvider.get()).thenReturn(Optional.ofNullable(edition));
240 Branch branch = mock(Branch.class);
241 when(branch.isMain()).thenReturn(true);
242 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
244 underTest.setBranch(branch);
246 assertThat(underTest.getBranch()).isSameAs(branch);
250 @UseDataProvider("anyEditionIncludingNoneButCommunity")
251 public void setBranch_does_not_fail_if_non_main_on_any_edition_but_Community(@Nullable Edition edition) {
252 when(editionProvider.get()).thenReturn(Optional.ofNullable(edition));
253 Branch branch = mock(Branch.class);
254 when(branch.isMain()).thenReturn(false);
255 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
257 underTest.setBranch(branch);
259 assertThat(underTest.getBranch()).isSameAs(branch);
263 public void setBranch_fails_if_non_main_branch_on_Community_edition() {
264 when(editionProvider.get()).thenReturn(Optional.of(Edition.COMMUNITY));
265 Branch branch = mock(Branch.class);
266 when(branch.isMain()).thenReturn(false);
267 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
269 assertThatThrownBy(() -> underTest.setBranch(branch))
270 .isInstanceOf(IllegalStateException.class)
271 .hasMessage("Branches and Pull Requests are not supported in Community Edition");
275 public static Object[][] anyEditionIncludingNone() {
276 return Stream.concat(
277 Stream.of((Edition) null),
278 Arrays.stream(Edition.values()))
279 .map(t -> new Object[] {t})
280 .toArray(Object[][]::new);
284 public static Object[][] anyEditionIncludingNoneButCommunity() {
285 return Stream.concat(
286 Stream.of((Edition) null),
287 Arrays.stream(Edition.values())).filter(t -> t != Edition.COMMUNITY)
288 .map(t -> new Object[] {t})
289 .toArray(Object[][]::new);
293 public void setPullRequestId() {
294 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
296 String pullRequestId = "pr-123";
297 underTest.setPullRequestKey(pullRequestId);
299 assertThat(underTest.getPullRequestKey()).isEqualTo(pullRequestId);
303 public void getPullRequestId_throws_ISE_when_holder_is_not_initialized() {
304 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getPullRequestKey())
305 .isInstanceOf(IllegalStateException.class)
306 .hasMessage("Pull request key has not been set");
310 public void setPullRequestId_throws_ISE_when_called_twice() {
311 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
312 underTest.setPullRequestKey("pr-123");
314 assertThatThrownBy(() -> underTest.setPullRequestKey("pr-234"))
315 .isInstanceOf(IllegalStateException.class)
316 .hasMessage("Pull request key has already been set");
320 public void set_and_get_project() {
321 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
323 Project project = Project.from(newPrivateProjectDto());
324 underTest.setProject(project);
326 assertThat(underTest.getProject()).isSameAs(project);
330 public void getProject_throws_ISE_when_holder_is_not_initialized() {
331 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getProject())
332 .isInstanceOf(IllegalStateException.class)
333 .hasMessage("Project has not been set");
337 public void setProject_throws_ISE_when_called_twice() {
338 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
339 underTest.setProject(Project.from(newPrivateProjectDto()));
341 assertThatThrownBy(() -> underTest.setProject(Project.from(newPrivateProjectDto())))
342 .isInstanceOf(IllegalStateException.class)
343 .hasMessage("Project has already been set");
347 public void getRootComponentRef() {
348 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
350 underTest.setRootComponentRef(10);
352 assertThat(underTest.getRootComponentRef()).isEqualTo(10);
356 public void getRootComponentRef_throws_ISE_when_holder_is_not_initialized() {
357 assertThatThrownBy(() -> new AnalysisMetadataHolderImpl(editionProvider).getRootComponentRef())
358 .isInstanceOf(IllegalStateException.class)
359 .hasMessage("Root component ref has not been set");
363 public void setRootComponentRef_throws_ISE_when_called_twice() {
364 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
365 underTest.setRootComponentRef(10);
367 assertThatThrownBy(() -> underTest.setRootComponentRef(9))
368 .isInstanceOf(IllegalStateException.class)
369 .hasMessage("Root component ref has already been set");
373 public void getPullRequestBranch_returns_true() {
374 Branch branch = mock(Branch.class);
375 when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
377 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
378 underTest.setBranch(branch);
380 assertThat(underTest.isPullRequest()).isTrue();
384 public void setScmRevision_throws_ISE_when_called_twice() {
385 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
386 underTest.setScmRevision("bd56dab");
388 assertThatThrownBy(() -> underTest.setScmRevision("bd56dab"))
389 .isInstanceOf(IllegalStateException.class)
390 .hasMessage("ScmRevision has already been set");
394 public void getScmRevision_returns_empty_if_scmRevision_is_not_initialized() {
395 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
397 assertThat(underTest.getScmRevision()).isNotPresent();
401 public void getScmRevision_returns_scmRevision_if_scmRevision_is_initialized() {
402 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
403 underTest.setScmRevision("bd56dab");
404 assertThat(underTest.getScmRevision()).hasValue("bd56dab");
408 public void getScmRevision_does_not_return_empty_string() {
409 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
410 underTest.setScmRevision("");
411 assertThat(underTest.getScmRevision()).isEmpty();
415 public void getScmRevision_does_not_return_blank_string() {
416 AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
417 underTest.setScmRevision(" ");
418 assertThat(underTest.getScmRevision()).isEmpty();
422 public void isBranch_returns_true_for_initialized_branch() {
423 Branch branch = mock(Branch.class);
424 when(branch.getType()).thenReturn(BranchType.BRANCH);
425 underTest.setBranch(branch);
427 assertThat(underTest.isBranch()).isTrue();
431 public void isBranch_returns_false_for_pr() {
432 Branch branch = mock(Branch.class);
433 when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
434 underTest.setBranch(branch);
436 assertThat(underTest.isBranch()).isFalse();
440 public void isBranch_throws_ISE_for_not_initialized_branch() {
441 assertThatThrownBy(underTest::isBranch)
442 .isInstanceOf(IllegalStateException.class)
443 .hasMessage("Branch has not been set");