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.

IssueFieldsSetterTest.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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;
  21. import java.util.Arrays;
  22. import java.util.Calendar;
  23. import java.util.Date;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Random;
  28. import java.util.Set;
  29. import org.apache.commons.lang.time.DateUtils;
  30. import org.junit.Test;
  31. import org.sonar.api.issue.Issue;
  32. import org.sonar.api.issue.impact.Severity;
  33. import org.sonar.api.issue.impact.SoftwareQuality;
  34. import org.sonar.api.rules.CleanCodeAttribute;
  35. import org.sonar.api.rules.RuleType;
  36. import org.sonar.api.utils.Duration;
  37. import org.sonar.core.issue.DefaultIssue;
  38. import org.sonar.core.issue.FieldDiffs;
  39. import org.sonar.core.issue.IssueChangeContext;
  40. import org.sonar.core.issue.status.IssueStatus;
  41. import org.sonar.db.protobuf.DbCommons;
  42. import org.sonar.db.protobuf.DbIssues;
  43. import org.sonar.db.protobuf.DbIssues.MessageFormattingType;
  44. import org.sonar.db.user.UserDto;
  45. import org.sonar.db.user.UserIdDto;
  46. import static org.assertj.core.api.Assertions.assertThat;
  47. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  48. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByUserBuilder;
  49. import static org.sonar.db.protobuf.DbIssues.MessageFormattingType.CODE;
  50. import static org.sonar.db.user.UserTesting.newUserDto;
  51. import static org.sonar.server.issue.IssueFieldsSetter.ASSIGNEE;
  52. import static org.sonar.server.issue.IssueFieldsSetter.SEVERITY;
  53. import static org.sonar.server.issue.IssueFieldsSetter.STATUS;
  54. import static org.sonar.server.issue.IssueFieldsSetter.TECHNICAL_DEBT;
  55. import static org.sonar.server.issue.IssueFieldsSetter.TYPE;
  56. import static org.sonar.server.issue.IssueFieldsSetter.UNUSED;
  57. public class IssueFieldsSetterTest {
  58. private final String DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY = "spring";
  59. private final DefaultIssue issue = new DefaultIssue();
  60. private final IssueChangeContext context = issueChangeContextByUserBuilder(new Date(), "user_uuid").build();
  61. private final IssueFieldsSetter underTest = new IssueFieldsSetter();
  62. @Test
  63. public void assign() {
  64. UserDto user = newUserDto().setLogin("emmerik").setName("Emmerik");
  65. boolean updated = underTest.assign(issue, user, context);
  66. assertThat(updated).isTrue();
  67. assertThat(issue.assignee()).isEqualTo(user.getUuid());
  68. assertThat(issue.mustSendNotifications()).isTrue();
  69. FieldDiffs.Diff diff = issue.currentChange().get(ASSIGNEE);
  70. assertThat(diff.oldValue()).isEqualTo(UNUSED);
  71. assertThat(diff.newValue()).isEqualTo(user.getName());
  72. }
  73. @Test
  74. public void unassign() {
  75. issue.setAssigneeUuid("user_uuid");
  76. boolean updated = underTest.assign(issue, null, context);
  77. assertThat(updated).isTrue();
  78. assertThat(issue.assignee()).isNull();
  79. assertThat(issue.mustSendNotifications()).isTrue();
  80. FieldDiffs.Diff diff = issue.currentChange().get(ASSIGNEE);
  81. assertThat(diff.oldValue()).isEqualTo(UNUSED);
  82. assertThat(diff.newValue()).isNull();
  83. }
  84. @Test
  85. public void change_assignee() {
  86. UserDto user = newUserDto().setLogin("emmerik").setName("Emmerik");
  87. issue.setAssigneeUuid("user_uuid");
  88. boolean updated = underTest.assign(issue, user, context);
  89. assertThat(updated).isTrue();
  90. assertThat(issue.assignee()).isEqualTo(user.getUuid());
  91. assertThat(issue.mustSendNotifications()).isTrue();
  92. FieldDiffs.Diff diff = issue.currentChange().get(ASSIGNEE);
  93. assertThat(diff.oldValue()).isEqualTo(UNUSED);
  94. assertThat(diff.newValue()).isEqualTo(user.getName());
  95. }
  96. @Test
  97. public void not_change_assignee() {
  98. UserDto user = newUserDto().setLogin("morgan").setName("Morgan");
  99. issue.setAssigneeUuid(user.getUuid());
  100. boolean updated = underTest.assign(issue, user, context);
  101. assertThat(updated).isFalse();
  102. assertThat(issue.currentChange()).isNull();
  103. assertThat(issue.mustSendNotifications()).isFalse();
  104. }
  105. @Test
  106. public void set_new_assignee() {
  107. boolean updated = underTest.setNewAssignee(issue, new UserIdDto("user_uuid", "user_login"), context);
  108. assertThat(updated).isTrue();
  109. assertThat(issue.assignee()).isEqualTo("user_uuid");
  110. assertThat(issue.assigneeLogin()).isEqualTo("user_login");
  111. assertThat(issue.mustSendNotifications()).isTrue();
  112. FieldDiffs.Diff diff = issue.currentChange().get(ASSIGNEE);
  113. assertThat(diff.oldValue()).isEqualTo(UNUSED);
  114. assertThat(diff.newValue()).isEqualTo("user_uuid");
  115. }
  116. @Test
  117. public void not_set_new_assignee_if_new_assignee_is_null() {
  118. boolean updated = underTest.setNewAssignee(issue, null, context);
  119. assertThat(updated).isFalse();
  120. assertThat(issue.currentChange()).isNull();
  121. assertThat(issue.mustSendNotifications()).isFalse();
  122. }
  123. @Test
  124. public void fail_with_ISE_when_setting_new_assignee_on_already_assigned_issue() {
  125. issue.setAssigneeUuid("user_uuid");
  126. UserIdDto userId = new UserIdDto("another_user_uuid", "another_user_login");
  127. assertThatThrownBy(() -> underTest.setNewAssignee(issue, userId, context))
  128. .isInstanceOf(IllegalStateException.class)
  129. .hasMessage("It's not possible to update the assignee with this method, please use assign()");
  130. }
  131. @Test
  132. public void set_type() {
  133. issue.setType(RuleType.CODE_SMELL);
  134. boolean updated = underTest.setType(issue, RuleType.BUG, context);
  135. assertThat(updated).isTrue();
  136. assertThat(issue.type()).isEqualTo(RuleType.BUG);
  137. assertThat(issue.manualSeverity()).isFalse();
  138. assertThat(issue.mustSendNotifications()).isFalse();
  139. FieldDiffs.Diff diff = issue.currentChange().get(TYPE);
  140. assertThat(diff.oldValue()).isEqualTo(RuleType.CODE_SMELL);
  141. assertThat(diff.newValue()).isEqualTo(RuleType.BUG);
  142. }
  143. @Test
  144. public void set_severity() {
  145. boolean updated = underTest.setSeverity(issue, "BLOCKER", context);
  146. assertThat(updated).isTrue();
  147. assertThat(issue.severity()).isEqualTo("BLOCKER");
  148. assertThat(issue.manualSeverity()).isFalse();
  149. assertThat(issue.mustSendNotifications()).isFalse();
  150. FieldDiffs.Diff diff = issue.currentChange().get(SEVERITY);
  151. assertThat(diff.oldValue()).isNull();
  152. assertThat(diff.newValue()).isEqualTo("BLOCKER");
  153. }
  154. @Test
  155. public void set_past_severity() {
  156. issue.setSeverity("BLOCKER");
  157. boolean updated = underTest.setPastSeverity(issue, "INFO", context);
  158. assertThat(updated).isTrue();
  159. assertThat(issue.severity()).isEqualTo("BLOCKER");
  160. assertThat(issue.mustSendNotifications()).isFalse();
  161. FieldDiffs.Diff diff = issue.currentChange().get(SEVERITY);
  162. assertThat(diff.oldValue()).isEqualTo("INFO");
  163. assertThat(diff.newValue()).isEqualTo("BLOCKER");
  164. }
  165. @Test
  166. public void update_severity() {
  167. issue.setSeverity("BLOCKER");
  168. boolean updated = underTest.setSeverity(issue, "MINOR", context);
  169. assertThat(updated).isTrue();
  170. assertThat(issue.severity()).isEqualTo("MINOR");
  171. assertThat(issue.mustSendNotifications()).isFalse();
  172. FieldDiffs.Diff diff = issue.currentChange().get(SEVERITY);
  173. assertThat(diff.oldValue()).isEqualTo("BLOCKER");
  174. assertThat(diff.newValue()).isEqualTo("MINOR");
  175. }
  176. @Test
  177. public void not_change_severity() {
  178. issue.setSeverity("MINOR");
  179. boolean updated = underTest.setSeverity(issue, "MINOR", context);
  180. assertThat(updated).isFalse();
  181. assertThat(issue.mustSendNotifications()).isFalse();
  182. assertThat(issue.currentChange()).isNull();
  183. }
  184. @Test
  185. public void not_revert_manual_severity() {
  186. issue.setSeverity("MINOR").setManualSeverity(true);
  187. try {
  188. underTest.setSeverity(issue, "MAJOR", context);
  189. } catch (IllegalStateException e) {
  190. assertThat(e).hasMessage("Severity can't be changed");
  191. }
  192. }
  193. @Test
  194. public void set_manual_severity() {
  195. issue.setSeverity("BLOCKER");
  196. boolean updated = underTest.setManualSeverity(issue, "MINOR", context);
  197. assertThat(updated).isTrue();
  198. assertThat(issue.severity()).isEqualTo("MINOR");
  199. assertThat(issue.manualSeverity()).isTrue();
  200. assertThat(issue.mustSendNotifications()).isTrue();
  201. FieldDiffs.Diff diff = issue.currentChange().get(SEVERITY);
  202. assertThat(diff.oldValue()).isEqualTo("BLOCKER");
  203. assertThat(diff.newValue()).isEqualTo("MINOR");
  204. }
  205. @Test
  206. public void not_change_manual_severity() {
  207. issue.setSeverity("MINOR").setManualSeverity(true);
  208. boolean updated = underTest.setManualSeverity(issue, "MINOR", context);
  209. assertThat(updated).isFalse();
  210. assertThat(issue.currentChange()).isNull();
  211. assertThat(issue.mustSendNotifications()).isFalse();
  212. }
  213. @Test
  214. public void unset_line() {
  215. int line = 1 + new Random().nextInt(500);
  216. issue.setLine(line);
  217. boolean updated = underTest.unsetLine(issue, context);
  218. assertThat(updated).isTrue();
  219. assertThat(issue.isChanged()).isTrue();
  220. assertThat(issue.line()).isNull();
  221. assertThat(issue.mustSendNotifications()).isFalse();
  222. assertThat(issue.currentChange())
  223. .extracting(f -> f.diffs().size())
  224. .isEqualTo(1);
  225. FieldDiffs.Diff diff = issue.currentChange().diffs().get("line");
  226. assertThat(diff.oldValue()).isEqualTo(line);
  227. assertThat(diff.newValue()).isEqualTo("");
  228. }
  229. @Test
  230. public void unset_line_has_no_effect_if_line_is_already_null() {
  231. issue.setLine(null);
  232. boolean updated = underTest.unsetLine(issue, context);
  233. assertThat(updated).isFalse();
  234. assertThat(issue.line()).isNull();
  235. assertThat(issue.isChanged()).isFalse();
  236. assertThat(issue.currentChange()).isNull();
  237. assertThat(issue.mustSendNotifications()).isFalse();
  238. }
  239. @Test
  240. public void set_past_line() {
  241. issue.setLine(42);
  242. boolean updated = underTest.setPastLine(issue, 123);
  243. assertThat(updated).isTrue();
  244. assertThat(issue.isChanged()).isTrue();
  245. assertThat(issue.line()).isEqualTo(42);
  246. assertThat(issue.mustSendNotifications()).isFalse();
  247. // do not save change
  248. assertThat(issue.currentChange()).isNull();
  249. }
  250. @Test
  251. public void set_past_line_has_no_effect_if_line_already_had_value() {
  252. issue.setLine(42);
  253. boolean updated = underTest.setPastLine(issue, 42);
  254. assertThat(updated).isFalse();
  255. assertThat(issue.isChanged()).isFalse();
  256. assertThat(issue.line()).isEqualTo(42);
  257. assertThat(issue.mustSendNotifications()).isFalse();
  258. // do not save change
  259. assertThat(issue.currentChange()).isNull();
  260. }
  261. @Test
  262. public void change_locations_if_primary_text_rage_changed() {
  263. DbCommons.TextRange range = DbCommons.TextRange.newBuilder().setStartLine(1).build();
  264. DbIssues.Locations locations = DbIssues.Locations.newBuilder()
  265. .setTextRange(range)
  266. .build();
  267. DbIssues.Locations locations2 = locations.toBuilder().setTextRange(range.toBuilder().setEndLine(2).build()).build();
  268. issue.setLocations(locations);
  269. boolean updated = underTest.setLocations(issue, locations2);
  270. assertThat(updated).isTrue();
  271. assertThat((Object) issue.getLocations()).isEqualTo(locations2);
  272. assertThat(issue.locationsChanged()).isTrue();
  273. assertThat(issue.currentChange()).isNull();
  274. assertThat(issue.mustSendNotifications()).isFalse();
  275. }
  276. @Test
  277. public void change_locations_if_secondary_text_rage_changed() {
  278. DbCommons.TextRange range = DbCommons.TextRange.newBuilder().setStartLine(1).build();
  279. DbIssues.Locations locations = DbIssues.Locations.newBuilder()
  280. .addFlow(DbIssues.Flow.newBuilder()
  281. .addLocation(DbIssues.Location.newBuilder().setTextRange(range))
  282. .build())
  283. .build();
  284. issue.setLocations(locations);
  285. DbIssues.Locations.Builder builder = locations.toBuilder();
  286. builder.getFlowBuilder(0).getLocationBuilder(0).setTextRange(range.toBuilder().setEndLine(2));
  287. boolean updated = underTest.setLocations(issue, builder.build());
  288. assertThat(updated).isTrue();
  289. }
  290. @Test
  291. public void change_locations_if_secondary_message_changed() {
  292. DbIssues.Locations locations = DbIssues.Locations.newBuilder()
  293. .addFlow(DbIssues.Flow.newBuilder()
  294. .addLocation(DbIssues.Location.newBuilder().setMsg("msg1"))
  295. .build())
  296. .build();
  297. issue.setLocations(locations);
  298. DbIssues.Locations.Builder builder = locations.toBuilder();
  299. builder.getFlowBuilder(0).getLocationBuilder(0).setMsg("msg2");
  300. boolean updated = underTest.setLocations(issue, builder.build());
  301. assertThat(updated).isTrue();
  302. }
  303. @Test
  304. public void change_locations_if_different_flow_count() {
  305. DbIssues.Locations locations = DbIssues.Locations.newBuilder()
  306. .addFlow(DbIssues.Flow.newBuilder()
  307. .addLocation(DbIssues.Location.newBuilder())
  308. .build())
  309. .build();
  310. issue.setLocations(locations);
  311. DbIssues.Locations.Builder builder = locations.toBuilder();
  312. builder.clearFlow();
  313. boolean updated = underTest.setLocations(issue, builder.build());
  314. assertThat(updated).isTrue();
  315. }
  316. @Test
  317. public void do_not_change_locations_if_primary_hash_changed() {
  318. DbCommons.TextRange range = DbCommons.TextRange.newBuilder().setStartLine(1).build();
  319. DbIssues.Locations locations = DbIssues.Locations.newBuilder()
  320. .setTextRange(range)
  321. .setChecksum("1")
  322. .build();
  323. issue.setLocations(locations);
  324. boolean updated = underTest.setLocations(issue, locations.toBuilder().setChecksum("2").build());
  325. assertThat(updated).isFalse();
  326. }
  327. @Test
  328. public void do_not_change_locations_if_secondary_hash_changed() {
  329. DbCommons.TextRange range = DbCommons.TextRange.newBuilder().setStartLine(1).build();
  330. DbIssues.Locations locations = DbIssues.Locations.newBuilder()
  331. .addFlow(DbIssues.Flow.newBuilder()
  332. .addLocation(DbIssues.Location.newBuilder().setTextRange(range))
  333. .build())
  334. .setChecksum("1")
  335. .build();
  336. issue.setLocations(locations);
  337. DbIssues.Locations.Builder builder = locations.toBuilder();
  338. builder.getFlowBuilder(0).getLocationBuilder(0).setChecksum("2");
  339. boolean updated = underTest.setLocations(issue, builder.build());
  340. assertThat(updated).isFalse();
  341. assertThat(issue.currentChange()).isNull();
  342. assertThat(issue.mustSendNotifications()).isFalse();
  343. }
  344. @Test
  345. public void set_locations_for_the_first_time() {
  346. issue.setLocations(null);
  347. boolean updated = underTest.setLocations(issue, "[1-4]");
  348. assertThat(updated).isTrue();
  349. assertThat(issue.getLocations().toString()).isEqualTo("[1-4]");
  350. assertThat(issue.currentChange()).isNull();
  351. assertThat(issue.mustSendNotifications()).isFalse();
  352. }
  353. @Test
  354. public void setResolution_shouldNotTriggerFieldChange() {
  355. boolean updated = underTest.setResolution(issue, Issue.STATUS_OPEN, context);
  356. assertThat(updated).isTrue();
  357. assertThat(issue.resolution()).isEqualTo(Issue.STATUS_OPEN);
  358. FieldDiffs.Diff diff = issue.currentChange().get(IssueFieldsSetter.RESOLUTION);
  359. assertThat(diff.oldValue()).isNull();
  360. assertThat(diff.newValue()).isEqualTo("OPEN");
  361. assertThat(issue.mustSendNotifications()).isTrue();
  362. }
  363. @Test
  364. public void not_change_resolution() {
  365. issue.setResolution(Issue.RESOLUTION_FIXED);
  366. boolean updated = underTest.setResolution(issue, Issue.RESOLUTION_FIXED, context);
  367. assertThat(updated).isFalse();
  368. assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FIXED);
  369. assertThat(issue.currentChange()).isNull();
  370. assertThat(issue.mustSendNotifications()).isFalse();
  371. }
  372. @Test
  373. public void set_status() {
  374. boolean updated = underTest.setStatus(issue, Issue.STATUS_OPEN, context);
  375. assertThat(updated).isTrue();
  376. assertThat(issue.status()).isEqualTo(Issue.STATUS_OPEN);
  377. FieldDiffs.Diff diff = issue.currentChange().get(STATUS);
  378. assertThat(diff.oldValue()).isNull();
  379. assertThat(diff.newValue()).isEqualTo(Issue.STATUS_OPEN);
  380. assertThat(issue.mustSendNotifications()).isTrue();
  381. }
  382. @Test
  383. public void setIssueStatus_shouldTriggerFieldChange() {
  384. issue.setResolution(null);
  385. issue.setStatus(Issue.STATUS_OPEN);
  386. IssueStatus issueStatus = issue.getIssueStatus();
  387. underTest.setResolution(issue, Issue.RESOLUTION_WONT_FIX, context);
  388. underTest.setStatus(issue, Issue.STATUS_RESOLVED, context);
  389. underTest.setIssueStatus(issue, issueStatus, issue.getIssueStatus(), context);
  390. FieldDiffs.Diff diff = issue.currentChange().diffs().get(IssueFieldsSetter.ISSUE_STATUS);
  391. assertThat(diff.oldValue()).isEqualTo(IssueStatus.OPEN);
  392. assertThat(diff.newValue()).isEqualTo(IssueStatus.ACCEPTED);
  393. }
  394. @Test
  395. public void setIssueStatus_shouldNotTriggerFieldChange_whenNoChanges() {
  396. issue.setResolution(null);
  397. issue.setStatus(Issue.STATUS_OPEN);
  398. IssueStatus issueStatus = issue.getIssueStatus();
  399. underTest.setIssueStatus(issue, issueStatus, issue.getIssueStatus(), context);
  400. assertThat(issue.currentChange()).isNull();
  401. }
  402. @Test
  403. public void setIssueStatus_shouldNotTriggerFieldChange_whenSecurityHotspot() {
  404. issue.setResolution(null);
  405. issue.setStatus(Issue.STATUS_TO_REVIEW);
  406. IssueStatus issueStatus = issue.getIssueStatus();
  407. issue.setResolution(Issue.RESOLUTION_SAFE);
  408. issue.setStatus(Issue.STATUS_REVIEWED);
  409. underTest.setIssueStatus(issue, issueStatus, issue.getIssueStatus(), context);
  410. assertThat(issue.currentChange()).isNull();
  411. }
  412. @Test
  413. public void not_change_status() {
  414. issue.setStatus("CLOSED");
  415. boolean updated = underTest.setStatus(issue, "CLOSED", context);
  416. assertThat(updated).isFalse();
  417. assertThat(issue.status()).isEqualTo("CLOSED");
  418. assertThat(issue.currentChange()).isNull();
  419. assertThat(issue.mustSendNotifications()).isFalse();
  420. }
  421. @Test
  422. public void set_gap_to_fix() {
  423. boolean updated = underTest.setGap(issue, 3.14, context);
  424. assertThat(updated).isTrue();
  425. assertThat(issue.isChanged()).isTrue();
  426. assertThat(issue.gap()).isEqualTo(3.14);
  427. assertThat(issue.mustSendNotifications()).isFalse();
  428. }
  429. @Test
  430. public void not_set_gap_to_fix_if_unchanged() {
  431. issue.setGap(3.14);
  432. boolean updated = underTest.setGap(issue, 3.14, context);
  433. assertThat(updated).isFalse();
  434. assertThat(issue.isChanged()).isFalse();
  435. assertThat(issue.gap()).isEqualTo(3.14);
  436. assertThat(issue.mustSendNotifications()).isFalse();
  437. }
  438. @Test
  439. public void set_past_gap() {
  440. issue.setGap(3.14);
  441. boolean updated = underTest.setPastGap(issue, 1.0, context);
  442. assertThat(updated).isTrue();
  443. assertThat(issue.gap()).isEqualTo(3.14);
  444. // do not save change
  445. assertThat(issue.currentChange()).isNull();
  446. assertThat(issue.mustSendNotifications()).isFalse();
  447. }
  448. @Test
  449. public void set_past_technical_debt() {
  450. Duration newDebt = Duration.create(15 * 8 * 60);
  451. Duration previousDebt = Duration.create(10 * 8 * 60);
  452. issue.setEffort(newDebt);
  453. boolean updated = underTest.setPastEffort(issue, previousDebt, context);
  454. assertThat(updated).isTrue();
  455. assertThat(issue.effort()).isEqualTo(newDebt);
  456. assertThat(issue.mustSendNotifications()).isFalse();
  457. FieldDiffs.Diff diff = issue.currentChange().get(TECHNICAL_DEBT);
  458. assertThat(diff.oldValue()).isEqualTo(10L * 8 * 60);
  459. assertThat(diff.newValue()).isEqualTo(15L * 8 * 60);
  460. }
  461. @Test
  462. public void set_past_technical_debt_without_previous_value() {
  463. Duration newDebt = Duration.create(15 * 8 * 60);
  464. issue.setEffort(newDebt);
  465. boolean updated = underTest.setPastEffort(issue, null, context);
  466. assertThat(updated).isTrue();
  467. assertThat(issue.effort()).isEqualTo(newDebt);
  468. assertThat(issue.mustSendNotifications()).isFalse();
  469. FieldDiffs.Diff diff = issue.currentChange().get(TECHNICAL_DEBT);
  470. assertThat(diff.oldValue()).isNull();
  471. assertThat(diff.newValue()).isEqualTo(15L * 8 * 60);
  472. }
  473. @Test
  474. public void set_past_technical_debt_with_null_new_value() {
  475. issue.setEffort(null);
  476. Duration previousDebt = Duration.create(10 * 8 * 60);
  477. boolean updated = underTest.setPastEffort(issue, previousDebt, context);
  478. assertThat(updated).isTrue();
  479. assertThat(issue.effort()).isNull();
  480. assertThat(issue.mustSendNotifications()).isFalse();
  481. FieldDiffs.Diff diff = issue.currentChange().get(TECHNICAL_DEBT);
  482. assertThat(diff.oldValue()).isEqualTo(10L * 8 * 60);
  483. assertThat(diff.newValue()).isNull();
  484. }
  485. @Test
  486. public void setCodeVariants_whenCodeVariantAdded_shouldBeUpdated() {
  487. Set<String> currentCodeVariants = new HashSet<>(Arrays.asList("linux"));
  488. Set<String> newCodeVariants = new HashSet<>(Arrays.asList("linux", "windows"));
  489. issue.setCodeVariants(newCodeVariants);
  490. boolean updated = underTest.setCodeVariants(issue, currentCodeVariants, context);
  491. assertThat(updated).isTrue();
  492. assertThat(issue.codeVariants()).contains("linux", "windows");
  493. FieldDiffs.Diff diff = issue.currentChange().get("code_variants");
  494. assertThat(diff.oldValue()).isEqualTo("linux");
  495. assertThat(diff.newValue()).isEqualTo("linux windows");
  496. assertThat(issue.mustSendNotifications()).isTrue();
  497. }
  498. @Test
  499. public void setImpacts_whenImpactAdded_shouldBeUpdated() {
  500. Map<SoftwareQuality, Severity> currentImpacts = Map.of(SoftwareQuality.RELIABILITY, Severity.LOW);
  501. Map<SoftwareQuality, Severity> newImpacts = Map.of(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);
  502. issue.replaceImpacts(newImpacts);
  503. boolean updated = underTest.setImpacts(issue, currentImpacts, context);
  504. assertThat(updated).isTrue();
  505. assertThat(issue.impacts()).isEqualTo(newImpacts);
  506. }
  507. @Test
  508. public void setCodeVariants_whenCodeVariantsUnchanged_shouldNotBeUpdated() {
  509. Set<String> currentCodeVariants = new HashSet<>(Arrays.asList("linux", "windows"));
  510. Set<String> newCodeVariants = new HashSet<>(Arrays.asList("windows", "linux"));
  511. issue.setCodeVariants(newCodeVariants);
  512. boolean updated = underTest.setCodeVariants(issue, currentCodeVariants, context);
  513. assertThat(updated).isFalse();
  514. assertThat(issue.currentChange()).isNull();
  515. }
  516. @Test
  517. public void set_message() {
  518. boolean updated = underTest.setMessage(issue, "the message", context);
  519. assertThat(updated).isTrue();
  520. assertThat(issue.isChanged()).isTrue();
  521. assertThat(issue.message()).isEqualTo("the message");
  522. assertThat(issue.mustSendNotifications()).isFalse();
  523. }
  524. @Test
  525. public void set_past_message() {
  526. issue.setMessage("new message");
  527. boolean updated = underTest.setPastMessage(issue, "past message", null, context);
  528. assertThat(updated).isTrue();
  529. assertThat(issue.message()).isEqualTo("new message");
  530. // do not save change
  531. assertThat(issue.currentChange()).isNull();
  532. assertThat(issue.mustSendNotifications()).isFalse();
  533. }
  534. @Test
  535. public void set_past_message_formatting() {
  536. issue.setMessage("past message");
  537. DbIssues.MessageFormattings newFormatting = formattings(formatting(0, 3, CODE));
  538. DbIssues.MessageFormattings pastFormatting = formattings(formatting(0, 7, CODE));
  539. issue.setMessageFormattings(newFormatting);
  540. boolean updated = underTest.setPastMessage(issue, "past message", pastFormatting, context);
  541. assertThat(updated).isTrue();
  542. assertThat(issue.message()).isEqualTo("past message");
  543. assertThat((DbIssues.MessageFormattings) issue.getMessageFormattings()).isEqualTo(newFormatting);
  544. // do not save change
  545. assertThat(issue.currentChange()).isNull();
  546. assertThat(issue.mustSendNotifications()).isFalse();
  547. }
  548. @Test
  549. public void set_past_message_formatting_no_changes() {
  550. issue.setMessage("past message");
  551. DbIssues.MessageFormattings sameFormatting = formattings(formatting(0, 3, CODE));
  552. issue.setMessageFormattings(sameFormatting);
  553. boolean updated = underTest.setPastMessage(issue, "past message", sameFormatting, context);
  554. assertThat(updated).isFalse();
  555. assertThat(issue.message()).isEqualTo("past message");
  556. assertThat((DbIssues.MessageFormattings) issue.getMessageFormattings()).isEqualTo(sameFormatting);
  557. // do not save change
  558. assertThat(issue.currentChange()).isNull();
  559. assertThat(issue.mustSendNotifications()).isFalse();
  560. }
  561. @Test
  562. public void message_formatting_different_size_is_changed() {
  563. issue.setMessageFormattings(formattings(formatting(0, 3, CODE)));
  564. boolean updated = underTest.setLocations(issue, formattings(formatting(0, 3, CODE), formatting(4, 6, CODE)));
  565. assertThat(updated).isTrue();
  566. }
  567. @Test
  568. public void message_formatting_different_start_is_changed() {
  569. issue.setMessageFormattings(formattings(formatting(0, 3, CODE)));
  570. boolean updated = underTest.setLocations(issue, formattings(formatting(1, 3, CODE)));
  571. assertThat(updated).isTrue();
  572. }
  573. @Test
  574. public void message_formatting_different_end_is_changed() {
  575. issue.setMessageFormattings(formattings(formatting(0, 3, CODE)));
  576. boolean updated = underTest.setLocations(issue, formattings(formatting(0, 4, CODE)));
  577. assertThat(updated).isTrue();
  578. }
  579. private static DbIssues.MessageFormatting formatting(int start, int end, MessageFormattingType type) {
  580. return DbIssues.MessageFormatting
  581. .newBuilder()
  582. .setStart(start)
  583. .setEnd(end)
  584. .setType(type)
  585. .build();
  586. }
  587. private static DbIssues.MessageFormattings formattings(DbIssues.MessageFormatting... messageFormatting) {
  588. return DbIssues.MessageFormattings.newBuilder()
  589. .addAllMessageFormatting(List.of(messageFormatting))
  590. .build();
  591. }
  592. @Test
  593. public void set_author() {
  594. boolean updated = underTest.setAuthorLogin(issue, "eric", context);
  595. assertThat(updated).isTrue();
  596. assertThat(issue.authorLogin()).isEqualTo("eric");
  597. FieldDiffs.Diff diff = issue.currentChange().get("author");
  598. assertThat(diff.oldValue()).isNull();
  599. assertThat(diff.newValue()).isEqualTo("eric");
  600. assertThat(issue.mustSendNotifications()).isFalse();
  601. }
  602. @Test
  603. public void set_new_author() {
  604. boolean updated = underTest.setNewAuthor(issue, "simon", context);
  605. assertThat(updated).isTrue();
  606. FieldDiffs.Diff diff = issue.currentChange().get("author");
  607. assertThat(diff.oldValue()).isNull();
  608. assertThat(diff.newValue()).isEqualTo("simon");
  609. assertThat(issue.mustSendNotifications()).isFalse();
  610. }
  611. @Test
  612. public void not_set_new_author_if_new_author_is_null() {
  613. boolean updated = underTest.setNewAuthor(issue, null, context);
  614. assertThat(updated).isFalse();
  615. assertThat(issue.currentChange()).isNull();
  616. assertThat(issue.mustSendNotifications()).isFalse();
  617. }
  618. @Test
  619. public void fail_with_ISE_when_setting_new_author_on_issue() {
  620. issue.setAuthorLogin("simon");
  621. assertThatThrownBy(() -> underTest.setNewAuthor(issue, "julien", context))
  622. .isInstanceOf(IllegalStateException.class)
  623. .hasMessage("It's not possible to update the author with this method, please use setAuthorLogin()");
  624. }
  625. @Test
  626. public void setIssueComponent_has_no_effect_if_component_uuid_is_not_changed() {
  627. String componentKey = "key";
  628. String componentUuid = "uuid";
  629. issue.setComponentUuid(componentUuid);
  630. issue.setComponentKey(componentKey);
  631. underTest.setIssueComponent(issue, componentUuid, componentKey, context.date());
  632. assertThat(issue.componentUuid()).isEqualTo(componentUuid);
  633. assertThat(issue.componentKey()).isEqualTo(componentKey);
  634. assertThat(issue.isChanged()).isFalse();
  635. assertThat(issue.updateDate()).isNull();
  636. assertThat(issue.mustSendNotifications()).isFalse();
  637. }
  638. @Test
  639. public void setIssueComponent_changes_component_uuid() {
  640. String oldComponentUuid = "a";
  641. String newComponentUuid = "b";
  642. String componentKey = "key";
  643. issue.setComponentUuid(oldComponentUuid);
  644. underTest.setIssueComponent(issue, newComponentUuid, componentKey, context.date());
  645. assertThat(issue.componentUuid()).isEqualTo(newComponentUuid);
  646. assertThat(issue.componentKey()).isEqualTo(componentKey);
  647. assertThat(issue.isChanged()).isTrue();
  648. assertThat(issue.updateDate()).isEqualTo(DateUtils.truncate(context.date(), Calendar.SECOND));
  649. }
  650. @Test
  651. public void setRuleDescriptionContextKey_setContextKeyIfPreviousValueIsNull() {
  652. issue.setRuleDescriptionContextKey(DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY);
  653. boolean updated = underTest.setRuleDescriptionContextKey(issue, null);
  654. assertThat(updated).isTrue();
  655. assertThat(issue.getRuleDescriptionContextKey()).contains(DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY);
  656. }
  657. @Test
  658. public void setRuleDescriptionContextKey_dontSetContextKeyIfPreviousValueIsTheSame() {
  659. issue.setRuleDescriptionContextKey(DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY);
  660. boolean updated = underTest.setRuleDescriptionContextKey(issue, DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY);
  661. assertThat(updated).isFalse();
  662. assertThat(issue.getRuleDescriptionContextKey()).contains(DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY);
  663. }
  664. @Test
  665. public void setRuleDescriptionContextKey_dontSetContextKeyIfBothValuesAreNull() {
  666. issue.setRuleDescriptionContextKey(null);
  667. boolean updated = underTest.setRuleDescriptionContextKey(issue, null);
  668. assertThat(updated).isFalse();
  669. assertThat(issue.getRuleDescriptionContextKey()).isEmpty();
  670. }
  671. @Test
  672. public void setRuleDescriptionContextKey_setContextKeyIfValuesAreDifferent() {
  673. issue.setRuleDescriptionContextKey(DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY);
  674. boolean updated = underTest.setRuleDescriptionContextKey(issue, "hibernate");
  675. assertThat(updated).isTrue();
  676. assertThat(issue.getRuleDescriptionContextKey()).contains(DEFAULT_RULE_DESCRIPTION_CONTEXT_KEY);
  677. }
  678. @Test
  679. public void setCleanCodeAttribute_whenCleanCodeAttributeChanged_shouldUpdateIssue() {
  680. issue.setCleanCodeAttribute(CleanCodeAttribute.CLEAR);
  681. boolean updated = underTest.setCleanCodeAttribute(issue, CleanCodeAttribute.COMPLETE, context);
  682. assertThat(updated).isTrue();
  683. assertThat(issue.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.CLEAR);
  684. assertThat(issue.currentChange().get("cleanCodeAttribute"))
  685. .extracting(FieldDiffs.Diff::oldValue, FieldDiffs.Diff::newValue)
  686. .containsExactly(CleanCodeAttribute.COMPLETE, CleanCodeAttribute.CLEAR.name());
  687. }
  688. @Test
  689. public void setCleanCodeAttribute_whenCleanCodeAttributeNotChanged_shouldNotUpdateIssue() {
  690. issue.setCleanCodeAttribute(CleanCodeAttribute.CLEAR);
  691. boolean updated = underTest.setCleanCodeAttribute(issue, CleanCodeAttribute.CLEAR, context);
  692. assertThat(updated).isFalse();
  693. assertThat(issue.getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.CLEAR);
  694. }
  695. }