@Test void testCreate() { ParametersDto parametersDto = ParametersDto.builder().withAwsParameters(AwsParametersDto.builder().withDynamoDbTableName("dynamo").build()).build(); String environmentCrn = "crn"; final EnvironmentCreationDto environmentCreationDto = EnvironmentCreationDto.builder() .withName(ENVIRONMENT_NAME) .withCreator(CRN) .withAccountId(ACCOUNT_ID) .withCrn(environmentCrn) .withAuthentication(AuthenticationDto.builder().build()) .withParameters(parametersDto) .withLocation(LocationDto.builder() .withName("test") .withDisplayName("test") .withLatitude(0.1) .withLongitude(0.1) .build()) .build(); final Environment environment = new Environment(); environment.setName(ENVIRONMENT_NAME); environment.setId(1L); environment.setAccountId(ACCOUNT_ID); Credential credential = new Credential(); credential.setCloudPlatform("platform"); when(environmentService.isNameOccupied(eq(ENVIRONMENT_NAME), eq(ACCOUNT_ID))).thenReturn(false); when(environmentDtoConverter.creationDtoToEnvironment(eq(environmentCreationDto))).thenReturn(environment); when(environmentResourceService.getCredentialFromRequest(any(), eq(ACCOUNT_ID))) .thenReturn(credential); when(validatorService.validateParentChildRelation(any(), any())).thenReturn(ValidationResult.builder().build()); when(validatorService.validateNetworkCreation(any(), any())).thenReturn(ValidationResult.builder()); when(validatorService.validateFreeIpaCreation(any())).thenReturn(ValidationResult.builder().build()); when(authenticationDtoConverter.dtoToAuthentication(any())).thenReturn(new EnvironmentAuthentication()); when(environmentService.getRegionsByEnvironment(eq(environment))).thenReturn(getCloudRegions()); when(environmentService.save(any())).thenReturn(environment); environmentCreationServiceUnderTest.create(environmentCreationDto); verify(environmentService).save(any()); verify(parametersService).saveParameters(eq(environment), eq(parametersDto)); verify(environmentResourceService).createAndSetNetwork(any(), any(), any(), any()); verify(reactorFlowManager).triggerCreationFlow(eq(1L), eq(ENVIRONMENT_NAME), eq(CRN), anyString()); }
public EnvironmentDto create(EnvironmentCreationDto creationDto) { LOGGER.info("Environment creation initiated."); if (environmentService.isNameOccupied(creationDto.getName(), creationDto.getAccountId())) { throw new BadRequestException(String.format("Environment with name '%s' already exists in account '%s'.", creationDto.getName(), creationDto.getAccountId())); } Environment environment = initializeEnvironment(creationDto); if (StringUtils.isNotEmpty(creationDto.getParentEnvironmentName())) { LOGGER.debug("Setting parent environment '{}'.", creationDto.getParentEnvironmentName()); Optional<Environment> parentEnvironment = environmentService. findByNameAndAccountIdAndArchivedIsFalse(creationDto.getParentEnvironmentName(), creationDto.getAccountId()); parentEnvironment.ifPresent(environment::setParentEnvironment); } environmentService.setSecurityAccess(environment, creationDto.getSecurityAccess()); validateCreation(creationDto, environment); try { environment = environmentService.save(environment); environmentResourceService.createAndSetNetwork(environment, creationDto.getNetwork(), creationDto.getAccountId(), getIfNotNull(creationDto.getNetwork(), NetworkDto::getSubnetMetas)); createAndSetParameters(environment, creationDto.getParameters()); environmentService.saveWithOwnerRoleAssignment(environment); reactorFlowManager.triggerCreationFlow(environment.getId(), environment.getName(), creationDto.getCreator(), environment.getResourceCrn()); } catch (Exception e) { environment.setStatus(EnvironmentStatus.CREATE_FAILED); environment.setStatusReason(e.getMessage()); environmentService.save(environment); throw e; } return environmentDtoConverter.environmentToDto(environment); }
EnvironmentCreationService { public EnvironmentDto create(EnvironmentCreationDto creationDto) { LOGGER.info("Environment creation initiated."); if (environmentService.isNameOccupied(creationDto.getName(), creationDto.getAccountId())) { throw new BadRequestException(String.format("Environment with name '%s' already exists in account '%s'.", creationDto.getName(), creationDto.getAccountId())); } Environment environment = initializeEnvironment(creationDto); if (StringUtils.isNotEmpty(creationDto.getParentEnvironmentName())) { LOGGER.debug("Setting parent environment '{}'.", creationDto.getParentEnvironmentName()); Optional<Environment> parentEnvironment = environmentService. findByNameAndAccountIdAndArchivedIsFalse(creationDto.getParentEnvironmentName(), creationDto.getAccountId()); parentEnvironment.ifPresent(environment::setParentEnvironment); } environmentService.setSecurityAccess(environment, creationDto.getSecurityAccess()); validateCreation(creationDto, environment); try { environment = environmentService.save(environment); environmentResourceService.createAndSetNetwork(environment, creationDto.getNetwork(), creationDto.getAccountId(), getIfNotNull(creationDto.getNetwork(), NetworkDto::getSubnetMetas)); createAndSetParameters(environment, creationDto.getParameters()); environmentService.saveWithOwnerRoleAssignment(environment); reactorFlowManager.triggerCreationFlow(environment.getId(), environment.getName(), creationDto.getCreator(), environment.getResourceCrn()); } catch (Exception e) { environment.setStatus(EnvironmentStatus.CREATE_FAILED); environment.setStatusReason(e.getMessage()); environmentService.save(environment); throw e; } return environmentDtoConverter.environmentToDto(environment); } }
EnvironmentCreationService { public EnvironmentDto create(EnvironmentCreationDto creationDto) { LOGGER.info("Environment creation initiated."); if (environmentService.isNameOccupied(creationDto.getName(), creationDto.getAccountId())) { throw new BadRequestException(String.format("Environment with name '%s' already exists in account '%s'.", creationDto.getName(), creationDto.getAccountId())); } Environment environment = initializeEnvironment(creationDto); if (StringUtils.isNotEmpty(creationDto.getParentEnvironmentName())) { LOGGER.debug("Setting parent environment '{}'.", creationDto.getParentEnvironmentName()); Optional<Environment> parentEnvironment = environmentService. findByNameAndAccountIdAndArchivedIsFalse(creationDto.getParentEnvironmentName(), creationDto.getAccountId()); parentEnvironment.ifPresent(environment::setParentEnvironment); } environmentService.setSecurityAccess(environment, creationDto.getSecurityAccess()); validateCreation(creationDto, environment); try { environment = environmentService.save(environment); environmentResourceService.createAndSetNetwork(environment, creationDto.getNetwork(), creationDto.getAccountId(), getIfNotNull(creationDto.getNetwork(), NetworkDto::getSubnetMetas)); createAndSetParameters(environment, creationDto.getParameters()); environmentService.saveWithOwnerRoleAssignment(environment); reactorFlowManager.triggerCreationFlow(environment.getId(), environment.getName(), creationDto.getCreator(), environment.getResourceCrn()); } catch (Exception e) { environment.setStatus(EnvironmentStatus.CREATE_FAILED); environment.setStatusReason(e.getMessage()); environmentService.save(environment); throw e; } return environmentDtoConverter.environmentToDto(environment); } EnvironmentCreationService(
            EnvironmentService environmentService,
            EnvironmentValidatorService environmentValidatorService,
            EnvironmentResourceService environmentResourceService,
            EnvironmentDtoConverter environmentDtoConverter,
            EnvironmentReactorFlowManager reactorFlowManager,
            AuthenticationDtoConverter authenticationDtoConverter,
            ParametersService parametersService,
            EntitlementService entitlementService); }
