### Question:
StopFreeIpaHandler extends EventSenderAwareHandler<EnvironmentDto> { @Override public void accept(Event<EnvironmentDto> environmentDtoEvent) { EnvironmentDto environmentDto = environmentDtoEvent.getData(); try { if (Strings.isNullOrEmpty(environmentDto.getParentEnvironmentCrn())) { freeIpaService.describe(environmentDto.getResourceCrn()).ifPresent(freeIpa -> { if (freeIpa.getStatus() != null && !freeIpa.getStatus().isStoppable()) { throw new FreeIpaOperationFailedException("FreeIPA is not in a stoppable state! Current state is: " + freeIpa.getStatus().name()); } }); freeIpaPollerService.stopAttachedFreeipaInstances(environmentDto.getId(), environmentDto.getResourceCrn()); } EnvStopEvent envStopEvent = EnvStopEvent.EnvStopEventBuilder.anEnvStopEvent() .withSelector(EnvStopStateSelectors.FINISH_ENV_STOP_EVENT.selector()) .withResourceId(environmentDto.getId()) .withResourceName(environmentDto.getName()) .build(); eventSender().sendEvent(envStopEvent, environmentDtoEvent.getHeaders()); } catch (Exception e) { EnvStopFailedEvent failedEvent = new EnvStopFailedEvent(environmentDto, e, EnvironmentStatus.STOP_FREEIPA_FAILED); eventSender().sendEvent(failedEvent, environmentDtoEvent.getHeaders()); } } protected  StopFreeIpaHandler(EventSender eventSender, FreeIpaPollerService freeIpaPollerService, FreeIpaService freeIpaService); @Override String selector(); @Override void accept(Event<EnvironmentDto> environmentDtoEvent);  }### Answer:
@Test public void shouldStopFreeipaGivenEnvironmentWithoutParent() { EnvironmentDto environmentDto = createEnvironmentDto(); Event<EnvironmentDto> environmentDtoEvent = Event.wrap(environmentDto); underTest.accept(environmentDtoEvent); verify(freeIpaPollerService).stopAttachedFreeipaInstances(any(), any()); verifyEnvStopEvent(environmentDtoEvent); }
@Test public void shouldNotStopFreeipaGivenEnvironmentWithParent() { EnvironmentDto environmentDto = createEnvironmentDto(); environmentDto.setParentEnvironmentCrn("crn:parent"); Event<EnvironmentDto> environmentDtoEvent = Event.wrap(environmentDto); underTest.accept(environmentDtoEvent); verify(freeIpaPollerService, times(0)).stopAttachedFreeipaInstances(any(), any()); verifyEnvStopEvent(environmentDtoEvent); }
@Test public void testWhenFreeIpaDescribeTellsFreeIpaIsNotStoppableThenExceptionComesAndNoStopEventHappens() { String envCrn = "someCrnValue"; EnvironmentDto environmentDto = createEnvironmentDto(); environmentDto.setResourceCrn(envCrn); Event<EnvironmentDto> environmentDtoEvent = Event.wrap(environmentDto); when(mockDescribeFreeIpaResponse.getStatus()).thenReturn(Status.DELETED_ON_PROVIDER_SIDE); when(freeIpaService.describe(envCrn)).thenReturn(Optional.of(mockDescribeFreeIpaResponse)); underTest.accept(environmentDtoEvent); verify(freeIpaService, times(1)).describe(any()); verify(freeIpaService, times(1)).describe(envCrn); verify(eventSender, times(1)).sendEvent(any(), any()); verify(eventSender, times(1)).sendEvent(any(EnvStopFailedEvent.class), eq(environmentDtoEvent.getHeaders())); }