OrganizationNotificationEmailSender implements EventSubscriber<OrganizationEvent> { @Override public void onEvent(OrganizationEvent event) { try { if (event.getInitiator() != null) { if (event.getOrganization().getParent() == null) { try { userManager.getByName(event.getOrganization().getName()); return; } catch (NotFoundException ex) { } } switch (event.getType()) { case MEMBER_ADDED: send((MemberAddedEventDto) event); break; case MEMBER_REMOVED: send((MemberRemovedEventDto) event); break; case ORGANIZATION_REMOVED: send((OrganizationRemovedEventDto) event); break; case ORGANIZATION_RENAMED: send((OrganizationRenamedEventDto) event); } } } catch (Exception ex) { LOG.error("Failed to send email notification '{}' cause : '{}'", ex.getMessage()); } } @Inject  OrganizationNotificationEmailSender(
      @Named("che.api") String apiEndpoint,
      MailSender mailSender,
      OrganizationManager organizationManager,
      UserManager userManager,
      OrganizationEmailNotifications emails); @Inject void subscribe(EventService eventService); @Override void onEvent(OrganizationEvent event);  }
@Test public void shouldSendNotificationAboutMembershipAdding() throws Exception { EmailBean email = mock(EmailBean.class, new SelfReturningAnswer()); when(emails.memberAdded(anyString(), anyString(), anyString(), anyString())).thenReturn(email); emailSender.onEvent( asDto( new MemberAddedEvent( "admin", new UserImpl("id", "email", null), new OrganizationImpl("id", "/parent/name", "parent")))); verify(emails).memberAdded("name", DASHBOARD_ENDPOINT, "/parent/name", "admin"); verify(email).withTo("email"); verify(mailSender).sendAsync(email); }
@Test public void shouldSendNotificationAboutMembershipRemoving() throws Exception { EmailBean email = mock(EmailBean.class, new SelfReturningAnswer()); when(emails.memberRemoved(anyString(), anyString())).thenReturn(email); emailSender.onEvent( asDto( new MemberRemovedEvent( "admin", new UserImpl("id", "email", null), new OrganizationImpl("id", "/parent/name", "parent")))); verify(emails).memberRemoved("name", "admin"); verify(email).withTo("email"); verify(mailSender).sendAsync(email); }
@Test public void shouldSendNotificationAboutOrganizationRenaming() throws Exception { MemberImpl member1 = new MemberImpl("user1", "org123", ImmutableList.of()); MemberImpl member2 = new MemberImpl("user2", "org123", ImmutableList.of()); doReturn(new Page<Member>(asList(member1, member2), 0, 2, 2)) .when(organizationManager) .getMembers(anyString(), anyInt(), anyLong()); doReturn(new UserImpl("user1", "email1", null, null, emptyList())) .when(userManager) .getById("user1"); doReturn(new UserImpl("user2", "email2", null, null, emptyList())) .when(userManager) .getById("user2"); EmailBean email = new EmailBean().withBody("Org Remaned Notification"); when(emails.organizationRenamed(anyString(), anyString())).thenReturn(email); emailSender.onEvent( asDto( new OrganizationRenamedEvent( "admin", "oldName", "newName", new OrganizationImpl("org123", "/parent/newName", "parent")))); verify(emails).organizationRenamed("oldName", "newName"); verify(mailSender).sendAsync(new EmailBean(email).withTo("email1")); verify(mailSender).sendAsync(new EmailBean(email).withTo("email2")); }
@Test public void shouldDoNotBreakSendingOfNotificationAboutOrganizationRenamingWhenUnableToRetrieveAUser() throws Exception { MemberImpl member1 = new MemberImpl("user1", "org123", emptyList()); MemberImpl member2 = new MemberImpl("user2", "org123", emptyList()); doReturn(new Page<Member>(asList(member1, member2), 0, 2, 2)) .when(organizationManager) .getMembers(anyString(), anyInt(), anyLong()); when(userManager.getById("user1")).thenThrow(new NotFoundException("")); doReturn(new UserImpl("user2", "email2", null, null, emptyList())) .when(userManager) .getById("user2"); EmailBean email = new EmailBean().withBody("Org Renamed Notification"); when(emails.organizationRenamed(anyString(), anyString())).thenReturn(email); emailSender.onEvent( asDto( new OrganizationRenamedEvent( "admin", "oldName", "newName", new OrganizationImpl("org123", "/parent/newName", "parent")))); verify(emails).organizationRenamed("oldName", "newName"); verify(mailSender).sendAsync(new EmailBean(email).withTo("email2")); }
@Test public void shouldSendNotificationAboutOrganizationRemoving() throws Exception { MemberImpl member1 = new MemberImpl("user1", "org123", emptyList()); MemberImpl member2 = new MemberImpl("user2", "org123", emptyList()); doReturn(new UserImpl("user1", "email1", null, null, emptyList())) .when(userManager) .getById("user1"); doReturn(new UserImpl("user2", "email2", null, null, emptyList())) .when(userManager) .getById("user2"); EmailBean email = new EmailBean().withBody("Org Removed Notification"); when(emails.organizationRemoved(anyString())).thenReturn(email); emailSender.onEvent( asDto( new OrganizationRemovedEvent( "admin", new OrganizationImpl("id", "/parent/q", "parent"), asList(member1.getUserId(), member2.getUserId())))); verify(emails).organizationRemoved("q"); verify(mailSender).sendAsync(new EmailBean(email).withTo("email1")); verify(mailSender).sendAsync(new EmailBean(email).withTo("email2")); }
@Test public void shouldDoNotBreakSendingOfNotificationAboutOrganizationRemovingWhenUnableToRetrieveAUser() throws Exception { MemberImpl member1 = new MemberImpl("user1", "org123", emptyList()); MemberImpl member2 = new MemberImpl("user2", "org123", emptyList()); doThrow(new NotFoundException("")).when(userManager).getById("user1"); doReturn(new UserImpl("user2", "email2", null, null, emptyList())) .when(userManager) .getById("user2"); EmailBean email = new EmailBean().withBody("Org Removed Notification"); when(emails.organizationRemoved(anyString())).thenReturn(email); emailSender.onEvent( asDto( new OrganizationRemovedEvent( "admin", new OrganizationImpl("id", "/parent/q", "parent"), asList(member1.getUserId(), member2.getUserId())))); verify(emails).organizationRemoved("q"); verify(mailSender).sendAsync(new EmailBean(email).withTo("email2")); }