### Question:
ServerRemoveHandler implements EventHandler<RemoveServersRequest> { @Override public void accept(Event<RemoveServersRequest> event) { RemoveServersRequest request = event.getData(); try { Set<String> hosts = request.getHosts(); LOGGER.debug("Removing servers [{}]", hosts); Pair<Set<String>, Map<String, String>> removeServersResult = cleanupService.removeServers(request.getResourceId(), hosts); RemoveServersResponse response = new RemoveServersResponse(request, removeServersResult.getFirst(), removeServersResult.getSecond()); eventBus.notify(response.getServerCleanupFailed().isEmpty() ? EventSelectorUtil.selector(RemoveServersResponse.class) : EventSelectorUtil.failureSelector(RemoveServersResponse.class), new Event<>(event.getHeaders(), response)); } catch (FreeIpaClientException e) { LOGGER.error("Removing failed for servers: [{}]", request.getHosts(), e); Map<String, String> failureResult = request.getHosts().stream().collect(Collectors.toMap(h -> h, h -> e.getMessage())); RemoveServersResponse response = new RemoveServersResponse(request, Collections.emptySet(), failureResult); eventBus.notify(EventSelectorUtil.failureSelector(RemoveServersResponse.class), new Event<>(event.getHeaders(), response)); } }  @Override String selector(); @Override void accept(Event<RemoveServersRequest> event);  }### Answer:
@Test void testSendsSuccessMessageWhenAllServersSucceed() throws FreeIpaClientException { CleanupEvent cleanupEvent = new CleanupEvent(STACK_ID, USERS, HOSTS, ROLES, IPS, STATES_TO_SKIP, ACCOUNT_ID, OPERATION_ID, CLUSTER_NAME, ENVIRONMENT_CRN); RemoveServersRequest request = new RemoveServersRequest(cleanupEvent); Set<String> successList = HOSTS; Map<String, String> errorMap = Map.of(); when(cleanupService.removeServers(any(), any())).thenReturn(Pair.of(successList, errorMap)); underTest.accept(new Event<>(request)); verify(cleanupService, times(1)).removeServers(eq(STACK_ID), eq(HOSTS)); verify(eventBus).notify(eq("REMOVESERVERSRESPONSE"), ArgumentMatchers.<Event>any()); }
@Test void testSendsFailureMessageWhenSomeServersFail() throws FreeIpaClientException { CleanupEvent cleanupEvent = new CleanupEvent(STACK_ID, USERS, HOSTS, ROLES, IPS, STATES_TO_SKIP, ACCOUNT_ID, OPERATION_ID, CLUSTER_NAME, ENVIRONMENT_CRN); RemoveServersRequest request = new RemoveServersRequest(cleanupEvent); Set<String> successList = Set.of("example1.com"); Map<String, String> errorMap = Map.of("example2.com", "expected exception"); when(cleanupService.removeServers(any(), any())).thenReturn(Pair.of(successList, errorMap)); underTest.accept(new Event<>(request)); verify(cleanupService, times(1)).removeServers(eq(STACK_ID), eq(HOSTS)); verify(eventBus).notify(eq("REMOVESERVERSRESPONSE_ERROR"), ArgumentMatchers.<Event>any()); }
@Test void testSendsFailureMessageWhenExceptionIsThrown() throws FreeIpaClientException { CleanupEvent cleanupEvent = new CleanupEvent(STACK_ID, USERS, HOSTS, ROLES, IPS, STATES_TO_SKIP, ACCOUNT_ID, OPERATION_ID, CLUSTER_NAME, ENVIRONMENT_CRN); RemoveServersRequest request = new RemoveServersRequest(cleanupEvent); when(cleanupService.removeServers(any(), any())).thenThrow(new FreeIpaClientException("expected exception")); underTest.accept(new Event<>(request)); verify(cleanupService, times(1)).removeServers(eq(STACK_ID), eq(HOSTS)); verify(eventBus).notify(eq("REMOVESERVERSRESPONSE_ERROR"), ArgumentMatchers.<Event>any()); }