Search in sources :

Example 16 with NewRepository

use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.

the class RegisterRulesTest method update_template_rule_key_should_also_update_custom_rules.

@Test
public void update_template_rule_key_should_also_update_custom_rules() {
    system.setNow(DATE1.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository("squid", "java");
        repo.createRule("rule").setName("Name1").setHtmlDescription("Description").setTemplate(true);
        repo.done();
    });
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of("squid", "rule"));
    // insert custom rule
    db.rules().insert(new RuleDefinitionDto().setRuleKey(RuleKey.of("squid", "custom")).setLanguage("java").setScope(Scope.ALL).setTemplateUuid(rule1.getUuid()).setName("custom1"));
    db.commit();
    // re-key rule
    execute(context -> {
        NewRepository repo = context.createRepository("java", "java");
        repo.createRule("rule").setName("Name1").setHtmlDescription("Description").addDeprecatedRuleKey("squid", "rule").setTemplate(true);
        repo.done();
    });
    // template rule and custom rule have been updated
    rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of("java", "rule"));
    RuleDto custom = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of("java", "custom"));
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RuleDto(org.sonar.db.rule.RuleDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 17 with NewRepository

use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.

the class RegisterRulesTest method rule_previously_created_as_adhoc_becomes_none_adhoc.

@Test
public void rule_previously_created_as_adhoc_becomes_none_adhoc() {
    RuleDefinitionDto rule = db.rules().insert(r -> r.setRepositoryKey("external_fake").setIsExternal(true).setIsAdHoc(true));
    system.setNow(DATE2.getTime());
    execute(context -> {
        NewRepository repo = context.createExternalRepository("fake", rule.getLanguage());
        repo.createRule(rule.getRuleKey()).setName(rule.getName()).setHtmlDescription(rule.getDescription());
        repo.done();
    });
    RuleDto reloaded = dbClient.ruleDao().selectByKey(db.getSession(), rule.getKey()).get();
    assertThat(reloaded.isAdHoc()).isFalse();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RuleDto(org.sonar.db.rule.RuleDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 18 with NewRepository

use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.

the class RegisterRulesTest method rules_that_remove_deprecated_key_must_remove_records.

@Test
public void rules_that_remove_deprecated_key_must_remove_records() {
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        createRule(repo, "rule1");
        repo.done();
    });
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        createRule(repo, "newKey").addDeprecatedRuleKey("fake", "rule1").addDeprecatedRuleKey("fake", "rule2");
        repo.done();
    });
    assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(1);
    Set<DeprecatedRuleKeyDto> deprecatedRuleKeys = dbClient.ruleDao().selectAllDeprecatedRuleKeys(db.getSession());
    assertThat(deprecatedRuleKeys).hasSize(2);
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        createRule(repo, "newKey");
        repo.done();
    });
    assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(1);
    deprecatedRuleKeys = dbClient.ruleDao().selectAllDeprecatedRuleKeys(db.getSession());
    assertThat(deprecatedRuleKeys).isEmpty();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) Test(org.junit.Test)

Example 19 with NewRepository

use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarqube by SonarSource.

the class RegisterRulesTest method createRule.

@SafeVarargs
private void createRule(Context context, String language, String repositoryKey, String ruleKey, Consumer<NewRule>... consumers) {
    NewRepository repo = context.createRepository(repositoryKey, language);
    NewRule newRule = repo.createRule(ruleKey).setName(ruleKey).setHtmlDescription("Description of One").setSeverity(BLOCKER).setType(RuleType.CODE_SMELL).setStatus(RuleStatus.BETA);
    Arrays.stream(consumers).forEach(c -> c.accept(newRule));
    repo.done();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) NewRule(org.sonar.api.server.rule.RulesDefinition.NewRule)

Aggregations

NewRepository (org.sonar.api.server.rule.RulesDefinition.NewRepository)19 Test (org.junit.Test)17 RuleDto (org.sonar.db.rule.RuleDto)11 SearchOptions (org.sonar.server.es.SearchOptions)8 RuleQuery (org.sonar.server.rule.index.RuleQuery)8 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)5 NewRule (org.sonar.api.server.rule.RulesDefinition.NewRule)3 DeprecatedRuleKeyDto (org.sonar.db.rule.DeprecatedRuleKeyDto)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)1 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)1 String.valueOf (java.lang.String.valueOf)1 Arrays (java.util.Arrays)1 Collections.emptySet (java.util.Collections.emptySet)1 Collections.singletonList (java.util.Collections.singletonList)1 Date (java.util.Date)1 List (java.util.List)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1