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);
}
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;
}
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);
}
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));
}
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);
}
Aggregations