### Question:
RemoveHostsHandler implements EventHandler<RemoveHostsFromOrchestrationRequest> { @Override public void accept(Event<RemoveHostsFromOrchestrationRequest> removeHostsRequestEvent) { RemoveHostsFromOrchestrationRequest request = removeHostsRequestEvent.getData(); Set<String> hostNames = request.getHosts(); Selectable result; try { if (!hostNames.isEmpty()) { Stack stack = stackService.getByIdWithListsInTransaction(request.getResourceId()); PollingResult orchestratorRemovalPollingResult = removeHostsFromOrchestrator(stack, new ArrayList<>(hostNames)); if (!PollingResult.isSuccess(orchestratorRemovalPollingResult)) { LOGGER.warn("Can not remove hosts from orchestrator: {}", hostNames); } List<String> remainingInstanceIds = stack.getNotDeletedInstanceMetaDataList().stream() .filter(metadata -> Objects.nonNull(metadata.getDiscoveryFQDN())) .filter(metadata -> !hostNames.contains(metadata.getDiscoveryFQDN())) .map(InstanceMetaData::getInstanceId) .collect(Collectors.toList()); bootstrapService.bootstrap(stack.getId(), remainingInstanceIds); } result = new RemoveHostsFromOrchestrationSuccess(request.getResourceId()); } catch (Exception e) { LOGGER.error("Failed to remove hosts from orchestration", e); result = new DownscaleFailureEvent(REMOVE_HOSTS_FROM_ORCHESTRATION_FAILED_EVENT.event(), request.getResourceId(), "Removing host from orchestration", Set.of(), Map.of(), e); } eventBus.notify(result.selector(), new Event<>(removeHostsRequestEvent.getHeaders(), result)); }  @Override String selector(); @Override void accept(Event<RemoveHostsFromOrchestrationRequest> removeHostsRequestEvent);  }### Answer:
@Test void testSendsSuccessMessageWhenAllServersSucceed() throws Exception { CleanupEvent cleanupEvent = new CleanupEvent(STACK_ID, USERS, HOSTS, ROLES, IPS, STATES_TO_SKIP, ACCOUNT_ID, OPERATION_ID, CLUSTER_NAME, ENVIRONMENT_CRN); RemoveHostsFromOrchestrationRequest request = new RemoveHostsFromOrchestrationRequest(cleanupEvent); Stack stack = new Stack(); when(stackService.getByIdWithListsInTransaction(any())).thenReturn(stack); underTest.accept(new Event<>(request)); verify(eventBus, times(1)).notify(eq("REMOVEHOSTSFROMORCHESTRATIONSUCCESS"), ArgumentMatchers.<Event>any()); verify(bootstrapService).bootstrap(any(), any()); verify(hostOrchestrator).tearDown(any(), any(), any(), any()); }
@Test void testSendsFailureMessageWhenExceptionIsThrown() { CleanupEvent cleanupEvent = new CleanupEvent(STACK_ID, USERS, HOSTS, ROLES, IPS, STATES_TO_SKIP, ACCOUNT_ID, OPERATION_ID, CLUSTER_NAME, ENVIRONMENT_CRN); RemoveHostsFromOrchestrationRequest request = new RemoveHostsFromOrchestrationRequest(cleanupEvent); when(stackService.getByIdWithListsInTransaction(any())).thenThrow(new RuntimeException("expected exception")); underTest.accept(new Event<>(request)); verify(eventBus, times(1)).notify(eq("REMOVE_HOSTS_FROM_ORCHESTRATION_FAILED_EVENT"), ArgumentMatchers.<Event>any()); }
@Test void testSendsSuccessMessageWhenNoHostnamesAreProvided() throws Exception { CleanupEvent cleanupEvent = new CleanupEvent(STACK_ID, USERS, Set.of(), ROLES, IPS, STATES_TO_SKIP, ACCOUNT_ID, OPERATION_ID, CLUSTER_NAME, ENVIRONMENT_CRN); RemoveHostsFromOrchestrationRequest request = new RemoveHostsFromOrchestrationRequest(cleanupEvent); underTest.accept(new Event<>(request)); verify(eventBus, times(1)).notify(eq("REMOVEHOSTSFROMORCHESTRATIONSUCCESS"), ArgumentMatchers.<Event>any()); }