Search in sources :

Example 1 with QProfileDto

use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.

the class UpdateQualityProfilesLastUsedDateStepTest method fail_when_profile_is_linked_to_unknown_parent.

@Test
public void fail_when_profile_is_linked_to_unknown_parent() {
    QProfileDto currentProfile = newProfile("current").setParentKee("unknown");
    qualityProfileDb.insert(currentProfile);
    measureRepository.addRawMeasure(1, QUALITY_PROFILES_KEY, Measure.newMeasureBuilder().create(toJson(currentProfile.getKee())));
    assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext())).isInstanceOf(RowNotFoundException.class);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Test(org.junit.Test)

Example 2 with QProfileDto

use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.

the class RuleIndex method buildFilters.

/* Build main filter (match based) */
private static Map<String, QueryBuilder> buildFilters(RuleQuery query) {
    Map<String, QueryBuilder> filters = new HashMap<>();
    /* Add enforced filter on main type Rule */
    filters.put(FIELD_INDEX_TYPE, boolQuery().must(QueryBuilders.termsQuery(FIELD_INDEX_TYPE, TYPE_RULE.getType())));
    /* Add enforced filter on rules that are REMOVED */
    filters.put(FIELD_RULE_STATUS, boolQuery().mustNot(QueryBuilders.termQuery(FIELD_RULE_STATUS, RuleStatus.REMOVED.toString())));
    if (StringUtils.isNotEmpty(query.getInternalKey())) {
        filters.put(FIELD_RULE_INTERNAL_KEY, QueryBuilders.termQuery(FIELD_RULE_INTERNAL_KEY, query.getInternalKey()));
    }
    if (StringUtils.isNotEmpty(query.getRuleKey())) {
        filters.put(FIELD_RULE_RULE_KEY, QueryBuilders.termQuery(FIELD_RULE_RULE_KEY, query.getRuleKey()));
    }
    if (isNotEmpty(query.getLanguages())) {
        filters.put(FIELD_RULE_LANGUAGE, QueryBuilders.termsQuery(FIELD_RULE_LANGUAGE, query.getLanguages()));
    }
    if (isNotEmpty(query.getRepositories())) {
        filters.put(FIELD_RULE_REPOSITORY, QueryBuilders.termsQuery(FIELD_RULE_REPOSITORY, query.getRepositories()));
    }
    if (isNotEmpty(query.getSeverities())) {
        filters.put(FIELD_RULE_SEVERITY, QueryBuilders.termsQuery(FIELD_RULE_SEVERITY, query.getSeverities()));
    }
    if (isNotEmpty(query.getCwe())) {
        filters.put(FIELD_RULE_CWE, boolQuery().must(QueryBuilders.termsQuery(FIELD_RULE_CWE, query.getCwe())).must(QueryBuilders.termsQuery(FIELD_RULE_TYPE, VULNERABILITY.name(), SECURITY_HOTSPOT.name())));
    }
    if (isNotEmpty(query.getOwaspTop10())) {
        filters.put(FIELD_RULE_OWASP_TOP_10, boolQuery().must(QueryBuilders.termsQuery(FIELD_RULE_OWASP_TOP_10, query.getOwaspTop10())).must(QueryBuilders.termsQuery(FIELD_RULE_TYPE, VULNERABILITY.name(), SECURITY_HOTSPOT.name())));
    }
    if (isNotEmpty(query.getSansTop25())) {
        filters.put(FIELD_RULE_SANS_TOP_25, boolQuery().must(QueryBuilders.termsQuery(FIELD_RULE_SANS_TOP_25, query.getSansTop25())).must(QueryBuilders.termsQuery(FIELD_RULE_TYPE, VULNERABILITY.name(), SECURITY_HOTSPOT.name())));
    }
    if (isNotEmpty(query.getSonarsourceSecurity())) {
        filters.put(FIELD_RULE_SONARSOURCE_SECURITY, boolQuery().must(QueryBuilders.termsQuery(FIELD_RULE_SONARSOURCE_SECURITY, query.getSonarsourceSecurity())).must(QueryBuilders.termsQuery(FIELD_RULE_TYPE, VULNERABILITY.name(), SECURITY_HOTSPOT.name())));
    }
    if (StringUtils.isNotEmpty(query.getKey())) {
        filters.put(FIELD_RULE_KEY, QueryBuilders.termQuery(FIELD_RULE_KEY, query.getKey()));
    }
    if (isNotEmpty(query.getTags())) {
        filters.put(FIELD_RULE_TAGS, buildTagsFilter(query.getTags()));
    }
    Collection<RuleType> types = query.getTypes();
    if (isNotEmpty(types)) {
        List<String> typeNames = types.stream().map(RuleType::toString).collect(MoreCollectors.toList());
        filters.put(FIELD_RULE_TYPE, QueryBuilders.termsQuery(FIELD_RULE_TYPE, typeNames));
    }
    if (query.getAvailableSinceLong() != null) {
        filters.put("availableSince", QueryBuilders.rangeQuery(FIELD_RULE_CREATED_AT).gte(query.getAvailableSinceLong()));
    }
    if (isNotEmpty(query.getStatuses())) {
        Collection<String> stringStatus = new ArrayList<>();
        for (RuleStatus status : query.getStatuses()) {
            stringStatus.add(status.name());
        }
        filters.put(FIELD_RULE_STATUS, QueryBuilders.termsQuery(FIELD_RULE_STATUS, stringStatus));
    }
    Boolean isTemplate = query.isTemplate();
    if (isTemplate != null) {
        filters.put(FIELD_RULE_IS_TEMPLATE, QueryBuilders.termQuery(FIELD_RULE_IS_TEMPLATE, Boolean.toString(isTemplate)));
    }
    boolean includeExternal = query.includeExternal();
    if (!includeExternal) {
        filters.put(FIELD_RULE_IS_EXTERNAL, QueryBuilders.termQuery(FIELD_RULE_IS_EXTERNAL, false));
    }
    String template = query.templateKey();
    if (template != null) {
        filters.put(FIELD_RULE_TEMPLATE_KEY, QueryBuilders.termQuery(FIELD_RULE_TEMPLATE_KEY, template));
    }
    /* Implementation of activation query */
    QProfileDto profile = query.getQProfile();
    if (query.getActivation() != null && profile != null) {
        QueryBuilder childQuery = buildActivationFilter(query, profile);
        if (TRUE.equals(query.getActivation())) {
            filters.put("activation", JoinQueryBuilders.hasChildQuery(TYPE_ACTIVE_RULE.getName(), childQuery, ScoreMode.None));
        } else if (FALSE.equals(query.getActivation())) {
            filters.put("activation", boolQuery().mustNot(JoinQueryBuilders.hasChildQuery(TYPE_ACTIVE_RULE.getName(), childQuery, ScoreMode.None)));
        }
        QProfileDto compareToQProfile = query.getCompareToQProfile();
        if (compareToQProfile != null) {
            filters.put("comparison", JoinQueryBuilders.hasChildQuery(TYPE_ACTIVE_RULE.getName(), boolQuery().must(QueryBuilders.termQuery(FIELD_ACTIVE_RULE_PROFILE_UUID, compareToQProfile.getRulesProfileUuid())), ScoreMode.None));
        }
    }
    return filters;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RuleType(org.sonar.api.rules.RuleType) HasParentQueryBuilder(org.elasticsearch.join.query.HasParentQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleStatus(org.sonar.api.rule.RuleStatus)

Example 3 with QProfileDto

use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.

the class RuleIndexTest method facet_by_activation_severity_is_ignored_when_profile_is_not_specified.

@Test
public void facet_by_activation_severity_is_ignored_when_profile_is_not_specified() {
    RuleDefinitionDto rule = createJavaRule();
    QProfileDto profile = createJavaProfile();
    db.qualityProfiles().activateRule(profile, rule);
    index();
    RuleQuery query = newRuleQuery();
    verifyNoFacet(query, RuleIndex.FACET_ACTIVE_SEVERITIES);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 4 with QProfileDto

use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.

the class RuleIndexTest method search_by_activation_and_severity.

@Test
public void search_by_activation_and_severity() {
    RuleDefinitionDto major = createRule(setSeverity(MAJOR));
    RuleDefinitionDto minor = createRule(setSeverity(MINOR));
    RuleDefinitionDto info = createRule(setSeverity(INFO));
    QProfileDto profile1 = createJavaProfile();
    QProfileDto profile2 = createJavaProfile();
    db.qualityProfiles().activateRule(profile1, major, ar -> ar.setSeverity(BLOCKER));
    db.qualityProfiles().activateRule(profile2, major, ar -> ar.setSeverity(BLOCKER));
    db.qualityProfiles().activateRule(profile1, minor, ar -> ar.setSeverity(CRITICAL));
    index();
    // count activation severities of all active rules
    RuleQuery query = newRuleQuery().setActivation(true).setQProfile(profile1);
    verifySearch(query, major, minor);
    verifyFacet(query, RuleIndex.FACET_ACTIVE_SEVERITIES, entry(BLOCKER, 1L), entry(CRITICAL, 1L));
    // check stickyness of active severity facet
    query = newRuleQuery().setActivation(true).setQProfile(profile1).setActiveSeverities(singletonList(CRITICAL));
    verifySearch(query, minor);
    verifyFacet(query, RuleIndex.FACET_ACTIVE_SEVERITIES, entry(BLOCKER, 1L), entry(CRITICAL, 1L));
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 5 with QProfileDto

use of org.sonar.db.qualityprofile.QProfileDto in project sonarqube by SonarSource.

the class RuleIndexTest method search_by_activation.

@Test
public void search_by_activation() {
    RuleDefinitionDto rule1 = createJavaRule();
    RuleDefinitionDto rule2 = createJavaRule();
    RuleDefinitionDto rule3 = createJavaRule();
    QProfileDto profile1 = createJavaProfile();
    QProfileDto profile2 = createJavaProfile();
    db.qualityProfiles().activateRule(profile1, rule1);
    db.qualityProfiles().activateRule(profile2, rule1);
    db.qualityProfiles().activateRule(profile1, rule2);
    index();
    // active rules
    verifySearch(newRuleQuery().setActivation(true).setQProfile(profile1), rule1, rule2);
    verifySearch(newRuleQuery().setActivation(true).setQProfile(profile2), rule1);
    // inactive rules
    verifySearch(newRuleQuery().setActivation(false).setQProfile(profile1), rule3);
    verifySearch(newRuleQuery().setActivation(false).setQProfile(profile2), rule2, rule3);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Aggregations

QProfileDto (org.sonar.db.qualityprofile.QProfileDto)389 Test (org.junit.Test)329 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)139 UserDto (org.sonar.db.user.UserDto)72 DbSession (org.sonar.db.DbSession)38 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)38 ProjectDto (org.sonar.db.project.ProjectDto)36 RuleParamDto (org.sonar.db.rule.RuleParamDto)36 GroupDto (org.sonar.db.user.GroupDto)35 NotFoundException (org.sonar.server.exceptions.NotFoundException)31 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)23 TestRequest (org.sonar.server.ws.TestRequest)23 System2 (org.sonar.api.utils.System2)22 RuleQuery (org.sonar.server.rule.index.RuleQuery)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)21 DbTester (org.sonar.db.DbTester)21 BadRequestException (org.sonar.server.exceptions.BadRequestException)20 UserSessionRule (org.sonar.server.tester.UserSessionRule)20 List (java.util.List)19 Rule (org.junit.Rule)19