Search in sources :

Example 1 with SubscriberPermissionsOnProject

use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject in project sonarqube by SonarSource.

the class DefaultNotificationManagerTest method findSubscribedEmailRecipients_does_not_call_DB_for_project_permission_filtering_if_there_is_no_global_subscriber.

@Test
public void findSubscribedEmailRecipients_does_not_call_DB_for_project_permission_filtering_if_there_is_no_global_subscriber() {
    String dispatcherKey = randomAlphabetic(12);
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    Set<EmailSubscriberDto> subscribers = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> EmailSubscriberDto.create("user" + i, false, "user" + i + "@sonarsource.com")).collect(Collectors.toSet());
    Set<String> logins = subscribers.stream().map(EmailSubscriberDto::getLogin).collect(Collectors.toSet());
    when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)).thenReturn(subscribers);
    when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, logins, projectKey, projectPermission)).thenReturn(logins);
    Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    Set<EmailRecipient> expected = subscribers.stream().map(i -> new EmailRecipient(i.getLogin(), i.getEmail())).collect(Collectors.toSet());
    assertThat(emailRecipients).isEqualTo(expected);
    verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission));
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission));
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Arrays(java.util.Arrays) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) PropertiesDao(org.sonar.db.property.PropertiesDao) Multimap(com.google.common.collect.Multimap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) DbSession(org.sonar.db.DbSession) InvalidClassException(java.io.InvalidClassException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) NotificationChannel(org.sonar.api.notifications.NotificationChannel) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationQueueDao(org.sonar.db.notification.NotificationQueueDao) Mockito.only(org.mockito.Mockito.only) Before(org.junit.Before) Subscriber(org.sonar.db.property.Subscriber) ImmutableSet(com.google.common.collect.ImmutableSet) InOrder(org.mockito.InOrder) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Notification(org.sonar.api.notifications.Notification) AuthorizationDao(org.sonar.db.permission.AuthorizationDao) Collection(java.util.Collection) Set(java.util.Set) NotificationQueueDto(org.sonar.db.notification.NotificationQueueDto) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) DbClient(org.sonar.db.DbClient) List(java.util.List) UserRole(org.sonar.api.web.UserRole) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Mockito.inOrder(org.mockito.Mockito.inOrder) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) Collections(java.util.Collections) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) Random(java.util.Random) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 2 with SubscriberPermissionsOnProject

use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject in project sonarqube by SonarSource.

the class DefaultNotificationManagerTest method do_not_call_db_for_project_permission_filtering_if_there_is_no_global_subscriber.

@Test
public void do_not_call_db_for_project_permission_filtering_if_there_is_no_global_subscriber() {
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectKey)).thenReturn(newHashSet(new Subscriber("user3", false)));
    when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectKey)).thenReturn(newHashSet(new Subscriber("user3", false)));
    when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3"), projectKey, projectPermission)).thenReturn(newHashSet("user3"));
    Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectKey, new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    assertThat(multiMap.entries()).hasSize(2);
    Map<String, Collection<NotificationChannel>> map = multiMap.asMap();
    assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel);
    verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission));
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission));
}
Also used : NotificationChannel(org.sonar.api.notifications.NotificationChannel) Subscriber(org.sonar.db.property.Subscriber) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) Collection(java.util.Collection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 3 with SubscriberPermissionsOnProject

use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject in project sonarqube by SonarSource.

the class DefaultNotificationManagerTest method findSubscribedEmailRecipients_returns_empty_if_no_email_recipients_in_project_for_dispatcher_key.

@Test
public void findSubscribedEmailRecipients_returns_empty_if_no_email_recipients_in_project_for_dispatcher_key() {
    String dispatcherKey = randomAlphabetic(12);
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)).thenReturn(Collections.emptySet());
    Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    assertThat(emailRecipients).isEmpty();
    verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(any(DbSession.class), anySet(), anyString(), anyString());
}
Also used : EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) DbSession(org.sonar.db.DbSession) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 4 with SubscriberPermissionsOnProject

use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject in project sonarqube by SonarSource.

the class DefaultNotificationManagerTest method should_apply_distinct_permission_filtering_global_or_project_subscribers.

