Search in sources :

Example 1 with Result

use of org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result in project sonarqube by SonarSource.

the class RuleActivatorTest method reset_overridden_active_rule.

@Test
public void reset_overridden_active_rule() {
    RuleDefinitionDto 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.BLOCKER), null);
    ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
    QProfileDto childProfile = createChildProfile(parentProfile);
    ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule, RulePriority.valueOf(Severity.MINOR), OVERRIDES);
    ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "15");
    DbSession session = db.getSession();
    RuleActivation resetRequest = RuleActivation.createReset(rule.getUuid());
    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, resetRequest, context);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getParameters()).containsEntry("min", "10");
    assertThat(result.get(0).getSeverity()).isEqualTo(Severity.BLOCKER);
    assertThat(result.get(0).getInheritance()).isEqualTo(ActiveRuleInheritance.INHERITED);
}
Also used : INHERITED(org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertThrows(org.junit.Assert.assertThrows) TypeValidations(org.sonar.server.util.TypeValidations) DbSession(org.sonar.db.DbSession) TestSystem2(org.sonar.api.impl.utils.TestSystem2) BadRequestException(org.sonar.server.exceptions.BadRequestException) Collections.singletonList(java.util.Collections.singletonList) Severity(org.sonar.api.rule.Severity) Arrays.asList(java.util.Arrays.asList) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) IntegerTypeValidation(org.sonar.server.util.IntegerTypeValidation) Nullable(javax.annotation.Nullable) RulePriority(org.sonar.api.rules.RulePriority) QualityProfileChangeEventService(org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) Collections.emptyList(java.util.Collections.emptyList) Test(org.junit.Test) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) List(java.util.List) Rule(org.junit.Rule) RuleParamDto(org.sonar.db.rule.RuleParamDto) OVERRIDES(org.sonar.server.qualityprofile.ActiveRuleInheritance.OVERRIDES) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) StringTypeValidation(org.sonar.server.util.StringTypeValidation) ActiveRuleInheritance(org.sonar.server.qualityprofile.ActiveRuleInheritance) Mockito.mock(org.mockito.Mockito.mock) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) DbSession(org.sonar.db.DbSession) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 2 with Result

use of org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result in project sonarqube by SonarSource.

the class RuleActivatorTest method request_new_severity_and_param_for_child_rule.

@Test
public void request_new_severity_and_param_for_child_rule() {
    RuleDefinitionDto 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.BLOCKER), null);
    ActiveRuleParamDto parentActiveRuleParam = activateRuleParamInDb(parentActiveRuleDto, ruleParam, "10");
    QProfileDto childProfile = createChildProfile(parentProfile);
    ActiveRuleDto childActiveRuleDto = activateRuleInDb(RulesProfileDto.from(childProfile), rule, RulePriority.valueOf(Severity.BLOCKER), INHERITED);
    ActiveRuleParamDto childActiveRuleParam = activateRuleParamInDb(childActiveRuleDto, ruleParam, "10");
    DbSession session = db.getSession();
    RuleActivation resetRequest = RuleActivation.create(rule.getUuid(), Severity.MINOR, ImmutableMap.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, resetRequest, context);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getParameters()).containsEntry("min", "15");
    assertThat(result.get(0).getSeverity()).isEqualTo(Severity.MINOR);
    assertThat(result.get(0).getInheritance()).isEqualTo(OVERRIDES);
}
Also used : INHERITED(org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertThrows(org.junit.Assert.assertThrows) TypeValidations(org.sonar.server.util.TypeValidations) DbSession(org.sonar.db.DbSession) TestSystem2(org.sonar.api.impl.utils.TestSystem2) BadRequestException(org.sonar.server.exceptions.BadRequestException) Collections.singletonList(java.util.Collections.singletonList) Severity(org.sonar.api.rule.Severity) Arrays.asList(java.util.Arrays.asList) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) IntegerTypeValidation(org.sonar.server.util.IntegerTypeValidation) Nullable(javax.annotation.Nullable) RulePriority(org.sonar.api.rules.RulePriority) QualityProfileChangeEventService(org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) Collections.emptyList(java.util.Collections.emptyList) Test(org.junit.Test) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) List(java.util.List) Rule(org.junit.Rule) RuleParamDto(org.sonar.db.rule.RuleParamDto) OVERRIDES(org.sonar.server.qualityprofile.ActiveRuleInheritance.OVERRIDES) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) StringTypeValidation(org.sonar.server.util.StringTypeValidation) ActiveRuleInheritance(org.sonar.server.qualityprofile.ActiveRuleInheritance) Mockito.mock(org.mockito.Mockito.mock) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) DbSession(org.sonar.db.DbSession) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 3 with Result

use of org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result in project sonarqube by SonarSource.

the class RuleActivatorTest method set_severity_and_param_for_child_rule_when_activating.

