@Test void testValidateAuthenticationModificationWhenHasPublicKeyAndPublicKeyIdAsWell() { Environment environment = new Environment(); environment.setCloudPlatform("AWS"); EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder() .withAuthentication(AuthenticationDto.builder() .withPublicKeyId("pub-key-id") .withPublicKey("ssh-key") .build()) .build(); PublicKeyConnector connector = mock(PublicKeyConnector.class); when(environmentResourceService.isPublicKeyIdExists(environment, "pub-key-id")).thenReturn(true); when(environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform())).thenReturn(Optional.of(connector)); ValidationResult validationResult = underTest.validateAuthenticationModification(environmentEditDto, environment); assertEquals("1. You should define either publicKey or publicKeyId only", validationResult.getFormattedErrors()); }
public ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment) { ValidationResultBuilder validationResultBuilder = ValidationResult.builder(); AuthenticationDto authenticationDto = editDto.getAuthentication(); String publicKeyId = authenticationDto.getPublicKeyId(); Optional<PublicKeyConnector> publicKeyConnector = environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform()); if (publicKeyConnector.isEmpty() && isNotEmpty(publicKeyId)) { validationResultBuilder.error("The change of publicKeyId is not supported on " + environment.getCloudPlatform()); } else { String publicKey = authenticationDto.getPublicKey(); if (isNotEmpty(publicKeyId) && isNotEmpty(publicKey)) { validationResultBuilder.error("You should define either publicKey or publicKeyId only"); } if (StringUtils.isEmpty(publicKeyId) && StringUtils.isEmpty(publicKey)) { validationResultBuilder.error("You should define publicKey or publicKeyId"); } if (isNotEmpty(publicKeyId) && !environmentResourceService.isPublicKeyIdExists(environment, publicKeyId)) { validationResultBuilder.error(String.format("The publicKeyId with name of '%s' does not exists on the provider", publicKeyId)); } } return validationResultBuilder.build(); }
EnvironmentValidatorService { public ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment) { ValidationResultBuilder validationResultBuilder = ValidationResult.builder(); AuthenticationDto authenticationDto = editDto.getAuthentication(); String publicKeyId = authenticationDto.getPublicKeyId(); Optional<PublicKeyConnector> publicKeyConnector = environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform()); if (publicKeyConnector.isEmpty() && isNotEmpty(publicKeyId)) { validationResultBuilder.error("The change of publicKeyId is not supported on " + environment.getCloudPlatform()); } else { String publicKey = authenticationDto.getPublicKey(); if (isNotEmpty(publicKeyId) && isNotEmpty(publicKey)) { validationResultBuilder.error("You should define either publicKey or publicKeyId only"); } if (StringUtils.isEmpty(publicKeyId) && StringUtils.isEmpty(publicKey)) { validationResultBuilder.error("You should define publicKey or publicKeyId"); } if (isNotEmpty(publicKeyId) && !environmentResourceService.isPublicKeyIdExists(environment, publicKeyId)) { validationResultBuilder.error(String.format("The publicKeyId with name of '%s' does not exists on the provider", publicKeyId)); } } return validationResultBuilder.build(); } }
EnvironmentValidatorService { public ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment) { ValidationResultBuilder validationResultBuilder = ValidationResult.builder(); AuthenticationDto authenticationDto = editDto.getAuthentication(); String publicKeyId = authenticationDto.getPublicKeyId(); Optional<PublicKeyConnector> publicKeyConnector = environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform()); if (publicKeyConnector.isEmpty() && isNotEmpty(publicKeyId)) { validationResultBuilder.error("The change of publicKeyId is not supported on " + environment.getCloudPlatform()); } else { String publicKey = authenticationDto.getPublicKey(); if (isNotEmpty(publicKeyId) && isNotEmpty(publicKey)) { validationResultBuilder.error("You should define either publicKey or publicKeyId only"); } if (StringUtils.isEmpty(publicKeyId) && StringUtils.isEmpty(publicKey)) { validationResultBuilder.error("You should define publicKey or publicKeyId"); } if (isNotEmpty(publicKeyId) && !environmentResourceService.isPublicKeyIdExists(environment, publicKeyId)) { validationResultBuilder.error(String.format("The publicKeyId with name of '%s' does not exists on the provider", publicKeyId)); } } return validationResultBuilder.build(); } EnvironmentValidatorService(NetworkCreationValidator networkCreationValidator,
            PlatformParameterService platformParameterService,
            EnvironmentResourceService environmentResourceService,
            CredentialService credentialService,
            @Value("${environment.enabledParentPlatforms}") Set<String> enabledParentPlatforms,
            @Value("${environment.enabledChildPlatforms}") Set<String> enabledChildPlatforms); }
