]> source.dussan.org Git - sonarqube.git/blob
6362ab8ca685cc4a04b85ef4fe7f37723eb3608a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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.analysis;
21
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;
35
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;
43
44 @RunWith(DataProviderRunner.class)
45 public class AnalysisMetadataHolderImplTest {
46
47   private static final Analysis baseProjectAnalysis = new Analysis.Builder()
48     .setUuid("uuid_1")
49     .setCreatedAt(123456789L)
50     .build();
51   private static final long SOME_DATE = 10000000L;
52
53   private final PlatformEditionProvider editionProvider = mock(PlatformEditionProvider.class);
54   private final AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
55
56   @Test
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");
61   }
62
63   @Test
64   public void setUuid_throws_ISE_if_called_twice() {
65     underTest.setUuid("org1");
66
67     assertThatThrownBy(() -> underTest.setUuid("org1"))
68       .isInstanceOf(IllegalStateException.class)
69       .hasMessage("Analysis uuid has already been set");
70   }
71
72   @Test
73   public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() {
74
75     underTest.setAnalysisDate(SOME_DATE);
76
77     assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
78   }
79
80   @Test
81   public void get_new_code_reference_branch() {
82
83     String newCodeReferenceBranch = "newCodeReferenceBranch";
84     underTest.setNewCodeReferenceBranch(newCodeReferenceBranch);
85
86     assertThat(underTest.getNewCodeReferenceBranch()).hasValue(newCodeReferenceBranch);
87   }
88
89   @Test
90   public void get_new_code_reference_branch_return_empty_when_holder_is_not_initialized() {
91
92     assertThat(underTest.getNewCodeReferenceBranch()).isEmpty();
93   }
94
95   @Test
96   public void set_new_code_reference_branch_throws_ISE_when_called_twice() {
97
98     String newCodeReferenceBranch = "newCodeReferenceBranch";
99     underTest.setNewCodeReferenceBranch(newCodeReferenceBranch);
100
101     assertThatThrownBy(() -> underTest.setNewCodeReferenceBranch(newCodeReferenceBranch))
102       .isInstanceOf(IllegalStateException.class)
103       .hasMessage("newCodeReferenceBranch has already been set");
104   }
105
106   @Test
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");
111   }
112
113   @Test
114   public void setAnalysisDate_throws_ISE_when_called_twice() {
115     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
116     underTest.setAnalysisDate(SOME_DATE);
117
118     assertThatThrownBy(() -> underTest.setAnalysisDate(SOME_DATE))
119       .isInstanceOf(IllegalStateException.class)
120       .hasMessage("Analysis date has already been set");
121   }
122
123   @Test
124   public void hasAnalysisDateBeenSet_returns_false_when_holder_is_not_initialized() {
125     assertThat(new AnalysisMetadataHolderImpl(editionProvider).hasAnalysisDateBeenSet()).isFalse();
126   }
127
128   @Test
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();
133   }
134
135   @Test
136   public void isFirstAnalysis_return_true() {
137     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
138
139     underTest.setBaseAnalysis(null);
140     assertThat(underTest.isFirstAnalysis()).isTrue();
141   }
142
143   @Test
144   public void isFirstAnalysis_return_false() {
145     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
146
147     underTest.setBaseAnalysis(baseProjectAnalysis);
148     assertThat(underTest.isFirstAnalysis()).isFalse();
149   }
150
151   @Test
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");
156   }
157
158   @Test
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");
163   }
164
165   @Test
166   public void setBaseProjectSnapshot_throws_ISE_when_called_twice() {
167     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
168     underTest.setBaseAnalysis(baseProjectAnalysis);
169
170     assertThatThrownBy(() -> underTest.setBaseAnalysis(baseProjectAnalysis))
171       .isInstanceOf(IllegalStateException.class)
172       .hasMessage("Base project snapshot has already been set");
173   }
174
175   @Test
176   public void isCrossProjectDuplicationEnabled_return_true() {
177     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
178
179     underTest.setCrossProjectDuplicationEnabled(true);
180
181     assertThat(underTest.isCrossProjectDuplicationEnabled()).isTrue();
182   }
183
184   @Test
185   public void isCrossProjectDuplicationEnabled_return_false() {
186     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
187
188     underTest.setCrossProjectDuplicationEnabled(false);
189
190     assertThat(underTest.isCrossProjectDuplicationEnabled()).isFalse();
191   }
192
193   @Test
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");
198   }
199
200   @Test
201   public void setIsCrossProjectDuplicationEnabled_throws_ISE_when_called_twice() {
202     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
203     underTest.setCrossProjectDuplicationEnabled(true);
204
205     assertThatThrownBy(() -> underTest.setCrossProjectDuplicationEnabled(false))
206       .isInstanceOf(IllegalStateException.class)
207       .hasMessage("Cross project duplication flag has already been set");
208   }
209
210   @Test
211   public void set_branch() {
212     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
213
214     underTest.setBranch(new DefaultBranchImpl(DEFAULT_PROJECT_MAIN_BRANCH_NAME));
215
216     assertThat(underTest.getBranch().getName()).isEqualTo("main");
217   }
218
219   @Test
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");
224   }
225
226   @Test
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));
230
231     assertThatThrownBy(() -> underTest.setBranch(new DefaultBranchImpl("main")))
232       .isInstanceOf(IllegalStateException.class)
233       .hasMessage("Branch has already been set");
234   }
235
236   @Test
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);
243
244     underTest.setBranch(branch);
245
246     assertThat(underTest.getBranch()).isSameAs(branch);
247   }
248
249   @Test
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);
256
257     underTest.setBranch(branch);
258
259     assertThat(underTest.getBranch()).isSameAs(branch);
260   }
261
262   @Test
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);
268
269     assertThatThrownBy(() -> underTest.setBranch(branch))
270       .isInstanceOf(IllegalStateException.class)
271       .hasMessage("Branches and Pull Requests are not supported in Community Edition");
272   }
273
274   @DataProvider
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);
281   }
282
283   @DataProvider
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);
290   }
291
292   @Test
293   public void setPullRequestId() {
294     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
295
296     String pullRequestId = "pr-123";
297     underTest.setPullRequestKey(pullRequestId);
298
299     assertThat(underTest.getPullRequestKey()).isEqualTo(pullRequestId);
300   }
301
302   @Test
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");
307   }
308
309   @Test
310   public void setPullRequestId_throws_ISE_when_called_twice() {
311     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
312     underTest.setPullRequestKey("pr-123");
313
314     assertThatThrownBy(() -> underTest.setPullRequestKey("pr-234"))
315       .isInstanceOf(IllegalStateException.class)
316       .hasMessage("Pull request key has already been set");
317   }
318
319   @Test
320   public void set_and_get_project() {
321     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
322
323     Project project = Project.from(newPrivateProjectDto());
324     underTest.setProject(project);
325
326     assertThat(underTest.getProject()).isSameAs(project);
327   }
328
329   @Test
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");
334   }
335
336   @Test
337   public void setProject_throws_ISE_when_called_twice() {
338     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
339     underTest.setProject(Project.from(newPrivateProjectDto()));
340
341     assertThatThrownBy(() -> underTest.setProject(Project.from(newPrivateProjectDto())))
342       .isInstanceOf(IllegalStateException.class)
343       .hasMessage("Project has already been set");
344   }
345
346   @Test
347   public void getRootComponentRef() {
348     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
349
350     underTest.setRootComponentRef(10);
351
352     assertThat(underTest.getRootComponentRef()).isEqualTo(10);
353   }
354
355   @Test
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");
360   }
361
362   @Test
363   public void setRootComponentRef_throws_ISE_when_called_twice() {
364     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
365     underTest.setRootComponentRef(10);
366
367     assertThatThrownBy(() -> underTest.setRootComponentRef(9))
368       .isInstanceOf(IllegalStateException.class)
369       .hasMessage("Root component ref has already been set");
370   }
371
372   @Test
373   public void getPullRequestBranch_returns_true() {
374     Branch branch = mock(Branch.class);
375     when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
376
377     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
378     underTest.setBranch(branch);
379
380     assertThat(underTest.isPullRequest()).isTrue();
381   }
382
383   @Test
384   public void setScmRevision_throws_ISE_when_called_twice() {
385     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
386     underTest.setScmRevision("bd56dab");
387
388     assertThatThrownBy(() -> underTest.setScmRevision("bd56dab"))
389       .isInstanceOf(IllegalStateException.class)
390       .hasMessage("ScmRevision has already been set");
391   }
392
393   @Test
394   public void getScmRevision_returns_empty_if_scmRevision_is_not_initialized() {
395     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
396
397     assertThat(underTest.getScmRevision()).isNotPresent();
398   }
399
400   @Test
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");
405   }
406
407   @Test
408   public void getScmRevision_does_not_return_empty_string() {
409     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
410     underTest.setScmRevision("");
411     assertThat(underTest.getScmRevision()).isEmpty();
412   }
413
414   @Test
415   public void getScmRevision_does_not_return_blank_string() {
416     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(editionProvider);
417     underTest.setScmRevision("    ");
418     assertThat(underTest.getScmRevision()).isEmpty();
419   }
420
421   @Test
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);
426
427     assertThat(underTest.isBranch()).isTrue();
428   }
429
430   @Test
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);
435
436     assertThat(underTest.isBranch()).isFalse();
437   }
438
439   @Test
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");
444   }
445 }