EnvironmentCreationService { public EnvironmentDto create(EnvironmentCreationDto creationDto) { LOGGER.info("Environment creation initiated."); if (environmentService.isNameOccupied(creationDto.getName(), creationDto.getAccountId())) { throw new BadRequestException(String.format("Environment with name '%s' already exists in account '%s'.", creationDto.getName(), creationDto.getAccountId())); } Environment environment = initializeEnvironment(creationDto); if (StringUtils.isNotEmpty(creationDto.getParentEnvironmentName())) { LOGGER.debug("Setting parent environment '{}'.", creationDto.getParentEnvironmentName()); Optional<Environment> parentEnvironment = environmentService. findByNameAndAccountIdAndArchivedIsFalse(creationDto.getParentEnvironmentName(), creationDto.getAccountId()); parentEnvironment.ifPresent(environment::setParentEnvironment); } environmentService.setSecurityAccess(environment, creationDto.getSecurityAccess()); validateCreation(creationDto, environment); try { environment = environmentService.save(environment); environmentResourceService.createAndSetNetwork(environment, creationDto.getNetwork(), creationDto.getAccountId(), getIfNotNull(creationDto.getNetwork(), NetworkDto::getSubnetMetas)); createAndSetParameters(environment, creationDto.getParameters()); environmentService.saveWithOwnerRoleAssignment(environment); reactorFlowManager.triggerCreationFlow(environment.getId(), environment.getName(), creationDto.getCreator(), environment.getResourceCrn()); } catch (Exception e) { environment.setStatus(EnvironmentStatus.CREATE_FAILED); environment.setStatusReason(e.getMessage()); environmentService.save(environment); throw e; } return environmentDtoConverter.environmentToDto(environment); } EnvironmentCreationService(
            EnvironmentService environmentService,
            EnvironmentValidatorService environmentValidatorService,
            EnvironmentResourceService environmentResourceService,
            EnvironmentDtoConverter environmentDtoConverter,
            EnvironmentReactorFlowManager reactorFlowManager,
            AuthenticationDtoConverter authenticationDtoConverter,
            ParametersService parametersService,
            EntitlementService entitlementService); EnvironmentDto create(EnvironmentCreationDto creationDto); }
