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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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.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.portfolio.PortfolioDto;
  27. import org.sonar.db.project.ProjectDto;
  28. import static com.google.common.base.Preconditions.checkArgument;
  29. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  30. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  31. import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
  32. import static org.sonar.db.component.ComponentDto.formatUuidPathFromParent;
  33. import static org.sonar.db.component.ComponentDto.generateBranchKey;
  34. import static org.sonar.db.portfolio.PortfolioDto.SelectionMode.NONE;
  35. public class ComponentTesting {
  36. public static ComponentDto newFileDto(ComponentDto branch) {
  37. return newFileDto(branch, (ComponentDto) null);
  38. }
  39. public static ComponentDto newFileDto(ComponentDto branch, @Nullable ComponentDto directory) {
  40. return newFileDto(branch, directory, Uuids.createFast());
  41. }
  42. public static ComponentDto newFileDto(ComponentDto branch, String mainBranchUuid) {
  43. return newFileDto(mainBranchUuid, branch, null);
  44. }
  45. public static ComponentDto newFileDto(String mainBranchUuid, ComponentDto projectOrBranch, @Nullable ComponentDto directory) {
  46. return newFileDto(projectOrBranch, directory, Uuids.createFast(), mainBranchUuid);
  47. }
  48. public static ComponentDto newFileDto(ComponentDto branch, @Nullable ComponentDto directory, String fileUuid) {
  49. return newFileDto(branch, directory, fileUuid, null);
  50. }
  51. public static ComponentDto newFileDto(ComponentDto branch, @Nullable ComponentDto directory, String fileUuid, @Nullable String mainBranchUuid) {
  52. String filename = "NAME_" + fileUuid;
  53. String path = directory != null ? directory.path() + "/" + filename : branch.path() + "/" + filename;
  54. return newChildComponent(fileUuid, branch, directory == null ? branch : directory)
  55. .setKey("FILE_KEY_" + fileUuid)
  56. .setName(filename)
  57. .setLongName(path)
  58. .setScope(Scopes.FILE)
  59. .setBranchUuid(branch.branchUuid())
  60. .setQualifier(Qualifiers.FILE)
  61. .setPath(path)
  62. .setCreatedAt(new Date())
  63. .setLanguage("xoo");
  64. }
  65. public static ComponentDto newDirectory(ComponentDto branch, String path) {
  66. return newDirectory(branch, Uuids.createFast(), path);
  67. }
  68. public static ComponentDto newDirectoryOnBranch(ComponentDto branch, String path, String mainBranchUuid) {
  69. return newDirectory(branch, Uuids.createFast(), path, mainBranchUuid);
  70. }
  71. private static ComponentDto newDirectory(ComponentDto branch, String uuid, String path, String mainBranchUuid) {
  72. String key = !path.equals("/") ? branch.getKey() + ":" + path : branch.getKey() + ":/";
  73. return newChildComponent(uuid, branch, branch)
  74. .setKey(key)
  75. .setName(path)
  76. .setLongName(path)
  77. .setBranchUuid(branch.branchUuid())
  78. .setPath(path)
  79. .setScope(Scopes.DIRECTORY)
  80. .setQualifier(Qualifiers.DIRECTORY);
  81. }
  82. public static ComponentDto newDirectory(ComponentDto branch, String uuid, String path) {
  83. return newDirectory(branch, uuid, path, null);
  84. }
  85. public static ComponentDto newSubPortfolio(ComponentDto portfolioOrSubPortfolio, String uuid, String key) {
  86. return newChildComponent(uuid, portfolioOrSubPortfolio, portfolioOrSubPortfolio)
  87. .setKey(key)
  88. .setName(key)
  89. .setLongName(key)
  90. .setScope(Scopes.PROJECT)
  91. .setQualifier(Qualifiers.SUBVIEW)
  92. .setPath(null);
  93. }
  94. public static ComponentDto newSubPortfolio(ComponentDto viewOrSubView) {
  95. String uuid = Uuids.createFast();
  96. return newSubPortfolio(viewOrSubView, uuid, "KEY_" + uuid);
  97. }
  98. public static ComponentDto newPrivateProjectDto() {
  99. return newProjectDto(Uuids.createFast(), true);
  100. }
  101. public static ComponentDto newPrivateProjectDto(String uuid) {
  102. return newProjectDto(uuid, true);
  103. }
  104. public static ComponentDto newPublicProjectDto() {
  105. return newProjectDto(Uuids.createFast(), false);
  106. }
  107. public static ComponentDto newPublicProjectDto(String uuid) {
  108. return newProjectDto(uuid, false);
  109. }
  110. private static ComponentDto newProjectDto(String uuid, boolean isPrivate) {
  111. return new ComponentDto()
  112. .setUuid(uuid)
  113. .setUuidPath(UUID_PATH_OF_ROOT)
  114. .setBranchUuid(uuid)
  115. .setKey("KEY_" + uuid)
  116. .setName("NAME_" + uuid)
  117. .setLongName("LONG_NAME_" + uuid)
  118. .setDescription("DESCRIPTION_" + uuid)
  119. .setScope(Scopes.PROJECT)
  120. .setQualifier(Qualifiers.PROJECT)
  121. .setPath(null)
  122. .setLanguage(null)
  123. .setEnabled(true)
  124. .setPrivate(isPrivate);
  125. }
  126. public static ComponentDto newPortfolio() {
  127. return newPortfolio(Uuids.createFast());
  128. }
  129. public static ComponentDto newPortfolio(String uuid) {
  130. return newPrivateProjectDto(uuid)
  131. .setUuid(uuid)
  132. .setScope(Scopes.PROJECT)
  133. .setQualifier(Qualifiers.VIEW)
  134. .setPrivate(false);
  135. }
  136. public static PortfolioDto newPortfolioDto(String uuid, String key, String name, @Nullable PortfolioDto parent) {
  137. return new PortfolioDto()
  138. .setUuid(uuid)
  139. .setKey(key)
  140. .setParentUuid(parent == null ? null : parent.getUuid())
  141. .setRootUuid(parent == null ? uuid : parent.getRootUuid())
  142. .setSelectionMode(NONE.name())
  143. .setCreatedAt(1L)
  144. .setUpdatedAt(1L)
  145. .setPrivate(false)
  146. .setName(name);
  147. }
  148. public static ComponentDto newApplication() {
  149. return newPortfolio(Uuids.createFast()).setQualifier(Qualifiers.APP);
  150. }
  151. public static ComponentDto newProjectCopy(ProjectData project, ProjectData view) {
  152. return newProjectCopy(Uuids.createFast(), project.getMainBranchComponent(), view.getMainBranchComponent());
  153. }
  154. public static ComponentDto newProjectCopy(ProjectData project, PortfolioData view) {
  155. return newProjectCopy(Uuids.createFast(), project.getMainBranchComponent(), view.getRootComponent());
  156. }
  157. public static ComponentDto newProjectCopy(ComponentDto project, ComponentDto view) {
  158. return newProjectCopy(Uuids.createFast(), project, view);
  159. }
  160. public static ComponentDto newProjectCopy(String uuid, ComponentDto project, ComponentDto view) {
  161. return newChildComponent(uuid, view, view)
  162. .setKey(view.getKey() + project.getKey())
  163. .setName(project.name())
  164. .setLongName(project.longName())
  165. .setCopyComponentUuid(project.uuid())
  166. .setScope(Scopes.FILE)
  167. .setQualifier(Qualifiers.PROJECT)
  168. .setPath(null)
  169. .setLanguage(null);
  170. }
  171. public static ComponentDto newProjectBranchCopy(String uuid, ComponentDto project, ComponentDto view, String branch) {
  172. return newChildComponent(uuid, view, view)
  173. .setKey(generateBranchKey(view.getKey() + project.getKey(), branch))
  174. .setName(project.name())
  175. .setLongName(project.longName())
  176. .setCopyComponentUuid(project.uuid())
  177. .setScope(Scopes.FILE)
  178. .setQualifier(Qualifiers.PROJECT)
  179. .setPath(null)
  180. .setLanguage(null);
  181. }
  182. public static ComponentDto newChildComponent(String uuid, ComponentDto branch, ComponentDto parent) {
  183. checkArgument(branch.isPrivate() == parent.isPrivate(),
  184. "private flag inconsistent between branch (%s) and parent (%s)",
  185. branch.isPrivate(), parent.isPrivate());
  186. return new ComponentDto()
  187. .setUuid(uuid)
  188. .setUuidPath(formatUuidPathFromParent(parent))
  189. .setKey(uuid)
  190. .setBranchUuid(branch.branchUuid())
  191. .setCreatedAt(new Date())
  192. .setEnabled(true)
  193. .setPrivate(branch.isPrivate());
  194. }
  195. public static BranchDto newBranchDto(@Nullable String projectUuid, BranchType branchType) {
  196. String key = "branch_" + randomAlphanumeric(248);
  197. return new BranchDto()
  198. .setKey(key)
  199. .setUuid(Uuids.createFast())
  200. .setIsMain(false)
  201. .setProjectUuid(projectUuid)
  202. .setBranchType(branchType);
  203. }
  204. public static BranchDto newBranchDto(ComponentDto project) {
  205. return newBranchDto(project.branchUuid(), BranchType.BRANCH);
  206. }
  207. public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType, String projectUuid) {
  208. String key = "branch_" + randomAlphanumeric(248);
  209. return new BranchDto()
  210. .setKey(key)
  211. .setIsMain(false)
  212. .setUuid(branchComponent.uuid())
  213. .setProjectUuid(projectUuid)
  214. .setBranchType(branchType);
  215. }
  216. public static BranchDto newMainBranchDto(ComponentDto branchComponent, String projectUUid) {
  217. return new BranchDto()
  218. .setKey(DEFAULT_MAIN_BRANCH_NAME)
  219. .setIsMain(true)
  220. .setUuid(branchComponent.uuid())
  221. .setProjectUuid(projectUUid)
  222. .setBranchType(BranchType.BRANCH);
  223. }
  224. public static BranchDto newMainBranchDto(String projectUUid) {
  225. return new BranchDto()
  226. .setKey(DEFAULT_MAIN_BRANCH_NAME)
  227. .setIsMain(true)
  228. .setUuid(Uuids.createFast())
  229. .setProjectUuid(projectUUid)
  230. .setBranchType(BranchType.BRANCH);
  231. }
  232. public static ProjectDto newProjectDto() {
  233. return newProjectDto("uuid").setPrivate(true);
  234. }
  235. public static ProjectDto newProjectDto(String projectUuid) {
  236. return new ProjectDto()
  237. .setKey("projectKey")
  238. .setUuid(projectUuid)
  239. .setName("projectName")
  240. .setQualifier(Qualifiers.PROJECT);
  241. }
  242. public static ProjectDto newApplicationDto() {
  243. return new ProjectDto()
  244. .setKey("appKey")
  245. .setUuid("uuid")
  246. .setName("appName")
  247. .setQualifier(Qualifiers.APP);
  248. }
  249. public static ComponentDto newBranchComponent(ProjectDto project, BranchDto branchDto) {
  250. String uuid = branchDto.getUuid();
  251. return new ComponentDto()
  252. .setUuid(uuid)
  253. .setUuidPath(UUID_PATH_OF_ROOT)
  254. .setBranchUuid(uuid)
  255. .setKey(project.getKey())
  256. .setName(project.getName())
  257. .setLongName(project.getName())
  258. .setDescription(project.getDescription())
  259. .setScope(Scopes.PROJECT)
  260. .setQualifier(project.getQualifier())
  261. .setPath(null)
  262. .setLanguage(null)
  263. .setEnabled(true)
  264. .setPrivate(project.isPrivate());
  265. }
  266. public static ComponentDto newBranchComponent(ComponentDto project, BranchDto branchDto) {
  267. checkArgument(project.qualifier().equals(Qualifiers.PROJECT) || project.qualifier().equals(Qualifiers.APP));
  268. String uuid = branchDto.getUuid();
  269. return new ComponentDto()
  270. .setUuid(uuid)
  271. .setUuidPath(UUID_PATH_OF_ROOT)
  272. .setBranchUuid(uuid)
  273. .setKey(project.getKey())
  274. .setName(project.name())
  275. .setLongName(project.longName())
  276. .setDescription(project.description())
  277. .setScope(project.scope())
  278. .setQualifier(project.qualifier())
  279. .setPath(null)
  280. .setLanguage(null)
  281. .setEnabled(true)
  282. .setPrivate(project.isPrivate());
  283. }
  284. }