import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.HashSet;
+import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.builder.HashCodeBuilder;
+import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
return defaultImpacts;
}
+ public Map<SoftwareQuality, Severity> getDefaultImpactsMap() {
+ return defaultImpacts.stream()
+ .collect(Collectors.toMap(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity));
+ }
+
public RuleDto addDefaultImpact(ImpactDto defaultImpactDto) {
defaultImpacts.stream().filter(impactDto -> impactDto.getSoftwareQuality() == defaultImpactDto.getSoftwareQuality()).findFirst()
.ifPresent(impactDto -> {
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
+import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.jetbrains.annotations.NotNull;
tuple(SoftwareQuality.SECURITY, Severity.LOW));
}
+ @Test
+ void getDefaultImpactsMap_shouldReturnExpectedResult() {
+ RuleDto dto = new RuleDto();
+ dto.addDefaultImpact(newImpactDto(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM));
+ dto.addDefaultImpact(newImpactDto(SoftwareQuality.SECURITY, Severity.HIGH));
+ dto.addDefaultImpact(newImpactDto(SoftwareQuality.RELIABILITY, Severity.LOW));
+
+ assertThat(dto.getDefaultImpactsMap())
+ .containsExactlyInAnyOrderEntriesOf(Map.of(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM,
+ SoftwareQuality.SECURITY, Severity.HIGH,
+ SoftwareQuality.RELIABILITY, Severity.LOW));
+ }
+
+ @Test
+ void getDefaultImpactsMap_whenIsEmpty_shouldReturnEmptyMap() {
+ RuleDto dto = new RuleDto();
+ assertThat(dto.getDefaultImpactsMap()).isEmpty();
+ }
+
@Test
void getEnumType_shouldReturnCorrectValue() {
RuleDto ruleDto = new RuleDto();
package org.sonar.server.qualityprofile;
import com.google.common.base.MoreObjects;
+import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
+import org.sonar.api.issue.impact.Severity;
+import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.QProfileChangeDto;
private final ActiveRuleKey key;
private final String ruleUuid;
private String severity = null;
+ private final Map<SoftwareQuality, Severity> impactSeverities = new EnumMap<>(SoftwareQuality.class);
private Boolean prioritizedRule = null;
private ActiveRuleInheritance inheritance = null;
private final Map<String, String> parameters = new HashMap<>();
return this;
}
- public ActiveRuleChange setPrioritizedRule(@Nullable Boolean prioritizedRule){
+ public Map<SoftwareQuality, Severity> getImpactSeverities() {
+ return impactSeverities;
+ }
+
+ public ActiveRuleChange setImpactSeverities(Map<SoftwareQuality, Severity> impactSeverities) {
+ this.impactSeverities.clear();
+ this.impactSeverities.putAll(impactSeverities);
+ return this;
+ }
+
+ public ActiveRuleChange setPrioritizedRule(@Nullable Boolean prioritizedRule) {
this.prioritizedRule = prioritizedRule;
return this;
}
@CheckForNull
- public Boolean isPrioritizedRule(){
+ public Boolean isPrioritizedRule() {
return prioritizedRule;
}
.add("inheritance", inheritance)
.add("parameters", parameters)
.add("prioritizedRule", prioritizedRule)
+ .add("impactSeverities", impactSeverities)
.toString();
}
}
package org.sonar.server.qualityprofile;
import com.google.common.base.Strings;
+import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
+import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.Severity;
/**
private final String ruleUuid;
private final boolean reset;
private final String severity;
+ private final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impactSeverities;
private final Boolean prioritizedRule;
private final Map<String, String> parameters = new HashMap<>();
- private RuleActivation(String ruleUuid, boolean reset, @Nullable String severity, @Nullable Boolean prioritizedRule, @Nullable Map<String,
- String> parameters) {
+ private RuleActivation(String ruleUuid, boolean reset, @Nullable String severity, @Nullable Boolean prioritizedRule, @Nullable Map<String, String> parameters,
+ Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impactSeverities) {
this.ruleUuid = ruleUuid;
this.reset = reset;
this.severity = severity;
this.prioritizedRule = prioritizedRule;
+ this.impactSeverities = impactSeverities.isEmpty() ? Map.of() : new EnumMap<>(impactSeverities);
if (severity != null && !Severity.ALL.contains(severity)) {
throw new IllegalArgumentException("Unknown severity: " + severity);
}
}
public static RuleActivation createReset(String ruleUuid) {
- return new RuleActivation(ruleUuid, true, null, null, null);
+ return new RuleActivation(ruleUuid, true, null, null, null, Map.of());
}
public static RuleActivation create(String ruleUuid, @Nullable String severity, @Nullable Boolean prioritizedRule,
@Nullable Map<String, String> parameters) {
- return new RuleActivation(ruleUuid, false, severity, prioritizedRule, parameters);
+ return new RuleActivation(ruleUuid, false, severity, prioritizedRule, parameters, Map.of());
}
public static RuleActivation create(String ruleUuid, @Nullable String severity, @Nullable Map<String, String> parameters) {
return create(ruleUuid, severity, null, parameters);
}
+ public static RuleActivation createOverrideImpacts(String ruleUuid, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impactSeverities,
+ @Nullable Map<String, String> parameters) {
+ return new RuleActivation(ruleUuid, false, null, null, parameters, impactSeverities);
+ }
+
public static RuleActivation create(String ruleUuid) {
return create(ruleUuid, null, null, null);
}
return severity;
}
+ public Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> getImpactSeverities() {
+ return impactSeverities;
+ }
+
public String getRuleUuid() {
return ruleUuid;
}
import org.sonar.api.PropertyType;
import org.sonar.api.config.Configuration;
import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
+import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.utils.System2;
import org.sonar.core.config.CorePropertyDefinitions;
import org.sonar.core.platform.SonarQubeVersion;
import org.sonar.db.DbTester;
+import org.sonar.db.issue.ImpactDto;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.OrgActiveRuleDto;
import org.sonar.db.qualityprofile.QProfileDto;
RuleActivation activation = RuleActivation.create(rule.getUuid(), BLOCKER, null);
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, BLOCKER, null, emptyMap());
+ assertThatRuleIsActivated(profile, rule, changes, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, emptyMap());
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
RuleActivation activation = RuleActivation.create(rule.getUuid(), BLOCKER, null);
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, BLOCKER, null, emptyMap());
+ assertThatRuleIsActivated(profile, rule, changes, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, emptyMap());
assertThatProfileIsUpdatedByUser(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
RuleActivation activation = RuleActivation.create(rule.getUuid());
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, ofEntries(entry("min", "10")));
+ assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, ofEntries(entry("min", "10")));
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
RuleActivation activation = RuleActivation.create(rule.getUuid(), null, of(ruleParam.getName(), "15"));
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, of("min", "15"));
+ assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, of("min", "15"));
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
RuleActivation activation = RuleActivation.create(rule.getUuid());
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, emptyMap());
+ assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
RuleActivation activation = RuleActivation.create(rule.getUuid(), null, of("min", ""));
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, of("min", "10"));
+ assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, of("min", "10"));
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
RuleActivation activation = RuleActivation.create(rule.getUuid(), null, of(paramWithoutDefault.getName(), "-10"));
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null,
+ assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null,
of(paramWithoutDefault.getName(), "-10", paramWithDefault.getName(), paramWithDefault.getDefaultValue()));
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
RuleActivation activation = RuleActivation.create(rule.getUuid(), null, of("xxx", "yyy"));
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, of(param.getName(), param.getDefaultValue()));
+ assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, of(param.getName(), param.getDefaultValue()));
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
RuleActivation updateActivation = RuleActivation.create(rule.getUuid(), CRITICAL, of(param.getName(), "20"));
changes = activate(profile, updateActivation);
- assertThatRuleIsUpdated(profile, rule, CRITICAL, null, of(param.getName(), "20"));
+ assertThatRuleIsUpdated(profile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, of(param.getName(), "20"));
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
}
changes = activate(profile, updateActivation);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
- assertThatRuleIsUpdated(profile, rule, MAJOR, null, of(paramWithDefault.getName(), "10", paramWithoutDefault.getName(), "3"));
+ assertThatRuleIsUpdated(profile, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), null,
+ of(paramWithDefault.getName(), "10", paramWithoutDefault.getName(), "3"));
assertThatProfileIsUpdatedBySystem(profile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
}
RuleActivation updateActivation = RuleActivation.create(rule.getUuid(), null, of(paramWithDefault.getName(), ""));
changes = activate(profile, updateActivation);
- assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), null, of(paramWithDefault.getName(), "10"));
+ assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, of(paramWithDefault.getName(), "10"));
assertThat(changes).hasSize(1);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
}
RuleActivation updateActivation = RuleActivation.create(rule.getUuid(), null, of(paramWithoutDefault.getName(), ""));
changes = activate(profile, updateActivation);
- assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), null, of(paramWithDefault.getName(),
+ assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, of(paramWithDefault.getName(),
paramWithDefault.getDefaultValue()));
assertThat(changes).hasSize(1);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
RuleActivation activation = RuleActivation.create(rule.getUuid());
List<ActiveRuleChange> changes = activate(profile, activation);
db.getDbClient().activeRuleDao().deleteParametersByRuleProfileUuids(db.getSession(), asList(profile.getRulesProfileUuid()));
- assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), null, emptyMap());
+ assertThatRuleIsActivated(profile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
// contrary to activerule, the param is supposed to be inserted but not updated
RuleActivation updateActivation = RuleActivation.create(rule.getUuid(), null, of(param.getName(), ""));
changes = activate(profile, updateActivation);
- assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), null, of(param.getName(), param.getDefaultValue()));
+ assertThatRuleIsUpdated(profile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, of(param.getName(), param.getDefaultValue()));
assertThat(changes).hasSize(1);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
}
List<ActiveRuleChange> changes = activate(profile, activation);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
-
// update without any severity or params => keep
RuleActivation update = RuleActivation.create(rule.getUuid());
changes = activate(profile, update);
// initial activation
RuleActivation activation = RuleActivation.create(customRule.getUuid(), MAJOR, emptyMap());
List<ActiveRuleChange> changes = activate(profile, activation);
- assertThatRuleIsActivated(profile, customRule, null, MAJOR, null, of("format", "txt"));
+ assertThatRuleIsActivated(profile, customRule, null, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), null, of("format", "txt"));
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
// update -> parameter is not changed
RuleActivation updateActivation = RuleActivation.create(customRule.getUuid(), BLOCKER, of("format", "xml"));
changes = activate(profile, updateActivation);
- assertThatRuleIsActivated(profile, customRule, null, BLOCKER, null, of("format", "txt"));
+ assertThatRuleIsActivated(profile, customRule, null, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, of("format", "txt"));
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(profile.getLanguage()));
}
List<ActiveRuleChange> changes = activate(childProfile, RuleActivation.create(rule.getUuid()));
assertThatProfileHasNoActiveRules(parentProfile);
- assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(grandChildProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(grandChildProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(childProfile.getLanguage()));
}
// Rule already active on childProfile2
List<ActiveRuleChange> changes = activate(childProfile2, activation);
- assertThatRuleIsActivated(childProfile2, rule, changes, rule.getSeverityString(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile2, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
deactivate(childProfile3, rule);
assertThatProfileHasNoActiveRules(childProfile3);
changes = activate(parentProfile, activation);
- assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
- assertThatRuleIsUpdated(childProfile2, rule, rule.getSeverityString(), INHERITED, emptyMap());
- assertThatRuleIsActivated(childProfile3, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
+ assertThatRuleIsUpdated(childProfile2, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(childProfile3, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
assertThat(changes).hasSize(4);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
}
@Test
void activate_whenChildAlreadyActivatedRuleWithOverriddenValues_shouldNotOverrideValues() {
RuleDto rule = createRule();
+ rule.replaceAllDefaultImpacts(List.of(new ImpactDto().setSoftwareQuality(SoftwareQuality.MAINTAINABILITY).setSeverity(org.sonar.api.issue.impact.Severity.HIGH)));
QProfileDto parentProfile = createProfile(rule);
QProfileDto childProfile = createChildProfile(parentProfile);
QProfileDto childProfile2 = createChildProfile(childProfile);
QProfileDto childProfile3 = createChildProfile(childProfile2);
List<ActiveRuleChange> changes = activate(childProfile2, RuleActivation.create(rule.getUuid(), CRITICAL, emptyMap()));
- assertThatRuleIsActivated(childProfile2, rule, changes, CRITICAL, null, emptyMap());
- assertThatRuleIsActivated(childProfile3, rule, changes, CRITICAL, INHERITED, emptyMap());
+ assertThatRuleIsActivated(childProfile2, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, emptyMap());
+ assertThatRuleIsActivated(childProfile3, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), INHERITED, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
changes = activate(parentProfile, RuleActivation.create(rule.getUuid(), MAJOR, emptyMap()));
- assertThatRuleIsActivated(parentProfile, rule, changes, MAJOR, null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, MAJOR, INHERITED, emptyMap());
- assertThatRuleIsUpdated(childProfile2, rule, CRITICAL, OVERRIDES, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), INHERITED, emptyMap());
+ assertThatRuleIsUpdated(childProfile2, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), OVERRIDES, emptyMap());
// childProfile3 is neither activated nor updated, it keeps its inherited value from childProfile2
assertThat(changes).hasSize(3);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
QProfileDto childProfile = createChildProfile(parentProfile);
List<ActiveRuleChange> changes = activate(parentProfile, RuleActivation.create(rule.getUuid(), CRITICAL, emptyMap()));
- assertThatRuleIsActivated(parentProfile, rule, changes, CRITICAL, null, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, emptyMap());
deactivate(childProfile, rule);
assertThatProfileHasNoActiveRules(childProfile);
changes = activate(childProfile, RuleActivation.create(rule.getUuid(), CRITICAL, emptyMap()));
- assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, INHERITED, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), INHERITED, emptyMap());
assertThat(changes).hasSize(1);
}
QProfileDto childProfile = createChildProfile(parentProfile);
List<ActiveRuleChange> changes = activate(parentProfile, RuleActivation.create(rule.getUuid(), CRITICAL, emptyMap()));
- assertThatRuleIsActivated(parentProfile, rule, changes, CRITICAL, null, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, emptyMap());
deactivate(childProfile, rule);
assertThatProfileHasNoActiveRules(childProfile);
changes = activate(childProfile, RuleActivation.create(rule.getUuid(), MAJOR, emptyMap()));
- assertThatRuleIsActivated(childProfile, rule, changes, MAJOR, OVERRIDES, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), OVERRIDES, emptyMap());
assertThat(changes).hasSize(1);
}
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(childProfile.getLanguage()));
assertThatProfileHasNoActiveRules(parentProfile);
- assertThatRuleIsUpdated(childProfile, rule, CRITICAL, null, of(param.getName(), "bar"));
- assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, INHERITED, of(param.getName(), "bar"));
+ assertThatRuleIsUpdated(childProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, of(param.getName(), "bar"));
+ assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), INHERITED,
+ of(param.getName(), "bar"));
assertThat(changes).hasSize(2);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(childProfile.getLanguage()));
}
changes = activate(grandChildProfile, overrideActivation);
assertThatProfileHasNoActiveRules(parentProfile);
- assertThatRuleIsUpdated(childProfile, rule, MAJOR, null, of(param.getName(), "foo"));
- assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, OVERRIDES, of(param.getName(), "bar"));
+ assertThatRuleIsUpdated(childProfile, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), null, of(param.getName(), "foo"));
+ assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), OVERRIDES,
+ of(param.getName(), "bar"));
assertThat(changes).hasSize(1);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(childProfile.getLanguage()));
}
changes = activate(childProfile, updateActivation);
assertThatProfileHasNoActiveRules(parentProfile);
- assertThatRuleIsUpdated(childProfile, rule, BLOCKER, null, of(param.getName(), "baz"));
- assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, OVERRIDES, of(param.getName(), "bar"));
+ assertThatRuleIsUpdated(childProfile, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, of(param.getName(), "baz"));
+ assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), OVERRIDES,
+ of(param.getName(), "bar"));
assertThat(changes).hasSize(1);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(childProfile.getLanguage()));
}
Map<String, String> test = new HashMap<>();
test.put(param.getName(), param.getDefaultValue());
- assertThatRuleIsUpdated(parentProfile, rule, rule.getSeverityString(), null, test);
- assertThatRuleIsUpdated(childProfile, rule, rule.getSeverityString(), INHERITED, of(param.getName(), param.getDefaultValue()));
- assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, OVERRIDES, of(param.getName(), "bar"));
+ assertThatRuleIsUpdated(parentProfile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, test);
+ assertThatRuleIsUpdated(childProfile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, of(param.getName(), param.getDefaultValue()));
+ assertThatRuleIsUpdated(grandChildProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), OVERRIDES,
+ of(param.getName(), "bar"));
assertThat(changes).hasSize(2);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
}
RuleActivation parentActivation = RuleActivation.create(rule.getUuid(), MAJOR, of(param.getName(), "bar"));
changes = activate(parentProfile, parentActivation);
- assertThatRuleIsUpdated(parentProfile, rule, MAJOR, null, of(param.getName(), "bar"));
- assertThatRuleIsUpdated(childProfile, rule, MAJOR, OVERRIDES, of(param.getName(), "foo"));
+ assertThatRuleIsUpdated(parentProfile, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), null, of(param.getName(), "bar"));
+ assertThatRuleIsUpdated(childProfile, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), OVERRIDES, of(param.getName(), "foo"));
assertThat(changes).hasSize(2);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
}
RuleActivation parentActivation = RuleActivation.create(rule.getUuid(), MAJOR, of(param.getName(), "foo"));
changes = activate(parentProfile, parentActivation);
- assertThatRuleIsUpdated(parentProfile, rule, MAJOR, null, of(param.getName(), "foo"));
- assertThatRuleIsUpdated(childProfile, rule, MAJOR, INHERITED, of(param.getName(), "foo"));
+ assertThatRuleIsUpdated(parentProfile, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), null, of(param.getName(), "foo"));
+ assertThatRuleIsUpdated(childProfile, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), INHERITED, of(param.getName(), "foo"));
assertThat(changes).hasSize(2);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
}
RuleActivation overrideActivation = RuleActivation.create(rule.getUuid(), MAJOR, of(param.getName(), "foo"));
changes = activate(childProfile, overrideActivation);
- assertThatRuleIsUpdated(childProfile, rule, MAJOR, INHERITED, of(param.getName(), "foo"));
+ assertThatRuleIsUpdated(childProfile, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM), INHERITED, of(param.getName(), "foo"));
assertThat(changes).isEmpty();
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(childProfile.getLanguage()));
}
RuleActivation overrideActivation = RuleActivation.create(rule.getUuid(), CRITICAL, of(param.getName(), "bar"));
changes = activate(childProfile, overrideActivation);
- assertThatRuleIsUpdated(childProfile, rule, CRITICAL, OVERRIDES, of(param.getName(), "bar"));
+ assertThatRuleIsUpdated(childProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), OVERRIDES, of(param.getName(), "bar"));
assertThat(changes).hasSize(1);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(childProfile.getLanguage()));
}
RuleActivation activation = RuleActivation.create(rule.getUuid());
List<ActiveRuleChange> changes = activate(parentProfile, activation);
- assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
changes = deactivate(parentProfile, rule);
// Rule active on parentProfile, childProfile1 and childProfile3 but not on childProfile2
List<ActiveRuleChange> changes = activate(parentProfile, activation);
- assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
- assertThatRuleIsActivated(childProfile2, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
- assertThatRuleIsActivated(childProfile3, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(childProfile2, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(childProfile3, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
deactivate(childProfile2, rule);
changes = activate(childProfile3, activation);
assertThatProfileHasNoActiveRules(childProfile2);
- assertThatRuleIsActivated(childProfile3, rule, changes, rule.getSeverityString(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile3, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
changes = deactivate(parentProfile, rule);
assertThatProfileHasNoActiveRules(parentProfile);
RuleActivation activation = RuleActivation.create(rule.getUuid());
List<ActiveRuleChange> changes = activate(parentProfile, activation);
- assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(parentProfile.getLanguage()));
activation = RuleActivation.create(rule.getUuid(), CRITICAL, null);
changes = activate(childProfile, activation);
- assertThatRuleIsUpdated(childProfile, rule, CRITICAL, OVERRIDES, emptyMap());
+ assertThatRuleIsUpdated(childProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), OVERRIDES, emptyMap());
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(childProfile.getLanguage()));
changes = deactivate(parentProfile, rule);
RuleActivation activation = RuleActivation.create(rule.getUuid());
List<ActiveRuleChange> changes = activate(parentProfile, activation);
- assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
changes = deactivate(childProfile, rule);
assertThatProfileHasNoActiveRules(childProfile);
RuleActivation activation = RuleActivation.create(rule.getUuid());
List<ActiveRuleChange> changes = activate(parentProfile, activation);
- assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
assertThatThrownBy(() -> deactivate(childProfile, rule))
.isInstanceOf(BadRequestException.class)
RuleActivation activation = RuleActivation.create(rule.getUuid(), CRITICAL, null);
List<ActiveRuleChange> changes = activate(parentProfile, activation);
- assertThatRuleIsActivated(parentProfile, rule, changes, CRITICAL, null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), INHERITED, emptyMap());
assertThat(changes).hasSize(2);
RuleActivation childActivation = RuleActivation.create(rule.getUuid(), BLOCKER, null);
changes = activate(childProfile, childActivation);
- assertThatRuleIsUpdated(childProfile, rule, BLOCKER, OVERRIDES, emptyMap());
+ assertThatRuleIsUpdated(childProfile, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), OVERRIDES, emptyMap());
assertThat(changes).hasSize(1);
RuleActivation resetActivation = RuleActivation.createReset(rule.getUuid());
changes = activate(childProfile, resetActivation);
- assertThatRuleIsUpdated(childProfile, rule, CRITICAL, INHERITED, emptyMap());
- assertThatRuleIsUpdated(parentProfile, rule, CRITICAL, null, emptyMap());
+ assertThatRuleIsUpdated(childProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), INHERITED, emptyMap());
+ assertThatRuleIsUpdated(parentProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, emptyMap());
assertThat(changes).hasSize(1);
}
RuleActivation activation = RuleActivation.create(rule.getUuid(), CRITICAL, null);
List<ActiveRuleChange> changes = activate(baseProfile, activation);
- assertThatRuleIsActivated(baseProfile, rule, changes, CRITICAL, null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, INHERITED, emptyMap());
- assertThatRuleIsActivated(grandChildProfile, rule, changes, CRITICAL, INHERITED, emptyMap());
+ assertThatRuleIsActivated(baseProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), INHERITED, emptyMap());
+ assertThatRuleIsActivated(grandChildProfile, rule, changes, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), INHERITED, emptyMap());
assertThat(changes).hasSize(3);
RuleActivation childActivation = RuleActivation.create(rule.getUuid(), BLOCKER, null);
changes = activate(childProfile, childActivation);
- assertThatRuleIsUpdated(childProfile, rule, BLOCKER, OVERRIDES, emptyMap());
- assertThatRuleIsUpdated(grandChildProfile, rule, BLOCKER, INHERITED, emptyMap());
+ assertThatRuleIsUpdated(childProfile, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), OVERRIDES, emptyMap());
+ assertThatRuleIsUpdated(grandChildProfile, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), INHERITED, emptyMap());
assertThat(changes).hasSize(2);
// Reset on parent do not change child nor grandchild
RuleActivation resetActivation = RuleActivation.createReset(rule.getUuid());
changes = activate(baseProfile, resetActivation);
- assertThatRuleIsUpdated(baseProfile, rule, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsUpdated(childProfile, rule, BLOCKER, OVERRIDES, emptyMap());
- assertThatRuleIsUpdated(grandChildProfile, rule, BLOCKER, INHERITED, emptyMap());
+ assertThatRuleIsUpdated(baseProfile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsUpdated(childProfile, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), OVERRIDES, emptyMap());
+ assertThatRuleIsUpdated(grandChildProfile, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), INHERITED, emptyMap());
assertThat(changes).hasSize(1);
// Reset on child change grandchild
resetActivation = RuleActivation.createReset(rule.getUuid());
changes = activate(childProfile, resetActivation);
- assertThatRuleIsUpdated(baseProfile, rule, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsUpdated(childProfile, rule, rule.getSeverityString(), INHERITED, emptyMap());
- assertThatRuleIsUpdated(grandChildProfile, rule, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsUpdated(baseProfile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsUpdated(childProfile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
+ assertThatRuleIsUpdated(grandChildProfile, rule, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
assertThat(changes).hasSize(2);
}
assertThat(bulkChangeResult.countSucceeded()).isEqualTo(bulkSize);
assertThat(bulkChangeResult.getChanges()).hasSize(bulkSize);
assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile)).hasSize(bulkSize);
- rules.forEach(r -> assertThatRuleIsActivated(profile, r, null, MINOR, true, null, emptyMap()));
+ rules
+ .forEach(r -> assertThatRuleIsActivated(profile, r, null, MINOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW), true, null, emptyMap()));
}
@Test
QProfileDto childProfile = createChildProfile(parentProfile);
activate(parentProfile, RuleActivation.create(rule.getUuid()));
- assertThatRuleIsActivated(parentProfile, rule, null, rule.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule, null, rule.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule, null, rule.getSeverityString(), rule.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule, null, rule.getSeverityString(), rule.getDefaultImpactsMap(), INHERITED, emptyMap());
ruleIndexer.indexAll();
assertThat(result.countFailed()).isZero();
// Rule1 must be activated with BLOCKER on all profiles
- assertThatRuleIsActivated(parentProfile, rule1, null, BLOCKER, true, null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule1, null, BLOCKER, true, INHERITED, emptyMap());
- assertThatRuleIsActivated(grandchildProfile, rule1, null, BLOCKER, true, INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule1, null, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), true, null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule1, null, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), true, INHERITED,
+ emptyMap());
+ assertThatRuleIsActivated(grandchildProfile, rule1, null, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), true, INHERITED,
+ emptyMap());
// Rule2 did not changed
- assertThatRuleIsActivated(parentProfile, rule2, null, rule2.getSeverityString(), null, emptyMap());
- assertThatRuleIsActivated(childProfile, rule2, null, rule2.getSeverityString(), INHERITED, emptyMap());
- assertThatRuleIsActivated(grandchildProfile, rule2, null, rule2.getSeverityString(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(parentProfile, rule2, null, rule2.getSeverityString(), rule2.getDefaultImpactsMap(), null, emptyMap());
+ assertThatRuleIsActivated(childProfile, rule2, null, rule2.getSeverityString(), rule2.getDefaultImpactsMap(), INHERITED, emptyMap());
+ assertThatRuleIsActivated(grandchildProfile, rule2, null, rule2.getSeverityString(), rule2.getDefaultImpactsMap(), INHERITED, emptyMap());
}
@Test
}
private void assertThatRuleIsActivated(QProfileDto profile, RuleDto rule, @Nullable List<ActiveRuleChange> changes,
- String expectedSeverity, boolean expectedPrioritizedRule, @Nullable ActiveRuleInheritance expectedInheritance,
+ String expectedSeverity, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts, boolean expectedPrioritizedRule,
+ @Nullable ActiveRuleInheritance expectedInheritance,
Map<String, String> expectedParams) {
OrgActiveRuleDto activeRule = db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile)
.stream()
.orElseThrow(IllegalStateException::new);
assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
+ assertThat(activeRule.getImpacts()).isEqualTo(impacts);
assertThat(activeRule.isPrioritizedRule()).isEqualTo(expectedPrioritizedRule);
assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance != null ? expectedInheritance.name() : null);
}
private void assertThatRuleIsActivated(QProfileDto profile, RuleDto rule, @Nullable List<ActiveRuleChange> changes,
- String expectedSeverity, @Nullable ActiveRuleInheritance expectedInheritance, Map<String, String> expectedParams) {
- assertThatRuleIsActivated(profile, rule, changes, expectedSeverity, false, expectedInheritance, expectedParams);
+ String expectedSeverity, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> expectedImpacts, @Nullable ActiveRuleInheritance expectedInheritance,
+ Map<String, String> expectedParams) {
+ assertThatRuleIsActivated(profile, rule, changes, expectedSeverity, expectedImpacts, false, expectedInheritance, expectedParams);
}
private void assertThatRuleIsNotPresent(QProfileDto profile, RuleDto rule) {
}
private void assertThatRuleIsUpdated(QProfileDto profile, RuleDto rule,
- String expectedSeverity, @Nullable ActiveRuleInheritance expectedInheritance, Map<String, String> expectedParams) {
+ String expectedSeverity, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> expectedImpacts, @Nullable ActiveRuleInheritance expectedInheritance,
+ Map<String, String> expectedParams) {
OrgActiveRuleDto activeRule = db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile)
.stream()
.filter(ar -> ar.getRuleKey().equals(rule.getKey()))
.orElseThrow(IllegalStateException::new);
assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
+ assertThat(activeRule.getImpacts()).isEqualTo(expectedImpacts);
assertThat(activeRule.getInheritance()).isEqualTo(expectedInheritance != null ? expectedInheritance.name() : null);
List<ActiveRuleParamDto> params = db.getDbClient().activeRuleDao().selectParamsByActiveRuleUuid(db.getSession(), activeRule.getUuid());
}
private RuleDto createRule() {
- return db.rules().insert(r -> r.setSeverity(Severity.MAJOR));
+ return db.rules().insert(r -> r.setSeverity(Severity.MAJOR)
+ .replaceAllDefaultImpacts(List.of(new ImpactDto().setSoftwareQuality(SoftwareQuality.MAINTAINABILITY).setSeverity(org.sonar.api.issue.impact.Severity.MEDIUM))));
}
private RuleDto createJavaRule() {
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.ArgumentCaptor;
import org.sonar.api.config.Configuration;
+import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule;
import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile;
-import org.sonar.api.testfixtures.log.LogTester;
+import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.Version;
import org.sonar.core.platform.SonarQubeVersion;
-import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.core.util.UuidFactoryImpl;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED;
import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED;
-public class RegisterQualityProfilesNotificationIT {
+class RegisterQualityProfilesNotificationIT {
private static final Random RANDOM = new SecureRandom();
private System2 system2 = mock(System2.class);
- @Rule
+ @RegisterExtension
public DbTester db = DbTester.create(system2);
- @Rule
+ @RegisterExtension
public UserSessionRule userSessionRule = UserSessionRule.standalone();
- @Rule
+ @RegisterExtension
public BuiltInQProfileRepositoryRule builtInQProfileRepositoryRule = new BuiltInQProfileRepositoryRule();
- @Rule
- public LogTester logTester = new LogTester();
-
- private DbClient dbClient = db.getDbClient();
- private TypeValidations typeValidations = mock(TypeValidations.class);
- private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
- private QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class);
- private ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class));
- private SonarQubeVersion sonarQubeVersion = new SonarQubeVersion(Version.create(10, 3));
- private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, ruleFinder, system2, UuidFactoryFast.getInstance(),
+ @RegisterExtension
+ public LogTesterJUnit5 logTester = new LogTesterJUnit5();
+
+ private final DbClient dbClient = db.getDbClient();
+ private final TypeValidations typeValidations = mock(TypeValidations.class);
+ private final ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
+ private final QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class);
+ private final ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class));
+ private final SonarQubeVersion sonarQubeVersion = new SonarQubeVersion(Version.create(10, 3));
+ private final BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, ruleFinder, system2, UuidFactoryImpl.INSTANCE,
typeValidations, activeRuleIndexer, sonarQubeVersion);
- private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, typeValidations, userSessionRule, mock(Configuration.class), sonarQubeVersion);
- private QProfileRules qProfileRules = new QProfileRulesImpl(dbClient, ruleActivator, mock(RuleIndex.class), activeRuleIndexer, qualityProfileChangeEventService);
- private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer, qualityProfileChangeEventService);
- private BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification = mock(BuiltInQualityProfilesUpdateListener.class);
+ private final RuleActivator ruleActivator = new RuleActivator(system2, dbClient, typeValidations, userSessionRule, mock(Configuration.class), sonarQubeVersion);
+ private final QProfileRules qProfileRules = new QProfileRulesImpl(dbClient, ruleActivator, mock(RuleIndex.class), activeRuleIndexer, qualityProfileChangeEventService);
+ private final BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer, qualityProfileChangeEventService);
+ private final BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification = mock(BuiltInQualityProfilesUpdateListener.class);
private final Languages languages = LanguageTesting.newLanguages();
- private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
+ private final RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient,
builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification, system2, languages);
@Test
- public void do_not_send_notification_on_new_profile() {
+ void do_not_send_notification_on_new_profile() {
String language = newLanguageKey();
builtInQProfileRepositoryRule.add(newLanguage(language), "Sonar way");
builtInQProfileRepositoryRule.initialize();
}
@Test
- public void do_not_send_notification_when_profile_is_not_updated() {
+ void do_not_send_notification_when_profile_is_not_updated() {
String language = newLanguageKey();
RuleDto dbRule = db.rules().insert(r -> r.setLanguage(language));
RulesProfileDto dbProfile = insertBuiltInProfile(language);
- activateRuleInDb(dbProfile, dbRule, MAJOR);
+ activateRuleInDb(dbProfile, dbRule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
addPluginProfile(dbProfile, dbRule);
builtInQProfileRepositoryRule.initialize();
}
@Test
- public void send_notification_when_a_new_rule_is_activated() {
+ void send_notification_when_a_new_rule_is_activated() {
String language = newLanguageKey();
RuleDto existingRule = db.rules().insert(r -> r.setLanguage(language));
RulesProfileDto dbProfile = insertBuiltInProfile(language);
- activateRuleInDb(dbProfile, existingRule, MAJOR);
+ activateRuleInDb(dbProfile, existingRule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
RuleDto newRule = db.rules().insert(r -> r.setLanguage(language));
addPluginProfile(dbProfile, existingRule, newRule);
builtInQProfileRepositoryRule.initialize();
}
@Test
- public void send_notification_when_a_rule_is_deactivated() {
+ void send_notification_when_a_rule_is_deactivated() {
String language = newLanguageKey();
RuleDto existingRule = db.rules().insert(r -> r.setLanguage(language));
RulesProfileDto dbProfile = insertBuiltInProfile(language);
- activateRuleInDb(dbProfile, existingRule, MAJOR);
+ activateRuleInDb(dbProfile, existingRule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
addPluginProfile(dbProfile);
builtInQProfileRepositoryRule.initialize();
}
@Test
- public void send_a_single_notification_when_multiple_rules_are_activated() {
+ void send_a_single_notification_when_multiple_rules_are_activated() {
String language = newLanguageKey();
RuleDto existingRule1 = db.rules().insert(r -> r.setLanguage(language));
RuleDto newRule1 = db.rules().insert(r -> r.setLanguage(language));
RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
- activateRuleInDb(dbProfile1, existingRule1, MAJOR);
+ activateRuleInDb(dbProfile1, existingRule1, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
addPluginProfile(dbProfile1, existingRule1, newRule1);
RuleDto existingRule2 = db.rules().insert(r -> r.setLanguage(language));
RuleDto newRule2 = db.rules().insert(r -> r.setLanguage(language));
RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
- activateRuleInDb(dbProfile2, existingRule2, MAJOR);
+ activateRuleInDb(dbProfile2, existingRule2, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
addPluginProfile(dbProfile2, existingRule2, newRule2);
builtInQProfileRepositoryRule.initialize();
}
@Test
- public void notification_does_not_include_inherited_profiles_when_rule_is_added() {
+ void notification_does_not_include_inherited_profiles_when_rule_is_added() {
String language = newLanguageKey();
RuleDto newRule = db.rules().insert(r -> r.setLanguage(language));
}
@Test
- public void notification_does_not_include_inherited_profiled_when_rule_is_changed() {
+ void notification_does_not_include_inherited_profiled_when_rule_is_changed() {
String language = newLanguageKey();
RuleDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
}
@Test
- public void notification_does_not_include_inherited_profiles_when_rule_is_deactivated() {
+ void notification_does_not_include_inherited_profiles_when_rule_is_deactivated() {
String language = newLanguageKey();
RuleDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
}
@Test
- public void notification_contains_send_start_and_end_date() {
+ void notification_contains_send_start_and_end_date() {
String language = newLanguageKey();
RuleDto existingRule = db.rules().insert(r -> r.setLanguage(language));
RulesProfileDto dbProfile = insertBuiltInProfile(language);
- activateRuleInDb(dbProfile, existingRule, MAJOR);
+ activateRuleInDb(dbProfile, existingRule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
RuleDto newRule = db.rules().insert(r -> r.setLanguage(language));
addPluginProfile(dbProfile, existingRule, newRule);
builtInQProfileRepositoryRule.initialize();
return ruleProfileDto;
}
- private void activateRuleInDb(RulesProfileDto profile, RuleDto rule, RulePriority severity) {
+ private void activateRuleInDb(RulesProfileDto profile, RuleDto rule, RulePriority severity, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts) {
ActiveRuleDto dto = new ActiveRuleDto()
.setProfileUuid(profile.getUuid())
.setSeverity(severity.name())
+ .setImpacts(impacts)
.setRuleUuid(rule.getUuid())
.setCreatedAt(RANDOM.nextLong(Long.MAX_VALUE))
.setUpdatedAt(RANDOM.nextLong(Long.MAX_VALUE));
import java.util.Optional;
import javax.annotation.Nullable;
import org.assertj.core.groups.Tuple;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.api.config.Configuration;
import org.sonar.api.impl.utils.TestSystem2;
+import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.utils.Version;
import org.sonar.core.platform.SonarQubeVersion;
import org.sonar.db.DbTester;
+import org.sonar.db.issue.ImpactDto;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import static org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto;
import static org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED;
-public class BuiltInQProfileUpdateImplIT {
+class BuiltInQProfileUpdateImplIT {
private static final long NOW = 1_000;
private static final long PAST = NOW - 100;
- @Rule
+ @RegisterExtension
public BuiltInQProfileRepositoryRule builtInProfileRepository = new BuiltInQProfileRepositoryRule();
- @Rule
+ @RegisterExtension
public DbTester db = DbTester.create();
- @Rule
+ @RegisterExtension
public UserSessionRule userSession = UserSessionRule.standalone();
private System2 system2 = new TestSystem2().setNow(NOW);
private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
private RulesProfileDto persistedProfile;
- @Before
- public void setUp() {
+ @BeforeEach
+ void setUp() {
persistedProfile = newRuleProfileDto(rp -> rp
.setIsBuiltIn(true)
.setLanguage("xoo")
}
@Test
- public void activate_new_rules() {
- RuleDto rule1 = db.rules().insert(r -> r.setLanguage("xoo"));
+ void activate_new_rules() {
+ RuleDto rule1 = db.rules()
+ .insert(r -> r.setLanguage("xoo").replaceAllDefaultImpacts(List.of(newImpactDto(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH),
+ newImpactDto(SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW))));
RuleDto rule2 = db.rules().insert(r -> r.setLanguage("xoo"));
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo");
List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile);
assertThat(activeRules).hasSize(2);
- assertThatRuleIsNewlyActivated(activeRules, rule1, CRITICAL);
- assertThatRuleIsNewlyActivated(activeRules, rule2, MAJOR);
+ assertThatRuleIsNewlyActivated(activeRules, rule1, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH,
+ SoftwareQuality.RELIABILITY, org.sonar.api.issue.impact.Severity.LOW));
+ assertThatRuleIsNewlyActivated(activeRules, rule2, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
assertThatProfileIsMarkedAsUpdated(persistedProfile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
}
+ private static ImpactDto newImpactDto(SoftwareQuality maintainability, org.sonar.api.issue.impact.Severity high) {
+ return new ImpactDto().setSoftwareQuality(maintainability).setSeverity(high);
+ }
+
@Test
- public void already_activated_rule_is_updated_in_case_of_differences() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo"));
+ void already_activated_rule_is_updated_in_case_of_differences() {
+ RuleDto rule = db.rules().insert(
+ r -> r.setLanguage("xoo")
+ .replaceAllDefaultImpacts(List.of(new ImpactDto().setSoftwareQuality(SoftwareQuality.MAINTAINABILITY).setSeverity(org.sonar.api.issue.impact.Severity.HIGH))));
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo");
- newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()).overrideSeverity(Severity.CRITICAL);
+ newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey()).overrideSeverity(Severity.BLOCKER);
newQp.done();
BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule);
- activateRuleInDb(persistedProfile, rule, BLOCKER);
+ activateRuleInDb(persistedProfile, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
List<ActiveRuleChange> changes = underTest.update(db.getSession(), builtIn, persistedProfile);
List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile);
assertThat(activeRules).hasSize(1);
- assertThatRuleIsUpdated(activeRules, rule, CRITICAL);
+ assertThatRuleIsUpdated(activeRules, rule, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER));
assertThatProfileIsMarkedAsUpdated(persistedProfile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
}
@Test
- public void already_activated_rule_is_not_touched_if_no_differences() {
+ void already_activated_rule_is_not_touched_if_no_differences() {
RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo"));
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("Sonar way", "xoo");
newQp.done();
BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile("xoo", "Sonar way"), rule);
- activateRuleInDb(persistedProfile, rule, CRITICAL);
+ activateRuleInDb(persistedProfile, rule, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
underTest.update(db.getSession(), builtIn, persistedProfile);
}
@Test
- public void deactivate_rule_that_is_not_in_built_in_definition_anymore() {
+ void deactivate_rule_that_is_not_in_built_in_definition_anymore() {
RuleDto rule1 = db.rules().insert(r -> r.setLanguage("xoo"));
RuleDto rule2 = db.rules().insert(r -> r.setLanguage("xoo"));
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
// built-in definition contains only rule2
// so rule1 must be deactivated
- activateRuleInDb(persistedProfile, rule1, CRITICAL);
+ activateRuleInDb(persistedProfile, rule1, CRITICAL, Map.of());
List<ActiveRuleChange> changes = underTest.update(db.getSession(), builtIn, persistedProfile);
}
@Test
- public void activate_deactivate_and_update_three_rules_at_the_same_time() {
+ void activate_deactivate_and_update_three_rules_at_the_same_time() {
RuleDto rule1 = db.rules().insert(r -> r.setLanguage("xoo"));
RuleDto rule2 = db.rules().insert(r -> r.setLanguage("xoo"));
RuleDto rule3 = db.rules().insert(r -> r.setLanguage("xoo"));
// rule1 must be updated (blocker to critical)
// rule2 must be activated
// rule3 must be deactivated
- activateRuleInDb(persistedProfile, rule1, BLOCKER);
- activateRuleInDb(persistedProfile, rule3, BLOCKER);
+ activateRuleInDb(persistedProfile, rule1, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
+ activateRuleInDb(persistedProfile, rule3, BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
List<ActiveRuleChange> changes = underTest.update(db.getSession(), builtIn, persistedProfile);
List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile);
assertThat(activeRules).hasSize(2);
- assertThatRuleIsUpdated(activeRules, rule1, CRITICAL);
- assertThatRuleIsNewlyActivated(activeRules, rule2, MAJOR);
+ assertThatRuleIsUpdated(activeRules, rule1, CRITICAL, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
+ assertThatRuleIsNewlyActivated(activeRules, rule2, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM));
assertThatRuleIsDeactivated(activeRules, rule3);
assertThatProfileIsMarkedAsUpdated(persistedProfile);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
// SONAR-10473
@Test
- public void activate_rule_on_built_in_profile_resets_severity_to_default_if_not_overridden() {
+ void activate_rule_on_built_in_profile_resets_severity_to_default_if_not_overridden() {
RuleDto rule = db.rules().insert(r -> r.setSeverity(Severity.MAJOR).setLanguage("xoo"));
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
underTest.update(db.getSession(), builtIn, persistedProfile);
List<ActiveRuleDto> activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile);
- assertThatRuleIsNewlyActivated(activeRules, rule, MAJOR);
+ assertThatRuleIsNewlyActivated(activeRules, rule, MAJOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
// emulate an upgrade of analyzer that changes the default severity of the rule
rule.setSeverity(Severity.MINOR);
+ rule.replaceAllDefaultImpacts(List.of(newImpactDto(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW)));
db.rules().update(rule);
List<ActiveRuleChange> changes = underTest.update(db.getSession(), builtIn, persistedProfile);
activeRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), persistedProfile);
- assertThatRuleIsNewlyActivated(activeRules, rule, MINOR);
+ assertThatRuleIsNewlyActivated(activeRules, rule, MINOR, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW));
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
}
@Test
- public void activate_rule_on_built_in_profile_resets_params_to_default_if_not_overridden() {
+ void activate_rule_on_built_in_profile_resets_params_to_default_if_not_overridden() {
RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo"));
RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
}
@Test
- public void propagate_activation_to_descendant_profiles() {
+ void propagate_activation_to_descendant_profiles() {
RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo"));
QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
// SONAR-14559
@Test
- public void propagate_rule_update_to_descendant_active_rule() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
+ void propagate_rule_update_to_descendant_active_rule() {
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER)
+ .replaceAllDefaultImpacts(List.of(newImpactDto(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER))));
QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
- activateRuleInDb(RulesProfileDto.from(parentProfile), rule, RulePriority.valueOf(Severity.MINOR), null);
+ activateRuleInDb(RulesProfileDto.from(parentProfile), rule, RulePriority.valueOf(Severity.MINOR),
+ Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW));
QProfileDto childProfile = createChildProfile(parentProfile);
- activateRuleInDb(RulesProfileDto.from(childProfile), rule, RulePriority.valueOf(Severity.MINOR), INHERITED);
+ activateRuleInDb(RulesProfileDto.from(childProfile), rule, RulePriority.valueOf(Severity.MINOR),
+ Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW), INHERITED);
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(parentProfile.getName(), parentProfile.getLanguage());
assertThat(changes).hasSize(2);
List<ActiveRuleDto> parentActiveRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), RulesProfileDto.from(parentProfile));
- assertThatRuleIsUpdated(parentActiveRules, rule, RulePriority.BLOCKER, null);
+ assertThatRuleIsUpdated(parentActiveRules, rule, RulePriority.BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER));
List<ActiveRuleDto> childActiveRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), RulesProfileDto.from(childProfile));
- assertThatRuleIsUpdated(childActiveRules, rule, RulePriority.BLOCKER, INHERITED);
+ assertThatRuleIsUpdated(childActiveRules, rule, RulePriority.BLOCKER, Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), INHERITED);
verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
}
@Test
- public void propagate_rule_param_update_to_descendant_active_rule_params() {
+ void propagate_rule_param_update_to_descendant_active_rule_params() {
RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
- RulePriority.valueOf(Severity.MINOR), null);
+ RulePriority.valueOf(Severity.MINOR), Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
activateRuleParamInDb(parentActiveRuleDto, ruleParam, "20");
QProfileDto childProfile = createChildProfile(parentProfile);
ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
- RulePriority.valueOf(Severity.MINOR), INHERITED);
+ RulePriority.valueOf(Severity.MINOR), Map.of(), INHERITED);
activateRuleParamInDb(childActiveRuleDto, ruleParam, "20");
BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
}
@Test
- public void propagate_deactivation_to_descendant_profiles() {
+ void propagate_deactivation_to_descendant_profiles() {
RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo"));
QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
.containsExactlyInAnyOrder(expectedParams);
}
- private static void assertThatRuleIsNewlyActivated(List<ActiveRuleDto> activeRules, RuleDto rule, RulePriority severity) {
+ private static void assertThatRuleIsNewlyActivated(List<ActiveRuleDto> activeRules, RuleDto rule, RulePriority severity,
+ Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts) {
ActiveRuleDto activeRule = findRule(activeRules, rule).get();
assertThat(activeRule.getInheritance()).isNull();
assertThat(activeRule.getSeverityString()).isEqualTo(severity.name());
+ assertThat(activeRule.getImpacts()).isEqualTo(impacts);
assertThat(activeRule.getCreatedAt()).isEqualTo(NOW);
assertThat(activeRule.getUpdatedAt()).isEqualTo(NOW);
}
- private static void assertThatRuleIsUpdated(List<ActiveRuleDto> activeRules, RuleDto rule, RulePriority severity, @Nullable ActiveRuleInheritance expectedInheritance) {
+ private static void assertThatRuleIsUpdated(List<ActiveRuleDto> activeRules, RuleDto rule, RulePriority severity,
+ Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts, @Nullable ActiveRuleInheritance expectedInheritance) {
ActiveRuleDto activeRule = findRule(activeRules, rule).get();
if (expectedInheritance != null) {
assertThat(activeRule.getInheritance()).isNull();
}
assertThat(activeRule.getSeverityString()).isEqualTo(severity.name());
+ assertThat(activeRule.getImpacts()).isEqualTo(impacts);
assertThat(activeRule.getCreatedAt()).isEqualTo(PAST);
assertThat(activeRule.getUpdatedAt()).isEqualTo(NOW);
}
- private static void assertThatRuleIsUpdated(List<ActiveRuleDto> activeRules, RuleDto rule, RulePriority severity) {
- assertThatRuleIsUpdated(activeRules, rule, severity, null);
- }
-
- private static void assertThatRuleIsUpdated(ActiveRuleDto activeRules, RuleDto rule, RulePriority severity, @Nullable ActiveRuleInheritance expectedInheritance) {
- assertThatRuleIsUpdated(singletonList(activeRules), rule, severity, expectedInheritance);
+ private static void assertThatRuleIsUpdated(List<ActiveRuleDto> activeRules, RuleDto rule, RulePriority severity,
+ Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts) {
+ assertThatRuleIsUpdated(activeRules, rule, severity, impacts, null);
}
private static void assertThatRuleIsUntouched(List<ActiveRuleDto> activeRules, RuleDto rule, RulePriority severity) {
.findFirst();
}
- private ActiveRuleDto activateRuleInDb(RulesProfileDto profile, RuleDto rule, RulePriority severity) {
- return activateRuleInDb(profile, rule, severity, null);
+ private ActiveRuleDto activateRuleInDb(RulesProfileDto profile, RuleDto rule, RulePriority severity, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts) {
+ return activateRuleInDb(profile, rule, severity, impacts, null);
}
- private ActiveRuleDto activateRuleInDb(RulesProfileDto ruleProfile, RuleDto rule, RulePriority severity, @Nullable ActiveRuleInheritance inheritance) {
+ private ActiveRuleDto activateRuleInDb(RulesProfileDto ruleProfile, RuleDto rule, RulePriority severity, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts,
+ @Nullable ActiveRuleInheritance inheritance) {
ActiveRuleDto dto = new ActiveRuleDto()
.setKey(ActiveRuleKey.of(ruleProfile, RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey())))
.setProfileUuid(ruleProfile.getUuid())
.setSeverity(severity.name())
+ .setImpacts(impacts)
.setRuleUuid(rule.getUuid())
.setInheritance(inheritance != null ? inheritance.name() : null)
.setCreatedAt(PAST)
private void activateRuleParamInDb(ActiveRuleDto activeRuleDto, RuleParamDto ruleParamDto, String value) {
ActiveRuleParamDto dto = new ActiveRuleParamDto()
- .setActiveRuleUuid(activeRuleDto.getUuid())
- .setRulesParameterUuid(ruleParamDto.getUuid())
- .setKey(ruleParamDto.getName())
- .setValue(value);
+ .setActiveRuleUuid(activeRuleDto.getUuid())
+ .setRulesParameterUuid(ruleParamDto.getUuid())
+ .setKey(ruleParamDto.getName())
+ .setValue(value);
db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRuleDto, dto);
db.commit();
}
package org.sonar.server.qualityprofile.builtin;
import java.util.List;
+import java.util.Map;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.api.config.Configuration;
import org.sonar.api.impl.utils.TestSystem2;
+import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RulePriority;
import org.sonar.core.platform.SonarQubeVersion;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.issue.ImpactDto;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
+import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY;
+import static org.sonar.api.issue.impact.SoftwareQuality.RELIABILITY;
+import static org.sonar.api.issue.impact.SoftwareQuality.SECURITY;
import static org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED;
import static org.sonar.server.qualityprofile.ActiveRuleInheritance.OVERRIDES;
QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
- RulePriority.valueOf(Severity.BLOCKER), false, null);
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH), false, null);
ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
QProfileDto childProfile = createChildProfile(parentProfile);
ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
- RulePriority.valueOf(Severity.MINOR), true, OVERRIDES);
+ RulePriority.valueOf(Severity.MINOR), Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW), true, OVERRIDES);
ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "15");
DbSession session = db.getSession();
ActiveRuleChange activeRuleResult = result.get(0);
assertThat(activeRuleResult.getParameters()).containsEntry("min", "10");
assertThat(activeRuleResult.getSeverity()).isEqualTo(Severity.BLOCKER);
+ assertThat(activeRuleResult.getImpactSeverities()).isEqualTo(Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
assertThat(activeRuleResult.isPrioritizedRule()).isFalse();
assertThat(activeRuleResult.getInheritance()).isEqualTo(ActiveRuleInheritance.INHERITED);
}
@Test
void request_new_severity_and_prioritized_rule_and_param_for_child_rule() {
- RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER)
+ .replaceAllDefaultImpacts(List.of(newImpactDto(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER))));
RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
- RulePriority.valueOf(Severity.BLOCKER), null, null);
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, null);
ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
QProfileDto childProfile = createChildProfile(parentProfile);
ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
- RulePriority.valueOf(Severity.BLOCKER), null, INHERITED);
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, INHERITED);
ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "10");
DbSession session = db.getSession();
ActiveRuleChange activeRuleResult = result.get(0);
assertThat(activeRuleResult.getParameters()).containsEntry("min", "15");
assertThat(activeRuleResult.getSeverity()).isEqualTo(Severity.MINOR);
+ assertThat(activeRuleResult.getImpactSeverities()).containsExactlyEntriesOf(Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW));
assertThat(activeRuleResult.isPrioritizedRule()).isTrue();
assertThat(activeRuleResult.getInheritance()).isEqualTo(OVERRIDES);
}
+ @Test
+ void activate_whenOnlyOneImpactAndImpactDoesntMatchRuleType_shouldOverrideSeverity() {
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER)
+ .replaceAllDefaultImpacts(List.of(newImpactDto(SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER))));
+ RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
+
+ QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
+ ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, null);
+ ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
+
+ QProfileDto childProfile = createChildProfile(parentProfile);
+ ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, INHERITED);
+ ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "10");
+
+ DbSession session = db.getSession();
+ RuleActivation request = RuleActivation.createOverrideImpacts(rule.getUuid(), Map.of(SECURITY, org.sonar.api.issue.impact.Severity.LOW), of("min", "15"));
+ RuleActivationContext context = new RuleActivationContext.Builder()
+ .setProfiles(asList(parentProfile, childProfile))
+ .setBaseProfile(RulesProfileDto.from(childProfile))
+ .setDate(NOW)
+ .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
+ .setRules(singletonList(rule))
+ .setRuleParams(singletonList(ruleParam))
+ .setActiveRules(asList(parentActiveRuleDto, childActiveRuleDto))
+ .setActiveRuleParams(asList(parentActiveRuleParam, childActiveRuleParam))
+ .build();
+
+ List<ActiveRuleChange> result = underTest.activate(session, request, context);
+
+ assertThat(result).hasSize(1);
+ ActiveRuleChange activeRuleResult = result.get(0);
+ assertThat(activeRuleResult.getSeverity()).isEqualTo(Severity.MINOR);
+ assertThat(activeRuleResult.getImpactSeverities()).containsExactlyEntriesOf(Map.of(SECURITY, org.sonar.api.issue.impact.Severity.LOW));
+ assertThat(activeRuleResult.getInheritance()).isEqualTo(OVERRIDES);
+ }
+
+ @Test
+ void activate_whenTwoImpactsAndImpactsDoesntMatchRuleType_shouldNotOverrideSeverity() {
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER)
+ .replaceAllDefaultImpacts(
+ List.of(newImpactDto(SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER), new ImpactDto(RELIABILITY, org.sonar.api.issue.impact.Severity.BLOCKER))));
+ RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
+
+ QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
+ ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER, RELIABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, null);
+ ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
+
+ QProfileDto childProfile = createChildProfile(parentProfile);
+ ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(SECURITY, org.sonar.api.issue.impact.Severity.BLOCKER, RELIABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null,
+ INHERITED);
+ ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "10");
+
+ DbSession session = db.getSession();
+ RuleActivation request = RuleActivation.createOverrideImpacts(rule.getUuid(),
+ Map.of(SECURITY, org.sonar.api.issue.impact.Severity.LOW, RELIABILITY, org.sonar.api.issue.impact.Severity.LOW), of("min", "15"));
+ RuleActivationContext context = new RuleActivationContext.Builder()
+ .setProfiles(asList(parentProfile, childProfile))
+ .setBaseProfile(RulesProfileDto.from(childProfile))
+ .setDate(NOW)
+ .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
+ .setRules(singletonList(rule))
+ .setRuleParams(singletonList(ruleParam))
+ .setActiveRules(asList(parentActiveRuleDto, childActiveRuleDto))
+ .setActiveRuleParams(asList(parentActiveRuleParam, childActiveRuleParam))
+ .build();
+
+ List<ActiveRuleChange> result = underTest.activate(session, request, context);
+
+ assertThat(result).hasSize(1);
+ ActiveRuleChange activeRuleResult = result.get(0);
+ assertThat(activeRuleResult.getSeverity()).isEqualTo(Severity.BLOCKER);
+ assertThat(activeRuleResult.getImpactSeverities())
+ .containsExactlyInAnyOrderEntriesOf(Map.of(SECURITY, org.sonar.api.issue.impact.Severity.LOW, RELIABILITY, org.sonar.api.issue.impact.Severity.LOW));
+ assertThat(activeRuleResult.getInheritance()).isEqualTo(OVERRIDES);
+ }
+
+ private static ImpactDto newImpactDto(SoftwareQuality security, org.sonar.api.issue.impact.Severity severity) {
+ return new ImpactDto().setSoftwareQuality(security).setSeverity(severity);
+ }
+
+ @Test
+ void activate_whenImpactSeveritiesIsOverridden_shouldMapToRuleSeverity() {
+ RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER)
+ .replaceAllDefaultImpacts(List.of(newImpactDto(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER))));
+ RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setName("min").setDefaultValue("10"));
+
+ QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
+ ActiveRuleDto parentActiveRuleDto = activateRuleInDb(RulesProfileDto.from(parentProfile), rule,
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, null);
+ ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
+
+ QProfileDto childProfile = createChildProfile(parentProfile);
+ ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule,
+ RulePriority.valueOf(Severity.BLOCKER), Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.BLOCKER), null, INHERITED);
+ ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "10");
+
+ DbSession session = db.getSession();
+ RuleActivation request = RuleActivation.createOverrideImpacts(rule.getUuid(), Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW), of("min", "15"));
+ RuleActivationContext context = new RuleActivationContext.Builder()
+ .setProfiles(asList(parentProfile, childProfile))
+ .setBaseProfile(RulesProfileDto.from(childProfile))
+ .setDate(NOW)
+ .setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList()))
+ .setRules(singletonList(rule))
+ .setRuleParams(singletonList(ruleParam))
+ .setActiveRules(asList(parentActiveRuleDto, childActiveRuleDto))
+ .setActiveRuleParams(asList(parentActiveRuleParam, childActiveRuleParam))
+ .build();
+
+ List<ActiveRuleChange> result = underTest.activate(session, request, context);
+
+ assertThat(result).hasSize(1);
+ ActiveRuleChange activeRuleResult = result.get(0);
+ assertThat(activeRuleResult.getSeverity()).isEqualTo(Severity.MINOR);
+ assertThat(activeRuleResult.getImpactSeverities()).containsExactlyEntriesOf(Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW));
+ assertThat(activeRuleResult.getInheritance()).isEqualTo(OVERRIDES);
+ }
+
@Test
void set_severity_and_param_for_child_rule_when_activating() {
RuleDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
assertThat(result).hasSize(1);
assertThat(result.get(0).getParameters()).containsEntry("min", "10");
assertThat(result.get(0).getSeverity()).isEqualTo(Severity.BLOCKER);
+ assertThat(result.get(0).getImpactSeverities()).containsExactlyEntriesOf(Map.of(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH));
assertThat(result.get(0).getInheritance()).isNull();
}
.setActiveRuleParams(emptyList())
.build();
-
assertThrows("xoo rule repo:rule cannot be activated on xoo2 profile qp", BadRequestException.class,
() -> underTest.activate(session, resetRequest, context));
}
-
- private ActiveRuleDto activateRuleInDb(RulesProfileDto ruleProfile, RuleDto rule, RulePriority severity,
+ private ActiveRuleDto activateRuleInDb(RulesProfileDto ruleProfile, RuleDto rule, RulePriority severity, Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts,
@Nullable Boolean prioritizedRule, @Nullable ActiveRuleInheritance inheritance) {
ActiveRuleDto dto = new ActiveRuleDto()
.setKey(ActiveRuleKey.of(ruleProfile, RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey())))
.setProfileUuid(ruleProfile.getUuid())
.setSeverity(severity.name())
+ .setImpacts(impacts)
.setPrioritizedRule(TRUE.equals(prioritizedRule))
.setRuleUuid(rule.getUuid())
.setInheritance(inheritance != null ? inheritance.name() : null)
private QProfileDto createChildProfile(QProfileDto parent) {
return db.qualityProfiles().insert(p -> p
- .setLanguage(parent.getLanguage())
- .setParentKee(parent.getKee())
- .setName("Child of " + parent.getName()))
+ .setLanguage(parent.getLanguage())
+ .setParentKee(parent.getKee())
+ .setName("Child of " + parent.getName()))
.setIsBuiltIn(false);
}
}
import java.util.EnumMap;
import java.util.Map;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.RuleType;
private QProfileImpactSeverityMapper() {
}
- public static Map<SoftwareQuality, Severity> mapImpactSeverities(String severity, Map<SoftwareQuality, Severity> ruleImpacts, RuleType ruleType) {
- if (ruleType == RuleType.SECURITY_HOTSPOT) {
- return Map.of();
+ public static Map<SoftwareQuality, Severity> mapImpactSeverities(@Nullable String severity, Map<SoftwareQuality, Severity> ruleImpacts, RuleType ruleType) {
+ Map<SoftwareQuality, Severity> result = ruleImpacts.isEmpty() ? Map.of() : new EnumMap<>(ruleImpacts);
+ if (severity == null || ruleImpacts.isEmpty()) {
+ return result;
}
SoftwareQuality softwareQuality = ImpactMapper.convertToSoftwareQuality(ruleType);
- Map<SoftwareQuality, Severity> result = new EnumMap<>(ruleImpacts);
if (ruleImpacts.containsKey(softwareQuality)) {
result.put(softwareQuality, SEVERITY_MAPPING.inverse().get(severity));
} else if (ruleImpacts.size() == 1) {
}
@CheckForNull
- public static String mapSeverity(Map<SoftwareQuality, Severity> impacts, RuleType ruleType, String ruleSeverity) {
+ public static String mapSeverity(Map<SoftwareQuality, Severity> impacts, RuleType ruleType, @Nullable String ruleSeverity) {
SoftwareQuality softwareQuality = ImpactMapper.convertToSoftwareQuality(ruleType);
if (impacts.containsKey(softwareQuality)) {
return SEVERITY_MAPPING.get(impacts.get(softwareQuality));
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.sonar.api.config.Configuration;
+import org.sonar.api.issue.impact.Severity;
+import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.ServerSide;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.qualityprofile.ActiveRuleChange;
import org.sonar.server.qualityprofile.ActiveRuleInheritance;
+import org.sonar.server.qualityprofile.QProfileImpactSeverityMapper;
import org.sonar.server.qualityprofile.RuleActivation;
import org.sonar.server.qualityprofile.builtin.RuleActivationContext.ActiveRuleWrapper;
import org.sonar.server.qualityprofile.builtin.RuleActivationContext.RuleWrapper;
if (context.isCascading() && activeRule.get().getInheritance() == null) {
// The rule is being propagated, but it was activated directly on this profile before
change.setSeverity(activeRule.get().getSeverityString());
+ change.setImpactSeverities(activeRule.get().getImpacts());
for (ActiveRuleParamDto activeParam : activeRule.getParams()) {
change.setParameter(activeParam.getKey(), activeParam.getValue());
}
parentActiveRule != null ? parentActiveRule.get().getSeverityString() : null,
rule.get().getSeverityString());
change.setSeverity(severity);
+
+ Map<SoftwareQuality, Severity> impactsSeverities = parentActiveRule != null ? parentActiveRule.get().getImpacts()
+ : rule.get().getDefaultImpactsMap();
+ change.setImpactSeverities(impactsSeverities);
+
change.setPrioritizedRule(parentActiveRule != null && parentActiveRule.get().isPrioritizedRule());
for (RuleParamDto ruleParamDto : rule.getParams()) {
private void applySeverityAndPrioritizedRuleAndParamsWhenBuiltInProfile(RuleActivation request, RuleActivationContext context,
ActiveRuleChange change, RuleWrapper rule) {
// for builtin quality profiles, the severity from profile, when null use the default severity of the rule
- String severity = firstNonNull(request.getSeverity(), rule.get().getSeverityString());
- change.setSeverity(severity);
+ SeverityConfiguration severityConfiguration = determineSeverityConfiguration(request, rule.get(), null, null);
+ change.setSeverity(severityConfiguration.severity());
+ change.setImpactSeverities(severityConfiguration.impacts());
boolean prioritizedRule = TRUE.equals(request.isPrioritizedRule());
change.setPrioritizedRule(prioritizedRule);
}
}
+ private static SeverityConfiguration determineSeverityConfiguration(RuleActivation request, RuleDto ruleDto, @Nullable ActiveRuleWrapper activeRule,
+ @Nullable ActiveRuleWrapper parentActiveRule) {
+ String requestSeverity = request.getSeverity();
+ if (requestSeverity != null) {
+ // When standard severity is requested to be overridden, we translate it to the impact to override
+ return new SeverityConfiguration(requestSeverity,
+ QProfileImpactSeverityMapper.mapImpactSeverities(requestSeverity, ruleDto.getDefaultImpactsMap(), ruleDto.getEnumType()));
+ } else if (!request.getImpactSeverities().isEmpty()) {
+ // If an impact is request to be overridden, we translat it to the standard severity
+ return new SeverityConfiguration(
+ QProfileImpactSeverityMapper.mapSeverity(request.getImpactSeverities(), ruleDto.getEnumType(), ruleDto.getSeverityString()),
+ request.getImpactSeverities());
+ } else if (activeRule != null && activeRule.get().doesOverride()) {
+ return new SeverityConfiguration(activeRule.get().getSeverityString(), activeRule.get().getImpacts());
+ } else if (parentActiveRule != null) {
+ return new SeverityConfiguration(parentActiveRule.get().getSeverityString(), parentActiveRule.get().getImpacts());
+ } else if (activeRule != null) {
+ return new SeverityConfiguration(activeRule.get().getSeverityString(), activeRule.get().getImpacts());
+ } else {
+ return new SeverityConfiguration(ruleDto.getSeverityString(), ruleDto.getDefaultImpactsMap());
+ }
+ }
+
/**
* 1. apply requested severity and param
* 2. if rule activated and overridden - apply user value
private void applySeverityAndPrioritizedRuleAndParamsWhenNonBuiltInProfile(RuleActivation request, RuleActivationContext context,
ActiveRuleChange change,
RuleWrapper rule, @Nullable ActiveRuleWrapper activeRule, @Nullable ActiveRuleWrapper parentActiveRule) {
- String severity = getSeverityForNonBuiltInProfile(request, rule, activeRule, parentActiveRule);
+ SeverityConfiguration severityConfiguration = getSeverityConfigurationForNonBuiltInProfile(request, rule, activeRule, parentActiveRule);
boolean prioritizedRule = getPrioritizedRuleForNonBuiltInProfile(request, activeRule, parentActiveRule);
- change.setSeverity(severity);
+ change.setSeverity(severityConfiguration.severity());
+ change.setImpactSeverities(severityConfiguration.impacts());
change.setPrioritizedRule(prioritizedRule);
for (RuleParamDto ruleParamDto : rule.getParams()) {
}
}
- private static String getSeverityForNonBuiltInProfile(RuleActivation request, RuleWrapper rule, @Nullable ActiveRuleWrapper activeRule,
+ private record SeverityConfiguration(@Nullable String severity, Map<SoftwareQuality, Severity> impacts) {
+ }
+
+ private static SeverityConfiguration getSeverityConfigurationForNonBuiltInProfile(RuleActivation request, RuleWrapper rule, @Nullable ActiveRuleWrapper activeRule,
@Nullable ActiveRuleWrapper parentActiveRule) {
- String severity;
- if (activeRule != null) {
- ActiveRuleDto activeRuleDto = activeRule.get();
- // load severity from request, else keep existing one (if overridden), else from parent if rule inherited, else from default
- severity = firstNonNull(
- request.getSeverity(),
- activeRuleDto.doesOverride() ? activeRuleDto.getSeverityString() : null,
- parentActiveRule != null ? parentActiveRule.get().getSeverityString() : activeRuleDto.getSeverityString(),
- rule.get().getSeverityString());
- } else {
- // load severity from request, else from parent, else from default
- severity = firstNonNull(
- request.getSeverity(),
- parentActiveRule != null ? parentActiveRule.get().getSeverityString() : null,
- rule.get().getSeverityString());
- }
- return severity;
+ return determineSeverityConfiguration(request, rule.get(), activeRule, parentActiveRule);
}
private static boolean getPrioritizedRuleForNonBuiltInProfile(RuleActivation request, @Nullable ActiveRuleWrapper activeRule,
if (severity != null) {
activeRule.setSeverity(severity);
}
+ activeRule.setImpacts(change.getImpactSeverities());
activeRule.setPrioritizedRule(TRUE.equals(change.isPrioritizedRule()));
ActiveRuleInheritance inheritance = change.getInheritance();
if (inheritance != null) {
if (severity != null) {
ruleDto.setSeverity(severity);
}
+ ruleDto.setImpacts(change.getImpactSeverities());
Boolean prioritizedRule = change.isPrioritizedRule();
if (prioritizedRule != null) {
ruleDto.setPrioritizedRule(prioritizedRule);
if (severity != null && !severity.equals(activeRule.get().getSeverityString())) {
return false;
}
+ Map<SoftwareQuality, Severity> impactSeverities = change.getImpactSeverities();
+ if (!impactSeverities.equals(activeRule.get().getImpacts())) {
+ return false;
+ }
Boolean prioritizedRule = change.isPrioritizedRule();
if (prioritizedRule != null && prioritizedRule != activeRule.get().isPrioritizedRule()) {
return false;
if (!StringUtils.equals(change.getSeverity(), parentActiveRule.get().getSeverityString())) {
return false;
}
+ if (!change.getImpactSeverities().equals(parentActiveRule.get().getImpacts())) {
+ return false;
+ }
if (change.isPrioritizedRule() != null && !Objects.equals(change.isPrioritizedRule(), parentActiveRule.get().isPrioritizedRule())) {
return false;
}
@Test
void mapImpactSeverities_whenSecurityHotspot_shouldReturnEmptyMap() {
Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.MAJOR,
- Map.of(SoftwareQuality.MAINTAINABILITY,
- org.sonar.api.issue.impact.Severity.HIGH),
+ Map.of(),
RuleType.SECURITY_HOTSPOT);
assertThat(result).isEmpty();
}
+ @Test
+ void mapImpactSeverities_whenSeverityIsNull_shouldReturnRuleImpacts() {
+ Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH);
+ Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(null,
+ impacts,
+ RuleType.SECURITY_HOTSPOT);
+
+ assertThat(result).isEqualTo(impacts);
+ }
+
@Test
void mapImpactSeverities_whenOneImpact_shouldReturnOverriddenImpact() {
Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> result = QProfileImpactSeverityMapper.mapImpactSeverities(Severity.INFO,