diff options
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java | 16 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/notifications/DefaultNotificationManagerTest.java | 7 | ||||
-rw-r--r-- | sonar-core/src/test/resources/org/sonar/core/notifications/DefaultNotificationManagerTest/fixture.xml (renamed from sonar-server/src/test/resources/org/sonar/server/notifications/NotificationServiceDbTest/fixture.xml) | 2 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/notifications/NotificationService.java | 26 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceDbTest.java | 55 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceTest.java | 25 |
6 files changed, 42 insertions, 89 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java b/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java index 4f8b604726a..f9e2107743f 100644 --- a/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java +++ b/sonar-core/src/main/java/org/sonar/core/notifications/DefaultNotificationManager.java @@ -19,15 +19,17 @@ */ package org.sonar.core.notifications; +import java.util.Date; +import java.util.List; + import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.configuration.Property; +import org.sonar.api.database.model.User; import org.sonar.api.notifications.Notification; import org.sonar.api.notifications.NotificationManager; import org.sonar.jpa.entity.NotificationQueueElement; import org.sonar.jpa.session.DatabaseSessionFactory; -import java.util.Date; -import java.util.List; - /** * @since 2.10 */ @@ -61,4 +63,12 @@ public class DefaultNotificationManager implements NotificationManager { return notification; } + public boolean isEnabled(String username, String channelKey, String dispatcherKey) { + DatabaseSession session = sessionFactory.getSession(); + User user = session.getSingleResult(User.class, "login", username); + String notificationKey = "notification." + dispatcherKey + "." + channelKey; + Property property = session.getSingleResult(Property.class, "userId", user.getId(), "key", notificationKey); + return property != null && "true".equals(property.getValue()); + } + } diff --git a/sonar-core/src/test/java/org/sonar/core/notifications/DefaultNotificationManagerTest.java b/sonar-core/src/test/java/org/sonar/core/notifications/DefaultNotificationManagerTest.java index 70587fa8493..ae661ae43cc 100644 --- a/sonar-core/src/test/java/org/sonar/core/notifications/DefaultNotificationManagerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/notifications/DefaultNotificationManagerTest.java @@ -49,4 +49,11 @@ public class DefaultNotificationManagerTest extends AbstractDbUnitTestCase { assertThat(manager.getFromQueue(), nullValue()); } + @Test + public void shouldCheckEnablement() { + setupData("fixture"); + assertThat(manager.isEnabled("simon", "email", "CommentOnReviewAssignedToMe"), is(true)); + assertThat(manager.isEnabled("godin", "email", "CommentOnReviewAssignedToMe"), is(false)); + } + } diff --git a/sonar-server/src/test/resources/org/sonar/server/notifications/NotificationServiceDbTest/fixture.xml b/sonar-core/src/test/resources/org/sonar/core/notifications/DefaultNotificationManagerTest/fixture.xml index 83c713eb0e7..7933d24446d 100644 --- a/sonar-server/src/test/resources/org/sonar/server/notifications/NotificationServiceDbTest/fixture.xml +++ b/sonar-core/src/test/resources/org/sonar/core/notifications/DefaultNotificationManagerTest/fixture.xml @@ -3,6 +3,6 @@ <users id="1" login="simon" /> <users id="2" login="godin" /> - <properties user_id="1" prop_key="notification.CommentOnReviewAssignedToMe.EmailNotificationChannel" text_value="true"/> + <properties user_id="1" prop_key="notification.CommentOnReviewAssignedToMe.email" text_value="true"/> </dataset>
\ No newline at end of file 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 bdd2c3434f2..88184e1ed23 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 @@ -30,9 +30,6 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.configuration.Configuration; import org.sonar.api.ServerComponent; -import org.sonar.api.database.DatabaseSession; -import org.sonar.api.database.configuration.Property; -import org.sonar.api.database.model.User; import org.sonar.api.notifications.Notification; import org.sonar.api.notifications.NotificationChannel; import org.sonar.api.notifications.NotificationDispatcher; @@ -40,7 +37,6 @@ import org.sonar.api.utils.Logs; import org.sonar.api.utils.TimeProfiler; import org.sonar.core.notifications.DefaultNotificationManager; import org.sonar.jpa.entity.NotificationQueueElement; -import org.sonar.jpa.session.DatabaseSessionFactory; import com.google.common.collect.HashMultimap; import com.google.common.collect.SetMultimap; @@ -59,7 +55,6 @@ public class NotificationService implements ServerComponent { private ScheduledExecutorService executorService; private long delay; - private DatabaseSessionFactory sessionFactory; private DefaultNotificationManager manager; private NotificationChannel[] channels; private NotificationDispatcher[] dispatchers; @@ -67,15 +62,13 @@ public class NotificationService implements ServerComponent { /** * Default constructor when no channels. */ - public NotificationService(Configuration configuration, DatabaseSessionFactory sessionFactory, DefaultNotificationManager manager, NotificationDispatcher[] dispatchers) { - this(configuration, sessionFactory, manager, dispatchers, new NotificationChannel[0]); + public NotificationService(Configuration configuration, DefaultNotificationManager manager, NotificationDispatcher[] dispatchers) { + this(configuration, manager, dispatchers, new NotificationChannel[0]); Logs.INFO.warn("There is no channels - all notifications would be ignored!"); } - public NotificationService(Configuration configuration, DatabaseSessionFactory sessionFactory, DefaultNotificationManager manager, NotificationDispatcher[] dispatchers, - NotificationChannel[] channels) { + public NotificationService(Configuration configuration, DefaultNotificationManager manager, NotificationDispatcher[] dispatchers, NotificationChannel[] channels) { delay = configuration.getLong(DELAY, DELAY_DEFAULT); - this.sessionFactory = sessionFactory; this.manager = manager; this.channels = channels; this.dispatchers = dispatchers; @@ -136,7 +129,7 @@ public class NotificationService implements ServerComponent { Logs.INFO.warn("Unable to dispatch notification " + notification + " using " + dispatcher, e); } for (String username : possibleRecipients) { - if (isEnabled(username, channel, dispatcher)) { + if (manager.isEnabled(username, channel.getKey(), dispatcher.getKey())) { recipients.put(username, channel); } } @@ -164,15 +157,4 @@ public class NotificationService implements ServerComponent { return Arrays.asList(channels); } - /** - * Visibility has been relaxed for tests. - */ - boolean isEnabled(String username, NotificationChannel channel, NotificationDispatcher dispatcher) { - DatabaseSession session = sessionFactory.getSession(); - User user = session.getSingleResult(User.class, "login", username); - String notificationKey = "notification." + dispatcher.getKey() + "." + channel.getKey(); - Property property = session.getSingleResult(Property.class, "userId", user.getId(), "key", notificationKey); - return property != null && "true".equals(property.getValue()); - } - } diff --git a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceDbTest.java b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceDbTest.java deleted file mode 100644 index 82f1c4fbad7..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationServiceDbTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 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 static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.commons.configuration.Configuration; -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.notifications.NotificationChannel; -import org.sonar.api.notifications.NotificationDispatcher; -import org.sonar.jpa.test.AbstractDbUnitTestCase; - -public class NotificationServiceDbTest extends AbstractDbUnitTestCase { - - private NotificationService notificationService; - - @Before - public void setUp() { - setupData("fixture"); - notificationService = new NotificationService(mock(Configuration.class), getSessionFactory(), null, null); - } - - @Test - public void shouldCheckEnablement() { - NotificationChannel email = mock(NotificationChannel.class); - when(email.getKey()).thenReturn("EmailNotificationChannel"); - NotificationDispatcher commentOnReviewAssignedToMe = mock(NotificationDispatcher.class); - when(commentOnReviewAssignedToMe.getKey()).thenReturn("CommentOnReviewAssignedToMe"); - - assertThat(notificationService.isEnabled("simon", email, commentOnReviewAssignedToMe), is(true)); - assertThat(notificationService.isEnabled("godin", email, commentOnReviewAssignedToMe), is(false)); - } - -} 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 1bc824b1413..c75a702b3cf 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 @@ -20,6 +20,7 @@ package org.sonar.server.notifications; import static org.mockito.Matchers.any; +import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; @@ -89,8 +90,8 @@ public class NotificationServiceTest { manager = mock(DefaultNotificationManager.class); Configuration configuration = new BaseConfiguration(); configuration.setProperty("sonar.notifications.delay", "1"); // delay 1 second - service = spy(new NotificationService(configuration, null, manager, dispatchers, channels)); - doReturn(false).when(service).isEnabled(any(String.class), any(NotificationChannel.class), any(NotificationDispatcher.class)); + service = spy(new NotificationService(configuration, manager, dispatchers, channels)); + doReturn(false).when(manager).isEnabled(any(String.class), any(String.class), any(String.class)); } @Test @@ -120,8 +121,8 @@ public class NotificationServiceTest { */ @Test public void scenario1() { - doReturn(true).when(service).isEnabled(USER_SIMON, emailChannel, commentOnReviewAssignedToMe); - doReturn(true).when(service).isEnabled(USER_SIMON, emailChannel, commentOnReviewCreatedByMe); + doReturn(true).when(manager).isEnabled(USER_SIMON, "email", "comment on review assigned to me"); + doReturn(true).when(manager).isEnabled(USER_SIMON, "email", "comment on review created by me"); Notification notification = mock(Notification.class); creator = USER_SIMON; @@ -129,6 +130,8 @@ public class NotificationServiceTest { service.deliver(notification); + verify(emailChannel, atLeast(1)).getKey(); + verify(gtalkChannel, atLeast(1)).getKey(); verify(emailChannel).deliver(notification, USER_SIMON); verifyNoMoreInteractions(emailChannel); verifyNoMoreInteractions(gtalkChannel); @@ -147,8 +150,8 @@ public class NotificationServiceTest { */ @Test public void scenario2() { - doReturn(true).when(service).isEnabled(USER_EVGENY, gtalkChannel, commentOnReviewCreatedByMe); - doReturn(true).when(service).isEnabled(USER_SIMON, emailChannel, commentOnReviewAssignedToMe); + doReturn(true).when(manager).isEnabled(USER_EVGENY, "gtalk", "comment on review created by me"); + doReturn(true).when(manager).isEnabled(USER_SIMON, "email", "comment on review assigned to me"); Notification notification = mock(Notification.class); creator = USER_EVGENY; @@ -156,6 +159,8 @@ public class NotificationServiceTest { service.deliver(notification); + verify(emailChannel, atLeast(1)).getKey(); + verify(gtalkChannel, atLeast(1)).getKey(); verify(emailChannel).deliver(notification, USER_SIMON); verify(gtalkChannel).deliver(notification, USER_EVGENY); verifyNoMoreInteractions(emailChannel); @@ -174,8 +179,8 @@ public class NotificationServiceTest { */ @Test public void scenario3() { - doReturn(true).when(service).isEnabled(USER_SIMON, emailChannel, commentOnReviewAssignedToMe); - doReturn(true).when(service).isEnabled(USER_SIMON, gtalkChannel, commentOnReviewAssignedToMe); + doReturn(true).when(manager).isEnabled(USER_SIMON, "email", "comment on review assigned to me"); + doReturn(true).when(manager).isEnabled(USER_SIMON, "gtalk", "comment on review assigned to me"); Notification notification = mock(Notification.class); creator = USER_EVGENY; @@ -183,6 +188,8 @@ public class NotificationServiceTest { service.deliver(notification); + verify(emailChannel, atLeast(1)).getKey(); + verify(gtalkChannel, atLeast(1)).getKey(); verify(emailChannel).deliver(notification, USER_SIMON); verify(gtalkChannel).deliver(notification, USER_SIMON); verifyNoMoreInteractions(emailChannel); @@ -207,6 +214,8 @@ public class NotificationServiceTest { service.deliver(notification); + verify(emailChannel, atLeast(1)).getKey(); + verify(gtalkChannel, atLeast(1)).getKey(); verifyNoMoreInteractions(emailChannel); verifyNoMoreInteractions(gtalkChannel); } |