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.

ComponentUuidFactoryWithMigrationTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.function.Function;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  29. import org.sonar.db.DbTester;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.db.component.ComponentTesting;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  34. public class ComponentUuidFactoryWithMigrationTest {
  35. private final Branch mainBranch = new DefaultBranchImpl(DEFAULT_MAIN_BRANCH_NAME);
  36. @Rule
  37. public DbTester db = DbTester.create(System2.INSTANCE);
  38. private Function<String, String> pathToKey = path -> path != null ? "project:" + path : "project";
  39. @Test
  40. public void load_uuids_from_existing_components_in_db() {
  41. ComponentDto project = db.components().insertPrivateProject();
  42. ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project));
  43. Map<String, String> reportModulesPath = Collections.singletonMap(module.getKey(), "module1_path");
  44. pathToKey = path -> path != null ? project.getKey() + ":" + path : project.getKey();
  45. ComponentUuidFactoryWithMigration underTest =
  46. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, reportModulesPath);
  47. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  48. assertThat(underTest.getOrCreateForKey(module.getKey())).isEqualTo(module.uuid());
  49. }
  50. @Test
  51. public void migrate_project_with_modules() {
  52. ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
  53. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  54. .setKey("project:module1"));
  55. ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(module1)
  56. .setKey("project:module1:module2"));
  57. ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
  58. .setKey("project:file1")
  59. .setPath("file1_path"));
  60. ComponentDto file2 = db.components().insertComponent(ComponentTesting.newFileDto(module2)
  61. .setKey("project:module1:module2:file2")
  62. .setPath("file2_path"));
  63. assertThat(file2.moduleUuidPath()).isEqualTo("." + project.uuid() + "." + module1.uuid() + "." + module2.uuid() + ".");
  64. Map<String, String> modulesRelativePaths = new HashMap<>();
  65. modulesRelativePaths.put("project:module1", "module1_path");
  66. modulesRelativePaths.put("project:module1:module2", "module1_path/module2_path");
  67. ComponentUuidFactoryWithMigration underTest =
  68. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
  69. // migrated files
  70. assertThat(underTest.getOrCreateForKey("project:file1_path")).isEqualTo(file1.uuid());
  71. assertThat(underTest.getOrCreateForKey("project:module1_path/module2_path/file2_path")).isEqualTo(file2.uuid());
  72. // project remains the same
  73. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  74. }
  75. @Test
  76. public void migrate_project_with_disabled_components_no_path() {
  77. ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
  78. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  79. .setKey("project:module1"));
  80. ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
  81. .setKey("project:file1")
  82. .setPath("file1"));
  83. ComponentDto disabledFileNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project)
  84. .setKey("project:file2")
  85. .setPath(null)
  86. .setEnabled(false));
  87. Map<String, String> modulesRelativePaths = new HashMap<>();
  88. modulesRelativePaths.put("project:module1", "module1_path");
  89. ComponentUuidFactoryWithMigration underTest =
  90. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
  91. // migrated files
  92. assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
  93. // project remains the same
  94. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  95. }
  96. @Test
  97. public void migrate_project_with_disabled_components_same_path() {
  98. ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
  99. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  100. .setKey("project:module1"));
  101. ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
  102. .setKey("project:file1")
  103. .setPath("file1"));
  104. ComponentDto disabledFileSamePath = db.components().insertComponent(ComponentTesting.newFileDto(project)
  105. .setKey("project:file2")
  106. .setPath("file1")
  107. .setEnabled(false));
  108. Map<String, String> modulesRelativePaths = new HashMap<>();
  109. modulesRelativePaths.put("project:module1", "module1_path");
  110. ComponentUuidFactoryWithMigration underTest =
  111. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
  112. // migrated files
  113. assertThat(underTest.getOrCreateForKey("project:file1")).isEqualTo(file1.uuid());
  114. // project remains the same
  115. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  116. }
  117. @Test
  118. public void prefers_component_having_same_key() {
  119. ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
  120. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  121. .setKey("project:module1"));
  122. ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(module1)
  123. .setKey("project:module1:file1")
  124. .setPath("file1"));
  125. ComponentDto disabledFileSameKey = db.components().insertComponent(ComponentTesting.newFileDto(project)
  126. .setKey("project:module1/file1")
  127. .setPath("module1_path/file1")
  128. .setEnabled(false));
  129. Map<String, String> modulesRelativePaths = new HashMap<>();
  130. modulesRelativePaths.put("project:module1", "module1_path");
  131. ComponentUuidFactoryWithMigration underTest =
  132. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
  133. // in theory we should migrate file1. But since disabledFileSameKey already have the expected migrated key, let's reuse it.
  134. assertThat(underTest.getOrCreateForKey("project:module1/file1")).isEqualTo(disabledFileSameKey.uuid());
  135. // project remains the same
  136. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  137. }
  138. @Test
  139. public void migrate_branch_with_modules() {
  140. pathToKey = path -> path != null ? "project:" + path : "project";
  141. ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
  142. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  143. .setKey("project:module1"));
  144. ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(module1)
  145. .setKey("project:module1:module2"));
  146. ComponentDto file1 = db.components().insertComponent(ComponentTesting.newFileDto(project)
  147. .setKey("project:file1")
  148. .setPath("file1_path"));
  149. ComponentDto file2 = db.components().insertComponent(ComponentTesting.newFileDto(module2)
  150. .setKey("project:module1:module2:file2")
  151. .setPath("file2_path"));
  152. assertThat(file2.moduleUuidPath()).isEqualTo("." + project.uuid() + "." + module1.uuid() + "." + module2.uuid() + ".");
  153. Map<String, String> modulesRelativePaths = new HashMap<>();
  154. modulesRelativePaths.put("project:module1", "module1_path");
  155. modulesRelativePaths.put("project:module1:module2", "module1_path/module2_path");
  156. ComponentUuidFactoryWithMigration underTest =
  157. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
  158. // migrated files
  159. assertThat(underTest.getOrCreateForKey("project:file1_path")).isEqualTo(file1.uuid());
  160. assertThat(underTest.getOrCreateForKey("project:module1_path/module2_path/file2_path")).isEqualTo(file2.uuid());
  161. // project remains the same
  162. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  163. }
  164. @Test
  165. public void migrate_project_with_root_folders() {
  166. ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
  167. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  168. .setKey("project:module1"));
  169. ComponentDto dir1 = db.components().insertComponent(ComponentTesting.newDirectory(module1, "/")
  170. .setKey("project:module1:/"));
  171. Map<String, String> modulesRelativePaths = Collections.singletonMap("project:module1", "module1_path");
  172. ComponentUuidFactoryWithMigration underTest =
  173. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
  174. // project remains the same
  175. assertThat(underTest.getOrCreateForKey(project.getKey())).isEqualTo(project.uuid());
  176. // module migrated to folder
  177. assertThat(underTest.getOrCreateForKey("project:module1_path")).isEqualTo(module1.uuid());
  178. }
  179. @Test
  180. public void dont_override_root_uuid_if_module_path_is_not_sent() {
  181. ComponentDto project = db.components().insertPrivateProject(dto -> dto.setKey("project"));
  182. ComponentDto module1 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  183. .setKey("project:module1")
  184. .setEnabled(false));
  185. ComponentDto module2 = db.components().insertComponent(ComponentTesting.newModuleDto(project)
  186. .setKey("project:module2")
  187. .setEnabled(false));
  188. Map<String, String> modulesRelativePaths = new HashMap<>();
  189. modulesRelativePaths.put("project", "");
  190. modulesRelativePaths.put("project:module2", "module2");
  191. ComponentUuidFactoryWithMigration underTest =
  192. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), project.getKey(), mainBranch, pathToKey, modulesRelativePaths);
  193. // check root project.
  194. assertThat(underTest.getOrCreateForKey("project")).isEqualTo(project.uuid());
  195. }
  196. @Test
  197. public void generate_uuid_if_it_does_not_exist_in_db() {
  198. ComponentUuidFactoryWithMigration underTest =
  199. new ComponentUuidFactoryWithMigration(db.getDbClient(), db.getSession(), "theProjectKey", mainBranch, pathToKey, Collections.emptyMap());
  200. String generatedKey = underTest.getOrCreateForKey("foo");
  201. assertThat(generatedKey).isNotEmpty();
  202. // uuid is kept in memory for further calls with same key
  203. assertThat(underTest.getOrCreateForKey("foo")).isEqualTo(generatedKey);
  204. }
  205. }