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.

ChangelogActionTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 java.util.Date;
  22. import javax.annotation.Nullable;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.api.utils.DateUtils;
  29. import org.sonar.api.utils.System2;
  30. import org.sonar.core.issue.FieldDiffs;
  31. import org.sonar.db.DbTester;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.issue.IssueDto;
  34. import org.sonar.db.organization.OrganizationDto;
  35. import org.sonar.db.rule.RuleDto;
  36. import org.sonar.db.user.UserDto;
  37. import org.sonar.db.user.UserTesting;
  38. import org.sonar.server.exceptions.ForbiddenException;
  39. import org.sonar.server.issue.IssueFinder;
  40. import org.sonar.server.tester.UserSessionRule;
  41. import org.sonar.server.ws.TestRequest;
  42. import org.sonar.server.ws.WsActionTester;
  43. import org.sonarqube.ws.Issues.ChangelogWsResponse;
  44. import org.sonarqube.ws.Issues.ChangelogWsResponse.Changelog.Diff;
  45. import static java.util.Optional.ofNullable;
  46. import static org.assertj.core.api.Assertions.assertThat;
  47. import static org.assertj.core.groups.Tuple.tuple;
  48. import static org.sonar.api.web.UserRole.CODEVIEWER;
  49. import static org.sonar.api.web.UserRole.USER;
  50. import static org.sonar.db.component.ComponentTesting.newFileDto;
  51. import static org.sonar.db.issue.IssueTesting.newDto;
  52. import static org.sonar.db.rule.RuleTesting.newRuleDto;
  53. import static org.sonar.db.user.UserTesting.newUserDto;
  54. import static org.sonar.test.JsonAssert.assertJson;
  55. public class ChangelogActionTest {
  56. @Rule
  57. public ExpectedException expectedException = ExpectedException.none();
  58. @Rule
  59. public DbTester db = DbTester.create(System2.INSTANCE);
  60. @Rule
  61. public UserSessionRule userSession = UserSessionRule.standalone();
  62. private ComponentDto project;
  63. private ComponentDto file;
  64. private WsActionTester tester = new WsActionTester(new ChangelogAction(db.getDbClient(), new IssueFinder(db.getDbClient(), userSession), new AvatarResolverImpl(), userSession));
  65. @Before
  66. public void setUp() throws Exception {
  67. project = db.components().insertPrivateProject();
  68. file = db.components().insertComponent(newFileDto(project));
  69. }
  70. @Test
  71. public void return_changelog() {
  72. UserDto user = insertUser();
  73. IssueDto issueDto = db.issues().insertIssue(newIssue());
  74. userSession.logIn("john")
  75. .addMembership(db.getDefaultOrganization())
  76. .addProjectPermission(USER, project, file);
  77. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(user.getUuid()).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
  78. ChangelogWsResponse result = call(issueDto.getKey());
  79. assertThat(result.getChangelogList()).hasSize(1);
  80. assertThat(result.getChangelogList().get(0).getUser()).isNotNull().isEqualTo(user.getLogin());
  81. assertThat(result.getChangelogList().get(0).getUserName()).isNotNull().isEqualTo(user.getName());
  82. assertThat(result.getChangelogList().get(0).getAvatar()).isNotNull().isEqualTo("93942e96f5acd83e2e047ad8fe03114d");
  83. assertThat(result.getChangelogList().get(0).getCreationDate()).isNotEmpty();
  84. assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue).containsOnly(tuple("severity", "MAJOR", "BLOCKER"));
  85. }
  86. @Test
  87. public void return_empty_changelog_when_not_member() {
  88. UserDto user = insertUser();
  89. IssueDto issueDto = db.issues().insertIssue(newIssue());
  90. userSession.logIn("john")
  91. .addProjectPermission(USER, project, file);
  92. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(user.getUuid()).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
  93. ChangelogWsResponse result = call(issueDto.getKey());
  94. assertThat(result.getChangelogList()).hasSize(0);
  95. }
  96. @Test
  97. public void changelog_of_file_move_contains_file_names() {
  98. RuleDto rule = db.rules().insertRule(newRuleDto());
  99. OrganizationDto org = db.organizations().insert();
  100. ComponentDto project = db.components().insertPrivateProject(org);
  101. ComponentDto file1 = db.components().insertComponent(newFileDto(project));
  102. ComponentDto file2 = db.components().insertComponent(newFileDto(project));
  103. IssueDto issueDto = db.issues().insertIssue(newDto(rule, file2, project));
  104. userSession.logIn("john")
  105. .addMembership(org)
  106. .addProjectPermission(USER, project, file);
  107. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", file1.uuid(), file2.uuid()).setCreationDate(new Date()));
  108. ChangelogWsResponse result = call(issueDto.getKey());
  109. assertThat(result.getChangelogList()).hasSize(1);
  110. assertThat(result.getChangelogList().get(0).hasUser()).isFalse();
  111. assertThat(result.getChangelogList().get(0).getCreationDate()).isNotEmpty();
  112. assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue)
  113. .containsOnly(tuple("file", file1.longName(), file2.longName()));
  114. }
  115. @Test
  116. public void changelog_of_file_move_is_empty_when_files_does_not_exists() {
  117. IssueDto issueDto = db.issues().insertIssue(newIssue());
  118. userSession.logIn("john")
  119. .addMembership(db.getDefaultOrganization())
  120. .addProjectPermission(USER, project, file);
  121. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setDiff("file", "UNKNOWN_1", "UNKNOWN_2").setCreationDate(new Date()));
  122. ChangelogWsResponse result = call(issueDto.getKey());
  123. assertThat(result.getChangelogList()).hasSize(1);
  124. assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::hasOldValue, Diff::hasNewValue)
  125. .containsOnly(tuple("file", false, false));
  126. }
  127. @Test
  128. public void return_changelog_on_user_without_email() {
  129. UserDto user = db.users().insertUser(UserTesting.newUserDto("john", "John", null));
  130. IssueDto issueDto = db.issues().insertIssue(newIssue());
  131. userSession.logIn("john")
  132. .addMembership(db.getDefaultOrganization())
  133. .addProjectPermission(USER, project, file);
  134. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(user.getUuid()).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
  135. ChangelogWsResponse result = call(issueDto.getKey());
  136. assertThat(result.getChangelogList()).hasSize(1);
  137. assertThat(result.getChangelogList().get(0).getUser()).isNotNull().isEqualTo(user.getLogin());
  138. assertThat(result.getChangelogList().get(0).getUserName()).isNotNull().isEqualTo(user.getName());
  139. assertThat(result.getChangelogList().get(0).hasAvatar()).isFalse();
  140. }
  141. @Test
  142. public void return_changelog_not_having_user() {
  143. IssueDto issueDto = db.issues().insertIssue(newIssue());
  144. userSession.logIn("john")
  145. .addMembership(db.getDefaultOrganization())
  146. .addProjectPermission(USER, project, file);
  147. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(null).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
  148. ChangelogWsResponse result = call(issueDto.getKey());
  149. assertThat(result.getChangelogList()).hasSize(1);
  150. assertThat(result.getChangelogList().get(0).hasUser()).isFalse();
  151. assertThat(result.getChangelogList().get(0).hasUserName()).isFalse();
  152. assertThat(result.getChangelogList().get(0).hasAvatar()).isFalse();
  153. assertThat(result.getChangelogList().get(0).getDiffsList()).isNotEmpty();
  154. }
  155. @Test
  156. public void return_changelog_on_none_existing_user() {
  157. IssueDto issueDto = db.issues().insertIssue(newIssue());
  158. userSession.logIn("john")
  159. .addMembership(db.getDefaultOrganization())
  160. .addProjectPermission(USER, project, file);
  161. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid("UNKNOWN").setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()));
  162. ChangelogWsResponse result = call(issueDto.getKey());
  163. assertThat(result.getChangelogList()).hasSize(1);
  164. assertThat(result.getChangelogList().get(0).hasUser()).isFalse();
  165. assertThat(result.getChangelogList().get(0).hasUserName()).isFalse();
  166. assertThat(result.getChangelogList().get(0).hasAvatar()).isFalse();
  167. assertThat(result.getChangelogList().get(0).getDiffsList()).isNotEmpty();
  168. }
  169. @Test
  170. public void return_multiple_diffs() {
  171. UserDto user = insertUser();
  172. IssueDto issueDto = db.issues().insertIssue(newIssue());
  173. userSession.logIn("john")
  174. .addMembership(db.getDefaultOrganization())
  175. .addProjectPermission(USER, project, file);
  176. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(user.getUuid())
  177. .setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date())
  178. .setDiff("status", "RESOLVED", "CLOSED").setCreationDate(new Date()));
  179. ChangelogWsResponse result = call(issueDto.getKey());
  180. assertThat(result.getChangelogList()).hasSize(1);
  181. assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue)
  182. .containsOnly(tuple("severity", "MAJOR", "BLOCKER"), tuple("status", "RESOLVED", "CLOSED"));
  183. }
  184. @Test
  185. public void return_changelog_when_no_old_value() {
  186. UserDto user = insertUser();
  187. IssueDto issueDto = db.issues().insertIssue(newIssue());
  188. userSession.logIn("john")
  189. .addMembership(db.getDefaultOrganization())
  190. .addProjectPermission(USER, project, file);
  191. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(user.getUuid()).setDiff("severity", null, "BLOCKER").setCreationDate(new Date()));
  192. ChangelogWsResponse result = call(issueDto.getKey());
  193. assertThat(result.getChangelogList()).hasSize(1);
  194. assertThat(result.getChangelogList().get(0).getDiffsList().get(0).hasOldValue()).isFalse();
  195. }
  196. @Test
  197. public void return_changelog_when_no_new_value() {
  198. UserDto user = insertUser();
  199. IssueDto issueDto = db.issues().insertIssue(newIssue());
  200. userSession.logIn("john")
  201. .addMembership(db.getDefaultOrganization())
  202. .addProjectPermission(USER, project, file);
  203. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(user.getUuid()).setDiff("severity", "MAJOR", null).setCreationDate(new Date()));
  204. ChangelogWsResponse result = call(issueDto.getKey());
  205. assertThat(result.getChangelogList()).hasSize(1);
  206. assertThat(result.getChangelogList().get(0).getDiffsList().get(0).hasNewValue()).isFalse();
  207. }
  208. @Test
  209. public void return_many_changelog() {
  210. UserDto user = insertUser();
  211. IssueDto issueDto = db.issues().insertIssue(newIssue());
  212. userSession.logIn("john")
  213. .addMembership(db.getDefaultOrganization())
  214. .addProjectPermission(USER, project, file);
  215. db.issues().insertFieldDiffs(issueDto,
  216. new FieldDiffs().setUserUuid(user.getUuid()).setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date()),
  217. new FieldDiffs().setDiff("status", "RESOLVED", "CLOSED").setCreationDate(new Date()));
  218. ChangelogWsResponse result = call(issueDto.getKey());
  219. assertThat(result.getChangelogList()).hasSize(2);
  220. }
  221. @Test
  222. public void replace_technical_debt_key_by_effort() {
  223. UserDto user = insertUser();
  224. IssueDto issueDto = db.issues().insertIssue(newIssue());
  225. userSession.logIn("john")
  226. .addMembership(db.getDefaultOrganization())
  227. .addProjectPermission(USER, project, file);
  228. db.issues().insertFieldDiffs(issueDto, new FieldDiffs().setUserUuid(user.getUuid()).setDiff("technicalDebt", "10", "20").setCreationDate(new Date()));
  229. ChangelogWsResponse result = call(issueDto.getKey());
  230. assertThat(result.getChangelogList()).hasSize(1);
  231. assertThat(result.getChangelogList().get(0).getDiffsList()).extracting(Diff::getKey, Diff::getOldValue, Diff::getNewValue).containsOnly(tuple("effort", "10", "20"));
  232. }
  233. @Test
  234. public void return_empty_changelog_when_no_changes_on_issue() {
  235. IssueDto issueDto = db.issues().insertIssue(newIssue());
  236. userSession.logIn("john").addProjectPermission(USER, project, file);
  237. ChangelogWsResponse result = call(issueDto.getKey());
  238. assertThat(result.getChangelogList()).isEmpty();
  239. }
  240. @Test
  241. public void fail_when_not_enough_permission() {
  242. IssueDto issueDto = db.issues().insertIssue(newIssue());
  243. userSession.logIn("john").addProjectPermission(CODEVIEWER, project, file);
  244. expectedException.expect(ForbiddenException.class);
  245. call(issueDto.getKey());
  246. }
  247. @Test
  248. public void test_example() {
  249. UserDto user = db.users().insertUser(newUserDto("john.smith", "John Smith", "john@smith.com"));
  250. IssueDto issueDto = db.issues().insertIssue(newIssue());
  251. userSession.logIn("john")
  252. .addMembership(db.getDefaultOrganization())
  253. .addProjectPermission(USER, project, file);
  254. db.issues().insertFieldDiffs(issueDto, new FieldDiffs()
  255. .setUserUuid(user.getUuid())
  256. .setDiff("severity", "MAJOR", "BLOCKER").setCreationDate(new Date())
  257. .setCreationDate(DateUtils.parseDateTime("2014-03-04T23:03:44+0100")));
  258. String result = tester.newRequest().setParam("issue", issueDto.getKey()).execute().getInput();
  259. assertJson(result).isSimilarTo(getClass().getResource("changelog-example.json"));
  260. }
  261. @Test
  262. public void test_definition() {
  263. WebService.Action action = tester.getDef();
  264. assertThat(action.key()).isEqualTo("changelog");
  265. assertThat(action.isPost()).isFalse();
  266. assertThat(action.isInternal()).isFalse();
  267. assertThat(action.params()).hasSize(1);
  268. assertThat(action.responseExample()).isNotNull();
  269. }
  270. private ChangelogWsResponse call(@Nullable String issueKey) {
  271. TestRequest request = tester.newRequest();
  272. ofNullable(issueKey).ifPresent(e -> request.setParam("issue", e));
  273. return request.executeProtobuf(ChangelogWsResponse.class);
  274. }
  275. private IssueDto newIssue() {
  276. RuleDto rule = db.rules().insertRule(newRuleDto());
  277. return newDto(rule, file, project);
  278. }
  279. private UserDto insertUser() {
  280. return db.users().insertUser(user -> user.setEmail("test@email.com"));
  281. }
  282. }