private void writeActiveRule(ActiveRule activeRule, JsonWriter json) { json.beginObject() .prop("qProfile", activeRule.key().qProfile().toString()) .prop("inherit", activeRule.inheritance().toString()) .prop("severity", activeRule.severity()); ActiveRuleKey parentKey = activeRule.parentKey(); if (parentKey != null) { json.prop("parent", parentKey.toString()); } json.name("params").beginArray(); for (Map.Entry<String, String> param : activeRule.params().entrySet()) { json.beginObject().prop("key", param.getKey()).prop("value", param.getValue()).endObject(); } json.endArray().endObject(); }
@Test public void update_active_rule_parameters_when_updating_custom_rule() { // Create template rule with 3 parameters RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo"); ruleDao.insert(dbSession, templateRule); RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule) .setName("regex") .setType("STRING") .setDescription("Reg ex") .setDefaultValue(".*"); ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1); RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule) .setName("format") .setType("STRING") .setDescription("format") .setDefaultValue("csv"); ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2); RuleParamDto templateRuleParam3 = RuleParamDto.createFor(templateRule) .setName("message") .setType("STRING") .setDescription("message"); ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam3); // Create custom rule RuleDto customRule = RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo"); ruleDao.insert(dbSession, customRule); ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*")); ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue("txt")); ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam3); // Create a quality profile QualityProfileDto profileDto = QProfileTesting.newXooP1(); db.qualityProfileDao().insert(dbSession, profileDto); dbSession.commit(); // Activate the custom rule RuleActivation activation = new RuleActivation(customRule.getKey()).setSeverity(Severity.BLOCKER); tester.get(RuleActivator.class).activate(dbSession, activation, QProfileTesting.XOO_P1_NAME); dbSession.commit(); dbSession.clearCache(); // Update custom rule parameter 'regex', add 'message' and remove 'format' RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()) .setParameters(ImmutableMap.of("regex", "b.*", "message", "a message")); updater.update(update, userSessionRule); dbSession.clearCache(); // Verify custom rule parameters has been updated Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey()); assertThat(customRuleReloaded.params()).hasSize(3); assertThat(customRuleReloaded.param("regex")).isNotNull(); assertThat(customRuleReloaded.param("regex").defaultValue()).isEqualTo("b.*"); assertThat(customRuleReloaded.param("message")).isNotNull(); assertThat(customRuleReloaded.param("message").defaultValue()).isEqualTo("a message"); assertThat(customRuleReloaded.param("format")).isNotNull(); assertThat(customRuleReloaded.param("format").defaultValue()).isNull(); RuleParam param = customRuleReloaded.params().get(0); assertThat(param.defaultValue()).isEqualTo("b.*"); // Verify active rule parameters has been updated ActiveRule activeRule = tester .get(ActiveRuleIndex.class) .getByKey(ActiveRuleKey.of(profileDto.getKey(), customRule.getKey())); assertThat(activeRule.params()).hasSize(2); assertThat(activeRule.params().get("regex")).isEqualTo("b.*"); assertThat(activeRule.params().get("message")).isEqualTo("a message"); assertThat(activeRule.params().get("format")).isNull(); // Verify that severity has not changed assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER); }