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.

ComponentTesting.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.db.component;
  21. import java.util.Date;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.resources.Qualifiers;
  24. import org.sonar.api.resources.Scopes;
  25. import org.sonar.core.util.Uuids;
  26. import org.sonar.db.project.ProjectDto;
  27. import static com.google.common.base.Preconditions.checkArgument;
  28. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  29. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  30. import static org.sonar.db.component.ComponentDto.BRANCH_KEY_SEPARATOR;
  31. import static org.sonar.db.component.ComponentDto.PULL_REQUEST_SEPARATOR;
  32. import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
  33. import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
  34. import static org.sonar.db.component.ComponentDto.formatUuidPathFromParent;
  35. import static org.sonar.db.component.ComponentDto.generateBranchKey;
  36. import static org.sonar.db.component.ComponentDto.generatePullRequestKey;
  37. public class ComponentTesting {
  38. public static ComponentDto newFileDto(ComponentDto subProjectOrProject) {
  39. return newFileDto(subProjectOrProject, null);
  40. }
  41. public static ComponentDto newFileDto(ComponentDto subProjectOrProject, @Nullable ComponentDto directory) {
  42. return newFileDto(subProjectOrProject, directory, Uuids.createFast());
  43. }
  44. public static ComponentDto newFileDto(ComponentDto module, @Nullable ComponentDto directory, String fileUuid) {
  45. String filename = "NAME_" + fileUuid;
  46. String path = directory != null ? directory.path() + "/" + filename : module.path() + "/" + filename;
  47. return newChildComponent(fileUuid, module, directory == null ? module : directory)
  48. .setDbKey(generateKey("FILE_KEY_" + fileUuid, module))
  49. .setName(filename)
  50. .setLongName(path)
  51. .setScope(Scopes.FILE)
  52. .setQualifier(Qualifiers.FILE)
  53. .setPath(path)
  54. .setCreatedAt(new Date())
  55. .setLanguage("xoo");
  56. }
  57. public static ComponentDto newDirectory(ComponentDto module, String path) {
  58. return newDirectory(module, Uuids.createFast(), path);
  59. }
  60. public static ComponentDto newDirectory(ComponentDto module, String uuid, String path) {
  61. String key = !path.equals("/") ? module.getKey() + ":" + path : module.getKey() + ":/";
  62. return newChildComponent(uuid, module, module)
  63. .setDbKey(generateKey(key, module))
  64. .setName(path)
  65. .setLongName(path)
  66. .setPath(path)
  67. .setScope(Scopes.DIRECTORY)
  68. .setQualifier(Qualifiers.DIRECTORY);
  69. }
  70. public static ComponentDto newSubView(ComponentDto viewOrSubView, String uuid, String key) {
  71. return newModuleDto(uuid, viewOrSubView)
  72. .setDbKey(key)
  73. .setName(key)
  74. .setLongName(key)
  75. .setScope(Scopes.PROJECT)
  76. .setQualifier(Qualifiers.SUBVIEW)
  77. .setPath(null);
  78. }
  79. public static ComponentDto newSubView(ComponentDto viewOrSubView) {
  80. String uuid = Uuids.createFast();
  81. return newSubView(viewOrSubView, uuid, "KEY_" + uuid);
  82. }
  83. public static ComponentDto newModuleDto(String uuid, ComponentDto parentModuleOrProject) {
  84. return newChildComponent(uuid, parentModuleOrProject, parentModuleOrProject)
  85. .setModuleUuidPath(parentModuleOrProject.moduleUuidPath() + uuid + UUID_PATH_SEPARATOR)
  86. .setDbKey(generateKey("MODULE_KEY_" + uuid, parentModuleOrProject))
  87. .setName("NAME_" + uuid)
  88. .setLongName("LONG_NAME_" + uuid)
  89. .setPath("module")
  90. .setScope(Scopes.PROJECT)
  91. .setQualifier(Qualifiers.MODULE)
  92. .setLanguage(null);
  93. }
  94. private static String generateKey(String key, ComponentDto parentModuleOrProject) {
  95. String branch = parentModuleOrProject.getBranch();
  96. if (branch != null) {
  97. return generateBranchKey(key, branch);
  98. }
  99. String pullRequest = parentModuleOrProject.getPullRequest();
  100. if (pullRequest != null) {
  101. return generatePullRequestKey(key, pullRequest);
  102. }
  103. return key;
  104. }
  105. public static ComponentDto newModuleDto(ComponentDto subProjectOrProject) {
  106. return newModuleDto(Uuids.createFast(), subProjectOrProject);
  107. }
  108. public static ComponentDto newPrivateProjectDto() {
  109. return newProjectDto(Uuids.createFast(), true);
  110. }
  111. public static ComponentDto newPrivateProjectDto(String uuid) {
  112. return newProjectDto(uuid, true);
  113. }
  114. public static ComponentDto newPublicProjectDto() {
  115. return newProjectDto(Uuids.createFast(), false);
  116. }
  117. public static ComponentDto newPublicProjectDto(String uuid) {
  118. return newProjectDto(uuid, false);
  119. }
  120. private static ComponentDto newProjectDto(String uuid, boolean isPrivate) {
  121. return new ComponentDto()
  122. .setUuid(uuid)
  123. .setUuidPath(UUID_PATH_OF_ROOT)
  124. .setProjectUuid(uuid)
  125. .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR)
  126. .setRootUuid(uuid)
  127. .setDbKey("KEY_" + uuid)
  128. .setName("NAME_" + uuid)
  129. .setLongName("LONG_NAME_" + uuid)
  130. .setDescription("DESCRIPTION_" + uuid)
  131. .setScope(Scopes.PROJECT)
  132. .setQualifier(Qualifiers.PROJECT)
  133. .setPath(null)
  134. .setLanguage(null)
  135. .setEnabled(true)
  136. .setPrivate(isPrivate);
  137. }
  138. public static ComponentDto newView() {
  139. return newView(Uuids.createFast());
  140. }
  141. public static ComponentDto newView(String uuid) {
  142. return newPrivateProjectDto(uuid)
  143. .setUuid(uuid)
  144. .setScope(Scopes.PROJECT)
  145. .setQualifier(Qualifiers.VIEW)
  146. .setPrivate(false);
  147. }
  148. public static ComponentDto newApplication() {
  149. return newView(Uuids.createFast()).setQualifier(Qualifiers.APP);
  150. }
  151. public static ComponentDto newProjectCopy(ComponentDto project, ComponentDto view) {
  152. return newProjectCopy(Uuids.createFast(), project, view);
  153. }
  154. public static ComponentDto newProjectCopy(String uuid, ComponentDto project, ComponentDto view) {
  155. return newChildComponent(uuid, view, view)
  156. .setDbKey(view.getDbKey() + project.getDbKey())
  157. .setName(project.name())
  158. .setLongName(project.longName())
  159. .setCopyComponentUuid(project.uuid())
  160. .setScope(Scopes.FILE)
  161. .setQualifier(Qualifiers.PROJECT)
  162. .setPath(null)
  163. .setLanguage(null);
  164. }
  165. public static ComponentDto newChildComponent(String uuid, ComponentDto moduleOrProject, ComponentDto parent) {
  166. checkArgument(moduleOrProject.isPrivate() == parent.isPrivate(),
  167. "private flag inconsistent between moduleOrProject (%s) and parent (%s)",
  168. moduleOrProject.isPrivate(), parent.isPrivate());
  169. return new ComponentDto()
  170. .setUuid(uuid)
  171. .setUuidPath(formatUuidPathFromParent(parent))
  172. .setProjectUuid(moduleOrProject.projectUuid())
  173. .setRootUuid(moduleOrProject.uuid())
  174. .setModuleUuid(moduleOrProject.uuid())
  175. .setModuleUuidPath(moduleOrProject.moduleUuidPath())
  176. .setMainBranchProjectUuid(moduleOrProject.getMainBranchProjectUuid())
  177. .setCreatedAt(new Date())
  178. .setEnabled(true)
  179. .setPrivate(moduleOrProject.isPrivate());
  180. }
  181. public static BranchDto newBranchDto(@Nullable String projectUuid, BranchType branchType) {
  182. String key = projectUuid == null ? null : "branch_" + randomAlphanumeric(248);
  183. return new BranchDto()
  184. .setKey(key)
  185. .setUuid(Uuids.createFast())
  186. // MainBranchProjectUuid will be null if it's a main branch
  187. .setProjectUuid(projectUuid)
  188. .setBranchType(branchType);
  189. }
  190. public static BranchDto newBranchDto(ComponentDto project) {
  191. return newBranchDto(project.projectUuid(), BranchType.BRANCH);
  192. }
  193. public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType) {
  194. boolean isMain = branchComponent.getMainBranchProjectUuid() == null;
  195. String projectUuid = isMain ? branchComponent.uuid() : branchComponent.getMainBranchProjectUuid();
  196. String key = isMain ? "master" : "branch_" + randomAlphanumeric(248);
  197. return new BranchDto()
  198. .setKey(key)
  199. .setUuid(branchComponent.uuid())
  200. .setProjectUuid(projectUuid)
  201. .setBranchType(branchType);
  202. }
  203. public static ComponentDto newBranchComponent(ProjectDto project, BranchDto branchDto) {
  204. String branchName = branchDto.getKey();
  205. String branchSeparator = branchDto.getBranchType() == PULL_REQUEST ? PULL_REQUEST_SEPARATOR : BRANCH_KEY_SEPARATOR;
  206. String uuid = branchDto.getUuid();
  207. return new ComponentDto()
  208. .setUuid(uuid)
  209. .setUuidPath(UUID_PATH_OF_ROOT)
  210. .setProjectUuid(uuid)
  211. .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR)
  212. .setRootUuid(uuid)
  213. // name of the branch is not mandatory on the main branch
  214. .setDbKey(branchName != null ? project.getKey() + branchSeparator + branchName : project.getKey())
  215. .setMainBranchProjectUuid(project.getUuid())
  216. .setName(project.getName())
  217. .setLongName(project.getName())
  218. .setDescription(project.getDescription())
  219. .setScope(Scopes.PROJECT)
  220. .setQualifier(project.getQualifier())
  221. .setPath(null)
  222. .setLanguage(null)
  223. .setEnabled(true)
  224. .setPrivate(project.isPrivate());
  225. }
  226. public static ComponentDto newBranchComponent(ComponentDto project, BranchDto branchDto) {
  227. checkArgument(project.qualifier().equals(Qualifiers.PROJECT) || project.qualifier().equals(Qualifiers.APP));
  228. checkArgument(project.getMainBranchProjectUuid() == null);
  229. String branchName = branchDto.getKey();
  230. String branchSeparator = branchDto.getBranchType() == PULL_REQUEST ? PULL_REQUEST_SEPARATOR : BRANCH_KEY_SEPARATOR;
  231. String uuid = branchDto.getUuid();
  232. return new ComponentDto()
  233. .setUuid(uuid)
  234. .setUuidPath(UUID_PATH_OF_ROOT)
  235. .setProjectUuid(uuid)
  236. .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR)
  237. .setRootUuid(uuid)
  238. // name of the branch is not mandatory on the main branch
  239. .setDbKey(branchName != null ? project.getDbKey() + branchSeparator + branchName : project.getKey())
  240. .setMainBranchProjectUuid(project.uuid())
  241. .setName(project.name())
  242. .setLongName(project.longName())
  243. .setDescription(project.description())
  244. .setScope(project.scope())
  245. .setQualifier(project.qualifier())
  246. .setPath(null)
  247. .setLanguage(null)
  248. .setEnabled(true)
  249. .setPrivate(project.isPrivate());
  250. }
  251. }