### Question:
EnvironmentStopService { public void stopByCrn(String crn) { String accountId = ThreadBasedUserCrnProvider.getAccountId(); EnvironmentDto environment = environmentService.getByCrnAndAccountId(crn, accountId); stop(environment, accountId); } EnvironmentStopService(EnvironmentReactorFlowManager reactorFlowManager,
            EnvironmentService environmentService); void stopByCrn(String crn); void stopByName(String name);  }### Answer:
@Test public void shouldStopEnvWithoutChildren() { EnvironmentDto environmentDto = getEnvironmentDto(); when(environmentService.getByCrnAndAccountId(ENV_CRN, ACCOUNT_ID)).thenReturn(environmentDto); when(environmentService.findAllByAccountIdAndParentEnvIdAndArchivedIsFalse(ACCOUNT_ID, ENV_ID)).thenReturn(Lists.emptyList()); ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.stopByCrn(ENV_CRN)); verify(reactorFlowManager).triggerStopFlow(ENV_ID, ENV_NAME, USER_CRN); }
@Test public void shouldStopEnvWithOnlyStoppedChildren() { EnvironmentDto environmentDto = getEnvironmentDto(); when(environmentService.getByCrnAndAccountId(ENV_CRN, ACCOUNT_ID)).thenReturn(environmentDto); Environment childEnvironment = new Environment(); childEnvironment.setStatus(EnvironmentStatus.ENV_STOPPED); when(environmentService.findAllByAccountIdAndParentEnvIdAndArchivedIsFalse(ACCOUNT_ID, ENV_ID)).thenReturn(List.of(childEnvironment)); ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.stopByCrn(ENV_CRN)); verify(reactorFlowManager).triggerStopFlow(ENV_ID, ENV_NAME, USER_CRN); }
@Test public void shouldThrowBadRequestExceptionGivenEnvHasRunningChildren() { EnvironmentDto environmentDto = getEnvironmentDto(); when(environmentService.getByCrnAndAccountId(ENV_CRN, ACCOUNT_ID)).thenReturn(environmentDto); Environment childEnvironment = new Environment(); childEnvironment.setStatus(EnvironmentStatus.AVAILABLE); childEnvironment.setName("Child-env-name"); when(environmentService.findAllByAccountIdAndParentEnvIdAndArchivedIsFalse(ACCOUNT_ID, ENV_ID)).thenReturn(List.of(childEnvironment)); Assertions.assertThatThrownBy(() -> ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.stopByCrn(ENV_CRN))) .isInstanceOf(BadRequestException.class) .hasMessageContaining(childEnvironment.getName()); }
@Test public void shouldThrowBadRequestExceptionGivenFreeIpaIsRunningOnSpotInstances() { EnvironmentDto environmentDto = getEnvironmentDto(); environmentDto.setFreeIpaCreation(FreeIpaCreationDto.builder() .withAws(FreeIpaCreationAwsParametersDto.builder() .withSpot(FreeIpaCreationAwsSpotParametersDto.builder() .withPercentage(100) .build()) .build()) .build()); when(environmentService.getByCrnAndAccountId(ENV_CRN, ACCOUNT_ID)).thenReturn(environmentDto); when(environmentService.findAllByAccountIdAndParentEnvIdAndArchivedIsFalse(ACCOUNT_ID, ENV_ID)).thenReturn(Lists.emptyList()); Assertions.assertThatThrownBy(() -> ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> underTest.stopByCrn(ENV_CRN))) .isInstanceOf(BadRequestException.class) .hasMessage("Environment [name] can not be stopped because FreeIpa is running on spot instances."); }