RemoveInvitesBeforeWorkspaceRemovedEventSubscriber extends CascadeEventSubscriber<BeforeWorkspaceRemovedEvent> { @Override public void onCascadeEvent(BeforeWorkspaceRemovedEvent event) throws Exception { String workspaceId = event.getWorkspace().getId(); for (Invite invite : Pages.iterate( (maxItems, skipCount) -> inviteManager.getInvites( WorkspaceDomain.DOMAIN_ID, workspaceId, skipCount, maxItems))) { inviteManager.remove(invite.getDomainId(), invite.getInstanceId(), invite.getEmail()); } } @Inject  RemoveInvitesBeforeWorkspaceRemovedEventSubscriber(InviteManager inviteManager); @Inject void subscribe(EventService eventService); @Override void onCascadeEvent(BeforeWorkspaceRemovedEvent event);  }
@Test public void shouldRemoveInvitesOnBeforeWorkspaceEvent() throws Exception { WorkspaceImpl toRemove = new WorkspaceImpl(WS_ID, null, null); InviteImpl invite1 = new InviteImpl( EMAIL_1, WorkspaceDomain.DOMAIN_ID, WS_ID, singletonList(WorkspaceDomain.DELETE)); InviteImpl invite2 = new InviteImpl( EMAIL_2, WorkspaceDomain.DOMAIN_ID, WS_ID, singletonList(WorkspaceDomain.CONFIGURE)); doReturn(new Page<>(singletonList(invite1), 0, 1, 2)) .doReturn(new Page<>(singletonList(invite2), 1, 1, 2)) .when(inviteManager) .getInvites(anyString(), anyString(), anyLong(), anyInt()); subscriber.onCascadeEvent(new BeforeWorkspaceRemovedEvent(toRemove)); verify(inviteManager, times(2)) .getInvites(eq(WorkspaceDomain.DOMAIN_ID), eq(WS_ID), anyLong(), anyInt()); verify(inviteManager).remove(WorkspaceDomain.DOMAIN_ID, WS_ID, EMAIL_1); verify(inviteManager).remove(WorkspaceDomain.DOMAIN_ID, WS_ID, EMAIL_2); }