RemoveInvitesBeforeOrganizationRemovedEventSubscriber extends CascadeEventSubscriber<BeforeOrganizationRemovedEvent> { @Override public void onCascadeEvent(BeforeOrganizationRemovedEvent event) throws Exception { String organizationId = event.getOrganization().getId(); for (Invite invite : Pages.iterate( (maxItems, skipCount) -> inviteManager.getInvites( OrganizationDomain.DOMAIN_ID, organizationId, skipCount, maxItems))) { inviteManager.remove(invite.getDomainId(), invite.getInstanceId(), invite.getEmail()); } } @Inject  RemoveInvitesBeforeOrganizationRemovedEventSubscriber(InviteManager inviteManager); @Inject void subscribe(EventService eventService); @Override void onCascadeEvent(BeforeOrganizationRemovedEvent event);  }
@Test public void shouldRemoveInvitesOnBeforeOrganizationEvent() throws Exception { OrganizationImpl toRemove = new OrganizationImpl("orgId", "orgName", null); InviteImpl invite1 = new InviteImpl( EMAIL_1, OrganizationDomain.DOMAIN_ID, ORG_ID, OrganizationDomain.getActions()); InviteImpl invite2 = new InviteImpl( EMAIL_2, OrganizationDomain.DOMAIN_ID, ORG_ID, singletonList(OrganizationDomain.CREATE_WORKSPACES)); 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 BeforeOrganizationRemovedEvent(toRemove)); verify(inviteManager, times(2)) .getInvites(eq(OrganizationDomain.DOMAIN_ID), eq(ORG_ID), anyLong(), anyInt()); verify(inviteManager).remove(OrganizationDomain.DOMAIN_ID, ORG_ID, EMAIL_1); verify(inviteManager).remove(OrganizationDomain.DOMAIN_ID, ORG_ID, EMAIL_2); }