EnvironmentValidatorService { public ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment) { ValidationResultBuilder validationResultBuilder = ValidationResult.builder(); AuthenticationDto authenticationDto = editDto.getAuthentication(); String publicKeyId = authenticationDto.getPublicKeyId(); Optional<PublicKeyConnector> publicKeyConnector = environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform()); if (publicKeyConnector.isEmpty() && isNotEmpty(publicKeyId)) { validationResultBuilder.error("The change of publicKeyId is not supported on " + environment.getCloudPlatform()); } else { String publicKey = authenticationDto.getPublicKey(); if (isNotEmpty(publicKeyId) && isNotEmpty(publicKey)) { validationResultBuilder.error("You should define either publicKey or publicKeyId only"); } if (StringUtils.isEmpty(publicKeyId) && StringUtils.isEmpty(publicKey)) { validationResultBuilder.error("You should define publicKey or publicKeyId"); } if (isNotEmpty(publicKeyId) && !environmentResourceService.isPublicKeyIdExists(environment, publicKeyId)) { validationResultBuilder.error(String.format("The publicKeyId with name of '%s' does not exists on the provider", publicKeyId)); } } return validationResultBuilder.build(); } EnvironmentValidatorService(NetworkCreationValidator networkCreationValidator,
            PlatformParameterService platformParameterService,
            EnvironmentResourceService environmentResourceService,
            CredentialService credentialService,
            @Value("${environment.enabledParentPlatforms}") Set<String> enabledParentPlatforms,
            @Value("${environment.enabledChildPlatforms}") Set<String> enabledChildPlatforms); ValidationResultBuilder validateNetworkCreation(Environment environment, NetworkDto network); ValidationResult validateParentChildRelation(Environment environment, String parentEnvironmentName); ValidationResult validateAwsEnvironmentRequest(EnvironmentRequest environmentRequest); ValidationResult validateSecurityAccessModification(SecurityAccessDto securityAccessDto, Environment environment); ValidationResult validateSecurityGroups(EnvironmentEditDto editDto, Environment environment); ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment); ValidationResult validateFreeIpaCreation(FreeIpaCreationDto freeIpaCreation); }
EnvironmentValidatorService { public ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment) { ValidationResultBuilder validationResultBuilder = ValidationResult.builder(); AuthenticationDto authenticationDto = editDto.getAuthentication(); String publicKeyId = authenticationDto.getPublicKeyId(); Optional<PublicKeyConnector> publicKeyConnector = environmentResourceService.getPublicKeyConnector(environment.getCloudPlatform()); if (publicKeyConnector.isEmpty() && isNotEmpty(publicKeyId)) { validationResultBuilder.error("The change of publicKeyId is not supported on " + environment.getCloudPlatform()); } else { String publicKey = authenticationDto.getPublicKey(); if (isNotEmpty(publicKeyId) && isNotEmpty(publicKey)) { validationResultBuilder.error("You should define either publicKey or publicKeyId only"); } if (StringUtils.isEmpty(publicKeyId) && StringUtils.isEmpty(publicKey)) { validationResultBuilder.error("You should define publicKey or publicKeyId"); } if (isNotEmpty(publicKeyId) && !environmentResourceService.isPublicKeyIdExists(environment, publicKeyId)) { validationResultBuilder.error(String.format("The publicKeyId with name of '%s' does not exists on the provider", publicKeyId)); } } return validationResultBuilder.build(); } EnvironmentValidatorService(NetworkCreationValidator networkCreationValidator,
            PlatformParameterService platformParameterService,
            EnvironmentResourceService environmentResourceService,
            CredentialService credentialService,
            @Value("${environment.enabledParentPlatforms}") Set<String> enabledParentPlatforms,
            @Value("${environment.enabledChildPlatforms}") Set<String> enabledChildPlatforms); ValidationResultBuilder validateNetworkCreation(Environment environment, NetworkDto network); ValidationResult validateParentChildRelation(Environment environment, String parentEnvironmentName); ValidationResult validateAwsEnvironmentRequest(EnvironmentRequest environmentRequest); ValidationResult validateSecurityAccessModification(SecurityAccessDto securityAccessDto, Environment environment); ValidationResult validateSecurityGroups(EnvironmentEditDto editDto, Environment environment); ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment); ValidationResult validateFreeIpaCreation(FreeIpaCreationDto freeIpaCreation);  }