Search in sources :

Example 1 with NewCustomRule

use of org.sonar.server.rule.NewCustomRule in project sonarqube by SonarSource.

the class ShowActionMediumTest method encode_html_description_of_custom_rule.

@Test
public void encode_html_description_of_custom_rule() throws Exception {
    // Template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(session, templateRule);
    session.commit();
    // Custom rule
    NewCustomRule customRule = NewCustomRule.createForCustomRule("MY_CUSTOM", templateRule.getKey()).setName("My custom").setSeverity(MINOR).setStatus(RuleStatus.READY).setMarkdownDescription("<div>line1\nline2</div>");
    RuleKey customRuleKey = tester.get(RuleCreator.class).create(customRule);
    session.clearCache();
    WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show").setParam("key", customRuleKey.toString());
    request.execute().assertJson(getClass(), "encode_html_description_of_custom_rule.json");
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) NewCustomRule(org.sonar.server.rule.NewCustomRule) RuleCreator(org.sonar.server.rule.RuleCreator) Test(org.junit.Test)

Example 2 with NewCustomRule

use of org.sonar.server.rule.NewCustomRule in project sonarqube by SonarSource.

the class CreateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String customKey = request.mandatoryParam(PARAM_CUSTOM_KEY);
    try (DbSession dbSession = dbClient.openSession(false)) {
        try {
            NewCustomRule newRule = NewCustomRule.createForCustomRule(customKey, RuleKey.parse(request.mandatoryParam(PARAM_TEMPLATE_KEY))).setName(request.mandatoryParam(PARAM_NAME)).setMarkdownDescription(request.mandatoryParam(PARAM_DESCRIPTION)).setSeverity(request.mandatoryParam(PARAM_SEVERITY)).setStatus(RuleStatus.valueOf(request.mandatoryParam(PARAM_STATUS))).setPreventReactivation(request.mandatoryParamAsBoolean(PARAM_PREVENT_REACTIVATION));
            String params = request.param(PARAMS);
            if (!isNullOrEmpty(params)) {
                newRule.setParameters(KeyValueFormat.parse(params));
            }
            writeResponse(dbSession, request, response, ruleCreator.create(newRule));
        } catch (ReactivationException e) {
            write409(dbSession, request, response, e.ruleKey());
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) NewCustomRule(org.sonar.server.rule.NewCustomRule) ReactivationException(org.sonar.server.rule.ReactivationException)

Example 3 with NewCustomRule

use of org.sonar.server.rule.NewCustomRule in project sonarqube by SonarSource.

the class UpdateActionMediumTest method update_custom_rule.

@Test
public void update_custom_rule() throws Exception {
    // Template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(session, templateRule);
    RuleParamDto param = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
    ruleDao.insertRuleParam(session, templateRule, param);
    session.commit();
    // Custom rule
    NewCustomRule newRule = NewCustomRule.createForCustomRule("MY_CUSTOM", templateRule.getKey()).setName("Old custom").setHtmlDescription("Old description").setSeverity(Severity.MINOR).setStatus(RuleStatus.BETA).setParameters(ImmutableMap.of("regex", "a"));
    RuleKey customRuleKey = tester.get(RuleCreator.class).create(newRule);
    session.clearCache();
    WsTester.TestRequest request = wsTester.newPostRequest("api/rules", "update").setParam("key", customRuleKey.toString()).setParam("name", "My custom rule").setParam("markdown_description", "Description").setParam("severity", "MAJOR").setParam("status", "BETA").setParam("params", "regex=a.*");
    request.execute().assertJson(getClass(), "update_custom_rule.json");
}
Also used : WsTester(org.sonar.server.ws.WsTester) RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) NewCustomRule(org.sonar.server.rule.NewCustomRule) RuleParamDto(org.sonar.db.rule.RuleParamDto) RuleCreator(org.sonar.server.rule.RuleCreator) Test(org.junit.Test)

Aggregations

NewCustomRule (org.sonar.server.rule.NewCustomRule)3 Test (org.junit.Test)2 RuleKey (org.sonar.api.rule.RuleKey)2 RuleDto (org.sonar.db.rule.RuleDto)2 RuleCreator (org.sonar.server.rule.RuleCreator)2 WsTester (org.sonar.server.ws.WsTester)2 DbSession (org.sonar.db.DbSession)1 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)1 RuleParamDto (org.sonar.db.rule.RuleParamDto)1 ReactivationException (org.sonar.server.rule.ReactivationException)1