]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5007 Add ignored medium tests when rule debt is overridden
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 24 Jul 2014 13:42:47 +0000 (15:42 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 24 Jul 2014 13:42:47 +0000 (15:42 +0200)
sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java
sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java
sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json
sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_and_overridden_debt_values.json [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json [new file with mode: 0644]

index 6b7abdfe2715dd39a92bd318b29d70768a7912b0..508fa7a88c6448ed7a1254948e0da9977d7f228d 100644 (file)
@@ -244,7 +244,13 @@ public class RuleUpdaterMediumTest {
     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
@@ -272,7 +278,47 @@ public class RuleUpdaterMediumTest {
     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
@@ -301,7 +347,13 @@ public class RuleUpdaterMediumTest {
     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
index 1fadc2332259235b99259d1180921b2d3faa3359..43e1c2fc12718e8db77b8cb4ca05cc9b83a7bc28 100644 (file)
 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;
@@ -34,7 +32,9 @@ import org.sonar.core.qualityprofile.db.QualityProfileDao;
 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;
@@ -61,13 +61,16 @@ public class RulesWebServiceMediumTest {
   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);
@@ -170,20 +173,102 @@ public class RulesWebServiceMediumTest {
 
   @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);
@@ -430,4 +515,20 @@ public class RulesWebServiceMediumTest {
       .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();
+  }
 }
index 1505da0346bdd276017c9fcfe754c0b0f1c6ac79..3f83f0921a36f9b494799df285f222dbfba36dae 100644 (file)
@@ -1,8 +1,18 @@
 {"total": 1, "p": 1, "ps": 10, "rules": [
   {
     "key": "xoo:x1",
-    "debtRemFnType": "LINEAR",
+    "debtChar": "RELIABILITY",
+    "debtCharName": "Reliability",
+    "debtSubChar": "HARD_RELIABILITY",
+    "debtSubCharName": "Hard Reliability",
+    "debtRemFnType": "LINEAR_OFFSET",
     "debtRemFnCoeff": "1h",
-    "debtRemFnOffset": "5min"
+    "debtRemFnOffset": "15min",
+    "debtOverloaded": false,
+    "defaultDebtChar": "RELIABILITY",
+    "defaultDebtSubChar": "HARD_RELIABILITY",
+    "defaultDebtRemFnType": "LINEAR_OFFSET",
+    "defaultDebtRemFnCoeff": "1h",
+    "defaultDebtRemFnOffset": "15min"
   }
 ]}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_and_overridden_debt_values.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_and_overridden_debt_values.json
new file mode 100644 (file)
index 0000000..3aa6211
--- /dev/null
@@ -0,0 +1,18 @@
+{"total": 1, "p": 1, "ps": 10, "rules": [
+  {
+    "key": "xoo:x1",
+    "debtChar": "RELIABILITY",
+    "debtCharName": "Reliability",
+    "debtSubChar": "SOFT_RELIABILITY",
+    "debtSubCharName": "Soft Reliability",
+    "debtRemFnType": "LINEAR_OFFSET",
+    "debtRemFnCoeff": "2h",
+    "debtRemFnOffset": "25min",
+    "debtOverloaded": true,
+    "defaultDebtChar": "RELIABILITY",
+    "defaultDebtSubChar": "HARD_RELIABILITY",
+    "defaultDebtRemFnType": "LINEAR_OFFSET",
+    "defaultDebtRemFnCoeff": "1h",
+    "defaultDebtRemFnOffset": "15min"
+  }
+]}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json
new file mode 100644 (file)
index 0000000..6ae75ab
--- /dev/null
@@ -0,0 +1,17 @@
+{"total": 1, "p": 1, "ps": 10, "rules": [
+  {
+    "key": "xoo:x1",
+    "debtChar": "RELIABILITY",
+    "debtCharName": "Reliability",
+    "debtSubChar": "SOFT_RELIABILITY",
+    "debtSubCharName": "Soft Reliability",
+    "debtRemFnType": "CONSTANT_ISSUE",
+    "debtRemFnOffset": "5min",
+    "debtOverloaded": true,
+    "defaultDebtChar": "RELIABILITY",
+    "defaultDebtSubChar": "HARD_RELIABILITY",
+    "defaultDebtRemFnType": "LINEAR_OFFSET",
+    "defaultDebtRemFnCoeff": "1h",
+    "defaultDebtRemFnOffset": "15min"
+  }
+]}
diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json
new file mode 100644 (file)
index 0000000..b1c457e
--- /dev/null
@@ -0,0 +1,17 @@
+{"total": 1, "p": 1, "ps": 10, "rules": [
+  {
+    "key": "xoo:x1",
+    "debtChar": "RELIABILITY",
+    "debtCharName": "Reliability",
+    "debtSubChar": "SOFT_RELIABILITY",
+    "debtSubCharName": "Soft Reliability",
+    "debtRemFnType": "LINEAR",
+    "debtRemFnCoeff": "1h",
+    "debtOverloaded": true,
+    "defaultDebtChar": "RELIABILITY",
+    "defaultDebtSubChar": "HARD_RELIABILITY",
+    "defaultDebtRemFnType": "LINEAR_OFFSET",
+    "defaultDebtRemFnCoeff": "1h",
+    "defaultDebtRemFnOffset": "15min"
+  }
+]}