Search in sources :

Example 6 with NewRepository

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

the class RegisterRulesTest method update_if_repository_changed_and_deprecated_key_declared.

@Test
public void update_if_repository_changed_and_deprecated_key_declared() {
    String ruleKey = "rule";
    String repository1 = "fake1";
    String repository2 = "fake2";
    system.setNow(DATE1.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository(repository1, "java");
        repo.createRule(ruleKey).setName("Name1").setHtmlDescription("Description");
        repo.done();
    });
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository1, ruleKey));
    SearchIdResult<String> searchRule1 = ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions());
    assertThat(searchRule1.getUuids()).containsOnly(rule1.getUuid());
    assertThat(searchRule1.getTotal()).isOne();
    system.setNow(DATE2.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository(repository2, "java");
        repo.createRule(ruleKey).setName("Name2").setHtmlDescription("Description").addDeprecatedRuleKey(repository1, ruleKey);
        repo.done();
    });
    // rule2 is actually rule1
    RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository2, ruleKey));
    assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid());
    assertThat(rule2.getName()).isEqualTo("Name2");
    assertThat(rule2.getDescription()).isEqualTo(rule1.getDescription());
    SearchIdResult<String> searchRule2 = ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions());
    assertThat(searchRule2.getUuids()).containsOnly(rule2.getUuid());
    assertThat(searchRule2.getTotal()).isOne();
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getTotal()).isZero();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RuleDto(org.sonar.db.rule.RuleDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 7 with NewRepository

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

the class RegisterRulesTest method declaring_a_rule_with_an_existing_RuleKey_still_used_should_throw_IAE.

@Test
public void declaring_a_rule_with_an_existing_RuleKey_still_used_should_throw_IAE() {
    assertThatThrownBy(() -> {
        execute(context -> {
            NewRepository repo = context.createRepository("fake", "java");
            createRule(repo, "newKey1");
            createRule(repo, "newKey1");
            repo.done();
        });
    }).isInstanceOf(IllegalArgumentException.class).hasMessage("The rule 'newKey1' of repository 'fake' is declared several times");
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) Test(org.junit.Test)

Example 8 with NewRepository

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

the class RegisterRulesTest method add_new_security_standards.

@Test
public void add_new_security_standards() {
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        repo.createRule("rule1").setName("Rule One").setHtmlDescription("Description of Rule One").addOwaspTop10(Y2021, OwaspTop10.A1).addCwe(123);
        repo.done();
    });
    RuleDto rule = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RULE_KEY1);
    assertThat(rule.getSecurityStandards()).containsOnly("cwe:123", "owaspTop10-2021:a1");
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        repo.createRule("rule1").setName("Rule One").setHtmlDescription("Description of Rule One").addOwaspTop10(Y2021, OwaspTop10.A1, OwaspTop10.A3).addCwe(1, 123, 863);
        repo.done();
    });
    rule = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RULE_KEY1);
    assertThat(rule.getSecurityStandards()).containsOnly("cwe:1", "cwe:123", "cwe:863", "owaspTop10-2021:a1", "owaspTop10-2021:a3");
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 9 with NewRepository

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

the class RegisterRulesTest method add_new_tag.

@Test
public void add_new_tag() {
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        repo.createRule("rule1").setName("Rule One").setHtmlDescription("Description of Rule One").setTags("tag1");
        repo.done();
    });
    RuleDto rule = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RULE_KEY1);
    assertThat(rule.getSystemTags()).containsOnly("tag1");
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        repo.createRule("rule1").setName("Rule One").setHtmlDescription("Description of Rule One").setTags("tag1", "tag2");
        repo.done();
    });
    rule = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RULE_KEY1);
    assertThat(rule.getSystemTags()).containsOnly("tag1", "tag2");
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RuleDto(org.sonar.db.rule.RuleDto) Test(org.junit.Test)

Example 10 with NewRepository

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

the class RegisterRulesTest method rules_that_deprecate_previous_rule_must_be_recorded.

@Test
public void rules_that_deprecate_previous_rule_must_be_recorded() {
    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();
    });
    List<RuleDefinitionDto> rules = dbClient.ruleDao().selectAllDefinitions(db.getSession());
    Set<DeprecatedRuleKeyDto> deprecatedRuleKeys = dbClient.ruleDao().selectAllDeprecatedRuleKeys(db.getSession());
    assertThat(rules).hasSize(1);
    assertThat(deprecatedRuleKeys).hasSize(2);
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

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