From 995c4f0fa9c494a710208abc60f0200e1338a81c Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 24 Jul 2014 15:42:47 +0200 Subject: [PATCH] SONAR-5007 Add ignored medium tests when rule debt is overridden --- .../server/rule/RuleUpdaterMediumTest.java | 52 ++++++++ .../rule/ws/RulesWebServiceMediumTest.java | 119 ++++++++++++++++-- .../search_debt_rule.json | 14 ++- ...th_default_and_overridden_debt_values.json | 18 +++ ...r_offset_and_overridden_constant_debt.json | 17 +++ ...ear_offset_and_overridden_linear_debt.json | 17 +++ 6 files changed, 226 insertions(+), 11 deletions(-) create mode 100644 sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_and_overridden_debt_values.json create mode 100644 sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json create mode 100644 sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java index 6b7abdfe271..508fa7a88c6 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java @@ -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 diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java index 1fadc233225..43e1c2fc127 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ws/RulesWebServiceMediumTest.java @@ -20,11 +20,9 @@ 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(); + } } diff --git a/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json index 1505da0346b..3f83f0921a3 100644 --- a/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json +++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rule.json @@ -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 index 00000000000..3aa6211c764 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_and_overridden_debt_values.json @@ -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 index 00000000000..6ae75abecb5 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_constant_debt.json @@ -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 index 00000000000..b1c457e13c0 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/rule/ws/RulesWebServiceMediumTest/search_debt_rules_with_default_linear_offset_and_overridden_linear_debt.json @@ -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" + } +]} -- 2.39.5