Search in sources :

Example 1 with SQ_CATEGORY_KEYS_ORDERING

use of org.sonar.server.security.SecurityStandards.SQ_CATEGORY_KEYS_ORDERING in project sonarqube by SonarSource.

the class RuleIndexerTest method log_debug_if_hotspot_rule_maps_to_multiple_SQCategories.

@Test
@UseDataProvider("twoDifferentCategoriesButOTHERS")
public void log_debug_if_hotspot_rule_maps_to_multiple_SQCategories(SQCategory sqCategory1, SQCategory sqCategory2) {
    Set<String> standards = Stream.of(sqCategory1, sqCategory2).flatMap(t -> CWES_BY_SQ_CATEGORY.get(t).stream().map(e -> "cwe:" + e)).collect(toSet());
    SecurityStandards securityStandards = SecurityStandards.fromSecurityStandards(standards);
    RuleDefinitionDto rule = dbTester.rules().insert(RuleTesting.newRule().setType(RuleType.SECURITY_HOTSPOT).setSecurityStandards(standards).setDescription(VALID_HOTSPOT_RULE_DESCRIPTION));
    underTest.commitAndIndex(dbTester.getSession(), rule.getUuid());
    assertThat(logTester.getLogs()).hasSize(1);
    assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).isEqualTo(format("Rule %s with CWEs '%s' maps to multiple SQ Security Categories: %s", rule.getKey(), String.join(", ", securityStandards.getCwe()), ImmutableSet.of(sqCategory1, sqCategory2).stream().map(SQCategory::getKey).sorted(SQ_CATEGORY_KEYS_ORDERING).collect(joining(", "))));
}
Also used : IntStream(java.util.stream.IntStream) SecurityStandards(org.sonar.server.security.SecurityStandards) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) EsTester(org.sonar.server.es.EsTester) RuleStatus(org.sonar.api.rule.RuleStatus) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) TYPE_RULE(org.sonar.server.rule.index.RuleIndexDefinition.TYPE_RULE) Random(java.util.Random) CWES_BY_SQ_CATEGORY(org.sonar.server.security.SecurityStandards.CWES_BY_SQ_CATEGORY) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) DataProviderRunner(com.tngtech.java.junit.dataprovider.DataProviderRunner) DbSession(org.sonar.db.DbSession) RuleType(org.sonar.api.rules.RuleType) Severity(org.sonar.api.rule.Severity) SQ_CATEGORY_KEYS_ORDERING(org.sonar.server.security.SecurityStandards.SQ_CATEGORY_KEYS_ORDERING) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Scope(org.sonar.db.rule.RuleDto.Scope) EnumSet(java.util.EnumSet) Nullable(javax.annotation.Nullable) Collectors.toSet(java.util.stream.Collectors.toSet) DbTester(org.sonar.db.DbTester) RuleTesting(org.sonar.db.rule.RuleTesting) ImmutableSet(com.google.common.collect.ImmutableSet) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Collections.emptyList(java.util.Collections.emptyList) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider) Set(java.util.Set) Test(org.junit.Test) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) DbClient(org.sonar.db.DbClient) Stream(java.util.stream.Stream) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) RuleDto(org.sonar.db.rule.RuleDto) SQCategory(org.sonar.server.security.SecurityStandards.SQCategory) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) SecurityStandards(org.sonar.server.security.SecurityStandards) SQCategory(org.sonar.server.security.SecurityStandards.SQCategory) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 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.format (java.lang.String.format)1 Collections.emptyList (java.util.Collections.emptyList)1 EnumSet (java.util.EnumSet)1 Random (java.util.Random)1 Set (java.util.Set)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 RunWith (org.junit.runner.RunWith)1