Search in sources :

Example 1 with Context

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

the class RulesDownloaderTest method prepare.

@Before
public void prepare() {
    Context context = new Context();
    context.createRepository("javascript", "js").done();
    context.createRepository("squid", "java").done();
}
Also used : Context(org.sonar.api.server.rule.RulesDefinition.Context) Before(org.junit.Before)

Example 2 with Context

use of org.sonar.api.server.rule.RulesDefinition.Context 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 3 with Context

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

the class RulesDefinitionExtractor method extractRules.

public List<SonarLintRuleDefinition> extractRules(PluginInstancesRepository pluginInstancesRepository, Set<Language> enabledLanguages, boolean includeTemplateRules) {
    Context context;
    try {
        var container = new RulesDefinitionExtractorContainer(pluginInstancesRepository);
        container.execute();
        context = container.getContext();
    } catch (Exception e) {
        throw new IllegalStateException("Unable to extract rules metadata", e);
    }
    List<SonarLintRuleDefinition> rules = new ArrayList<>();
    for (RulesDefinition.Repository repoDef : context.repositories()) {
        if (repoDef.isExternal()) {
            continue;
        }
        var repoLanguage = Language.forKey(repoDef.language());
        if (repoLanguage.isEmpty() || !enabledLanguages.contains(repoLanguage.get())) {
            continue;
        }
        for (RulesDefinition.Rule ruleDef : repoDef.rules()) {
            if (ruleDef.type() == RuleType.SECURITY_HOTSPOT || (ruleDef.template() && !includeTemplateRules)) {
                continue;
            }
            rules.add(new SonarLintRuleDefinition(ruleDef));
        }
    }
    return rules;
}
Also used : Context(org.sonar.api.server.rule.RulesDefinition.Context) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) ArrayList(java.util.ArrayList)

Aggregations

Context (org.sonar.api.server.rule.RulesDefinition.Context)3 Before (org.junit.Before)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)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 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)1 String.valueOf (java.lang.String.valueOf)1 ArrayList (java.util.ArrayList)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 IntStream (java.util.stream.IntStream)1 RandomStringUtils.randomAlphanumeric (org.apache.commons.lang.RandomStringUtils.randomAlphanumeric)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1