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.

ComponentUpdaterTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.Optional;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.api.resources.Qualifiers;
  26. import org.sonar.api.resources.Scopes;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.DbTester;
  30. import org.sonar.db.component.BranchDto;
  31. import org.sonar.db.component.BranchType;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.organization.OrganizationDto;
  34. import org.sonar.db.user.UserDto;
  35. import org.sonar.server.es.ProjectIndexer;
  36. import org.sonar.server.es.TestProjectIndexers;
  37. import org.sonar.server.exceptions.BadRequestException;
  38. import org.sonar.server.favorite.FavoriteUpdater;
  39. import org.sonar.server.l18n.I18nRule;
  40. import org.sonar.server.permission.PermissionTemplateService;
  41. import static java.util.stream.IntStream.rangeClosed;
  42. import static org.assertj.core.api.Assertions.assertThat;
  43. import static org.mockito.ArgumentMatchers.any;
  44. import static org.mockito.ArgumentMatchers.eq;
  45. import static org.mockito.Mockito.mock;
  46. import static org.mockito.Mockito.verify;
  47. import static org.mockito.Mockito.when;
  48. import static org.sonar.api.resources.Qualifiers.APP;
  49. import static org.sonar.api.resources.Qualifiers.VIEW;
  50. public class ComponentUpdaterTest {
  51. private static final String DEFAULT_PROJECT_KEY = "project-key";
  52. private static final String DEFAULT_PROJECT_NAME = "project-name";
  53. private System2 system2 = System2.INSTANCE;
  54. @Rule
  55. public ExpectedException expectedException = ExpectedException.none();
  56. @Rule
  57. public DbTester db = DbTester.create(system2);
  58. @Rule
  59. public I18nRule i18n = new I18nRule().put("qualifier.TRK", "Project");
  60. private TestProjectIndexers projectIndexers = new TestProjectIndexers();
  61. private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
  62. private ComponentUpdater underTest = new ComponentUpdater(db.getDbClient(), i18n, system2,
  63. permissionTemplateService,
  64. new FavoriteUpdater(db.getDbClient()),
  65. projectIndexers);
  66. @Test
  67. public void persist_and_index_when_creating_project() {
  68. NewComponent project = NewComponent.newComponentBuilder()
  69. .setKey(DEFAULT_PROJECT_KEY)
  70. .setName(DEFAULT_PROJECT_NAME)
  71. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  72. .setPrivate(true)
  73. .build();
  74. ComponentDto returned = underTest.create(db.getSession(), project, null);
  75. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  76. assertThat(loaded.getDbKey()).isEqualTo(DEFAULT_PROJECT_KEY);
  77. assertThat(loaded.name()).isEqualTo(DEFAULT_PROJECT_NAME);
  78. assertThat(loaded.longName()).isEqualTo(DEFAULT_PROJECT_NAME);
  79. assertThat(loaded.qualifier()).isEqualTo(Qualifiers.PROJECT);
  80. assertThat(loaded.scope()).isEqualTo(Scopes.PROJECT);
  81. assertThat(loaded.getOrganizationUuid()).isEqualTo(db.getDefaultOrganization().getUuid());
  82. assertThat(loaded.uuid()).isNotNull();
  83. assertThat(loaded.projectUuid()).isEqualTo(loaded.uuid());
  84. assertThat(loaded.moduleUuid()).isNull();
  85. assertThat(loaded.moduleUuidPath()).isEqualTo("." + loaded.uuid() + ".");
  86. assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate());
  87. assertThat(loaded.getCreatedAt()).isNotNull();
  88. assertThat(db.getDbClient().componentDao().selectOrFailByKey(db.getSession(), DEFAULT_PROJECT_KEY)).isNotNull();
  89. assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue();
  90. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid());
  91. assertThat(branch).isPresent();
  92. assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
  93. assertThat(branch.get().getMergeBranchUuid()).isNull();
  94. assertThat(branch.get().getBranchType()).isEqualTo(BranchType.LONG);
  95. assertThat(branch.get().getUuid()).isEqualTo(returned.uuid());
  96. assertThat(branch.get().getProjectUuid()).isEqualTo(returned.uuid());
  97. }
  98. @Test
  99. public void persist_private_flag_true_when_creating_project() {
  100. OrganizationDto organization = db.organizations().insert();
  101. NewComponent project = NewComponent.newComponentBuilder()
  102. .setKey(DEFAULT_PROJECT_KEY)
  103. .setName(DEFAULT_PROJECT_NAME)
  104. .setOrganizationUuid(organization.getUuid())
  105. .setPrivate(true)
  106. .build();
  107. ComponentDto returned = underTest.create(db.getSession(), project, null);
  108. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  109. assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate());
  110. }
  111. @Test
  112. public void persist_private_flag_false_when_creating_project() {
  113. OrganizationDto organization = db.organizations().insert();
  114. NewComponent project = NewComponent.newComponentBuilder()
  115. .setKey(DEFAULT_PROJECT_KEY)
  116. .setName(DEFAULT_PROJECT_NAME)
  117. .setOrganizationUuid(organization.getUuid())
  118. .setPrivate(false)
  119. .build();
  120. ComponentDto returned = underTest.create(db.getSession(), project, null);
  121. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  122. assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate());
  123. }
  124. @Test
  125. public void create_project_with_deprecated_branch() {
  126. ComponentDto project = underTest.create(db.getSession(),
  127. NewComponent.newComponentBuilder()
  128. .setKey(DEFAULT_PROJECT_KEY)
  129. .setName(DEFAULT_PROJECT_NAME)
  130. .setDeprecatedBranch("origin/master")
  131. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  132. .build(),
  133. null);
  134. assertThat(project.getDbKey()).isEqualTo("project-key:origin/master");
  135. }
  136. @Test
  137. public void create_view() {
  138. NewComponent view = NewComponent.newComponentBuilder()
  139. .setKey("view-key")
  140. .setName("view-name")
  141. .setQualifier(VIEW)
  142. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  143. .build();
  144. ComponentDto returned = underTest.create(db.getSession(), view, null);
  145. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  146. assertThat(loaded.getDbKey()).isEqualTo("view-key");
  147. assertThat(loaded.name()).isEqualTo("view-name");
  148. assertThat(loaded.qualifier()).isEqualTo("VW");
  149. assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue();
  150. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid());
  151. assertThat(branch).isNotPresent();
  152. }
  153. @Test
  154. public void create_application() {
  155. NewComponent application = NewComponent.newComponentBuilder()
  156. .setKey("app-key")
  157. .setName("app-name")
  158. .setQualifier(APP)
  159. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  160. .build();
  161. ComponentDto returned = underTest.create(db.getSession(), application, null);
  162. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  163. assertThat(loaded.getDbKey()).isEqualTo("app-key");
  164. assertThat(loaded.name()).isEqualTo("app-name");
  165. assertThat(loaded.qualifier()).isEqualTo("APP");
  166. assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue();
  167. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid());
  168. assertThat(branch).isPresent();
  169. assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
  170. assertThat(branch.get().getMergeBranchUuid()).isNull();
  171. assertThat(branch.get().getBranchType()).isEqualTo(BranchType.LONG);
  172. assertThat(branch.get().getUuid()).isEqualTo(returned.uuid());
  173. assertThat(branch.get().getProjectUuid()).isEqualTo(returned.uuid());
  174. }
  175. @Test
  176. public void apply_default_permission_template() {
  177. int userId = 42;
  178. NewComponent project = NewComponent.newComponentBuilder()
  179. .setKey(DEFAULT_PROJECT_KEY)
  180. .setName(DEFAULT_PROJECT_NAME)
  181. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  182. .build();
  183. ComponentDto dto = underTest.create(db.getSession(), project, userId);
  184. verify(permissionTemplateService).applyDefault(db.getSession(), dto, userId);
  185. }
  186. @Test
  187. public void add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() {
  188. UserDto userDto = db.users().insertUser();
  189. NewComponent project = NewComponent.newComponentBuilder()
  190. .setKey(DEFAULT_PROJECT_KEY)
  191. .setName(DEFAULT_PROJECT_NAME)
  192. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  193. .build();
  194. when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), any(ComponentDto.class)))
  195. .thenReturn(true);
  196. ComponentDto dto = underTest.create(db.getSession(), project, userDto.getId());
  197. assertThat(db.favorites().hasFavorite(dto, userDto.getId())).isTrue();
  198. }
  199. @Test
  200. public void do_not_add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template_and_already_100_favorites() {
  201. UserDto user = db.users().insertUser();
  202. rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), user.getId()));
  203. NewComponent project = NewComponent.newComponentBuilder()
  204. .setKey(DEFAULT_PROJECT_KEY)
  205. .setName(DEFAULT_PROJECT_NAME)
  206. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  207. .build();
  208. when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(eq(db.getSession()), any(ComponentDto.class)))
  209. .thenReturn(true);
  210. ComponentDto dto = underTest.create(db.getSession(),
  211. project,
  212. user.getId());
  213. assertThat(db.favorites().hasFavorite(dto, user.getId())).isFalse();
  214. }
  215. @Test
  216. public void does_not_add_project_to_favorite_when_anonymously_created() {
  217. ComponentDto project = underTest.create(db.getSession(),
  218. NewComponent.newComponentBuilder()
  219. .setKey(DEFAULT_PROJECT_KEY)
  220. .setName(DEFAULT_PROJECT_NAME)
  221. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  222. .build(),
  223. null);
  224. assertThat(db.favorites().hasNoFavorite(project)).isTrue();
  225. }
  226. @Test
  227. public void does_not_add_project_to_favorite_when_project_has_no_permission_on_template() {
  228. ComponentDto project = underTest.create(db.getSession(),
  229. NewComponent.newComponentBuilder()
  230. .setKey(DEFAULT_PROJECT_KEY)
  231. .setName(DEFAULT_PROJECT_NAME)
  232. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  233. .build(),
  234. null);
  235. assertThat(db.favorites().hasNoFavorite(project)).isTrue();
  236. }
  237. @Test
  238. public void fail_when_project_key_already_exists() {
  239. ComponentDto existing = db.components().insertPrivateProject();
  240. expectedException.expect(BadRequestException.class);
  241. expectedException.expectMessage("Could not create Project, key already exists: " + existing.getDbKey());
  242. underTest.create(db.getSession(),
  243. NewComponent.newComponentBuilder()
  244. .setKey(existing.getDbKey())
  245. .setName(DEFAULT_PROJECT_NAME)
  246. .setOrganizationUuid(existing.getOrganizationUuid())
  247. .build(),
  248. null);
  249. }
  250. @Test
  251. public void fail_when_project_key_already_exists_on_other_organization() {
  252. ComponentDto existing = db.components().insertPrivateProject(db.organizations().insert());
  253. expectedException.expect(BadRequestException.class);
  254. expectedException.expectMessage("Could not create Project, key already exists: " + existing.getDbKey());
  255. underTest.create(db.getSession(),
  256. NewComponent.newComponentBuilder()
  257. .setKey(existing.getDbKey())
  258. .setName(DEFAULT_PROJECT_NAME)
  259. .setOrganizationUuid(existing.getOrganizationUuid())
  260. .build(),
  261. null);
  262. }
  263. @Test
  264. public void fail_when_key_has_bad_format() {
  265. expectedException.expect(BadRequestException.class);
  266. expectedException.expectMessage("Malformed key for Project: ' '");
  267. underTest.create(db.getSession(),
  268. NewComponent.newComponentBuilder()
  269. .setKey(" ")
  270. .setName(DEFAULT_PROJECT_NAME)
  271. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  272. .build(),
  273. null);
  274. }
  275. @Test
  276. public void properly_fail_when_key_contains_percent_character() {
  277. expectedException.expect(BadRequestException.class);
  278. expectedException.expectMessage("Malformed key for Project: ' '");
  279. underTest.create(db.getSession(),
  280. NewComponent.newComponentBuilder()
  281. .setKey(" ")
  282. .setName(DEFAULT_PROJECT_NAME)
  283. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  284. .build(),
  285. null);
  286. }
  287. @Test
  288. public void fail_to_create_new_component_on_invalid_branch() {
  289. expectedException.expect(BadRequestException.class);
  290. expectedException.expectMessage("Malformed branch for Project: origin?branch. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.");
  291. underTest.create(db.getSession(),
  292. NewComponent.newComponentBuilder()
  293. .setKey(DEFAULT_PROJECT_KEY)
  294. .setName(DEFAULT_PROJECT_NAME)
  295. .setDeprecatedBranch("origin?branch")
  296. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  297. .build(),
  298. null);
  299. }
  300. }