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.

ComponentUuidFactoryImplTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.component;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  25. import org.sonar.db.DbTester;
  26. import org.sonar.db.component.BranchType;
  27. import org.sonar.db.component.ComponentDto;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  32. public class ComponentUuidFactoryImplTest {
  33. private final Branch mainBranch = new DefaultBranchImpl(DEFAULT_MAIN_BRANCH_NAME);
  34. private final Branch mockedBranch = mock(Branch.class);
  35. @Rule
  36. public DbTester db = DbTester.create(System2.INSTANCE);
  37. @Test
  38. public void getOrCreateForKey_when_existingComponentsInDbForMainBranch_should_load() {
  39. ComponentDto project = db.components().insertPrivateProject();
  40. ComponentUuidFactory underTest = new ComponentUuidFactoryImpl(db.getDbClient(), db.getSession(), project.getKey(), mainBranch);
  41. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  42. }
  43. @Test
  44. public void getOrCreateForKey_when_existingComponentsInDbForNonMainBranch_should_load() {
  45. ComponentDto project = db.components().insertPrivateProject();
  46. ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("b1"));
  47. when(mockedBranch.getType()).thenReturn(BranchType.BRANCH);
  48. when(mockedBranch.isMain()).thenReturn(false);
  49. when(mockedBranch.getName()).thenReturn("b1");
  50. ComponentUuidFactory underTest = new ComponentUuidFactoryImpl(db.getDbClient(), db.getSession(), project.getKey(), mockedBranch);
  51. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(branch.uuid());
  52. }
  53. @Test
  54. public void getOrCreateForKey_when_existingComponentsInDbForPr_should_load() {
  55. ComponentDto project = db.components().insertPrivateProject();
  56. ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST).setKey("pr1"));
  57. when(mockedBranch.getType()).thenReturn(BranchType.PULL_REQUEST);
  58. when(mockedBranch.isMain()).thenReturn(false);
  59. when(mockedBranch.getPullRequestKey()).thenReturn("pr1");
  60. ComponentUuidFactory underTest = new ComponentUuidFactoryImpl(db.getDbClient(), db.getSession(), project.getKey(), mockedBranch);
  61. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(pr.uuid());
  62. }
  63. @Test
  64. public void getOrCreateForKey_when_componentsNotInDb_should_generate() {
  65. ComponentUuidFactory underTest = new ComponentUuidFactoryImpl(db.getDbClient(), db.getSession(), "theProjectKey", mainBranch);
  66. String generatedKey = underTest.getOrCreateForKey("foo");
  67. assertThat(generatedKey).isNotEmpty();
  68. // uuid is kept in memory for further calls with same key
  69. assertThat(underTest.getOrCreateForKey("foo")).isEqualTo(generatedKey);
  70. }
  71. }