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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_view() {
  126. NewComponent view = NewComponent.newComponentBuilder()
  127. .setKey("view-key")
  128. .setName("view-name")
  129. .setQualifier(VIEW)
  130. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  131. .build();
  132. ComponentDto returned = underTest.create(db.getSession(), view, null);
  133. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  134. assertThat(loaded.getDbKey()).isEqualTo("view-key");
  135. assertThat(loaded.name()).isEqualTo("view-name");
  136. assertThat(loaded.qualifier()).isEqualTo("VW");
  137. assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue();
  138. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid());
  139. assertThat(branch).isNotPresent();
  140. }
  141. @Test
  142. public void create_application() {
  143. NewComponent application = NewComponent.newComponentBuilder()
  144. .setKey("app-key")
  145. .setName("app-name")
  146. .setQualifier(APP)
  147. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  148. .build();
  149. ComponentDto returned = underTest.create(db.getSession(), application, null);
  150. ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
  151. assertThat(loaded.getDbKey()).isEqualTo("app-key");
  152. assertThat(loaded.name()).isEqualTo("app-name");
  153. assertThat(loaded.qualifier()).isEqualTo("APP");
  154. assertThat(projectIndexers.hasBeenCalled(loaded.uuid(), ProjectIndexer.Cause.PROJECT_CREATION)).isTrue();
  155. Optional<BranchDto> branch = db.getDbClient().branchDao().selectByUuid(db.getSession(), returned.uuid());
  156. assertThat(branch).isPresent();
  157. assertThat(branch.get().getKey()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
  158. assertThat(branch.get().getMergeBranchUuid()).isNull();
  159. assertThat(branch.get().getBranchType()).isEqualTo(BranchType.LONG);
  160. assertThat(branch.get().getUuid()).isEqualTo(returned.uuid());
  161. assertThat(branch.get().getProjectUuid()).isEqualTo(returned.uuid());
  162. }
  163. @Test
  164. public void apply_default_permission_template() {
  165. int userId = 42;
  166. NewComponent project = NewComponent.newComponentBuilder()
  167. .setKey(DEFAULT_PROJECT_KEY)
  168. .setName(DEFAULT_PROJECT_NAME)
  169. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  170. .build();
  171. ComponentDto dto = underTest.create(db.getSession(), project, userId);
  172. verify(permissionTemplateService).applyDefault(db.getSession(), dto, userId);
  173. }
  174. @Test
  175. public void add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() {
  176. UserDto userDto = db.users().insertUser();
  177. NewComponent project = NewComponent.newComponentBuilder()
  178. .setKey(DEFAULT_PROJECT_KEY)
  179. .setName(DEFAULT_PROJECT_NAME)
  180. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  181. .build();
  182. when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(any(DbSession.class), any(ComponentDto.class)))
  183. .thenReturn(true);
  184. ComponentDto dto = underTest.create(db.getSession(), project, userDto.getId());
  185. assertThat(db.favorites().hasFavorite(dto, userDto.getId())).isTrue();
  186. }
  187. @Test
  188. public void do_not_add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template_and_already_100_favorites() {
  189. UserDto user = db.users().insertUser();
  190. rangeClosed(1, 100).forEach(i -> db.favorites().add(db.components().insertPrivateProject(), user.getId()));
  191. NewComponent project = NewComponent.newComponentBuilder()
  192. .setKey(DEFAULT_PROJECT_KEY)
  193. .setName(DEFAULT_PROJECT_NAME)
  194. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  195. .build();
  196. when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(eq(db.getSession()), any(ComponentDto.class)))
  197. .thenReturn(true);
  198. ComponentDto dto = underTest.create(db.getSession(),
  199. project,
  200. user.getId());
  201. assertThat(db.favorites().hasFavorite(dto, user.getId())).isFalse();
  202. }
  203. @Test
  204. public void does_not_add_project_to_favorite_when_anonymously_created() {
  205. ComponentDto project = underTest.create(db.getSession(),
  206. NewComponent.newComponentBuilder()
  207. .setKey(DEFAULT_PROJECT_KEY)
  208. .setName(DEFAULT_PROJECT_NAME)
  209. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  210. .build(),
  211. null);
  212. assertThat(db.favorites().hasNoFavorite(project)).isTrue();
  213. }
  214. @Test
  215. public void does_not_add_project_to_favorite_when_project_has_no_permission_on_template() {
  216. ComponentDto project = underTest.create(db.getSession(),
  217. NewComponent.newComponentBuilder()
  218. .setKey(DEFAULT_PROJECT_KEY)
  219. .setName(DEFAULT_PROJECT_NAME)
  220. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  221. .build(),
  222. null);
  223. assertThat(db.favorites().hasNoFavorite(project)).isTrue();
  224. }
  225. @Test
  226. public void fail_when_project_key_already_exists() {
  227. ComponentDto existing = db.components().insertPrivateProject();
  228. expectedException.expect(BadRequestException.class);
  229. expectedException.expectMessage("Could not create Project, key already exists: " + existing.getDbKey());
  230. underTest.create(db.getSession(),
  231. NewComponent.newComponentBuilder()
  232. .setKey(existing.getDbKey())
  233. .setName(DEFAULT_PROJECT_NAME)
  234. .setOrganizationUuid(existing.getOrganizationUuid())
  235. .build(),
  236. null);
  237. }
  238. @Test
  239. public void fail_when_project_key_already_exists_on_other_organization() {
  240. ComponentDto existing = db.components().insertPrivateProject(db.organizations().insert());
  241. expectedException.expect(BadRequestException.class);
  242. expectedException.expectMessage("Could not create Project, key already exists: " + existing.getDbKey());
  243. underTest.create(db.getSession(),
  244. NewComponent.newComponentBuilder()
  245. .setKey(existing.getDbKey())
  246. .setName(DEFAULT_PROJECT_NAME)
  247. .setOrganizationUuid(existing.getOrganizationUuid())
  248. .build(),
  249. null);
  250. }
  251. @Test
  252. public void fail_when_key_has_bad_format() {
  253. expectedException.expect(BadRequestException.class);
  254. expectedException.expectMessage("Malformed key for Project: ' '");
  255. underTest.create(db.getSession(),
  256. NewComponent.newComponentBuilder()
  257. .setKey(" ")
  258. .setName(DEFAULT_PROJECT_NAME)
  259. .setOrganizationUuid(db.getDefaultOrganization().getUuid())
  260. .build(),
  261. null);
  262. }
  263. @Test
  264. public void properly_fail_when_key_contains_percent_character() {
  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. }