InviteManager { @Transactional(rollbackOn = {RuntimeException.class, ServerException.class}) public void store(Invite invite) throws NotFoundException, ConflictException, ServerException { requireNonNull(invite, "Required non-null invite"); String domainId = invite.getDomainId(); if (!OrganizationDomain.DOMAIN_ID.equals(domainId) && !WorkspaceDomain.DOMAIN_ID.equals(domainId)) { throw new ConflictException("Invitations for specified domain are not supported"); } permissionsManager.checkActionsSupporting(domainId, invite.getActions()); try { userManager.getByEmail(invite.getEmail()); throw new ConflictException("User with specified id is already registered"); } catch (NotFoundException ignored) { } Optional<InviteImpl> existingInvite = inviteDao.store(new InviteImpl(invite)); if (!existingInvite.isPresent()) { Subject currentSubject = EnvironmentContext.getCurrent().getSubject(); eventService.publish( new InviteCreatedEvent( currentSubject.isAnonymous() ? null : currentSubject.getUserId(), invite)); } } @Inject  InviteManager(
      PermissionsManager permissionsManager,
      UserManager userManager,
      InviteDao inviteDao,
      EventService eventService); @Transactional(rollbackOn = {RuntimeException.class, ServerException.class}) void store(Invite invite); Invite getInvite(String domainId, String instanceId, String email); Page<? extends Invite> getInvites(String email, int maxItems, long skipCount); Page<? extends Invite> getInvites(
      String domainId, String instanceId, long skipCount, int maxItems); void remove(String domainId, String instanceId, String email);  }
@Test(dataProvider = "supportedDomains") public void shouldStoreInvite(String domain) throws Exception { when(userManager.getByEmail(anyString())).thenThrow(new NotFoundException("")); when(inviteDao.store(any())).thenReturn(Optional.empty()); InviteDto toStore = DtoFactory.newDto(InviteDto.class) .withDomainId(domain) .withInstanceId("instance123") .withActions(asList("read", "update")) .withEmail("user@test.com"); inviteManager.store(toStore); verify(permissionsManager).checkActionsSupporting(domain, asList("read", "update")); verify(userManager).getByEmail("user@test.com"); verify(inviteDao).store(new InviteImpl(toStore)); verify(eventService).publish(any(InviteCreatedEvent.class)); }
@Test(dataProvider = "supportedDomains") public void shouldUpdateInvite(String domain) throws Exception { when(userManager.getByEmail(anyString())).thenThrow(new NotFoundException("")); when(inviteDao.store(any())).thenReturn(Optional.of(new InviteImpl())); InviteDto toStore = DtoFactory.newDto(InviteDto.class) .withDomainId(domain) .withInstanceId("instance123") .withActions(asList("read", "update")) .withEmail("user@test.com"); inviteManager.store(toStore); verify(permissionsManager).checkActionsSupporting(domain, asList("read", "update")); verify(userManager).getByEmail("user@test.com"); verify(inviteDao).store(new InviteImpl(toStore)); verify(eventService, never()).publish(any()); }
@Test( expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "Required non-null invite" ) public void shouldThrowNPEOnStoringNullInvite() throws Exception { inviteManager.store(null); }
@Test( expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Invitations for specified domain are not supported" ) public void shouldThrowConflictExceptionOnStoringInviteWithUnsupportedDomain() throws Exception { inviteManager.store(DtoFactory.newDto(InviteDto.class).withDomainId("unsupported")); }
@Test( expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "User with specified id is already registered" ) public void shouldThrowConflictExceptionOnInvitingUserWhoIsAlreadyRegistered() throws Exception { inviteManager.store( DtoFactory.newDto(InviteDto.class) .withDomainId(OrganizationDomain.DOMAIN_ID) .withEmail("user@test.com")); }
@Test( expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Specified actions are not supported" ) public void shouldThrowConflictExceptionOnStoringInviteWithNonSupportedActions() throws Exception { doThrow(new ConflictException("Specified actions are not supported")) .when(permissionsManager) .checkActionsSupporting(any(), anyList()); inviteManager.store( DtoFactory.newDto(InviteDto.class) .withDomainId(OrganizationDomain.DOMAIN_ID) .withActions(singletonList("unsupportedAction"))); }