EnvironmentCreationService { public EnvironmentDto create(EnvironmentCreationDto creationDto) { LOGGER.info("Environment creation initiated."); if (environmentService.isNameOccupied(creationDto.getName(), creationDto.getAccountId())) { throw new BadRequestException(String.format("Environment with name '%s' already exists in account '%s'.", creationDto.getName(), creationDto.getAccountId())); } Environment environment = initializeEnvironment(creationDto); if (StringUtils.isNotEmpty(creationDto.getParentEnvironmentName())) { LOGGER.debug("Setting parent environment '{}'.", creationDto.getParentEnvironmentName()); Optional<Environment> parentEnvironment = environmentService. findByNameAndAccountIdAndArchivedIsFalse(creationDto.getParentEnvironmentName(), creationDto.getAccountId()); parentEnvironment.ifPresent(environment::setParentEnvironment); } environmentService.setSecurityAccess(environment, creationDto.getSecurityAccess()); validateCreation(creationDto, environment); try { environment = environmentService.save(environment); environmentResourceService.createAndSetNetwork(environment, creationDto.getNetwork(), creationDto.getAccountId(), getIfNotNull(creationDto.getNetwork(), NetworkDto::getSubnetMetas)); createAndSetParameters(environment, creationDto.getParameters()); environmentService.saveWithOwnerRoleAssignment(environment); reactorFlowManager.triggerCreationFlow(environment.getId(), environment.getName(), creationDto.getCreator(), environment.getResourceCrn()); } catch (Exception e) { environment.setStatus(EnvironmentStatus.CREATE_FAILED); environment.setStatusReason(e.getMessage()); environmentService.save(environment); throw e; } return environmentDtoConverter.environmentToDto(environment); } EnvironmentCreationService(
            EnvironmentService environmentService,
            EnvironmentValidatorService environmentValidatorService,
            EnvironmentResourceService environmentResourceService,
            EnvironmentDtoConverter environmentDtoConverter,
            EnvironmentReactorFlowManager reactorFlowManager,
            AuthenticationDtoConverter authenticationDtoConverter,
            ParametersService parametersService,
            EntitlementService entitlementService); EnvironmentDto create(EnvironmentCreationDto creationDto);  }