From 4546ffeb44cb56bcf909e4506f255c354eba3e86 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 22 Jun 2017 17:46:22 +0200 Subject: [PATCH] SONAR-9443 Add setting to avoid quality profile change notification --- .../ComputeEngineContainerImplTest.java | 2 +- .../platformlevel/PlatformLevelStartup.java | 4 +- .../BuiltInQualityProfilesNotification.java | 2 +- ...QualityProfilesNotificationDispatcher.java | 2 +- ...InQualityProfilesNotificationTemplate.java | 2 +- ...BuiltInQualityProfilesUpdateListener.java} | 13 +++-- .../RegisterQualityProfiles.java | 6 +-- ...uiltInQualityProfilesNotificationTest.java | 2 +- ...tInQualityProfilesUpdateListenerTest.java} | 49 ++++++++++++++----- ...gisterQualityProfilesNotificationTest.java | 12 ++--- .../RegisterQualityProfilesTest.java | 2 +- .../core/config/CorePropertyDefinitions.java | 8 +++ .../config/CorePropertyDefinitionsTest.java | 2 +- ...uiltInQualityProfilesNotificationTest.java | 41 ++++++++++++++-- 14 files changed, 111 insertions(+), 36 deletions(-) rename server/sonar-server/src/main/java/org/sonar/server/qualityprofile/{BuiltInQualityProfilesNotificationSender.java => BuiltInQualityProfilesUpdateListener.java} (83%) rename server/sonar-server/src/test/java/org/sonar/server/qualityprofile/{BuiltInQualityProfilesNotificationSenderTest.java => BuiltInQualityProfilesUpdateListenerTest.java} (77%) diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java index 542d916364e..4fdec8c4058 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java @@ -137,7 +137,7 @@ public class ComputeEngineContainerImplTest { + 23 // level 1 + 46 // content of DaoModule + 3 // content of EsSearchModule - + 57 // content of CorePropertyDefinitions + + 58 // content of CorePropertyDefinitions ); assertThat( picoContainer.getComponentAdapters().stream() diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java index 7c6e83e469f..77ae38c9666 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java @@ -30,7 +30,7 @@ import org.sonar.server.qualityprofile.BuiltInQProfileInsertImpl; import org.sonar.server.qualityprofile.BuiltInQProfileLoader; import org.sonar.server.qualityprofile.BuiltInQProfileUpdateImpl; import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationDispatcher; -import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender; +import org.sonar.server.qualityprofile.BuiltInQualityProfilesUpdateListener; import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationTemplate; import org.sonar.server.qualityprofile.RegisterQualityProfiles; import org.sonar.server.rule.RegisterRules; @@ -66,7 +66,7 @@ public class PlatformLevelStartup extends PlatformLevel { NotificationModule.class, BuiltInQualityProfilesNotificationDispatcher.class, BuiltInQualityProfilesNotificationTemplate.class, - BuiltInQualityProfilesNotificationSender.class, + BuiltInQualityProfilesUpdateListener.class, BuiltInQProfileInsertImpl.class, BuiltInQProfileUpdateImpl.class, RegisterQualityProfiles.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotification.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotification.java index e128416726a..f90c540d3e8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotification.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotification.java @@ -31,7 +31,7 @@ import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.String.format; import static java.util.Objects.requireNonNull; -import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES; +import static org.sonar.server.qualityprofile.BuiltInQualityProfilesUpdateListener.BUILT_IN_QUALITY_PROFILES; public class BuiltInQualityProfilesNotification { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationDispatcher.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationDispatcher.java index 49a688d2df7..e25395ada63 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationDispatcher.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationDispatcher.java @@ -26,7 +26,7 @@ import org.sonar.db.DbSession; import org.sonar.server.notification.NotificationDispatcher; import org.sonar.server.notification.email.EmailNotificationChannel; -import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES; +import static org.sonar.server.qualityprofile.BuiltInQualityProfilesUpdateListener.BUILT_IN_QUALITY_PROFILES; public class BuiltInQualityProfilesNotificationDispatcher extends NotificationDispatcher { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java index 3f64a96c170..69025332316 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java @@ -32,7 +32,7 @@ import org.sonar.plugins.emailnotifications.api.EmailTemplate; import static org.sonar.api.utils.DateUtils.formatDate; import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile; import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.parse; -import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES; +import static org.sonar.server.qualityprofile.BuiltInQualityProfilesUpdateListener.BUILT_IN_QUALITY_PROFILES; public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationSender.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesUpdateListener.java similarity index 83% rename from server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationSender.java rename to server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesUpdateListener.java index 4e7dc8aa21f..7f355769357 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationSender.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesUpdateListener.java @@ -22,28 +22,35 @@ package org.sonar.server.qualityprofile; import com.google.common.collect.Multimap; import java.util.Collection; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; import org.sonar.server.notification.NotificationManager; import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile; +import static org.sonar.core.config.CorePropertyDefinitions.DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES; import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED; import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED; import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED; -public class BuiltInQualityProfilesNotificationSender { +public class BuiltInQualityProfilesUpdateListener { static final String BUILT_IN_QUALITY_PROFILES = "built-in-quality-profiles"; private final NotificationManager notificationManager; private final Languages languages; + private final Settings settings; - public BuiltInQualityProfilesNotificationSender(NotificationManager notificationManager, Languages languages) { + public BuiltInQualityProfilesUpdateListener(NotificationManager notificationManager, Languages languages, Settings settings) { this.notificationManager = notificationManager; this.languages = languages; + this.settings = settings; } - void send(Multimap changedProfiles, long startDate, long endDate) { + void onChange(Multimap changedProfiles, long startDate, long endDate) { + if (settings.getBoolean(DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES)) { + return; + } BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification(); changedProfiles.keySet().stream() .map(changedProfile -> { diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java index ecc84b98c43..2281d186e85 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/RegisterQualityProfiles.java @@ -49,12 +49,12 @@ public class RegisterQualityProfiles { private final DbClient dbClient; private final BuiltInQProfileInsert builtInQProfileInsert; private final BuiltInQProfileUpdate builtInQProfileUpdate; - private final BuiltInQualityProfilesNotificationSender builtInQualityProfilesNotification; + private final BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification; private final System2 system2; public RegisterQualityProfiles(BuiltInQProfileRepository builtInQProfileRepository, DbClient dbClient, BuiltInQProfileInsert builtInQProfileInsert, BuiltInQProfileUpdate builtInQProfileUpdate, - BuiltInQualityProfilesNotificationSender builtInQualityProfilesNotification, System2 system2) { + BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification, System2 system2) { this.builtInQProfileRepository = builtInQProfileRepository; this.dbClient = dbClient; this.builtInQProfileInsert = builtInQProfileInsert; @@ -90,7 +90,7 @@ public class RegisterQualityProfiles { }); if (!changedProfiles.isEmpty()) { long endDate = system2.now(); - builtInQualityProfilesNotification.send(changedProfiles, startDate, endDate); + builtInQualityProfilesNotification.onChange(changedProfiles, startDate, endDate); } } profiler.stopDebug(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTest.java index 01c62edbfa1..a64f88d07d1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTest.java @@ -30,7 +30,7 @@ import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profil import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; -import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES; +import static org.sonar.server.qualityprofile.BuiltInQualityProfilesUpdateListener.BUILT_IN_QUALITY_PROFILES; public class BuiltInQualityProfilesNotificationTest { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationSenderTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesUpdateListenerTest.java similarity index 77% rename from server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationSenderTest.java rename to server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesUpdateListenerTest.java index 78444c9124b..0a826f10481 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationSenderTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesUpdateListenerTest.java @@ -27,6 +27,8 @@ import java.util.stream.IntStream; import org.assertj.core.groups.Tuple; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.sonar.api.config.MapSettings; +import org.sonar.api.config.Settings; import org.sonar.api.notifications.Notification; import org.sonar.api.resources.Language; import org.sonar.api.resources.Languages; @@ -40,24 +42,28 @@ import static org.assertj.core.api.Java6Assertions.tuple; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.sonar.core.config.CorePropertyDefinitions.DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES; import static org.sonar.server.language.LanguageTesting.newLanguage; import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED; import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.DEACTIVATED; import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.UPDATED; -public class BuiltInQualityProfilesNotificationSenderTest { +public class BuiltInQualityProfilesUpdateListenerTest { private static final Random RANDOM = new Random(); private NotificationManager notificationManager = mock(NotificationManager.class); + private Settings settings = new MapSettings(); @Test public void add_profile_to_notification_for_added_rules() throws Exception { + enableNotificationInGlobalSettings(); Multimap profiles = ArrayListMultimap.create(); Languages languages = new Languages(); Tuple expectedTuple = addProfile(profiles, languages, ACTIVATED); - BuiltInQualityProfilesNotificationSender underTest = new BuiltInQualityProfilesNotificationSender(notificationManager, languages); - underTest.send(profiles, 0, 1); + BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings); + underTest.onChange(profiles, 0, 1); ArgumentCaptor notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); @@ -69,12 +75,13 @@ public class BuiltInQualityProfilesNotificationSenderTest { @Test public void add_profile_to_notification_for_updated_rules() throws Exception { + enableNotificationInGlobalSettings(); Multimap profiles = ArrayListMultimap.create(); Languages languages = new Languages(); Tuple expectedTuple = addProfile(profiles, languages, UPDATED); - BuiltInQualityProfilesNotificationSender underTest = new BuiltInQualityProfilesNotificationSender(notificationManager, languages); - underTest.send(profiles, 0, 1); + BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings); + underTest.onChange(profiles, 0, 1); ArgumentCaptor notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); @@ -86,12 +93,13 @@ public class BuiltInQualityProfilesNotificationSenderTest { @Test public void add_profile_to_notification_for_removed_rules() throws Exception { + enableNotificationInGlobalSettings(); Multimap profiles = ArrayListMultimap.create(); Languages languages = new Languages(); Tuple expectedTuple = addProfile(profiles, languages, DEACTIVATED); - BuiltInQualityProfilesNotificationSender underTest = new BuiltInQualityProfilesNotificationSender(notificationManager, languages); - underTest.send(profiles, 0, 1); + BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings); + underTest.onChange(profiles, 0, 1); ArgumentCaptor notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); @@ -103,13 +111,14 @@ public class BuiltInQualityProfilesNotificationSenderTest { @Test public void add_multiple_profiles_to_notification() throws Exception { + enableNotificationInGlobalSettings(); Multimap profiles = ArrayListMultimap.create(); Languages languages = new Languages(); Tuple expectedTuple1 = addProfile(profiles, languages, ACTIVATED); Tuple expectedTuple2 = addProfile(profiles, languages, ACTIVATED); - BuiltInQualityProfilesNotificationSender underTest = new BuiltInQualityProfilesNotificationSender(notificationManager, languages); - underTest.send(profiles, 0, 1); + BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings); + underTest.onChange(profiles, 0, 1); ArgumentCaptor notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); @@ -121,14 +130,15 @@ public class BuiltInQualityProfilesNotificationSenderTest { @Test public void add_start_and_end_dates_to_notification() throws Exception { + enableNotificationInGlobalSettings(); Multimap profiles = ArrayListMultimap.create(); Languages languages = new Languages(); addProfile(profiles, languages, ACTIVATED); long startDate = RANDOM.nextInt(5000); long endDate = startDate + RANDOM.nextInt(5000); - BuiltInQualityProfilesNotificationSender underTest = new BuiltInQualityProfilesNotificationSender(notificationManager, languages); - underTest.send(profiles, startDate, endDate); + BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings); + underTest.onChange(profiles, startDate, endDate); ArgumentCaptor notificationArgumentCaptor = ArgumentCaptor.forClass(Notification.class); verify(notificationManager).scheduleForSending(notificationArgumentCaptor.capture()); @@ -138,6 +148,19 @@ public class BuiltInQualityProfilesNotificationSenderTest { .containsExactlyInAnyOrder(tuple(startDate, endDate)); } + @Test + public void avoid_notification_if_configured_in_settings() { + settings.setProperty(DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES, true); + Multimap profiles = ArrayListMultimap.create(); + Languages languages = new Languages(); + addProfile(profiles, languages, ACTIVATED); + + BuiltInQualityProfilesUpdateListener underTest = new BuiltInQualityProfilesUpdateListener(notificationManager, languages, settings); + underTest.onChange(profiles, 0, 1); + + verifyZeroInteractions(notificationManager); + } + private Tuple addProfile(Multimap profiles, Languages languages, ActiveRuleChange.Type type) { String profileName = randomLowerCaseText(); Language language = newLanguage(randomLowerCaseText(), randomLowerCaseText()); @@ -152,4 +175,8 @@ public class BuiltInQualityProfilesNotificationSenderTest { private static String randomLowerCaseText() { return randomAlphanumeric(20).toLowerCase(); } + + private void enableNotificationInGlobalSettings(){ + settings.setProperty(DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES, false); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java index 4cb10e90b81..fd5d65447b2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java @@ -85,7 +85,7 @@ public class RegisterQualityProfilesNotificationTest { private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, mock(RuleIndex.class), new RuleActivatorContextFactory(dbClient), typeValidations, activeRuleIndexer, userSessionRule); private BuiltInQProfileUpdate builtInQProfileUpdate = new BuiltInQProfileUpdateImpl(dbClient, ruleActivator, activeRuleIndexer); - private BuiltInQualityProfilesNotificationSender builtInQualityProfilesNotification = mock(BuiltInQualityProfilesNotificationSender.class); + private BuiltInQualityProfilesUpdateListener builtInQualityProfilesNotification = mock(BuiltInQualityProfilesUpdateListener.class); private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient, builtInQProfileInsert, builtInQProfileUpdate, builtInQualityProfilesNotification, system2); @@ -127,7 +127,7 @@ public class RegisterQualityProfilesNotificationTest { underTest.start(); ArgumentCaptor captor = ArgumentCaptor.forClass(Multimap.class); - verify(builtInQualityProfilesNotification).send(captor.capture(), anyLong(), anyLong()); + verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong()); Multimap updatedProfiles = captor.>getValue(); assertThat(updatedProfiles.keySet()) .extracting(QProfileName::getName, QProfileName::getLanguage) @@ -149,7 +149,7 @@ public class RegisterQualityProfilesNotificationTest { underTest.start(); ArgumentCaptor captor = ArgumentCaptor.forClass(Multimap.class); - verify(builtInQualityProfilesNotification).send(captor.capture(), anyLong(), anyLong()); + verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong()); Multimap updatedProfiles = captor.>getValue(); assertThat(updatedProfiles.keySet()) .extracting(QProfileName::getName, QProfileName::getLanguage) @@ -179,7 +179,7 @@ public class RegisterQualityProfilesNotificationTest { underTest.start(); ArgumentCaptor captor = ArgumentCaptor.forClass(Multimap.class); - verify(builtInQualityProfilesNotification).send(captor.capture(), anyLong(), anyLong()); + verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong()); Multimap updatedProfiles = captor.>getValue(); assertThat(updatedProfiles.keySet()) .extracting(QProfileName::getName, QProfileName::getLanguage) @@ -213,7 +213,7 @@ public class RegisterQualityProfilesNotificationTest { underTest.start(); ArgumentCaptor captor = ArgumentCaptor.forClass(Multimap.class); - verify(builtInQualityProfilesNotification).send(captor.capture(), anyLong(), anyLong()); + verify(builtInQualityProfilesNotification).onChange(captor.capture(), anyLong(), anyLong()); Multimap updatedProfiles = captor.>getValue(); assertThat(updatedProfiles.keySet()) .extracting(QProfileName::getName, QProfileName::getLanguage) @@ -238,7 +238,7 @@ public class RegisterQualityProfilesNotificationTest { underTest.start(); - verify(builtInQualityProfilesNotification).send(any(), eq(startDate), eq(endDate)); + verify(builtInQualityProfilesNotification).onChange(any(), eq(startDate), eq(endDate)); } private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesTest.java index 5ad349cbfa9..6dccf64a029 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesTest.java @@ -62,7 +62,7 @@ public class RegisterQualityProfilesTest { private DbClient dbClient = db.getDbClient(); private DummyBuiltInQProfileInsert insert = new DummyBuiltInQProfileInsert(); private DummyBuiltInQProfileUpdate update = new DummyBuiltInQProfileUpdate(); - private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient, insert, update, mock(BuiltInQualityProfilesNotificationSender.class), system2); + private RegisterQualityProfiles underTest = new RegisterQualityProfiles(builtInQProfileRepositoryRule, dbClient, insert, update, mock(BuiltInQualityProfilesUpdateListener.class), system2); @Test public void start_fails_if_BuiltInQProfileRepository_has_not_been_initialized() { diff --git a/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java b/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java index 27dc0081917..52041056c88 100644 --- a/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java +++ b/sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java @@ -47,6 +47,7 @@ public class CorePropertyDefinitions { public static final String ORGANIZATIONS_ANYONE_CAN_CREATE = "sonar.organizations.anyoneCanCreate"; public static final String ORGANIZATIONS_CREATE_PERSONAL_ORG = "sonar.organizations.createPersonalOrg"; public static final String ONBOARDING_TUTORIAL_SHOW_TO_NEW_USERS = "sonar.onboardingTutorial.showToNewUsers"; + public static final String DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES = "sonar.builtInQualityProfiles.disableNotificationOnUpdate"; private CorePropertyDefinitions() { // only static stuff @@ -160,6 +161,13 @@ public class CorePropertyDefinitions { .category(CoreProperties.CATEGORY_SCM) .onlyOnQualifiers(Qualifiers.PROJECT) .build(), + PropertyDefinition.builder(DISABLE_NOTIFICATION_ON_BUILT_IN_QPROFILES) + .name("Avoid quality profiles notification") + .description("Avoid sending email notification on each update of built-in quality profiles to quality profile administrators") + .defaultValue(Boolean.toString(false)) + .category(CoreProperties.CATEGORY_GENERAL) + .type(BOOLEAN) + .build(), // WEB LOOK&FEEL PropertyDefinition.builder(WebConstants.SONAR_LF_LOGO_URL) diff --git a/sonar-core/src/test/java/org/sonar/core/config/CorePropertyDefinitionsTest.java b/sonar-core/src/test/java/org/sonar/core/config/CorePropertyDefinitionsTest.java index 0d1e7f05b9f..50a3028672f 100644 --- a/sonar-core/src/test/java/org/sonar/core/config/CorePropertyDefinitionsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/config/CorePropertyDefinitionsTest.java @@ -33,7 +33,7 @@ public class CorePropertyDefinitionsTest { @Test public void all() { List defs = CorePropertyDefinitions.all(); - assertThat(defs).hasSize(57); + assertThat(defs).hasSize(58); } @Test diff --git a/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java b/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java index 54077419d60..a0d8d90d368 100644 --- a/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java +++ b/tests/src/test/java/org/sonarqube/tests/qualityProfile/BuiltInQualityProfilesNotificationTest.java @@ -70,8 +70,6 @@ public class BuiltInQualityProfilesNotificationTest { .addPlugin(pluginArtifact("foo-plugin-v1")) .setServerProperty("email.smtp_host.secured", "localhost") .setServerProperty("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort())) - // .setServerProperty("sonar.web.javaAdditionalOpts", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005")// FIXME - // remove web debugging .build(); orchestrator.start(); @@ -101,8 +99,6 @@ public class BuiltInQualityProfilesNotificationTest { .addPlugin(pluginArtifact("foo-plugin-v1")) .setServerProperty("email.smtp_host.secured", "localhost") .setServerProperty("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort())) - // .setServerProperty("sonar.web.javaAdditionalOpts", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8001")// FIXME - // remove web debugging .build(); orchestrator.start(); @@ -147,6 +143,43 @@ public class BuiltInQualityProfilesNotificationTest { .isEqualTo(messages.get(1).getMimeMessage().getContent().toString()); } + @Test + public void do_not_send_mail_if_notifications_are_disabled_in_settings() throws Exception { + orchestrator = Orchestrator.builderEnv() + .addPlugin(pluginArtifact("foo-plugin-v1")) + .setServerProperty("sonar.builtInQualityProfiles.disableNotificationOnUpdate", "true") + .setServerProperty("email.smtp_host.secured", "localhost") + .setServerProperty("email.smtp_port.secured", Integer.toString(smtpServer.getServer().getPort())) + .build(); + orchestrator.start(); + + userRule = UserRule.from(orchestrator); + + WsUsers.CreateWsResponse.User profileAdmin1 = userRule.generate(); + WsClient wsClient = ItUtils.newAdminWsClient(orchestrator); + wsClient.permissions().addUser(new AddUserWsRequest().setLogin(profileAdmin1.getLogin()).setPermission("profileadmin")); + + WsUsers.CreateWsResponse.User profileAdmin2 = userRule.generate(); + String groupName = randomAlphanumeric(20); + wsClient.wsConnector().call(new PostRequest("api/user_groups/create").setParam("name", groupName)).failIfNotSuccessful(); + wsClient.permissions().addGroup(new AddGroupWsRequest().setPermission("profileadmin").setGroupName(groupName)); + wsClient.wsConnector().call(new PostRequest("api/user_groups/add_user").setParam("name", groupName).setParam("login", profileAdmin2.getLogin())).failIfNotSuccessful(); + + WsUsers.CreateWsResponse.User noProfileAdmin = userRule.generate(); + + // uninstall plugin V1 + wsClient.wsConnector().call(new PostRequest("api/plugins/uninstall").setParam("key", "foo")).failIfNotSuccessful(); + + // install plugin V2 + File pluginsDir = new File(orchestrator.getServer().getHome() + "/extensions/plugins"); + orchestrator.getConfiguration().fileSystem().copyToDirectory(pluginArtifact("foo-plugin-v2"), pluginsDir); + + orchestrator.restartServer(); + + waitUntilAllNotificationsAreDelivered(1, 10, 100); + assertThat(smtpServer.getMessages()).isEmpty(); + } + private MimeMessage getMimeMessage(WiserMessage msg) { try { return msg.getMimeMessage(); -- 2.39.5