3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.issue;
22 import org.junit.Test;
23 import org.sonar.api.config.Settings;
24 import org.sonar.api.i18n.I18n;
25 import org.sonar.api.server.debt.DebtRemediationFunction;
26 import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction;
27 import org.sonar.api.utils.Durations;
28 import org.sonar.core.issue.DefaultIssue;
29 import org.sonar.db.rule.RuleTesting;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.mock;
34 public class DebtCalculatorTest {
36 DumbRule rule = new DumbRule(RuleTesting.XOO_X1);
37 DefaultIssue issue = new DefaultIssue().setRuleKey(rule.getKey());
40 public RuleRepositoryRule ruleRepository = new RuleRepositoryRule().add(rule);
42 DebtCalculator underTest = new DebtCalculator(ruleRepository, new Durations(new Settings(), mock(I18n.class)));
45 public void no_debt_if_function_is_not_defined() {
46 DefaultIssue issue = new DefaultIssue().setRuleKey(rule.getKey());
48 assertThat(underTest.calculate(issue)).isNull();
52 public void no_debt_if_no_sqale_characteristic() {
53 rule.setFunction(null);
55 DefaultIssue issue = new DefaultIssue().setRuleKey(rule.getKey());
57 assertThat(underTest.calculate(issue)).isNull();
61 public void default_effort_to_fix_is_one_for_linear_function() {
63 rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, coefficient + "min", null));
65 assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(coefficient * 1);
69 public void linear_function() {
70 double effortToFix = 3.0;
72 issue.setGap(effortToFix);
73 rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.LINEAR, coefficient + "min", null));
75 assertThat(underTest.calculate(issue).toMinutes()).isEqualTo((int) (coefficient * effortToFix));
79 public void constant_function() {
82 rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, constant + "min"));
84 assertThat(underTest.calculate(issue).toMinutes()).isEqualTo(2);
87 @Test(expected = IllegalArgumentException.class)
88 public void effort_to_fix_must_not_be_set_with_constant_function() {
91 rule.setFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, constant + "min"));
93 underTest.calculate(issue);
97 public void linear_with_offset_function() {
98 double effortToFix = 3.0;
101 issue.setGap(effortToFix);
102 rule.setFunction(new DefaultDebtRemediationFunction(
103 DebtRemediationFunction.Type.LINEAR_OFFSET, coefficient + "min", offset + "min"));
105 assertThat(underTest.calculate(issue).toMinutes()).isEqualTo((int) ((coefficient * effortToFix) + offset));