@Test public void remove_debt_rule() throws Exception { verifyRulesInDb(); // Set some default debt on x2 rule, which has no debt provided by th plugin RuleDto ruleDto = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X2); db.ruleDao() .update( dbSession, ruleDto .setDefaultSubCharacteristicId( db.debtCharacteristicDao() .selectByKey( RulesDefinition.SubCharacteristics.INTEGRATION_TESTABILITY, dbSession) .getId()) .setDefaultRemediationFunction("LINEAR_OFFSET") .setDefaultRemediationCoefficient("2h") .setDefaultRemediationOffset("35min")); dbSession.commit(); dbSession.clearCache(); // Re-execute startup tasks tester.get(Platform.class).executeStartupTasks(); // Verify default debt has been removed Rule ruleReloaded = index.getByKey(RuleTesting.XOO_X2); assertThat(ruleReloaded.debtSubCharacteristicKey()).isNull(); assertThat(ruleReloaded.debtRemediationFunction()).isNull(); }
@Test public void update_debt_rule() throws Exception { verifyRulesInDb(); // Update x1 rule RuleDto ruleDto = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1); db.ruleDao() .update( dbSession, ruleDto .setDefaultSubCharacteristicId(123456) .setDefaultRemediationFunction("LINEAR_OFFSET") .setDefaultRemediationCoefficient("2h") .setDefaultRemediationOffset("35min")); dbSession.commit(); dbSession.clearCache(); // Re-execute startup tasks tester.get(Platform.class).executeStartupTasks(); // Verify default debt has been reset to plugin definition Rule ruleReloaded = index.getByKey(RuleTesting.XOO_X1); assertThat(ruleReloaded.debtSubCharacteristicKey()) .isEqualTo(RulesDefinition.SubCharacteristics.INTEGRATION_TESTABILITY); assertThat(ruleReloaded.debtRemediationFunction().type()) .isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET); assertThat(ruleReloaded.debtRemediationFunction().coefficient()).isEqualTo("1h"); assertThat(ruleReloaded.debtRemediationFunction().offset()).isEqualTo("30min"); }
private void resetRules( List<RuleDto> ruleDtos, List<RulesDefinition.Rule> rules, List<CharacteristicDto> allCharacteristicDtos, Date updateDate, DbSession session) { for (RuleDto rule : ruleDtos) { // Restore default debt definitions RulesDefinition.Rule ruleDef; if (rule.getTemplateId() != null) { RuleDto templateRule = rule(rule.getTemplateId(), ruleDtos); ruleDef = ruleDef(templateRule.getRepositoryKey(), templateRule.getRuleKey(), rules); } else { ruleDef = ruleDef(rule.getRepositoryKey(), rule.getRuleKey(), rules); } if (ruleDef != null) { String subCharacteristicKey = ruleDef.debtSubCharacteristic(); CharacteristicDto subCharacteristicDto = characteristicByKey(subCharacteristicKey, allCharacteristicDtos, false); DebtRemediationFunction remediationFunction = ruleDef.debtRemediationFunction(); boolean hasDebtDefinition = subCharacteristicDto != null && remediationFunction != null; rule.setDefaultSubCharacteristicId(hasDebtDefinition ? subCharacteristicDto.getId() : null); rule.setDefaultRemediationFunction( hasDebtDefinition ? remediationFunction.type().name() : null); rule.setDefaultRemediationCoefficient( hasDebtDefinition ? remediationFunction.coefficient() : null); rule.setDefaultRemediationOffset(hasDebtDefinition ? remediationFunction.offset() : null); } // Reset overridden debt definitions rule.setSubCharacteristicId(null); rule.setRemediationFunction(null); rule.setRemediationCoefficient(null); rule.setRemediationOffset(null); rule.setUpdatedAt(updateDate); dbClient.ruleDao().update(session, rule); } }