EmailInviteSender implements EventSubscriber<InviteCreatedEvent> { @VisibleForTesting String getInitiatorInfo(String initiatorId) throws ServerException { User initiator; Profile initiatorProfile; try { initiator = userManager.getById(initiatorId); initiatorProfile = profileManager.getById(initiatorId); } catch (NotFoundException e) { throw new ServerException(e.getLocalizedMessage(), e); } Map<String, String> profileAttributes = initiatorProfile.getAttributes(); String firstName = nullToEmpty(profileAttributes.get("firstName")); String lastName = nullToEmpty(profileAttributes.get("lastName")); if (firstName.isEmpty() || lastName.isEmpty()) { return initiator.getEmail(); } else { return firstName + " " + lastName; } } @Inject  EmailInviteSender(
      @Named("che.api") String apiEndpoint,
      @Named("che.mail.from_email_address") String mailFrom,
      @Named("workspace.email.invite.subject") String workspaceInviteSubject,
      @Named("organization.email.invite.subject") String organizationInviteSubject,
      DefaultEmailResourceResolver resourceResolver,
      MailSender mailSender,
      UserManager userManager,
      ProfileManager profileManager,
      BearerTokenAuthenticationHandler tokenHandler,
      TemplateProcessor templateProcessor); @Inject void subscribe(EventService eventService); @Override void onEvent(InviteCreatedEvent event); void sendEmail(String initiatorId, Invite invite);  }
@Test public void shouldReturnInitiatorEmailIfFirstNameIsAbsent() throws Exception { when(initiator.getEmail()).thenReturn("inititator@test.com"); when(initiatorProfile.getAttributes()).thenReturn(ImmutableMap.of("lastName", "Last")); String initiator = emailSender.getInitiatorInfo(USER_INITIATOR_ID); assertEquals(initiator, "inititator@test.com"); verify(userManager).getById(USER_INITIATOR_ID); verify(profileManager).getById(USER_INITIATOR_ID); }
@Test public void shouldReturnInitiatorEmailIfLastNameIsAbsent() throws Exception { when(initiator.getEmail()).thenReturn("inititator@test.com"); when(initiatorProfile.getAttributes()).thenReturn(ImmutableMap.of("firstName", "First")); String initiator = emailSender.getInitiatorInfo(USER_INITIATOR_ID); assertEquals(initiator, "inititator@test.com"); verify(userManager).getById(USER_INITIATOR_ID); verify(profileManager).getById(USER_INITIATOR_ID); }
@Test public void shouldReturnFirstNamePlusLastNameIfTheyAreSpecified() throws Exception { when(initiatorProfile.getAttributes()) .thenReturn( ImmutableMap.of( "firstName", "First", "lastName", "Last")); String initiator = emailSender.getInitiatorInfo(USER_INITIATOR_ID); assertEquals(initiator, "First Last"); verify(userManager).getById(USER_INITIATOR_ID); verify(profileManager).getById(USER_INITIATOR_ID); }