You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewCodeReferenceBranchComponentUuidsTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.period;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
  25. import org.sonar.db.DbTester;
  26. import org.sonar.db.component.BranchType;
  27. import org.sonar.db.component.ComponentDto;
  28. import org.sonar.db.component.ComponentTesting;
  29. import org.sonar.db.newcodeperiod.NewCodePeriodType;
  30. import org.sonar.server.project.Project;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.when;
  35. import static org.sonar.db.component.SnapshotTesting.newAnalysis;
  36. public class NewCodeReferenceBranchComponentUuidsTest {
  37. @Rule
  38. public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
  39. @Rule
  40. public PeriodHolderRule periodHolder = new PeriodHolderRule();
  41. @Rule
  42. public DbTester db = DbTester.create();
  43. private NewCodeReferenceBranchComponentUuids underTest = new NewCodeReferenceBranchComponentUuids(analysisMetadataHolder, periodHolder, db.getDbClient());
  44. private ComponentDto branch1;
  45. private ComponentDto branch1File;
  46. private ComponentDto pr1File;
  47. private ComponentDto pr2File;
  48. private Project project = mock(Project.class);
  49. private ComponentDto pr1;
  50. private ComponentDto pr2;
  51. private ComponentDto branch2;
  52. private ComponentDto branch2File;
  53. @Before
  54. public void setUp() {
  55. analysisMetadataHolder.setProject(project);
  56. ComponentDto projectDto = db.components().insertPublicProject();
  57. when(project.getUuid()).thenReturn(projectDto.uuid());
  58. branch1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch1"));
  59. branch2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("branch2"));
  60. pr1 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr1").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
  61. pr2 = db.components().insertProjectBranch(projectDto, b -> b.setKey("pr2").setBranchType(BranchType.PULL_REQUEST).setMergeBranchUuid(branch1.uuid()));
  62. branch1File = ComponentTesting.newFileDto(branch1, null, "file").setUuid("branch1File");
  63. branch2File = ComponentTesting.newFileDto(branch2, null, "file").setUuid("branch2File");
  64. pr1File = ComponentTesting.newFileDto(pr1, null, "file").setUuid("file1");
  65. pr2File = ComponentTesting.newFileDto(pr2, null, "file").setUuid("file2");
  66. db.components().insertComponents(branch1File, pr1File, pr2File, branch2File);
  67. }
  68. @Test
  69. public void should_support_db_key_when_looking_for_reference_component() {
  70. periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
  71. db.components().insertSnapshot(newAnalysis(branch1));
  72. assertThat(underTest.getComponentUuid(pr1File.getDbKey())).isEqualTo(branch1File.uuid());
  73. }
  74. @Test
  75. public void should_support_key_when_looking_for_reference_component() {
  76. periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
  77. db.components().insertSnapshot(newAnalysis(branch1));
  78. assertThat(underTest.getComponentUuid(pr1File.getKey())).isEqualTo(branch1File.uuid());
  79. }
  80. @Test
  81. public void return_null_if_file_doesnt_exist() {
  82. periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
  83. db.components().insertSnapshot(newAnalysis(branch1));
  84. assertThat(underTest.getComponentUuid("doesnt exist")).isNull();
  85. }
  86. @Test
  87. public void skip_init_if_no_reference_branch_analysis() {
  88. periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "branch1", null));
  89. assertThat(underTest.getComponentUuid(pr1File.getDbKey())).isNull();
  90. }
  91. @Test
  92. public void skip_init_if_branch_not_found() {
  93. periodHolder.setPeriod(new Period(NewCodePeriodType.REFERENCE_BRANCH.name(), "unknown", null));
  94. assertThat(underTest.getComponentUuid(pr1File.getDbKey())).isNull();
  95. }
  96. @Test
  97. public void throw_ise_if_mode_is_not_reference_branch() {
  98. periodHolder.setPeriod(new Period(NewCodePeriodType.NUMBER_OF_DAYS.name(), "10", 1000L));
  99. assertThatThrownBy(() -> underTest.getComponentUuid(pr1File.getDbKey()))
  100. .isInstanceOf(IllegalStateException.class);
  101. }
  102. }