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.

DebtCalculatorTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.ce.task.projectanalysis.issue;
  21. import org.junit.Test;
  22. import org.sonar.api.server.debt.DebtRemediationFunction;
  23. import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
  24. import org.sonar.api.utils.Duration;
  25. import org.sonar.api.utils.Durations;
  26. import org.sonar.core.issue.DefaultIssue;
  27. import org.sonar.db.rule.RuleTesting;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  30. public class DebtCalculatorTest {
  31. DumbRule rule = new DumbRule(RuleTesting.XOO_X1);
  32. DefaultIssue issue = new DefaultIssue().setRuleKey(rule.getKey());
  33. @org.junit.Rule
  34. public RuleRepositoryRule ruleRepository = new RuleRepositoryRule().add(rule);
  35. DebtCalculator underTest = new DebtCalculator(ruleRepository, new Durations());
  36. @Test
  37. public void no_debt_if_function_is_not_defined() {
  38. DefaultIssue issue = new DefaultIssue().setRuleKey(rule.getKey());
  39. assertThat(underTest.calculate(issue)).isNull();
  40. }
  41. @Test
  42. public void no_debt_if_no_sqale_characteristic() {
  43. rule.setFunction(null);
  44. DefaultIssue issue = new DefaultIssue().setRuleKey(rule.getKey());
  45. assertThat(underTest.calculate(issue)).isNull();
  46. }
  47. @Test
  48. public void default_effort_to_fix_is_one_for_linear_function() {
  49. int coefficient = 2;
  50. rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, coefficient + "min", null));
  51. assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(coefficient * 1);
  52. }
  53. @Test
  54. public void linear_function() {
  55. double effortToFix = 3.0;
  56. int coefficient = 2;
  57. issue.setGap(effortToFix);
  58. rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, coefficient + "min", null));
  59. assertThat(underTest.calculate(issue).toMinutes()).isEqualTo((int) (coefficient * effortToFix));
  60. }
  61. @Test
  62. public void copy_effort_for_external_issues() {
  63. issue.setGap(null);
  64. issue.setIsFromExternalRuleEngine(true);
  65. issue.setEffort(Duration.create(20l));
  66. rule.setFunction(null);
  67. assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(20l);
  68. }
  69. @Test
  70. public void constant_function() {
  71. int constant = 2;
  72. issue.setGap(null);
  73. rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, constant + "min"));
  74. assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(2);
  75. }
  76. @Test
  77. public void effort_to_fix_must_not_be_set_with_constant_function() {
  78. int constant = 2;
  79. issue.setGap(3.0);
  80. rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, constant + "min"));
  81. assertThatThrownBy(() -> underTest.calculate(issue))
  82. .isInstanceOf(IllegalArgumentException.class);
  83. }
  84. @Test
  85. public void linear_with_offset_function() {
  86. double effortToFix = 3.0;
  87. int coefficient = 2;
  88. int offset = 5;
  89. issue.setGap(effortToFix);
  90. rule.setFunction(new DefaultDebtRemediationFunction(
  91. DebtRemediationFunction.Type.LINEAR_OFFSET, coefficient + "min", offset + "min"));
  92. assertThat(underTest.calculate(issue).toMinutes()).isEqualTo((int) ((coefficient * effortToFix) + offset));
  93. }
  94. }