Search in sources :

Example 6 with RulesProfileDto

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

the class QProfileFactoryImplTest method createBuiltInProfile.

private RulesProfileDto createBuiltInProfile() {
    RulesProfileDto rulesProfileDto = new RulesProfileDto().setIsBuiltIn(true).setUuid(Uuids.createFast()).setLanguage("xoo").setName("Sonar way");
    db.getDbClient().qualityProfileDao().insert(dbSession, rulesProfileDto);
    ActiveRuleDto activeRuleDto = new ActiveRuleDto().setProfileUuid(rulesProfileDto.getUuid()).setRuleUuid(rule.getUuid()).setSeverity(Severity.BLOCKER);
    db.getDbClient().activeRuleDao().insert(dbSession, activeRuleDto);
    ActiveRuleParamDto activeRuleParam = new ActiveRuleParamDto().setRulesParameterUuid(ruleParam.getUuid()).setKey("foo").setValue("bar");
    db.getDbClient().activeRuleDao().insertParam(dbSession, activeRuleDto, activeRuleParam);
    db.getDbClient().qProfileChangeDao().insert(dbSession, new QProfileChangeDto().setChangeType(ActiveRuleChange.Type.ACTIVATED.name()).setRulesProfileUuid(rulesProfileDto.getUuid()));
    db.commit();
    return rulesProfileDto;
}
Also used : ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QProfileChangeDto(org.sonar.db.qualityprofile.QProfileChangeDto)

Example 7 with RulesProfileDto

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

the class RegisterQualityProfilesNotificationTest method send_a_single_notification_when_multiple_rules_are_activated.

@Test
public void send_a_single_notification_when_multiple_rules_are_activated() {
    String language = newLanguageKey();
    RuleDefinitionDto existingRule1 = db.rules().insert(r -> r.setLanguage(language));
    RuleDefinitionDto newRule1 = db.rules().insert(r -> r.setLanguage(language));
    RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
    activateRuleInDb(dbProfile1, existingRule1, MAJOR);
    addPluginProfile(dbProfile1, existingRule1, newRule1);
    RuleDefinitionDto existingRule2 = db.rules().insert(r -> r.setLanguage(language));
    RuleDefinitionDto newRule2 = db.rules().insert(r -> r.setLanguage(language));
    RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
    activateRuleInDb(dbProfile2, existingRule2, MAJOR);
    addPluginProfile(dbProfile2, existingRule2, newRule2);
    builtInQProfileRepositoryRule.initialize();
    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(dbProfile1.getName(), dbProfile1.getLanguage()), tuple(dbProfile2.getName(), dbProfile2.getLanguage()));
    assertThat(updatedProfiles.values()).extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType).containsExactlyInAnyOrder(tuple(newRule1.getUuid(), ACTIVATED), tuple(newRule2.getUuid(), ACTIVATED));
}
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) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Test(org.junit.Test)

Example 8 with RulesProfileDto

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

the class RegisterQualityProfilesNotificationTest method do_not_send_notification_when_profile_is_not_updated.

@Test
public void do_not_send_notification_when_profile_is_not_updated() {
    String language = newLanguageKey();
    RuleDefinitionDto dbRule = db.rules().insert(r -> r.setLanguage(language));
    RulesProfileDto dbProfile = insertBuiltInProfile(language);
    activateRuleInDb(dbProfile, dbRule, MAJOR);
    addPluginProfile(dbProfile, dbRule);
    builtInQProfileRepositoryRule.initialize();
    underTest.start();
    verifyNoInteractions(builtInQualityProfilesNotification);
}
Also used : RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Test(org.junit.Test)

Example 9 with RulesProfileDto

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

the class RegisterQualityProfilesNotificationTest method send_notification_when_a_rule_is_deactivated.

@Test
public void send_notification_when_a_rule_is_deactivated() {
    String language = newLanguageKey();
    RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
    RulesProfileDto dbProfile = insertBuiltInProfile(language);
    activateRuleInDb(dbProfile, existingRule, MAJOR);
    addPluginProfile(dbProfile);
    builtInQProfileRepositoryRule.initialize();
    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(dbProfile.getName(), dbProfile.getLanguage()));
    assertThat(updatedProfiles.values()).extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType).containsExactlyInAnyOrder(tuple(existingRule.getUuid(), DEACTIVATED));
}
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) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto) Test(org.junit.Test)

Example 10 with RulesProfileDto

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

the class BuiltInQProfileUpdateImplTest method assertThatProfileIsNotMarkedAsUpdated.

private void assertThatProfileIsNotMarkedAsUpdated(RulesProfileDto dto) {
    RulesProfileDto reloaded = db.getDbClient().qualityProfileDao().selectBuiltInRuleProfiles(db.getSession()).stream().filter(p -> p.getUuid().equals(dto.getUuid())).findFirst().get();
    assertThat(reloaded.getRulesUpdatedAt()).isNull();
}
Also used : RulesProfileDto(org.sonar.db.qualityprofile.RulesProfileDto)

Aggregations

RulesProfileDto (org.sonar.db.qualityprofile.RulesProfileDto)22 Test (org.junit.Test)10 QProfileDto (org.sonar.db.qualityprofile.QProfileDto)9 RuleDefinitionDto (org.sonar.db.rule.RuleDefinitionDto)7 Map (java.util.Map)6 BuiltInQProfile (org.sonar.server.qualityprofile.builtin.BuiltInQProfile)6 List (java.util.List)5 Multimap (com.google.common.collect.Multimap)4 RuleKey (org.sonar.api.rule.RuleKey)4 BuiltInQualityProfilesDefinition (org.sonar.api.server.profile.BuiltInQualityProfilesDefinition)4 System2 (org.sonar.api.utils.System2)4 MoreCollectors (org.sonar.core.util.stream.MoreCollectors)4 DbClient (org.sonar.db.DbClient)4 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)4 QProfileName (org.sonar.server.qualityprofile.builtin.QProfileName)4 Preconditions.checkState (com.google.common.base.Preconditions.checkState)3 SecureRandom (java.security.SecureRandom)3 Arrays (java.util.Arrays)3 Collections.singleton (java.util.Collections.singleton)3 Random (java.util.Random)3