Search in sources :

Example 11 with NewRepository

use of org.sonar.api.server.rule.RulesDefinition.NewRepository in project sonarlint-core by SonarSource.

the class SonarLintRuleDefinitionTests method convertMarkdownDescriptionToHtml.

@Test
void convertMarkdownDescriptionToHtml() {
    RulesDefinition.Context context = new RulesDefinition.Context();
    NewRepository newRepository = context.createRepository("my-repo", "java");
    NewRule createRule = newRepository.createRule("my-rule-with-markdown-description").setName("My Rule");
    createRule.setMarkdownDescription("  = Title\n  * one\n* two");
    newRepository.done();
    Rule rule = context.repositories().get(0).rule("my-rule-with-markdown-description");
    SonarLintRuleDefinition underTest = new SonarLintRuleDefinition(rule);
    assertThat(underTest.getHtmlDescription()).isEqualTo("<h1>Title</h1><ul><li>one</li>\n" + "<li>two</li></ul>");
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) Rule(org.sonar.api.server.rule.RulesDefinition.Rule) NewRule(org.sonar.api.server.rule.RulesDefinition.NewRule) NewRule(org.sonar.api.server.rule.RulesDefinition.NewRule) Test(org.junit.jupiter.api.Test)

Example 12 with NewRepository

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

the class RegisterRulesTest method mass_insert_then_remove_rule.

@Test
public void mass_insert_then_remove_rule() {
    int numberOfRules = 5000;
    // register many rules
    execute(context -> {
        NewRepository repo = context.createRepository("fake", "java");
        IntStream.range(0, numberOfRules).mapToObj(i -> "rule-" + i).forEach(ruleKey -> repo.createRule(ruleKey).setName(randomAlphanumeric(20)).setHtmlDescription(randomAlphanumeric(20)));
        repo.done();
    });
    // verify db
    assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(numberOfRules).extracting(RuleDefinitionDto::getStatus).containsOnly(READY);
    // verify index
    assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isEqualTo(numberOfRules);
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).isNotEmpty();
    // register no rule
    execute(context -> context.createRepository("fake", "java").done());
    // verify db
    assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(numberOfRules).extracting(RuleDefinitionDto::getStatus).containsOnly(REMOVED);
    // verify index (documents are still in the index, but all are removed)
    assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isEqualTo(numberOfRules);
    assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getUuids()).isEmpty();
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) Arrays(java.util.Arrays) REMOVED(org.sonar.api.rule.RuleStatus.REMOVED) Y2021(org.sonar.api.server.rule.RulesDefinition.OwaspTop10Version.Y2021) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) Date(java.util.Date) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) Collections.singletonList(java.util.Collections.singletonList) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) Languages(org.sonar.api.resources.Languages) BLOCKER(org.sonar.api.rule.Severity.BLOCKER) DebtRemediationFunction(org.sonar.api.server.debt.DebtRemediationFunction) MetadataIndex(org.sonar.server.es.metadata.MetadataIndex) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) UuidFactoryFast(org.sonar.core.util.UuidFactoryFast) RuleIndexer(org.sonar.server.rule.index.RuleIndexer) DbTester(org.sonar.db.DbTester) SearchIdResult(org.sonar.server.es.SearchIdResult) Context(org.sonar.api.server.rule.RulesDefinition.Context) NewRule(org.sonar.api.server.rule.RulesDefinition.NewRule) Set(java.util.Set) DbClient(org.sonar.db.DbClient) QProfileRules(org.sonar.server.qualityprofile.QProfileRules) List(java.util.List) RuleRepositoryDto(org.sonar.db.rule.RuleRepositoryDto) SearchOptions(org.sonar.server.es.SearchOptions) INFO(org.sonar.api.rule.Severity.INFO) RuleKey(org.sonar.api.rule.RuleKey) RuleIndexDefinition(org.sonar.server.rule.index.RuleIndexDefinition) OwaspTop10(org.sonar.api.server.rule.RulesDefinition.OwaspTop10) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) READY(org.sonar.api.rule.RuleStatus.READY) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) EsTester(org.sonar.server.es.EsTester) RuleStatus(org.sonar.api.rule.RuleStatus) RunWith(org.junit.runner.RunWith) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) TestSystem2(org.sonar.api.impl.utils.TestSystem2) RuleType(org.sonar.api.rules.RuleType) DateUtils(org.sonar.api.utils.DateUtils) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) RuleScope(org.sonar.api.rule.RuleScope) Scope(org.sonar.db.rule.RuleDto.Scope) ServerPluginRepository(org.sonar.server.plugins.ServerPluginRepository) Language(org.sonar.api.resources.Language) Before(org.junit.Before) Collections.emptySet(java.util.Collections.emptySet) Assertions.tuple(org.assertj.core.api.Assertions.tuple) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) UuidFactory(org.sonar.core.util.UuidFactory) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RuleIndex(org.sonar.server.rule.index.RuleIndex) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) RuleQuery(org.sonar.server.rule.index.RuleQuery) LogTester(org.sonar.api.utils.log.LogTester) RuleParamDto(org.sonar.db.rule.RuleParamDto) String.valueOf(java.lang.String.valueOf) RuleDto(org.sonar.db.rule.RuleDto) DeprecatedRuleKeyDto(org.sonar.db.rule.DeprecatedRuleKeyDto) Mockito.reset(org.mockito.Mockito.reset) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 13 with NewRepository

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

the class RegisterRulesTest method update_if_only_renamed_and_deprecated_key_declared.

@Test
@UseDataProvider("allRenamingCases")
public void update_if_only_renamed_and_deprecated_key_declared(String ruleKey1, String repo1, String ruleKey2, String repo2) {
    String name = "Name1";
    String description = "Description";
    system.setNow(DATE1.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository(repo1, "java");
        repo.createRule(ruleKey1).setName(name).setHtmlDescription(description);
        repo.done();
    });
    RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repo1, ruleKey1));
    assertThat(ruleIndex.search(new RuleQuery().setQueryText(name), new SearchOptions()).getUuids()).containsOnly(rule1.getUuid());
    system.setNow(DATE2.getTime());
    execute(context -> {
        NewRepository repo = context.createRepository(repo2, "java");
        repo.createRule(ruleKey2).setName(name).setHtmlDescription(description).addDeprecatedRuleKey(repo1, ruleKey1);
        repo.done();
    });
    // rule2 is actually rule1
    RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repo2, ruleKey2));
    assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid());
    assertThat(rule2.getName()).isEqualTo(rule1.getName());
    assertThat(rule2.getDescription()).isEqualTo(rule1.getDescription());
    assertThat(ruleIndex.search(new RuleQuery().setQueryText(name), new SearchOptions()).getUuids()).containsOnly(rule2.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) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 14 with NewRepository

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

the class RegisterRulesTest method update_only_rule_name.

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

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

the class RegisterRulesTest method declaring_two_rules_with_same_deprecated_RuleKey_should_throw_ISE.

@Test
public void declaring_two_rules_with_same_deprecated_RuleKey_should_throw_ISE() {
    assertThatThrownBy(() -> {
        execute(context -> {
            NewRepository repo = context.createRepository("fake", "java");
            createRule(repo, "newKey1").addDeprecatedRuleKey("fake", "old");
            createRule(repo, "newKey2").addDeprecatedRuleKey("fake", "old");
            repo.done();
        });
    }).isInstanceOf(IllegalStateException.class).hasMessage("The following deprecated rule keys are declared at least twice [fake:old]");
}
Also used : NewRepository(org.sonar.api.server.rule.RulesDefinition.NewRepository) 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