@Test
public void set_severity_and_param_for_child_rule_when_activating() {
    RuleDefinitionDto 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));
    QProfileDto childProfile = createChildProfile(parentProfile);
    DbSession session = db.getSession();
    RuleActivation resetRequest = RuleActivation.create(rule.getUuid());
    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(emptyList()).setActiveRuleParams(emptyList()).build();
    List<ActiveRuleChange> result = underTest.activate(session, resetRequest, context);
    assertThat(result).hasSize(1);
    assertThat(result.get(0).getParameters()).containsEntry("min", "10");
    assertThat(result.get(0).getSeverity()).isEqualTo(Severity.BLOCKER);
    assertThat(result.get(0).getInheritance()).isNull();
}
Also used : INHERITED(org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertThrows(org.junit.Assert.assertThrows) TypeValidations(org.sonar.server.util.TypeValidations) DbSession(org.sonar.db.DbSession) TestSystem2(org.sonar.api.impl.utils.TestSystem2) BadRequestException(org.sonar.server.exceptions.BadRequestException) Collections.singletonList(java.util.Collections.singletonList) Severity(org.sonar.api.rule.Severity) Arrays.asList(java.util.Arrays.asList) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) IntegerTypeValidation(org.sonar.server.util.IntegerTypeValidation) Nullable(javax.annotation.Nullable) RulePriority(org.sonar.api.rules.RulePriority) QualityProfileChangeEventService(org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) Collections.emptyList(java.util.Collections.emptyList) Test(org.junit.Test) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) List(java.util.List) Rule(org.junit.Rule) RuleParamDto(org.sonar.db.rule.RuleParamDto) OVERRIDES(org.sonar.server.qualityprofile.ActiveRuleInheritance.OVERRIDES) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) StringTypeValidation(org.sonar.server.util.StringTypeValidation) ActiveRuleInheritance(org.sonar.server.qualityprofile.ActiveRuleInheritance) Mockito.mock(org.mockito.Mockito.mock) DbSession(org.sonar.db.DbSession) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) Test(org.junit.Test)

Example 4 with Result

use of org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result in project sonarqube by SonarSource.

the class RuleActivatorTest method fail_if_rule_language_doesnt_match_qp.

@Test
public void fail_if_rule_language_doesnt_match_qp() {
    RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo").setRepositoryKey("repo").setRuleKey("rule").setSeverity(Severity.BLOCKER));
    QProfileDto qp = db.qualityProfiles().insert(p -> p.setLanguage("xoo2").setKee("qp").setIsBuiltIn(true));
    DbSession session = db.getSession();
    RuleActivation resetRequest = RuleActivation.create(rule.getUuid());
    RuleActivationContext context = new RuleActivationContext.Builder().setProfiles(singletonList(qp)).setBaseProfile(RulesProfileDto.from(qp)).setDate(NOW).setDescendantProfilesSupplier((profiles, ruleUuids) -> new Result(emptyList(), emptyList(), emptyList())).setRules(singletonList(rule)).setRuleParams(emptyList()).setActiveRules(emptyList()).setActiveRuleParams(emptyList()).build();
    assertThrows("xoo rule repo:rule cannot be activated on xoo2 profile qp", BadRequestException.class, () -> underTest.activate(session, resetRequest, context));
}
Also used : INHERITED(org.sonar.server.qualityprofile.ActiveRuleInheritance.INHERITED) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assert.assertThrows(org.junit.Assert.assertThrows) TypeValidations(org.sonar.server.util.TypeValidations) DbSession(org.sonar.db.DbSession) TestSystem2(org.sonar.api.impl.utils.TestSystem2) BadRequestException(org.sonar.server.exceptions.BadRequestException) Collections.singletonList(java.util.Collections.singletonList) Severity(org.sonar.api.rule.Severity) Arrays.asList(java.util.Arrays.asList) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) IntegerTypeValidation(org.sonar.server.util.IntegerTypeValidation) Nullable(javax.annotation.Nullable) RulePriority(org.sonar.api.rules.RulePriority) QualityProfileChangeEventService(org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService) UserSessionRule(org.sonar.server.tester.UserSessionRule) DbTester(org.sonar.db.DbTester) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) ImmutableMap(com.google.common.collect.ImmutableMap) System2(org.sonar.api.utils.System2) Collections.emptyList(java.util.Collections.emptyList) Test(org.junit.Test) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) List(java.util.List) Rule(org.junit.Rule) RuleParamDto(org.sonar.db.rule.RuleParamDto) OVERRIDES(org.sonar.server.qualityprofile.ActiveRuleInheritance.OVERRIDES) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) StringTypeValidation(org.sonar.server.util.StringTypeValidation) ActiveRuleInheritance(org.sonar.server.qualityprofile.ActiveRuleInheritance) Mockito.mock(org.mockito.Mockito.mock) DbSession(org.sonar.db.DbSession) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleActivation(org.sonar.server.qualityprofile.RuleActivation) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Result(org.sonar.server.qualityprofile.builtin.DescendantProfilesSupplier.Result) Test(org.junit.Test)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)4 Arrays.asList (java.util.Arrays.asList)4 Collections.emptyList (java.util.Collections.emptyList)4 Collections.singletonList (java.util.Collections.singletonList)4 List (java.util.List)4 Nullable (javax.annotation.Nullable)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assert.assertThrows (org.junit.Assert.assertThrows)4 Rule (org.junit.Rule)4 Test (org.junit.Test)4 Mockito.mock (org.mockito.Mockito.mock)4 TestSystem2 (org.sonar.api.impl.utils.TestSystem2)4 RuleKey (org.sonar.api.rule.RuleKey)4 Severity (org.sonar.api.rule.Severity)4 RulePriority (org.sonar.api.rules.RulePriority)4 System2 (org.sonar.api.utils.System2)4 DbSession (org.sonar.db.DbSession)4 DbTester (org.sonar.db.DbTester)4 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)4 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)4