@Test
public void should_apply_distinct_permission_filtering_global_or_project_subscribers() {
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    String otherProjectKey = randomAlphabetic(7);
    when(propertiesDao.findUsersForNotification("NewViolations", "Email", projectKey)).thenReturn(newHashSet(new Subscriber("user1", false), new Subscriber("user3", false), new Subscriber("user3", true)));
    when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", otherProjectKey)).thenReturn(newHashSet(new Subscriber("user2", false)));
    when(propertiesDao.findUsersForNotification("NewViolations", "Twitter", projectKey)).thenReturn(newHashSet(new Subscriber("user3", true)));
    when(propertiesDao.findUsersForNotification("NewAlerts", "Twitter", projectKey)).thenReturn(newHashSet(new Subscriber("user4", false)));
    when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user3", "user4"), projectKey, globalPermission)).thenReturn(newHashSet("user3"));
    when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, newHashSet("user1", "user3"), projectKey, projectPermission)).thenReturn(newHashSet("user1", "user3"));
    Multimap<String, NotificationChannel> multiMap = underTest.findSubscribedRecipientsForDispatcher(dispatcher, projectKey, new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    assertThat(multiMap.entries()).hasSize(3);
    Map<String, Collection<NotificationChannel>> map = multiMap.asMap();
    assertThat(map.get("user1")).containsOnly(emailChannel);
    assertThat(map.get("user2")).isNull();
    assertThat(map.get("user3")).containsOnly(emailChannel, twitterChannel);
    assertThat(map.get("user4")).isNull();
    // code is optimized to perform only 2 SQL requests for all channels
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission));
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission));
}
Also used : NotificationChannel(org.sonar.api.notifications.NotificationChannel) Subscriber(org.sonar.db.property.Subscriber) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) Collection(java.util.Collection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with SubscriberPermissionsOnProject

use of org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject in project sonarqube by SonarSource.

the class DefaultNotificationManagerTest method findSubscribedEmailRecipients_with_logins_does_not_call_DB_for_project_permission_filtering_if_there_is_no_global_subscriber.

@Test
public void findSubscribedEmailRecipients_with_logins_does_not_call_DB_for_project_permission_filtering_if_there_is_no_global_subscriber() {
    String dispatcherKey = randomAlphabetic(12);
    String globalPermission = randomAlphanumeric(4);
    String projectPermission = randomAlphanumeric(5);
    String projectKey = randomAlphabetic(6);
    Set<EmailSubscriberDto> subscribers = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> EmailSubscriberDto.create("user" + i, false, "user" + i + "@sonarsource.com")).collect(Collectors.toSet());
    Set<String> logins = subscribers.stream().map(EmailSubscriberDto::getLogin).collect(Collectors.toSet());
    when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)).thenReturn(subscribers);
    when(authorizationDao.keepAuthorizedLoginsOnProject(dbSession, logins, projectKey, projectPermission)).thenReturn(logins);
    Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, logins, new SubscriberPermissionsOnProject(globalPermission, projectPermission));
    Set<EmailRecipient> expected = subscribers.stream().map(i -> new EmailRecipient(i.getLogin(), i.getEmail())).collect(Collectors.toSet());
    assertThat(emailRecipients).isEqualTo(expected);
    verify(authorizationDao, times(0)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(globalPermission));
    verify(authorizationDao, times(1)).keepAuthorizedLoginsOnProject(eq(dbSession), anySet(), anyString(), eq(projectPermission));
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) Arrays(java.util.Arrays) ALL_MUST_HAVE_ROLE_USER(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject.ALL_MUST_HAVE_ROLE_USER) RandomStringUtils.randomAlphanumeric(org.apache.commons.lang.RandomStringUtils.randomAlphanumeric) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) PropertiesDao(org.sonar.db.property.PropertiesDao) Multimap(com.google.common.collect.Multimap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Mockito.spy(org.mockito.Mockito.spy) DbSession(org.sonar.db.DbSession) InvalidClassException(java.io.InvalidClassException) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Map(java.util.Map) NotificationChannel(org.sonar.api.notifications.NotificationChannel) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) NotificationQueueDao(org.sonar.db.notification.NotificationQueueDao) Mockito.only(org.mockito.Mockito.only) Before(org.junit.Before) Subscriber(org.sonar.db.property.Subscriber) ImmutableSet(com.google.common.collect.ImmutableSet) InOrder(org.mockito.InOrder) System2(org.sonar.api.utils.System2) RandomStringUtils.randomAlphabetic(org.apache.commons.lang.RandomStringUtils.randomAlphabetic) Notification(org.sonar.api.notifications.Notification) AuthorizationDao(org.sonar.db.permission.AuthorizationDao) Collection(java.util.Collection) Set(java.util.Set) NotificationQueueDto(org.sonar.db.notification.NotificationQueueDto) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) DbClient(org.sonar.db.DbClient) List(java.util.List) UserRole(org.sonar.api.web.UserRole) VerificationModeFactory.times(org.mockito.internal.verification.VerificationModeFactory.times) Mockito.inOrder(org.mockito.Mockito.inOrder) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) Collections(java.util.Collections) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) EmailRecipient(org.sonar.server.notification.NotificationManager.EmailRecipient) EmailSubscriberDto(org.sonar.db.EmailSubscriberDto) Random(java.util.Random) SubscriberPermissionsOnProject(org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 SubscriberPermissionsOnProject (org.sonar.server.notification.NotificationManager.SubscriberPermissionsOnProject)11 Collection (java.util.Collection)8 NotificationChannel (org.sonar.api.notifications.NotificationChannel)8 Subscriber (org.sonar.db.property.Subscriber)8 EmailRecipient (org.sonar.server.notification.NotificationManager.EmailRecipient)8 ImmutableSet (com.google.common.collect.ImmutableSet)5 Multimap (com.google.common.collect.Multimap)5 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)5 InvalidClassException (java.io.InvalidClassException)5 Arrays (java.util.Arrays)5 Collections (java.util.Collections)5 List (java.util.List)5 Map (java.util.Map)5 Random (java.util.Random)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 IntStream (java.util.stream.IntStream)5 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)5