InviteToPermissionsConverter implements EventSubscriber<UserCreatedEvent> { @Override public void onEvent(UserCreatedEvent event) { final String createdUserId = event.getUser().getId(); final String email = event.getUser().getEmail(); try { for (Invite invite : Pages.iterate( (maxItems, skipCount) -> inviteManager.getInvites(email, maxItems, skipCount))) { try { permissionsManager.storePermission( DtoFactory.newDto(PermissionsDto.class) .withUserId(createdUserId) .withDomainId(invite.getDomainId()) .withInstanceId(invite.getInstanceId()) .withActions(invite.getActions())); inviteManager.remove(invite.getDomainId(), invite.getInstanceId(), email); } catch (ServerException | ConflictException | NotFoundException e) { LOG.warn( format( "Error while accepting invite for user with id '%s' in %s with id '%s'", event.getUser().getId(), invite.getDomainId(), invite.getInstanceId()), e); } } } catch (ServerException e) { LOG.warn( format("Error while fetching invites for user with id '%s'", event.getUser().getId()), e); } } @Inject  InviteToPermissionsConverter(
      InviteManager inviteManager, PermissionsManager permissionsManager); @Inject void subscribe(EventService eventService); @Override void onEvent(UserCreatedEvent event);  }
@Test public void shouldConvertInvitesToPermissionsWhenUserCreated() throws Exception { InviteImpl invite1 = new InviteImpl( USER_EMAIL, OrganizationDomain.DOMAIN_ID, "org123", asList("read", "update")); InviteImpl invite2 = new InviteImpl(USER_EMAIL, WorkspaceDomain.DOMAIN_ID, "ws321", singletonList("delete")); Permissions expectedPermission1 = convert(invite1); Permissions expectedPermission2 = convert(invite2); doReturn(new Page<>(singletonList(invite1), 0, 1, 2)) .doReturn(new Page<>(singletonList(invite2), 1, 1, 2)) .when(inviteManager) .getInvites(anyString(), anyInt(), anyLong()); converter.onEvent( new UserCreatedEvent( DtoFactory.newDto(UserDto.class).withEmail(USER_EMAIL).withId(USER_ID))); verify(inviteManager, times(2)).getInvites(eq(USER_EMAIL), anyInt(), anyLong()); verify(inviteManager).remove(invite1.getDomainId(), invite1.getInstanceId(), USER_EMAIL); verify(inviteManager).remove(invite2.getDomainId(), invite2.getInstanceId(), USER_EMAIL); verify(permissionsManager).storePermission(expectedPermission1); verify(permissionsManager).storePermission(expectedPermission2); }
@Test public void shouldSkipInviteWhenExceptionOccursOnInviteConverting() throws Exception { InviteImpl invite1 = new InviteImpl( USER_EMAIL, OrganizationDomain.DOMAIN_ID, "org123", asList("read", "update")); InviteImpl invite2 = new InviteImpl(USER_EMAIL, WorkspaceDomain.DOMAIN_ID, "ws321", singletonList("delete")); Permissions expectedPermission2 = convert(invite2); doReturn(new Page<>(singletonList(invite1), 0, 1, 2)) .doReturn(new Page<>(singletonList(invite2), 1, 1, 2)) .when(inviteManager) .getInvites(anyString(), anyInt(), anyLong()); doThrow(new ConflictException("")).doNothing().when(permissionsManager).storePermission(any()); converter.onEvent( new UserCreatedEvent( DtoFactory.newDto(UserDto.class).withEmail(USER_EMAIL).withId(USER_ID))); verify(inviteManager, times(2)).getInvites(eq(USER_EMAIL), anyInt(), anyLong()); verify(inviteManager).remove(invite2.getDomainId(), invite2.getInstanceId(), USER_EMAIL); verify(permissionsManager).storePermission(expectedPermission2); }