Search in sources :

Example 1 with NewRepository

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

the class RegisterRulesTest method update_if_repository_and_key_changed_and_deprecated_key_declared_among_others.

@Test
public void update_if_repository_and_key_changed_and_deprecated_key_declared_among_others() {
    String ruleKey1 = "rule1";
    String ruleKey2 = "rule2";
    String repository1 = "fake1";
    String repository2 = "fake2";
    system.setNow(DATE1.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository(repository1, "java");
        repo.createRule(ruleKey1).setName("Name1").setHtmlDescription("Description");
        repo.done();
    });
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository1, ruleKey1));
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getUuids()).containsOnly(rule1.getUuid());
    system.setNow(DATE2.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository(repository2, "java");
        repo.createRule(ruleKey2).setName("Name2").setHtmlDescription("Description").addDeprecatedRuleKey("foo", "bar").addDeprecatedRuleKey(repository1, ruleKey1).addDeprecatedRuleKey("some", "noise");
        repo.done();
    });
    // rule2 is actually rule1
    RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository2, ruleKey2));
    assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid());
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions()).getUuids()).containsOnly(rule1.getUuid());
}
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 2 with NewRepository

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

the class RegisterRulesTest method update_only_rule_description.

@Test
public void update_only_rule_description() {
    system.setNow(DATE1.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        repo.createRule("rule").setName("Name").setHtmlDescription("Desc1");
        repo.done();
    });
    system.setNow(DATE2.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        repo.createRule("rule").setName("Name").setHtmlDescription("Desc2");
        repo.done();
    });
    // rule1 has been updated
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of("fake", "rule"));
    assertThat(rule1.getName()).isEqualTo("Name");
    assertThat(rule1.getDescription()).isEqualTo("Desc2");
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc2"), new SearchOptions()).getTotal()).isOne();
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc1"), 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 3 with NewRepository

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

the class RegisterRulesTest method declaring_a_rule_with_a_deprecated_RuleKey_still_used_should_throw_ISE.

@Test
public void declaring_a_rule_with_a_deprecated_RuleKey_still_used_should_throw_ISE() {
    assertThatThrownBy(() -> {
        execute(context -> {
            NewRepository repo = context.createRepository("fake", "java");
            createRule(repo, "newKey1");
            createRule(repo, "newKey2").addDeprecatedRuleKey("fake", "newKey1");
            repo.done();
        });
    }).isInstanceOf(IllegalStateException.class).hasMessage("The following rule keys are declared both as deprecated and used key [fake:newKey1]");
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) Test(org.junit.Test)

Example 4 with NewRepository

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

the class RegisterRulesTest method insert_then_remove_rule.

@Test
public void insert_then_remove_rule() {
    String ruleKey = randomAlphanumeric(5);
    // register one rule
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        repo.createRule(ruleKey).setName(randomAlphanumeric(5)).setHtmlDescription(randomAlphanumeric(20));
        repo.done();
    });
    // verify db
    List<RuleDefinitionDto> rules = dbClient.ruleDao().selectAllDefinitions(db.getSession());
    assertThat(rules).extracting(RuleDefinitionDto::getKey).extracting(RuleKey::rule).containsExactly(ruleKey);
    RuleDefinitionDto rule = rules.iterator().next();
    // verify index
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).containsExactly(rule.getUuid());
    verifyIndicesMarkedAsInitialized();
    // register no rule
    execute(context -> context.createRepository("fake", "java").done());
    // verify db
    assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).extracting(RuleDefinitionDto::getKey).extracting(RuleKey::rule).containsExactly(ruleKey);
    assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).extracting(RuleDefinitionDto::getStatus).containsExactly(REMOVED);
    // verify index
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).isEmpty();
    verifyIndicesNotMarkedAsInitialized();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RuleQuery(org.sonar.server.rule.index.RuleQuery) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 5 with NewRepository

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

the class RegisterRulesTest method update_if_rule_key_renamed_and_deprecated_key_declared.

@Test
public void update_if_rule_key_renamed_and_deprecated_key_declared() {
    String ruleKey1 = "rule1";
    String ruleKey2 = "rule2";
    String repository = "fake";
    system.setNow(DATE1.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository(repository, "java");
        repo.createRule(ruleKey1).setName("Name1").setHtmlDescription("Description");
        repo.done();
    });
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository, ruleKey1));
    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(repository, "java");
        repo.createRule(ruleKey2).setName("Name2").setHtmlDescription("Description").addDeprecatedRuleKey(repository, ruleKey1);
        repo.done();
    });
    // rule2 is actually rule1
    RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository, ruleKey2));
    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)

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