assertThat(indexedRule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
assertThat(indexedRule.debtRemediationFunction().coefficient()).isNull();
assertThat(indexedRule.debtRemediationFunction().offset()).isEqualTo("1min");
+
assertThat(indexedRule.debtOverloaded()).isTrue();
+ assertThat(indexedRule.defaultDebtCharacteristicKey()).isEqualTo("RELIABILITY");
+ assertThat(indexedRule.defaultDebtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
+ assertThat(indexedRule.defaultDebtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+ assertThat(indexedRule.defaultDebtRemediationFunction().coefficient()).isEqualTo("1d");
+ assertThat(indexedRule.defaultDebtRemediationFunction().offset()).isEqualTo("5min");
}
@Test
assertThat(indexedRule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
assertThat(indexedRule.debtRemediationFunction().coefficient()).isEqualTo("2d");
assertThat(indexedRule.debtRemediationFunction().offset()).isNull();
+
assertThat(indexedRule.debtOverloaded()).isTrue();
+ assertThat(indexedRule.defaultDebtCharacteristicKey()).isEqualTo("RELIABILITY");
+ assertThat(indexedRule.defaultDebtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
+ assertThat(indexedRule.defaultDebtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(indexedRule.defaultDebtRemediationFunction().coefficient()).isEqualTo("1d");
+ assertThat(indexedRule.defaultDebtRemediationFunction().offset()).isNull();
+ }
+
+ @Test
+ public void override_debt_from_linear_with_offset_to_constant() throws Exception {
+ insertDebtCharacteristics(dbSession);
+ ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY)
+ .setDefaultSubCharacteristicId(hardReliabilityId)
+ .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setDefaultRemediationCoefficient("1d")
+ .setDefaultRemediationOffset("5min")
+ .setRemediationFunction(null)
+ .setRemediationCoefficient(null)
+ .setRemediationOffset(null));
+ dbSession.commit();
+
+ RuleUpdate update = RuleUpdate.createForPluginRule(RULE_KEY)
+ .setDebtRemediationFunction(new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE, null, "10min"));
+ updater.update(update, UserSession.get());
+ dbSession.clearCache();
+
+ // verify debt is overridden
+ Rule indexedRule = tester.get(RuleIndex.class).getByKey(RULE_KEY);
+ assertThat(indexedRule.debtCharacteristicKey()).isEqualTo("RELIABILITY");
+ assertThat(indexedRule.debtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
+ assertThat(indexedRule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
+ assertThat(indexedRule.debtRemediationFunction().coefficient()).isNull();
+ assertThat(indexedRule.debtRemediationFunction().offset()).isEqualTo("10min");
+
+ assertThat(indexedRule.debtOverloaded()).isTrue();
+ assertThat(indexedRule.defaultDebtCharacteristicKey()).isEqualTo("RELIABILITY");
+ assertThat(indexedRule.defaultDebtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
+ assertThat(indexedRule.defaultDebtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+ assertThat(indexedRule.defaultDebtRemediationFunction().coefficient()).isEqualTo("1d");
+ assertThat(indexedRule.defaultDebtRemediationFunction().offset()).isEqualTo("5min");
}
@Test
assertThat(indexedRule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
assertThat(indexedRule.debtRemediationFunction().coefficient()).isEqualTo("1d");
assertThat(indexedRule.debtRemediationFunction().offset()).isEqualTo("5min");
+
assertThat(indexedRule.debtOverloaded()).isFalse();
+ assertThat(indexedRule.defaultDebtCharacteristicKey()).isEqualTo("RELIABILITY");
+ assertThat(indexedRule.defaultDebtSubCharacteristicKey()).isEqualTo("HARD_RELIABILITY");
+ assertThat(indexedRule.defaultDebtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(indexedRule.defaultDebtRemediationFunction().coefficient()).isEqualTo("1d");
+ assertThat(indexedRule.defaultDebtRemediationFunction().offset()).isEqualTo("5min");
}
@Test
package org.sonar.server.rule.ws;
import com.google.common.collect.ImmutableSet;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.junit.*;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.DateUtils;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.qualityprofile.db.QualityProfileDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
+import org.sonar.core.technicaldebt.db.CharacteristicDto;
import org.sonar.server.db.DbClient;
+import org.sonar.server.debt.DebtTesting;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.qualityprofile.db.ActiveRuleDao;
import org.sonar.server.rule.RuleTesting;
private static final String API_SHOW_METHOD = "show";
private static final String API_TAGS_METHOD = "tags";
+ DbClient db;
RulesWebService ws;
RuleDao ruleDao;
DbSession session;
+ int reliabilityId, softReliabilityId, hardReliabilityId;
@Before
public void setUp() throws Exception {
tester.clearDbAndIndexes();
+ db = tester.get(DbClient.class);
ruleDao = tester.get(RuleDao.class);
ws = tester.get(RulesWebService.class);
session = tester.get(DbClient.class).openSession(false);
@Test
public void search_debt_rules() throws Exception {
+ insertDebtCharacteristics(session);
+
ruleDao.insert(session, RuleTesting.newXooX1()
- .setDefaultRemediationCoefficient("DefaultCoef")
- .setDefaultRemediationFunction("DefaultFunction")
- .setDefaultRemediationCoefficient("DefaultCoef")
- .setDefaultSubCharacteristicId(1));
+ .setDefaultSubCharacteristicId(hardReliabilityId)
+ .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setDefaultRemediationCoefficient("1h")
+ .setDefaultRemediationOffset("15min")
+
+ .setSubCharacteristicId(null)
+ .setRemediationFunction(null)
+ .setRemediationCoefficient(null)
+ .setRemediationOffset(null)
+ );
session.commit();
MockUserSession.set();
WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
- request.setParam(SearchOptions.PARAM_FIELDS, "debtRemFn,debtChar,debtCharName");
+ request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn");
WsTester.Result result = request.execute();
result.assertJson(this.getClass(), "search_debt_rule.json");
}
+ @Test
+ public void search_debt_rules_with_default_and_overridden_debt_values() throws Exception {
+ insertDebtCharacteristics(session);
+
+ ruleDao.insert(session, RuleTesting.newXooX1()
+ .setDefaultSubCharacteristicId(hardReliabilityId)
+ .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setDefaultRemediationCoefficient("1h")
+ .setDefaultRemediationOffset("15min")
+
+ .setSubCharacteristicId(softReliabilityId)
+ .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setRemediationCoefficient("2h")
+ .setRemediationOffset("25min")
+ );
+ session.commit();
+
+ MockUserSession.set();
+ WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
+ request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn");
+ WsTester.Result result = request.execute();
+ result.assertJson(this.getClass(), "search_debt_rules_with_default_and_overridden_debt_values.json");
+ }
+
+ @Test
+ @Ignore("To be fixed")
+ public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() throws Exception {
+ insertDebtCharacteristics(session);
+
+ ruleDao.insert(session, RuleTesting.newXooX1()
+ .setDefaultSubCharacteristicId(hardReliabilityId)
+ .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setDefaultRemediationCoefficient("1h")
+ .setDefaultRemediationOffset("15min")
+
+ .setSubCharacteristicId(softReliabilityId)
+ .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name())
+ .setRemediationCoefficient(null)
+ .setRemediationOffset("5min")
+ );
+ session.commit();
+
+ MockUserSession.set();
+ WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
+ request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn");
+ WsTester.Result result = request.execute();
+ result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json");
+ }
+
+ @Test
+ @Ignore("To be fixed")
+ public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() throws Exception {
+ insertDebtCharacteristics(session);
+
+ ruleDao.insert(session, RuleTesting.newXooX1()
+ .setDefaultSubCharacteristicId(hardReliabilityId)
+ .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
+ .setDefaultRemediationCoefficient("1h")
+ .setDefaultRemediationOffset("15min")
+
+ .setSubCharacteristicId(softReliabilityId)
+ .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name())
+ .setRemediationCoefficient("1h")
+ .setRemediationOffset(null)
+ );
+ session.commit();
+
+ MockUserSession.set();
+ WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_SEARCH_METHOD);
+ request.setParam(SearchOptions.PARAM_FIELDS, "debtChar,debtCharName,debtSubChar,debtSubCharName,debtRemFn,debtOverloaded,defaultDebtChar,defaultDebtSubChar,defaultDebtRemFn");
+ WsTester.Result result = request.execute();
+ result.assertJson(this.getClass(), "search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json");
+ }
+
@Test
public void search_template_rules() throws Exception {
RuleDto templateRule = RuleTesting.newXooX1().setIsTemplate(true);
.setInheritance(null)
.setSeverity("BLOCKER");
}
+
+ private void insertDebtCharacteristics(DbSession dbSession) {
+ CharacteristicDto reliability = DebtTesting.newCharacteristicDto("RELIABILITY").setName("Reliability");
+ db.debtCharacteristicDao().insert(reliability, dbSession);
+ reliabilityId = reliability.getId();
+
+ CharacteristicDto softReliability = DebtTesting.newCharacteristicDto("SOFT_RELIABILITY").setName("Soft Reliability")
+ .setParentId(reliability.getId());
+ db.debtCharacteristicDao().insert(softReliability, dbSession);
+ softReliabilityId = softReliability.getId();
+
+ CharacteristicDto hardReliability = DebtTesting.newCharacteristicDto("HARD_RELIABILITY").setName("Hard Reliability")
+ .setParentId(reliability.getId());
+ db.debtCharacteristicDao().insert(hardReliability, dbSession);
+ hardReliabilityId = hardReliability.getId();
+ }
}