您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ComponentUpdaterIT.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.server.component;
  21. import java.util.List;
  22. import java.util.Optional;
  23. import java.util.Set;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.sonar.api.resources.Qualifiers;
  29. import org.sonar.api.resources.Scopes;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.api.web.UserRole;
  32. import org.sonar.core.util.SequenceUuidFactory;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.DbTester;
  35. import org.sonar.db.audit.AuditPersister;
  36. import org.sonar.db.component.BranchDto;
  37. import org.sonar.db.component.BranchType;
  38. import org.sonar.db.component.ComponentDto;
  39. import org.sonar.db.component.ResourceTypesRule;
  40. import org.sonar.db.project.CreationMethod;
  41. import org.sonar.db.project.ProjectDto;
  42. import org.sonar.db.user.UserDto;
  43. import org.sonar.server.es.EsTester;
  44. import org.sonar.server.es.Indexers;
  45. import org.sonar.server.es.IndexersImpl;
  46. import org.sonar.server.es.TestIndexers;
  47. import org.sonar.server.exceptions.BadRequestException;
  48. import org.sonar.server.favorite.FavoriteUpdater;
  49. import org.sonar.server.l18n.I18nRule;
  50. import org.sonar.server.permission.GroupPermissionChanger;
  51. import org.sonar.server.permission.PermissionService;
  52. import org.sonar.server.permission.PermissionServiceImpl;
  53. import org.sonar.server.permission.PermissionTemplateService;
  54. import org.sonar.server.permission.PermissionUpdater;
  55. import org.sonar.server.permission.UserPermissionChange;
  56. import org.sonar.server.permission.UserPermissionChanger;
  57. import org.sonar.server.permission.index.FooIndexDefinition;
  58. import org.sonar.server.permission.index.PermissionIndexer;
  59. import org.sonar.server.project.DefaultBranchNameResolver;
  60. import static java.util.stream.IntStream.rangeClosed;
  61. import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
  62. import static org.assertj.core.api.Assertions.assertThat;
  63. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  64. import static org.mockito.ArgumentMatchers.any;
  65. import static org.mockito.ArgumentMatchers.argThat;
  66. import static org.mockito.ArgumentMatchers.eq;
  67. import static org.mockito.Mockito.mock;
  68. import static org.mockito.Mockito.never;
  69. import static org.mockito.Mockito.times;
  70. import static org.mockito.Mockito.verify;
  71. import static org.mockito.Mockito.when;
  72. import static org.sonar.api.resources.Qualifiers.APP;
  73. import static org.sonar.api.resources.Qualifiers.PROJECT;
  74. import static org.sonar.api.resources.Qualifiers.VIEW;
  75. import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
  76. public class ComponentUpdaterIT {
  77. private static final String DEFAULT_PROJECT_KEY = "project-key";
  78. private static final String DEFAULT_PROJECT_NAME = "project-name";
  79. private static final NewComponent DEFAULT_COMPONENT = NewComponent.newComponentBuilder()
  80. .setKey(DEFAULT_PROJECT_KEY)
  81. .setName(DEFAULT_PROJECT_NAME)
  82. .build();
  83. private static final NewComponent PRIVATE_COMPONENT = NewComponent.newComponentBuilder()
  84. .setKey(DEFAULT_PROJECT_KEY)
  85. .setName(DEFAULT_PROJECT_NAME)
  86. .setPrivate(true)
  87. .build();
  88. private static final String DEFAULT_USER_UUID = "user-uuid";
  89. public static final String DEFAULT_USER_LOGIN = "user-login";
  90. private final System2 system2 = System2.INSTANCE;
  91. private final AuditPersister auditPersister = mock();
  92. @Rule
  93. public final DbTester db = DbTester.create(system2, auditPersister);
  94. @Rule
  95. public final I18nRule i18n = new I18nRule().put("qualifier.TRK", "Project");
  96. private final TestIndexers projectIndexers = new TestIndexers();
  97. private final PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
  98. private final DefaultBranchNameResolver defaultBranchNameResolver = mock(DefaultBranchNameResolver.class);
  99. public EsTester es = EsTester.createCustom(new FooIndexDefinition());
  100. private final PermissionUpdater<UserPermissionChange> userPermissionUpdater = new PermissionUpdater(
  101. new IndexersImpl(new PermissionIndexer(db.getDbClient(), es.client())),
  102. Set.of(new UserPermissionChanger(db.getDbClient(), new SequenceUuidFactory()),
  103. new GroupPermissionChanger(db.getDbClient(), new SequenceUuidFactory())));
  104. private final PermissionService permissionService = new PermissionServiceImpl(new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT));
  105. private final ComponentUpdater underTest = new ComponentUpdater(db.getDbClient(), i18n, system2,
  106. permissionTemplateService,
  107. new FavoriteUpdater(db.getDbClient()),
  108. projectIndexers, new SequenceUuidFactory(), defaultBranchNameResolver, userPermissionUpdater, permissionService);
  109. @Before
  110. public void before() {
  111. when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn(DEFAULT_MAIN_BRANCH_NAME);
  112. }
  113. @Test
  114. public void persist_and_index_when_creating_project() {
  115. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  116. .newComponent(PRIVATE_COMPONENT)
  117. .creationMethod(CreationMethod.LOCAL_API)
  118. .build();
  119. ComponentCreationData returned = underTest.create(db.getSession(), creationParameters);
  120. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.mainBranchComponent().uuid());
  121. assertThat(loaded.getKey()).isEqualTo(DEFAULT_PROJECT_KEY);
  122. assertThat(loaded.name()).isEqualTo(DEFAULT_PROJECT_NAME);
  123. assertThat(loaded.longName()).isEqualTo(DEFAULT_PROJECT_NAME);
  124. assertThat(loaded.qualifier()).isEqualTo(Qualifiers.PROJECT);
  125. assertThat(loaded.scope()).isEqualTo(Scopes.PROJECT);
  126. assertThat(loaded.uuid()).isNotNull();
  127. assertThat(loaded.branchUuid()).isEqualTo(loaded.uuid());
  128. assertThat(loaded.isPrivate()).isEqualTo(PRIVATE_COMPONENT.isPrivate());
  129. assertThat(loaded.getCreatedAt()).isNotNull();
  130. assertThat(db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY)).isPresent();
  131. assertThat(projectIndexers.hasBeenCalledForEntity(returned.projectDto().getUuid(), Indexers.EntityEvent.CREATION)).isTrue();
  132. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.mainBranchComponent().uuid());
  133. assertThat(branch).isPresent();
  134. assertThat(branch.get().getKey()).isEqualTo(DEFAULT_MAIN_BRANCH_NAME);
  135. assertThat(branch.get().getMergeBranchUuid()).isNull();
  136. assertThat(branch.get().getBranchType()).isEqualTo(BranchType.BRANCH);
  137. assertThat(branch.get().getUuid()).isEqualTo(returned.mainBranchComponent().uuid());
  138. assertThat(branch.get().getProjectUuid()).isEqualTo(returned.projectDto().getUuid());
  139. }
  140. @Test
  141. public void create_project_with_main_branch_global_property() {
  142. when(defaultBranchNameResolver.getEffectiveMainBranchName()).thenReturn("main-branch-global");
  143. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  144. .newComponent(PRIVATE_COMPONENT)
  145. .creationMethod(CreationMethod.LOCAL_API)
  146. .build();
  147. ComponentDto returned = underTest.create(db.getSession(), creationParameters).mainBranchComponent();
  148. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.branchUuid());
  149. assertThat(branch).get().extracting(BranchDto::getBranchKey).isEqualTo("main-branch-global");
  150. }
  151. @Test
  152. public void persist_private_flag_true_when_creating_project() {
  153. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  154. .newComponent(PRIVATE_COMPONENT)
  155. .creationMethod(CreationMethod.LOCAL_API)
  156. .build();
  157. ComponentDto returned = underTest.create(db.getSession(), creationParameters).mainBranchComponent();
  158. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  159. assertThat(loaded.isPrivate()).isEqualTo(PRIVATE_COMPONENT.isPrivate());
  160. }
  161. @Test
  162. public void persist_private_flag_false_when_creating_project() {
  163. NewComponent project = NewComponent.newComponentBuilder()
  164. .setKey(DEFAULT_PROJECT_KEY)
  165. .setName(DEFAULT_PROJECT_NAME)
  166. .setPrivate(false)
  167. .build();
  168. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  169. .newComponent(project)
  170. .creationMethod(CreationMethod.LOCAL_API)
  171. .build();
  172. ComponentDto returned = underTest.create(db.getSession(), creationParameters).mainBranchComponent();
  173. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  174. assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate());
  175. }
  176. @Test
  177. public void create_view() {
  178. NewComponent view = NewComponent.newComponentBuilder()
  179. .setKey("view-key")
  180. .setName("view-name")
  181. .setQualifier(VIEW)
  182. .build();
  183. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  184. .newComponent(view)
  185. .creationMethod(CreationMethod.LOCAL_API)
  186. .build();
  187. ComponentDto returned = underTest.create(db.getSession(), creationParameters).mainBranchComponent();
  188. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  189. assertThat(loaded.getKey()).isEqualTo("view-key");
  190. assertThat(loaded.name()).isEqualTo("view-name");
  191. assertThat(loaded.qualifier()).isEqualTo("VW");
  192. assertThat(projectIndexers.hasBeenCalledForEntity(loaded.uuid(), Indexers.EntityEvent.CREATION)).isTrue();
  193. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid());
  194. assertThat(branch).isNotPresent();
  195. }
  196. @Test
  197. public void create_application() {
  198. NewComponent application = NewComponent.newComponentBuilder()
  199. .setKey("app-key")
  200. .setName("app-name")
  201. .setQualifier(APP)
  202. .build();
  203. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  204. .newComponent(application)
  205. .creationMethod(CreationMethod.LOCAL_API)
  206. .build();
  207. ComponentCreationData returned = underTest.create(db.getSession(), creationParameters);
  208. ProjectDto loaded = db.getDbClient().projectDao().selectByUuid(db.getSession(), returned.projectDto().getUuid()).get();
  209. assertThat(loaded.getKey()).isEqualTo("app-key");
  210. assertThat(loaded.getName()).isEqualTo("app-name");
  211. assertThat(loaded.getQualifier()).isEqualTo("APP");
  212. assertThat(projectIndexers.hasBeenCalledForEntity(loaded.getUuid(), Indexers.EntityEvent.CREATION)).isTrue();
  213. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.mainBranchComponent().uuid());
  214. assertThat(branch).isPresent();
  215. assertThat(branch.get().getKey()).isEqualTo(DEFAULT_MAIN_BRANCH_NAME);
  216. assertThat(branch.get().getMergeBranchUuid()).isNull();
  217. assertThat(branch.get().getBranchType()).isEqualTo(BranchType.BRANCH);
  218. assertThat(branch.get().getUuid()).isEqualTo(returned.mainBranchComponent().uuid());
  219. assertThat(branch.get().getProjectUuid()).isEqualTo(returned.projectDto().getUuid());
  220. }
  221. @Test
  222. public void apply_default_permission_template() {
  223. ComponentCreationParameters componentCreationParameters = ComponentCreationParameters.builder()
  224. .newComponent(DEFAULT_COMPONENT)
  225. .userLogin(DEFAULT_USER_LOGIN)
  226. .userUuid(DEFAULT_USER_UUID)
  227. .creationMethod(CreationMethod.LOCAL_API)
  228. .build();
  229. ProjectDto dto = underTest.create(db.getSession(), componentCreationParameters).projectDto();
  230. verify(permissionTemplateService).applyDefaultToNewComponent(db.getSession(), dto, DEFAULT_USER_UUID);
  231. }
  232. @Test
  233. public void add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() {
  234. UserDto userDto = db.users().insertUser();
  235. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  236. .newComponent(DEFAULT_COMPONENT)
  237. .userLogin(userDto.getLogin())
  238. .userUuid(userDto.getUuid())
  239. .creationMethod(CreationMethod.LOCAL_API)
  240. .build();
  241. when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), any(ProjectDto.class)))
  242. .thenReturn(true);
  243. ProjectDto dto = underTest.create(db.getSession(), creationParameters).projectDto();
  244. assertThat(db.favorites().hasFavorite(dto, userDto.getUuid())).isTrue();
  245. }
  246. @Test
  247. public void do_not_add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template_and_already_100_favorites() {
  248. UserDto user = db.users().insertUser();
  249. rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject().getProjectDto(), user.getUuid(), user.getLogin()));
  250. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  251. .newComponent(DEFAULT_COMPONENT)
  252. .userLogin(user.getLogin())
  253. .userUuid(user.getUuid())
  254. .creationMethod(CreationMethod.LOCAL_API)
  255. .build();
  256. when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(eq(db.getSession()), any(ProjectDto.class)))
  257. .thenReturn(true);
  258. ProjectDto dto = underTest.create(db.getSession(), creationParameters).projectDto();
  259. assertThat(db.favorites().hasFavorite(dto, user.getUuid())).isFalse();
  260. }
  261. @Test
  262. public void does_not_add_project_to_favorite_when_anonymously_created() {
  263. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  264. .newComponent(DEFAULT_COMPONENT)
  265. .creationMethod(CreationMethod.LOCAL_API)
  266. .build();
  267. ProjectDto projectDto = underTest.create(db.getSession(), creationParameters).projectDto();
  268. assertThat(db.favorites().hasNoFavorite(projectDto)).isTrue();
  269. }
  270. @Test
  271. public void fail_when_project_key_already_exists() {
  272. ComponentDto existing = db.components().insertPrivateProject().getMainBranchComponent();
  273. DbSession session = db.getSession();
  274. NewComponent project = NewComponent.newComponentBuilder()
  275. .setKey(existing.getKey())
  276. .setName(DEFAULT_PROJECT_NAME)
  277. .build();
  278. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  279. .newComponent(project)
  280. .creationMethod(CreationMethod.LOCAL_API)
  281. .build();
  282. assertThatThrownBy(() -> underTest.create(session, creationParameters))
  283. .isInstanceOf(BadRequestException.class)
  284. .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", existing.getKey(), existing.getKey());
  285. }
  286. @Test
  287. public void fail_when_key_has_bad_format() {
  288. DbSession session = db.getSession();
  289. NewComponent project = NewComponent.newComponentBuilder()
  290. .setKey("1234")
  291. .setName(DEFAULT_PROJECT_NAME)
  292. .build();
  293. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  294. .newComponent(project)
  295. .creationMethod(CreationMethod.LOCAL_API)
  296. .build();
  297. assertThatThrownBy(() -> underTest.create(session, creationParameters))
  298. .isInstanceOf(BadRequestException.class)
  299. .hasMessageContaining("Malformed key for Project: '1234'");
  300. }
  301. @Test
  302. public void fail_when_key_contains_percent_character() {
  303. DbSession session = db.getSession();
  304. NewComponent project = NewComponent.newComponentBuilder()
  305. .setKey("roject%Key")
  306. .setName(DEFAULT_PROJECT_NAME)
  307. .build();
  308. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  309. .newComponent(project)
  310. .creationMethod(CreationMethod.LOCAL_API)
  311. .build();
  312. assertThatThrownBy(() -> underTest.create(session, creationParameters))
  313. .isInstanceOf(BadRequestException.class)
  314. .hasMessageContaining("Malformed key for Project: 'roject%Key'");
  315. }
  316. @Test
  317. public void create_shouldFail_whenCreatingProjectWithExistingKeyButDifferentCase() {
  318. createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(PROJECT);
  319. }
  320. @Test
  321. public void create_shouldFail_whenCreatingPortfolioWithExistingKeyButDifferentCase() {
  322. createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(VIEW);
  323. }
  324. @Test
  325. public void create_shouldFail_whenCreatingApplicationWithExistingKeyButDifferentCase() {
  326. createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(APP);
  327. }
  328. private void createComponent_shouldFail_whenCreatingComponentWithExistingKeyButDifferentCase(String qualifier) {
  329. String existingKey = randomAlphabetic(5).toUpperCase();
  330. db.components().insertPrivateProject(component -> component.setKey(existingKey));
  331. String newKey = existingKey.toLowerCase();
  332. NewComponent project = NewComponent.newComponentBuilder()
  333. .setKey(newKey)
  334. .setName(DEFAULT_PROJECT_NAME)
  335. .setQualifier(qualifier)
  336. .build();
  337. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  338. .newComponent(project)
  339. .creationMethod(CreationMethod.LOCAL_API)
  340. .build();
  341. DbSession dbSession = db.getSession();
  342. assertThatThrownBy(() -> underTest.create(dbSession, creationParameters))
  343. .isInstanceOf(BadRequestException.class)
  344. .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s\"", newKey, existingKey);
  345. }
  346. @Test
  347. public void createComponent_shouldFail_whenCreatingComponentWithMultipleExistingKeyButDifferentCase() {
  348. String existingKey = randomAlphabetic(5).toUpperCase();
  349. String existingKeyLowerCase = existingKey.toLowerCase();
  350. db.components().insertPrivateProject(component -> component.setKey(existingKey));
  351. db.components().insertPrivateProject(component -> component.setKey(existingKeyLowerCase));
  352. String newKey = StringUtils.capitalize(existingKeyLowerCase);
  353. NewComponent project = NewComponent.newComponentBuilder()
  354. .setKey(newKey)
  355. .setName(DEFAULT_PROJECT_NAME)
  356. .build();
  357. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  358. .newComponent(project)
  359. .creationMethod(CreationMethod.LOCAL_API)
  360. .build();
  361. DbSession dbSession = db.getSession();
  362. assertThatThrownBy(() -> underTest.create(dbSession, creationParameters))
  363. .isInstanceOf(BadRequestException.class)
  364. .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s, %s\"", newKey, existingKey, existingKeyLowerCase);
  365. }
  366. @Test
  367. public void createComponent_shouldFail_whenCreatingComponentWithMultipleExistingPortfolioKeysButDifferentCase() {
  368. String existingKey = randomAlphabetic(5).toUpperCase();
  369. String existingKeyLowerCase = existingKey.toLowerCase();
  370. db.components().insertPrivatePortfolio(portfolio -> portfolio.setKey(existingKey));
  371. db.components().insertPrivatePortfolio(portfolio -> portfolio.setKey(existingKeyLowerCase));
  372. String newKey = StringUtils.capitalize(existingKeyLowerCase);
  373. NewComponent project = NewComponent.newComponentBuilder()
  374. .setKey(newKey)
  375. .setName(DEFAULT_PROJECT_NAME)
  376. .build();
  377. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  378. .newComponent(project)
  379. .creationMethod(CreationMethod.LOCAL_API)
  380. .build();
  381. DbSession dbSession = db.getSession();
  382. assertThatThrownBy(() -> underTest.create(dbSession, creationParameters))
  383. .isInstanceOf(BadRequestException.class)
  384. .hasMessage("Could not create Project with key: \"%s\". A similar key already exists: \"%s, %s\"", newKey, existingKey, existingKeyLowerCase);
  385. }
  386. @Test
  387. public void create_createsComponentWithMasterBranchName() {
  388. String componentNameAndKey = "createApplicationOrPortfolio";
  389. NewComponent app = NewComponent.newComponentBuilder()
  390. .setKey(componentNameAndKey)
  391. .setName(componentNameAndKey)
  392. .setQualifier("APP")
  393. .build();
  394. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  395. .newComponent(app)
  396. .creationMethod(CreationMethod.LOCAL_API)
  397. .build();
  398. ComponentDto appDto = underTest.create(db.getSession(), creationParameters).mainBranchComponent();
  399. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), appDto.branchUuid());
  400. assertThat(branch).isPresent();
  401. assertThat(branch.get().getBranchKey()).isEqualTo(DEFAULT_MAIN_BRANCH_NAME);
  402. }
  403. @Test
  404. public void createWithoutCommit_whenProjectIsManaged_doesntApplyPermissionTemplate() {
  405. UserDto userDto = db.users().insertUser();
  406. ComponentCreationParameters componentCreationParameters = ComponentCreationParameters.builder()
  407. .newComponent(DEFAULT_COMPONENT)
  408. .userLogin(userDto.getLogin())
  409. .userUuid(userDto.getUuid())
  410. .mainBranchName(null)
  411. .isManaged(true)
  412. .creationMethod(CreationMethod.LOCAL_API)
  413. .build();
  414. underTest.createWithoutCommit(db.getSession(), componentCreationParameters);
  415. verify(permissionTemplateService, never()).applyDefaultToNewComponent(any(), any(), any());
  416. }
  417. @Test
  418. public void createWithoutCommit_whenInsertingPortfolio_shouldOnlyAddOneEntryToAuditLogs() {
  419. String portfolioKey = "portfolio";
  420. NewComponent portfolio = NewComponent.newComponentBuilder()
  421. .setKey(portfolioKey)
  422. .setName(portfolioKey)
  423. .setQualifier(VIEW)
  424. .build();
  425. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  426. .newComponent(portfolio)
  427. .creationMethod(CreationMethod.LOCAL_API)
  428. .build();
  429. underTest.createWithoutCommit(db.getSession(), creationParameters);
  430. db.commit();
  431. verify(auditPersister, times(1)).addComponent(argThat(d -> d.equals(db.getSession())),
  432. argThat(newValue -> newValue.getComponentKey().equals(portfolioKey)));
  433. }
  434. @Test
  435. public void createWithoutCommit_whenProjectIsManagedAndPrivate_applyPublicPermissionsToCreator() {
  436. UserDto userDto = db.users().insertUser();
  437. NewComponent newComponent = NewComponent.newComponentBuilder()
  438. .setKey(DEFAULT_PROJECT_KEY)
  439. .setName(DEFAULT_PROJECT_NAME)
  440. .setPrivate(true)
  441. .build();
  442. DbSession session = db.getSession();
  443. ComponentCreationParameters componentCreationParameters = ComponentCreationParameters.builder()
  444. .newComponent(PRIVATE_COMPONENT)
  445. .userLogin(userDto.getLogin())
  446. .userUuid(userDto.getUuid())
  447. .mainBranchName(null)
  448. .isManaged(true)
  449. .creationMethod(CreationMethod.LOCAL_API)
  450. .build();
  451. ComponentCreationData componentCreationData = underTest.createWithoutCommit(session, componentCreationParameters);
  452. List<String> permissions = db.getDbClient().userPermissionDao().selectEntityPermissionsOfUser(session, userDto.getUuid(), componentCreationData.projectDto().getUuid());
  453. assertThat(permissions)
  454. .containsExactlyInAnyOrder(UserRole.USER, UserRole.CODEVIEWER);
  455. }
  456. @Test
  457. public void create_whenCreationMethodIsLocalApi_persistsIt() {
  458. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  459. .newComponent(DEFAULT_COMPONENT)
  460. .creationMethod(CreationMethod.LOCAL_API)
  461. .build();
  462. ProjectDto projectDto = underTest.create(db.getSession(), creationParameters).projectDto();
  463. assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.LOCAL_API);
  464. }
  465. @Test
  466. public void create_whenCreationMethodIsAlmImportBrowser_persistsIt() {
  467. ComponentCreationParameters creationParameters = ComponentCreationParameters.builder()
  468. .newComponent(DEFAULT_COMPONENT)
  469. .creationMethod(CreationMethod.ALM_IMPORT_BROWSER)
  470. .build();
  471. ProjectDto projectDto = underTest.create(db.getSession(), creationParameters).projectDto();
  472. assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.ALM_IMPORT_BROWSER);
  473. }
  474. }