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.

SetTagsActionTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.issue.ws;
  21. import com.google.common.base.Joiner;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import javax.annotation.Nullable;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.mockito.ArgumentCaptor;
  29. import org.sonar.api.server.ws.Request;
  30. import org.sonar.api.server.ws.Response;
  31. import org.sonar.api.server.ws.WebService.Action;
  32. import org.sonar.api.server.ws.WebService.Param;
  33. import org.sonar.api.utils.System2;
  34. import org.sonar.core.issue.FieldDiffs;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbTester;
  37. import org.sonar.db.component.ComponentDto;
  38. import org.sonar.db.issue.IssueDto;
  39. import org.sonar.db.issue.IssueTesting;
  40. import org.sonar.db.rule.RuleDefinitionDto;
  41. import org.sonar.server.es.EsTester;
  42. import org.sonar.server.exceptions.ForbiddenException;
  43. import org.sonar.server.exceptions.UnauthorizedException;
  44. import org.sonar.server.issue.IssueFieldsSetter;
  45. import org.sonar.server.issue.IssueFinder;
  46. import org.sonar.server.issue.IssueUpdater;
  47. import org.sonar.server.issue.TestIssueChangePostProcessor;
  48. import org.sonar.server.issue.WebIssueStorage;
  49. import org.sonar.server.issue.index.IssueIndexer;
  50. import org.sonar.server.issue.index.IssueIteratorFactory;
  51. import org.sonar.server.notification.NotificationManager;
  52. import org.sonar.server.organization.DefaultOrganizationProvider;
  53. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  54. import org.sonar.server.rule.DefaultRuleFinder;
  55. import org.sonar.server.tester.UserSessionRule;
  56. import org.sonar.server.ws.TestRequest;
  57. import org.sonar.server.ws.TestResponse;
  58. import org.sonar.server.ws.WsActionTester;
  59. import static java.util.Collections.singletonList;
  60. import static java.util.Optional.ofNullable;
  61. import static org.assertj.core.api.Assertions.assertThat;
  62. import static org.mockito.ArgumentMatchers.any;
  63. import static org.mockito.ArgumentMatchers.eq;
  64. import static org.mockito.Mockito.mock;
  65. import static org.mockito.Mockito.verify;
  66. import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
  67. import static org.sonar.core.util.stream.MoreCollectors.join;
  68. import static org.sonar.db.component.ComponentTesting.newFileDto;
  69. public class SetTagsActionTest {
  70. @Rule
  71. public ExpectedException expectedException = ExpectedException.none();
  72. @Rule
  73. public DbTester db = DbTester.create();
  74. @Rule
  75. public EsTester es = EsTester.create();
  76. @Rule
  77. public UserSessionRule userSession = UserSessionRule.standalone();
  78. private System2 system2 = mock(System2.class);
  79. private DbClient dbClient = db.getDbClient();
  80. private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
  81. private OperationResponseWriter responseWriter = mock(OperationResponseWriter.class);
  82. private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient));
  83. private ArgumentCaptor<SearchResponseData> preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class);
  84. private TestIssueChangePostProcessor issueChangePostProcessor = new TestIssueChangePostProcessor();
  85. private WsActionTester ws = new WsActionTester(new SetTagsAction(userSession, dbClient, new IssueFinder(dbClient, userSession), new IssueFieldsSetter(),
  86. new IssueUpdater(dbClient,
  87. new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, defaultOrganizationProvider), issueIndexer), mock(NotificationManager.class), issueChangePostProcessor),
  88. responseWriter));
  89. @Test
  90. public void set_tags() {
  91. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  92. logIn(issueDto);
  93. call(issueDto.getKey(), "bug", "todo");
  94. verify(responseWriter).write(eq(issueDto.getKey()), preloadedSearchResponseDataCaptor.capture(), any(Request.class), any(Response.class));
  95. verifyContentOfPreloadedSearchResponseData(issueDto);
  96. IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
  97. assertThat(issueReloaded.getTags()).containsOnly("bug", "todo");
  98. assertThat(issueChangePostProcessor.wasCalled()).isFalse();
  99. }
  100. @Test
  101. public void remove_existing_tags_when_value_is_not_set() {
  102. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  103. logIn(issueDto);
  104. call(issueDto.getKey());
  105. IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
  106. assertThat(issueReloaded.getTags()).isEmpty();
  107. assertThat(issueChangePostProcessor.wasCalled()).isFalse();
  108. }
  109. @Test
  110. public void remove_existing_tags_when_value_is_empty_string() {
  111. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  112. logIn(issueDto);
  113. call(issueDto.getKey(), "");
  114. IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
  115. assertThat(issueReloaded.getTags()).isEmpty();
  116. }
  117. @Test
  118. public void set_tags_using_deprecated_key_param() {
  119. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  120. logIn(issueDto);
  121. ws.newRequest().setParam("key", issueDto.getKey()).setParam("tags", "bug").execute();
  122. IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
  123. assertThat(issueReloaded.getTags()).containsOnly("bug");
  124. }
  125. @Test
  126. public void tags_are_stored_as_lowercase() {
  127. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  128. logIn(issueDto);
  129. call(issueDto.getKey(), "bug", "Convention");
  130. IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
  131. assertThat(issueReloaded.getTags()).containsOnly("bug", "convention");
  132. }
  133. @Test
  134. public void empty_tags_are_ignored() {
  135. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  136. logIn(issueDto);
  137. call(issueDto.getKey(), "security", "", "convention");
  138. IssueDto issueReloaded = dbClient.issueDao().selectByKey(db.getSession(), issueDto.getKey()).get();
  139. assertThat(issueReloaded.getTags()).containsOnly("security", "convention");
  140. }
  141. @Test
  142. public void insert_entry_in_changelog_when_setting_tags() {
  143. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  144. logIn(issueDto);
  145. call(issueDto.getKey(), "new-tag");
  146. List<FieldDiffs> fieldDiffs = dbClient.issueChangeDao().selectChangelogByIssue(db.getSession(), issueDto.getKey());
  147. assertThat(fieldDiffs).hasSize(1);
  148. assertThat(fieldDiffs.get(0).diffs()).hasSize(1);
  149. assertThat(fieldDiffs.get(0).diffs().get("tags").oldValue()).isEqualTo("old-tag");
  150. assertThat(fieldDiffs.get(0).diffs().get("tags").newValue()).isEqualTo("new-tag");
  151. }
  152. @Test
  153. public void fail_when_tag_use_bad_format() {
  154. IssueDto issueDto = db.issues().insertIssue(newIssue().setTags(singletonList("old-tag")));
  155. logIn(issueDto);
  156. expectedException.expect(IllegalArgumentException.class);
  157. expectedException.expectMessage("Tags 'pol op' are invalid. Rule tags accept only the characters: a-z, 0-9, '+', '-', '#', '.'");
  158. call(issueDto.getKey(), "pol op");
  159. }
  160. @Test
  161. public void fail_when_not_authenticated() {
  162. expectedException.expect(UnauthorizedException.class);
  163. call("ABCD", "bug");
  164. }
  165. @Test
  166. public void fail_when_missing_browse_permission() {
  167. IssueDto issueDto = db.issues().insertIssue();
  168. logInAndAddProjectPermission(issueDto, ISSUE_ADMIN);
  169. expectedException.expect(ForbiddenException.class);
  170. call(issueDto.getKey(), "bug");
  171. }
  172. @Test
  173. public void test_definition() {
  174. Action action = ws.getDef();
  175. assertThat(action.description()).isNotEmpty();
  176. assertThat(action.responseExampleAsString()).isNotEmpty();
  177. assertThat(action.isPost()).isTrue();
  178. assertThat(action.isInternal()).isFalse();
  179. assertThat(action.params()).hasSize(2);
  180. Param query = action.param("issue");
  181. assertThat(query.isRequired()).isTrue();
  182. assertThat(query.description()).isNotEmpty();
  183. assertThat(query.exampleValue()).isNotEmpty();
  184. Param pageSize = action.param("tags");
  185. assertThat(pageSize.isRequired()).isFalse();
  186. assertThat(pageSize.defaultValue()).isNull();
  187. assertThat(pageSize.description()).isNotEmpty();
  188. assertThat(pageSize.exampleValue()).isNotEmpty();
  189. }
  190. private TestResponse call(@Nullable String issueKey, String... tags) {
  191. TestRequest request = ws.newRequest();
  192. ofNullable(issueKey).ifPresent(issue -> request.setParam("issue", issue));
  193. if (tags.length > 0) {
  194. request.setParam("tags", Arrays.stream(tags).collect(join(Joiner.on(","))));
  195. }
  196. return request.execute();
  197. }
  198. private IssueDto newIssue() {
  199. RuleDefinitionDto rule = db.rules().insert();
  200. ComponentDto project = db.components().insertPublicProject();
  201. ComponentDto file = db.components().insertComponent(newFileDto(project));
  202. return IssueTesting.newIssue(rule, project, file);
  203. }
  204. private void logIn(IssueDto issueDto) {
  205. userSession.logIn("john").registerComponents(
  206. dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get(),
  207. dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getComponentUuid()).get());
  208. }
  209. private void logInAndAddProjectPermission(IssueDto issueDto, String permission) {
  210. userSession.logIn("john").addProjectPermission(permission, dbClient.componentDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get());
  211. }
  212. private void verifyContentOfPreloadedSearchResponseData(IssueDto issue) {
  213. SearchResponseData preloadedSearchResponseData = preloadedSearchResponseDataCaptor.getValue();
  214. assertThat(preloadedSearchResponseData.getIssues())
  215. .extracting(IssueDto::getKey)
  216. .containsOnly(issue.getKey());
  217. assertThat(preloadedSearchResponseData.getRules())
  218. .extracting(RuleDefinitionDto::getKey)
  219. .containsOnly(issue.getRuleKey());
  220. assertThat(preloadedSearchResponseData.getComponents())
  221. .extracting(ComponentDto::uuid)
  222. .containsOnly(issue.getComponentUuid(), issue.getProjectUuid());
  223. }
  224. }