Search in sources :

Example 76 with QProfileDto

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

the class BuiltInQProfileUpdateImplTest method propagate_deactivation_to_descendant_profiles.

@Test
public void propagate_deactivation_to_descendant_profiles() {
    RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo"));
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
    QProfileDto childProfile = createChildProfile(profile);
    QProfileDto grandChildProfile = createChildProfile(childProfile);
    BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
    NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage());
    newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey());
    newQp.done();
    // first run to activate the rule
    BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile(profile.getLanguage(), profile.getName()), rule);
    List<ActiveRuleChange> changes = underTest.update(db.getSession(), builtIn, RulesProfileDto.from(profile));
    assertThat(changes).hasSize(3).extracting(ActiveRuleChange::getType).containsOnly(ActiveRuleChange.Type.ACTIVATED);
    verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
    // second run to deactivate the rule
    context = new BuiltInQualityProfilesDefinition.Context();
    NewBuiltInQualityProfile updatedQp = context.createBuiltInQualityProfile(profile.getName(), profile.getLanguage());
    updatedQp.done();
    builtIn = builtInProfileRepository.create(context.profile(profile.getLanguage(), profile.getName()), rule);
    changes = underTest.update(db.getSession(), builtIn, RulesProfileDto.from(profile));
    assertThat(changes).hasSize(3).extracting(ActiveRuleChange::getType).containsOnly(ActiveRuleChange.Type.DEACTIVATED);
    verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
    assertThatRuleIsDeactivated(profile, rule);
    assertThatRuleIsDeactivated(childProfile, rule);
    assertThatRuleIsDeactivated(grandChildProfile, rule);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) BuiltInQualityProfilesDefinition(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition) NewBuiltInQualityProfile(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile) Test(org.junit.Test)

Example 77 with QProfileDto

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

the class BuiltInQProfileUpdateImplTest method propagate_rule_update_to_descendant_active_rule.

// SONAR-14559
@Test
public void propagate_rule_update_to_descendant_active_rule() {
    RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage("xoo").setSeverity(Severity.BLOCKER));
    QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(rule.getLanguage()).setIsBuiltIn(true));
    activateRuleInDb(RulesProfileDto.from(parentProfile), rule, RulePriority.valueOf(Severity.MINOR), null);
    QProfileDto childProfile = createChildProfile(parentProfile);
    activateRuleInDb(RulesProfileDto.from(childProfile), rule, RulePriority.valueOf(Severity.MINOR), INHERITED);
    BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
    NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile(parentProfile.getName(), parentProfile.getLanguage());
    newQp.activateRule(rule.getRepositoryKey(), rule.getRuleKey());
    newQp.done();
    BuiltInQProfile builtIn = builtInProfileRepository.create(context.profile(parentProfile.getLanguage(), parentProfile.getName()), rule);
    List<ActiveRuleChange> changes = underTest.update(db.getSession(), builtIn, RulesProfileDto.from(parentProfile));
    assertThat(changes).hasSize(2);
    List<ActiveRuleDto> parentActiveRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), RulesProfileDto.from(parentProfile));
    assertThatRuleIsUpdated(parentActiveRules, rule, RulePriority.BLOCKER, null);
    List<ActiveRuleDto> childActiveRules = db.getDbClient().activeRuleDao().selectByRuleProfile(db.getSession(), RulesProfileDto.from(childProfile));
    assertThatRuleIsUpdated(childActiveRules, rule, RulePriority.BLOCKER, INHERITED);
    verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), eq(changes), eq(persistedProfile.getLanguage()));
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ActiveRuleChange(org.sonar.server.qualityprofile.ActiveRuleChange) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) BuiltInQualityProfilesDefinition(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition) NewBuiltInQualityProfile(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile) OrgActiveRuleDto(org.sonar.db.qualityprofile.OrgActiveRuleDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Test(org.junit.Test)

Example 78 with QProfileDto

use of org.sonar.db.qualityprofile.QProfileDto 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 79 with QProfileDto

use of org.sonar.db.qualityprofile.QProfileDto 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 80 with QProfileDto

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

the class BuiltInQProfileInsertImplTest method flag_profile_as_default_if_declared_as_default_by_api.

@Test
public void flag_profile_as_default_if_declared_as_default_by_api() {
    BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
    NewBuiltInQualityProfile newQp = context.createBuiltInQualityProfile("the name", "xoo").setDefault(true);
    newQp.done();
    BuiltInQProfile builtIn = builtInQProfileRepository.create(context.profile("xoo", "the name"));
    call(builtIn);
    QProfileDto profile = verifyProfileInDb(builtIn);
    QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, "xoo");
    assertThat(defaultProfile.getKee()).isEqualTo(profile.getKee());
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) BuiltInQualityProfilesDefinition(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition) NewBuiltInQualityProfile(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile) Test(org.junit.Test)

Aggregations

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