diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-01-30 15:08:34 +0100 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2013-01-30 15:23:22 +0100 |
commit | c91f6e066a0d4917b2cbace325db9ac6a12c2913 (patch) | |
tree | ca2fc6dc23954c5a561ddbe9115dc75aec10a7bf /sonar-server | |
parent | 8f9dacd45c67012dae0dc06df4f2baa483164bc6 (diff) | |
download | sonarqube-c91f6e066a0d4917b2cbace325db9ac6a12c2913.tar.gz sonarqube-c91f6e066a0d4917b2cbace325db9ac6a12c2913.zip |
SONAR-3959 Add NotificationDispatcherMetadata class
Diffstat (limited to 'sonar-server')
7 files changed, 195 insertions, 13 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/notifications/NotificationCenter.java b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationCenter.java new file mode 100644 index 00000000000..620a4e285f7 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationCenter.java @@ -0,0 +1,103 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.notifications; + +import com.google.common.collect.Lists; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.ServerComponent; +import org.sonar.api.notifications.NotificationChannel; +import org.sonar.api.notifications.NotificationDispatcherMetadata; + +import javax.annotation.Nullable; + +import java.util.Arrays; +import java.util.List; + +/** + * @since 3.5 + */ +public class NotificationCenter implements ServerComponent { + + private static final Logger LOG = LoggerFactory.getLogger(NotificationCenter.class); + + private final NotificationDispatcherMetadata[] dispatchersMetadata; + private final NotificationChannel[] channels; + + /** + * Constructor for {@link NotificationCenter} + */ + public NotificationCenter(NotificationDispatcherMetadata[] metadata, NotificationChannel[] channels) { + this.dispatchersMetadata = metadata; + this.channels = channels; + LOG.info("====================> " + metadata.length); + LOG.info("====================> " + channels.length); + } + + /** + * Default constructor when no channels. + */ + public NotificationCenter(NotificationDispatcherMetadata[] metadata) { + this(metadata, new NotificationChannel[0]); + LOG.warn("There is no notification channel - no notification will be delivered!"); + } + + /** + * Default constructor when no dispatcher metadata. + */ + public NotificationCenter(NotificationChannel[] channels) { + this(new NotificationDispatcherMetadata[0], channels); + } + + /** + * Default constructor. + */ + public NotificationCenter() { + this(new NotificationDispatcherMetadata[0], new NotificationChannel[0]); + LOG.warn("There is no notification channel - no notification will be delivered!"); + } + + /** + * Returns all the available channels. + */ + public List<NotificationChannel> getChannels() { + return Arrays.asList(channels); + } + + /** + * Returns all the available dispatchers which metadata matches the given property and its value. + * <br/> + * If "propertyValue" is null, the verification is done on the existence of such a property (whatever the value). + */ + public List<String> getDispatcherKeysForProperty(String propertyKey, @Nullable String propertyValue) { + List<String> keys = Lists.newArrayList(); + for (NotificationDispatcherMetadata metadata : dispatchersMetadata) { + String dispatcherKey = metadata.getDispatcherKey(); + String value = metadata.getProperty(propertyKey); + if (value != null) { + if (propertyValue == null || value.equals(propertyValue)) { + keys.add(dispatcherKey); + } + } + } + return keys; + } + +} diff --git a/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java index 49b4e8237c0..c52906d3523 100644 --- a/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java +++ b/sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java @@ -164,12 +164,9 @@ public class NotificationService implements ServerComponent { } } - public List<NotificationDispatcher> getDispatchers() { + @VisibleForTesting + protected List<NotificationDispatcher> getDispatchers() { return Arrays.asList(dispatchers); } - public List<NotificationChannel> getChannels() { - return manager.getChannels(); - } - } diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 964382f6682..ae490bf7224 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -73,6 +73,7 @@ import org.sonar.server.charts.ChartFactory; import org.sonar.server.configuration.Backup; import org.sonar.server.configuration.ProfilesManager; import org.sonar.server.database.EmbeddedDatabaseFactory; +import org.sonar.server.notifications.NotificationCenter; import org.sonar.server.notifications.NotificationService; import org.sonar.server.notifications.reviews.ReviewsNotificationManager; import org.sonar.server.plugins.ApplicationDeployer; @@ -257,6 +258,7 @@ public final class Platform { // Notifications servicesContainer.addSingleton(EmailSettings.class); servicesContainer.addSingleton(NotificationService.class); + servicesContainer.addSingleton(NotificationCenter.class); servicesContainer.addSingleton(DefaultNotificationManager.class); servicesContainer.addSingleton(ReviewsNotificationManager.class); diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb index beac85292fb..875aa77cccc 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/account_controller.rb @@ -22,9 +22,9 @@ class AccountController < ApplicationController before_filter :login_required def index - notification_service = java_facade.getCoreComponentByClassname('org.sonar.server.notifications.NotificationService') + notification_service = java_facade.getCoreComponentByClassname('org.sonar.server.notifications.NotificationCenter') @channels = notification_service.getChannels() - @dispatchers = notification_service.getDispatchers() + @dispatchers = notification_service.getDispatcherKeysForProperty("globalNotification", "true") @notifications = {} for property in Property.find(:all, :conditions => ['prop_key like ? AND user_id = ?', 'notification.%', current_user.id]) @notifications[property.key.sub('notification.', '')] = true diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb index aaabf828d4d..c6285fe64b6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/account/index.html.erb @@ -68,11 +68,11 @@ </tr> <% for dispatcher in @dispatchers %> <tr> - <td><%= message('notification.dispatcher.' + dispatcher.getKey()) -%></td> + <td><%= message('notification.dispatcher.' + dispatcher) -%></td> <td> <% for channel in @channels - notification_id = dispatcher.getKey() + '.' + channel.getKey() + notification_id = dispatcher + '.' + channel.getKey() check_box_id = 'notifications[' + notification_id + ']' check_box_checked = @notifications[notification_id] %> diff --git a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationCenterTest.java b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationCenterTest.java new file mode 100644 index 00000000000..8eeb725bf1e --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationCenterTest.java @@ -0,0 +1,83 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.notifications; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.sonar.api.notifications.NotificationChannel; +import org.sonar.api.notifications.NotificationDispatcherMetadata; + +import static org.fest.assertions.Assertions.assertThat; + +public class NotificationCenterTest { + + @Mock + private NotificationChannel emailChannel; + + @Mock + private NotificationChannel gtalkChannel; + + private NotificationCenter notificationCenter; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + + NotificationDispatcherMetadata metadata1 = NotificationDispatcherMetadata.create("Dispatcher1").setProperty("global", "true").setProperty("on-project", "true"); + NotificationDispatcherMetadata metadata2 = NotificationDispatcherMetadata.create("Dispatcher2").setProperty("global", "true"); + NotificationDispatcherMetadata metadata3 = NotificationDispatcherMetadata.create("Dispatcher3").setProperty("global", "FOO").setProperty("on-project", "BAR"); + + notificationCenter = new NotificationCenter( + new NotificationDispatcherMetadata[] {metadata1, metadata2, metadata3}, + new NotificationChannel[] {emailChannel, gtalkChannel} + ); + } + + @Test + public void shouldReturnChannels() { + assertThat(notificationCenter.getChannels()).containsOnly(emailChannel, gtalkChannel); + } + + @Test + public void shouldReturnDispatcherKeysForSpecificPropertyValue() { + assertThat(notificationCenter.getDispatcherKeysForProperty("global", "true")).containsOnly("Dispatcher1", "Dispatcher2"); + } + + @Test + public void shouldReturnDispatcherKeysForExistenceOfProperty() { + assertThat(notificationCenter.getDispatcherKeysForProperty("on-project", null)).containsOnly("Dispatcher1", "Dispatcher3"); + } + + @Test + public void testDefaultConstructors() { + notificationCenter = new NotificationCenter(new NotificationChannel[] {emailChannel}); + assertThat(notificationCenter.getChannels()).hasSize(1); + + notificationCenter = new NotificationCenter(); + assertThat(notificationCenter.getChannels()).hasSize(0); + + notificationCenter = new NotificationCenter(new NotificationDispatcherMetadata[] {NotificationDispatcherMetadata.create("Dispatcher1").setProperty("global", "true")}); + assertThat(notificationCenter.getChannels()).hasSize(0); + assertThat(notificationCenter.getDispatcherKeysForProperty("global", null)).hasSize(1); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java index d2eeb8cce86..21e4a956282 100644 --- a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.notifications; -import com.google.common.collect.Lists; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -63,7 +62,6 @@ public class NotificationServiceTest { when(commentOnReviewCreatedByMe.getKey()).thenReturn("comment on review created by me"); when(queueElement.getNotification()).thenReturn(notification); when(manager.getFromQueue()).thenReturn(queueElement).thenReturn(null); - when(manager.getChannels()).thenReturn(Lists.newArrayList(emailChannel, gtalkChannel)); Settings settings = new Settings().setProperty("sonar.notifications.delay", 1L); @@ -179,10 +177,9 @@ public class NotificationServiceTest { } @Test - public void shouldReturnDispatcherAndChannelListsUsedInWebapp() { + public void shouldReturnDispatcherList() { setUpMocks(CREATOR_SIMON, ASSIGNEE_SIMON); - assertThat(service.getChannels()).containsOnly(emailChannel, gtalkChannel); assertThat(service.getDispatchers()).containsOnly(commentOnReviewAssignedToMe, commentOnReviewCreatedByMe); } |