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.

SetBaselineActionTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.projectanalysis.ws;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.tngtech.java.junit.dataprovider.DataProvider;
  23. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  24. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import javax.annotation.Nullable;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.ExpectedException;
  32. import org.junit.runner.RunWith;
  33. import org.sonar.api.server.ws.WebService;
  34. import org.sonar.api.utils.System2;
  35. import org.sonar.api.web.UserRole;
  36. import org.sonar.db.DbClient;
  37. import org.sonar.db.DbSession;
  38. import org.sonar.db.DbTester;
  39. import org.sonar.db.component.BranchDto;
  40. import org.sonar.db.component.BranchType;
  41. import org.sonar.db.component.ComponentDto;
  42. import org.sonar.db.component.ComponentTesting;
  43. import org.sonar.db.component.SnapshotDto;
  44. import org.sonar.server.component.TestComponentFinder;
  45. import org.sonar.server.exceptions.ForbiddenException;
  46. import org.sonar.server.exceptions.NotFoundException;
  47. import org.sonar.server.tester.UserSessionRule;
  48. import org.sonar.server.ws.TestRequest;
  49. import org.sonar.server.ws.WsActionTester;
  50. import static org.assertj.core.api.Assertions.assertThat;
  51. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS;
  52. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH;
  53. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_PROJECT;
  54. import static org.sonar.test.Matchers.regexMatcher;
  55. import static org.sonarqube.ws.client.WsRequest.Method.POST;
  56. @RunWith(DataProviderRunner.class)
  57. public class SetBaselineActionTest {
  58. @Rule
  59. public ExpectedException expectedException = ExpectedException.none();
  60. @Rule
  61. public UserSessionRule userSession = UserSessionRule.standalone();
  62. @Rule
  63. public DbTester db = DbTester.create(System2.INSTANCE);
  64. private DbClient dbClient = db.getDbClient();
  65. private DbSession dbSession = db.getSession();
  66. private WsActionTester ws = new WsActionTester(new SetBaselineAction(dbClient, userSession, TestComponentFinder.from(db)));
  67. @Test
  68. public void set_baseline_on_main_branch() {
  69. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  70. BranchDto branch = new BranchDto()
  71. .setBranchType(BranchType.LONG)
  72. .setProjectUuid(project.uuid())
  73. .setUuid(project.uuid())
  74. .setKey("master");
  75. db.components().insertComponent(project);
  76. db.getDbClient().branchDao().insert(dbSession, branch);
  77. SnapshotDto analysis = db.components().insertSnapshot(project);
  78. logInAsProjectAdministrator(project);
  79. call(ImmutableMap.of(PARAM_PROJECT, project.getKey(), PARAM_ANALYSIS, analysis.getUuid()));
  80. BranchDto loaded = dbClient.branchDao().selectByUuid(dbSession, branch.getUuid()).get();
  81. assertThat(loaded.getManualBaseline()).isEqualTo(analysis.getUuid());
  82. }
  83. @Test
  84. public void set_baseline_on_long_living_branch() {
  85. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  86. BranchDto branch = ComponentTesting.newBranchDto(project.projectUuid(), BranchType.LONG);
  87. db.components().insertProjectBranch(project, branch);
  88. ComponentDto branchComponentDto = ComponentTesting.newProjectBranch(project, branch);
  89. SnapshotDto analysis = db.components().insertSnapshot(branchComponentDto);
  90. logInAsProjectAdministrator(project);
  91. call(project.getKey(), branch.getKey(), analysis.getUuid());
  92. BranchDto loaded = dbClient.branchDao().selectByUuid(dbSession, branch.getUuid()).get();
  93. assertThat(loaded.getManualBaseline()).isEqualTo(analysis.getUuid());
  94. }
  95. @Test
  96. public void fail_when_user_is_not_admin() {
  97. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  98. BranchDto branch = ComponentTesting.newBranchDto(project.projectUuid(), BranchType.LONG);
  99. db.components().insertProjectBranch(project, branch);
  100. ComponentDto branchComponentDto = ComponentTesting.newProjectBranch(project, branch);
  101. SnapshotDto analysis = db.components().insertSnapshot(branchComponentDto);
  102. expectedException.expect(ForbiddenException.class);
  103. expectedException.expectMessage("Insufficient privileges");
  104. call(project.getKey(), branch.getKey(), analysis.getUuid());
  105. }
  106. @Test
  107. @UseDataProvider("missingOrEmptyParamsAndFailureMessage")
  108. public void fail_with_IAE_when_required_param_missing_or_empty(Map<String, String> params, String message) {
  109. expectedException.expect(IllegalArgumentException.class);
  110. expectedException.expectMessage(message);
  111. call(params);
  112. }
  113. @DataProvider
  114. public static Object[][] missingOrEmptyParamsAndFailureMessage() {
  115. MapBuilder builder = new MapBuilder()
  116. .put(PARAM_PROJECT, "project key")
  117. .put(PARAM_BRANCH, "branch key")
  118. .put(PARAM_ANALYSIS, "analysis uuid");
  119. return new Object[][] {
  120. {builder.put(PARAM_PROJECT, null).map, "The 'project' parameter is missing"},
  121. {builder.put(PARAM_PROJECT, "").map, "The 'project' parameter must not be empty"},
  122. {builder.put(PARAM_BRANCH, "").map, "The 'branch' parameter must not be empty"},
  123. {builder.put(PARAM_ANALYSIS, null).map, "The 'analysis' parameter is missing"},
  124. {builder.put(PARAM_ANALYSIS, "").map, "The 'analysis' parameter must not be empty"},
  125. };
  126. }
  127. @Test
  128. @UseDataProvider("nonexistentParamsAndFailureMessage")
  129. public void fail_with_IAE_when_required_param_nonexistent(Map<String, String> nonexistentParams, String regex) {
  130. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  131. BranchDto branch = ComponentTesting.newBranchDto(project.projectUuid(), BranchType.LONG);
  132. db.components().insertProjectBranch(project, branch);
  133. ComponentDto branchComponentDto = ComponentTesting.newProjectBranch(project, branch);
  134. SnapshotDto analysis = db.components().insertSnapshot(branchComponentDto);
  135. logInAsProjectAdministrator(project);
  136. Map<String, String> params = new HashMap<>();
  137. params.put(PARAM_PROJECT, project.getKey());
  138. params.put(PARAM_BRANCH, branch.getKey());
  139. params.put(PARAM_ANALYSIS, analysis.getUuid());
  140. params.putAll(nonexistentParams);
  141. expectedException.expect(NotFoundException.class);
  142. expectedException.expectMessage(regexMatcher(regex));
  143. call(params);
  144. }
  145. @DataProvider
  146. public static Object[][] nonexistentParamsAndFailureMessage() {
  147. MapBuilder builder = new MapBuilder();
  148. return new Object[][] {
  149. {builder.put(PARAM_PROJECT, "nonexistent").map, "Component 'nonexistent' on branch .* not found"},
  150. {builder.put(PARAM_BRANCH, "nonexistent").map, "Component .* on branch 'nonexistent' not found"},
  151. {builder.put(PARAM_ANALYSIS, "nonexistent").map, "Analysis 'nonexistent' is not found"},
  152. };
  153. }
  154. @Test
  155. public void fail_when_branch_does_not_belong_to_project() {
  156. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  157. BranchDto branch = ComponentTesting.newBranchDto(project.projectUuid(), BranchType.LONG);
  158. db.components().insertProjectBranch(project, branch);
  159. ComponentDto branchComponentDto = ComponentTesting.newProjectBranch(project, branch);
  160. SnapshotDto analysis = db.components().insertSnapshot(branchComponentDto);
  161. logInAsProjectAdministrator(project);
  162. ComponentDto otherProject = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  163. BranchDto otherBranch = ComponentTesting.newBranchDto(otherProject.projectUuid(), BranchType.LONG);
  164. db.components().insertProjectBranch(otherProject, otherBranch);
  165. ComponentTesting.newProjectBranch(otherProject, otherBranch);
  166. expectedException.expect(NotFoundException.class);
  167. expectedException.expectMessage(String.format("Component '%s' on branch '%s' not found", project.getKey(), otherBranch.getKey()));
  168. call(project.getKey(), otherBranch.getKey(), analysis.getUuid());
  169. }
  170. @Test
  171. public void fail_when_analysis_does_not_belong_to_main_branch_of_project() {
  172. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  173. BranchDto branch = new BranchDto()
  174. .setBranchType(BranchType.LONG)
  175. .setProjectUuid(project.uuid())
  176. .setUuid(project.uuid())
  177. .setKey("master");
  178. db.components().insertComponent(project);
  179. db.getDbClient().branchDao().insert(dbSession, branch);
  180. logInAsProjectAdministrator(project);
  181. ComponentDto otherProject = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  182. SnapshotDto otherAnalysis = db.components().insertSnapshot(otherProject);
  183. expectedException.expect(IllegalArgumentException.class);
  184. expectedException.expectMessage(String.format("Analysis '%s' does not belong to project '%s'",
  185. otherAnalysis.getUuid(), project.getKey()));
  186. call(ImmutableMap.of(PARAM_PROJECT, project.getKey(), PARAM_ANALYSIS, otherAnalysis.getUuid()));
  187. }
  188. @Test
  189. public void fail_when_analysis_does_not_belong_to_non_main_branch_of_project() {
  190. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  191. BranchDto branch = ComponentTesting.newBranchDto(project.projectUuid(), BranchType.LONG);
  192. db.components().insertProjectBranch(project, branch);
  193. logInAsProjectAdministrator(project);
  194. ComponentDto otherProject = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  195. SnapshotDto otherAnalysis = db.components().insertProjectAndSnapshot(otherProject);
  196. expectedException.expect(IllegalArgumentException.class);
  197. expectedException.expectMessage(String.format("Analysis '%s' does not belong to branch '%s' of project '%s'",
  198. otherAnalysis.getUuid(), branch.getKey(), project.getKey()));
  199. call(project.getKey(), branch.getKey(), otherAnalysis.getUuid());
  200. }
  201. @Test
  202. public void fail_when_branch_is_not_long() {
  203. ComponentDto project = ComponentTesting.newPrivateProjectDto(db.organizations().insert());
  204. BranchDto branch = ComponentTesting.newBranchDto(project.projectUuid(), BranchType.SHORT);
  205. db.components().insertProjectBranch(project, branch);
  206. ComponentDto branchComponentDto = ComponentTesting.newProjectBranch(project, branch);
  207. SnapshotDto analysis = db.components().insertSnapshot(branchComponentDto);
  208. logInAsProjectAdministrator(project);
  209. expectedException.expect(IllegalArgumentException.class);
  210. expectedException.expectMessage(String.format("Not a long-living branch: '%s'", branch.getKey()));
  211. call(project.getKey(), branch.getKey(), analysis.getUuid());
  212. }
  213. @Test
  214. public void ws_parameters() {
  215. WebService.Action definition = ws.getDef();
  216. assertThat(definition.isPost()).isTrue();
  217. assertThat(definition.key()).isEqualTo("set_baseline");
  218. assertThat(definition.since()).isEqualTo("7.7");
  219. assertThat(definition.isInternal()).isFalse();
  220. }
  221. private void logInAsProjectAdministrator(ComponentDto project) {
  222. userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
  223. }
  224. private void call(Map<String, String> params) {
  225. TestRequest httpRequest = ws.newRequest().setMethod(POST.name());
  226. for (Map.Entry<String, String> param : params.entrySet()) {
  227. httpRequest.setParam(param.getKey(), param.getValue());
  228. }
  229. httpRequest.execute();
  230. }
  231. private void call(String projectKey, String branchKey, String analysisUuid) {
  232. call(ImmutableMap.of(
  233. PARAM_PROJECT, projectKey,
  234. PARAM_BRANCH, branchKey,
  235. PARAM_ANALYSIS, analysisUuid));
  236. }
  237. private static class MapBuilder {
  238. private final Map<String, String> map;
  239. private MapBuilder() {
  240. this.map = Collections.emptyMap();
  241. }
  242. private MapBuilder(Map<String, String> map) {
  243. this.map = map;
  244. }
  245. public MapBuilder put(String key, @Nullable String value) {
  246. Map<String, String> copy = new HashMap<>(map);
  247. if (value == null) {
  248. copy.remove(key);
  249. } else {
  250. copy.put(key, value);
  251. }
  252. return new MapBuilder(copy);
  253. }
  254. }
  255. }