Search in sources :

Example 86 with ArgumentCaptor

use of org.mockito.ArgumentCaptor 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 87 with ArgumentCaptor

use of org.mockito.ArgumentCaptor 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)

Example 88 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.

the class RegisterQualityProfilesNotificationTest method notification_does_not_include_inherited_profiles_when_rule_is_added.

@Test
public void notification_does_not_include_inherited_profiles_when_rule_is_added() {
    String language = newLanguageKey();
    RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
    QProfileDto builtInQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
    QProfileDto childQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
    addPluginProfile(builtInQProfileDto, newRule);
    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(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
    assertThat(updatedProfiles.values()).extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType).containsExactlyInAnyOrder(tuple(newRule.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) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Example 89 with ArgumentCaptor

use of org.mockito.ArgumentCaptor 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 90 with ArgumentCaptor

use of org.mockito.ArgumentCaptor in project sonarqube by SonarSource.

the class RegisterQualityProfilesNotificationTest method notification_does_not_include_inherited_profiles_when_rule_is_deactivated.

@Test
public void notification_does_not_include_inherited_profiles_when_rule_is_deactivated() {
    String language = newLanguageKey();
    RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(language).setSeverity(Severity.MINOR));
    QProfileDto builtInQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(true).setLanguage(language));
    db.qualityProfiles().activateRule(builtInQProfileDto, rule);
    QProfileDto childQProfileDto = insertProfile(orgQProfile -> orgQProfile.setIsBuiltIn(false).setLanguage(language).setParentKee(builtInQProfileDto.getKee()));
    qProfileRules.activateAndCommit(db.getSession(), childQProfileDto, singleton(RuleActivation.create(rule.getUuid())));
    db.commit();
    addPluginProfile(builtInQProfileDto);
    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(builtInQProfileDto.getName(), builtInQProfileDto.getLanguage()));
    assertThat(updatedProfiles.values()).extracting(value -> value.getActiveRule().getRuleUuid(), ActiveRuleChange::getType).containsExactlyInAnyOrder(tuple(rule.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) QProfileDto(org.sonar.db.qualityprofile.QProfileDto) RuleDefinitionDto(org.sonar.db.rule.RuleDefinitionDto) Test(org.junit.Test)

Aggregations

ArgumentCaptor (org.mockito.ArgumentCaptor)231 Mockito.verify (org.mockito.Mockito.verify)156 List (java.util.List)128 Test (org.junit.Test)116 Mockito.mock (org.mockito.Mockito.mock)113 Mockito.when (org.mockito.Mockito.when)100 Test (org.junit.jupiter.api.Test)86 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)85 Mockito (org.mockito.Mockito)85 Map (java.util.Map)76 BeforeEach (org.junit.jupiter.api.BeforeEach)72 Arrays (java.util.Arrays)69 Collectors (java.util.stream.Collectors)69 Collections (java.util.Collections)68 Before (org.junit.Before)65 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)64 Mockito.times (org.mockito.Mockito.times)64 ArrayList (java.util.ArrayList)60 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)60 Mockito.never (org.mockito.Mockito.never)58