Search in sources :

Example 66 with QProfileDto

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

the class QProfileResetImplTest method reset.

@Test
public void reset() {
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE));
    RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
    qProfileRules.activateAndCommit(db.getSession(), profile, singleton(RuleActivation.create(existingRule.getUuid())));
    RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
    BulkChangeResult result = underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(newRule.getUuid())));
    assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile)).extracting(OrgActiveRuleDto::getRuleKey).containsExactlyInAnyOrder(newRule.getKey());
    // Only activated rules are returned in the result
    assertThat(result.getChanges()).extracting(ActiveRuleChange::getKey, ActiveRuleChange::getType).containsExactlyInAnyOrder(tuple(ActiveRuleKey.of(profile, newRule.getKey()), ACTIVATED));
    verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 67 with QProfileDto

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

the class QProfileResetImplTest method fail_when_profile_is_not_persisted.

@Test
public void fail_when_profile_is_not_persisted() {
    QProfileDto profile = QualityProfileTesting.newQualityProfileDto().setRulesProfileUuid(null).setLanguage(LANGUAGE);
    RuleDefinitionDto defaultRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
    assertThatThrownBy(() -> {
        underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getUuid())));
    }).isInstanceOf(NullPointerException.class).hasMessage("Quality profile must be persisted");
    verifyNoInteractions(qualityProfileChangeEventService);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 68 with QProfileDto

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

the class QProfileResetImplTest method fail_when_profile_is_built_in.

@Test
public void fail_when_profile_is_built_in() {
    QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE).setIsBuiltIn(true));
    RuleDefinitionDto defaultRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
    assertThatThrownBy(() -> {
        underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getUuid())));
    }).isInstanceOf(IllegalArgumentException.class).hasMessage(String.format("Operation forbidden for built-in Quality Profile '%s'", profile.getKee()));
    verifyNoInteractions(qualityProfileChangeEventService);
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 69 with QProfileDto

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

the class QProfileRulesImplTest method activate_one_rule.

@Test
public void activate_one_rule() {
    QProfileDto qProfile = db.qualityProfiles().insert();
    RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage()));
    RuleActivation ruleActivation = RuleActivation.create(rule.getUuid(), Severity.CRITICAL, Collections.emptyMap());
    qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation));
    assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), qProfile)).extracting(ActiveRuleDto::getRuleKey, ActiveRuleDto::getSeverityString).containsExactlyInAnyOrder(tuple(rule.getKey(), Severity.CRITICAL));
    verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(qProfile.getLanguage()));
}
Also used : QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 70 with QProfileDto

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

the class RegisterQualityProfilesNotificationTest method notification_does_not_include_inherited_profiled_when_rule_is_changed.

@Test
public void notification_does_not_include_inherited_profiled_when_rule_is_changed() {
    String language = newLanguageKey();
    RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
    QProfileDto builtInProfile = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
    db.qualityProfiles().activateRule(builtInProfile, rule, ar -> ar.setSeverity(Severity.MINOR));
    QProfileDto childProfile = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInProfile.getKee()));
    db.qualityProfiles().activateRule(childProfile, rule, ar -> ar.setInheritance(ActiveRuleDto.INHERITED).setSeverity(Severity.MINOR));
    addPluginProfile(builtInProfile, rule);
    builtInQProfileRepositoryRule.initialize();
    db.commit();
    underTest.start();
    ArgumentCaptor<Multimap> captor = ArgumentCaptor.forClass(Multimap.class);
    verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong());
    Multimap<QProfileName, ActiveRuleChange> updatedProfiles = captor.getValue();
    assertThat(updatedProfiles.keySet()).extracting(QProfileName::getName, QProfileName::getLanguage).containsExactlyInAnyOrder(tuple(builtInProfile.getName(), builtInProfile.getLanguage()));
    assertThat(updatedProfiles.values()).extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType).containsExactlyInAnyOrder(tuple(rule.getUuid(), UPDATED));
}
Also used : QProfileName(org.sonar.server.qualityprofile.builtin.QProfileName) Arrays(java.util.Arrays) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) ActiveRuleIndexer(org.sonar.server.qualityprofile.index.ActiveRuleIndexer) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) SecureRandom(java.security.SecureRandom) UPDATED(org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED) Collections.singleton(java.util.Collections.singleton) BuiltInQualityProfilesDefinition(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition) Map(java.util.Map) UuidFactoryFast(org.sonar.core.util.UuidFactoryFast) RandomUtils.nextLong(org.apache.commons.lang.math.RandomUtils.nextLong) RulePriority(org.sonar.api.rules.RulePriority) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) BuiltInQProfileInsertImpl(org.sonar.server.qualityprofile.builtin.BuiltInQProfileInsertImpl) BuiltInQualityProfilesUpdateListener(org.sonar.server.qualityprofile.builtin.BuiltInQualityProfilesUpdateListener) BuiltInQProfileUpdateImpl(org.sonar.server.qualityprofile.builtin.BuiltInQProfileUpdateImpl) QProfileName(org.sonar.server.qualityprofile.builtin.QProfileName) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DbClient(org.sonar.db.DbClient) List(java.util.List) BuiltInQProfileRepositoryRule(org.sonar.server.qualityprofile.builtin.BuiltInQProfileRepositoryRule) MAJOR(org.sonar.api.rules.RulePriority.MAJOR) RuleKey(org.sonar.api.rule.RuleKey) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) ACTIVATED(org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ServerRuleFinder(org.sonar.server.rule.ServerRuleFinder) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RuleActivator(org.sonar.server.qualityprofile.builtin.RuleActivator) DEACTIVATED(org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED) Multimap(com.google.common.collect.Multimap) TypeValidations(org.sonar.server.util.TypeValidations) BuiltInQProfileInsert(org.sonar.server.qualityprofile.builtin.BuiltInQProfileInsert) BuiltInQProfileUpdate(org.sonar.server.qualityprofile.builtin.BuiltInQProfileUpdate) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) ArgumentCaptor(org.mockito.ArgumentCaptor) Severity(org.sonar.api.rule.Severity) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) BuiltInQProfile(org.sonar.server.qualityprofile.builtin.BuiltInQProfile) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) QualityProfileChangeEventService(org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService) UserSessionRule(org.sonar.server.tester.UserSessionRule) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) Assertions.tuple(org.assertj.core.api.Assertions.tuple) BuiltInActiveRule(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInActiveRule) DefaultRuleFinder(org.sonar.server.rule.DefaultRuleFinder) LanguageTesting.newLanguage(org.sonar.server.language.LanguageTesting.newLanguage) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RuleIndex(org.sonar.server.rule.index.RuleIndex) QualityProfileTesting.newRuleProfileDto(org.sonar.db.qualityprofile.QualityProfileTesting.newRuleProfileDto) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) NewBuiltInQualityProfile(org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile) Multimap(com.google.common.collect.Multimap) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Aggregations

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