@Test void testEditAuthenticationIfChangedWhenHasValidationError() { AuthenticationDto authenticationDto = AuthenticationDto.builder().build(); EnvironmentEditDto environmentEditDto = EnvironmentEditDto.builder().withAuthentication(authenticationDto).build(); Environment environment = new Environment(); ValidationResult validationResult = ValidationResult.builder().error("Error").build(); when(environmentService.getValidatorService()).thenReturn(validatorService); when(validatorService.validateAuthenticationModification(environmentEditDto, environment)).thenReturn(validationResult); BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> environmentModificationServiceUnderTest.editAuthenticationIfChanged(environmentEditDto, environment)); assertEquals(badRequestException.getMessage(), "1. Error"); }
@VisibleForTesting void editAuthenticationIfChanged(EnvironmentEditDto editDto, Environment environment) { AuthenticationDto authenticationDto = editDto.getAuthentication(); if (authenticationDto != null) { EnvironmentValidatorService validatorService = environmentService.getValidatorService(); ValidationResult validationResult = validatorService.validateAuthenticationModification(editDto, environment); if (validationResult.hasError()) { throw new BadRequestException(validationResult.getFormattedErrors()); } EnvironmentAuthentication originalAuthentication = environment.getAuthentication(); environment.setAuthentication(authenticationDtoConverter.dtoToAuthentication(authenticationDto)); boolean cleanupOldSshKey = true; if (StringUtils.isNotEmpty(authenticationDto.getPublicKey())) { cleanupOldSshKey = environmentResourceService.createAndUpdateSshKey(environment); } if (cleanupOldSshKey) { String oldSshKeyId = originalAuthentication.getPublicKeyId(); LOGGER.info("The '{}' of ssh key is replaced with {}", oldSshKeyId, environment.getAuthentication().getPublicKeyId()); if (originalAuthentication.isManagedKey()) { environmentResourceService.deletePublicKey(environment, oldSshKeyId); } } else { LOGGER.info("Create and update was not run successfully. We are reverting the authentication to the previous version"); environment.setAuthentication(originalAuthentication); } } }
EnvironmentModificationService { @VisibleForTesting void editAuthenticationIfChanged(EnvironmentEditDto editDto, Environment environment) { AuthenticationDto authenticationDto = editDto.getAuthentication(); if (authenticationDto != null) { EnvironmentValidatorService validatorService = environmentService.getValidatorService(); ValidationResult validationResult = validatorService.validateAuthenticationModification(editDto, environment); if (validationResult.hasError()) { throw new BadRequestException(validationResult.getFormattedErrors()); } EnvironmentAuthentication originalAuthentication = environment.getAuthentication(); environment.setAuthentication(authenticationDtoConverter.dtoToAuthentication(authenticationDto)); boolean cleanupOldSshKey = true; if (StringUtils.isNotEmpty(authenticationDto.getPublicKey())) { cleanupOldSshKey = environmentResourceService.createAndUpdateSshKey(environment); } if (cleanupOldSshKey) { String oldSshKeyId = originalAuthentication.getPublicKeyId(); LOGGER.info("The '{}' of ssh key is replaced with {}", oldSshKeyId, environment.getAuthentication().getPublicKeyId()); if (originalAuthentication.isManagedKey()) { environmentResourceService.deletePublicKey(environment, oldSshKeyId); } } else { LOGGER.info("Create and update was not run successfully. We are reverting the authentication to the previous version"); environment.setAuthentication(originalAuthentication); } } } }
EnvironmentModificationService { @VisibleForTesting void editAuthenticationIfChanged(EnvironmentEditDto editDto, Environment environment) { AuthenticationDto authenticationDto = editDto.getAuthentication(); if (authenticationDto != null) { EnvironmentValidatorService validatorService = environmentService.getValidatorService(); ValidationResult validationResult = validatorService.validateAuthenticationModification(editDto, environment); if (validationResult.hasError()) { throw new BadRequestException(validationResult.getFormattedErrors()); } EnvironmentAuthentication originalAuthentication = environment.getAuthentication(); environment.setAuthentication(authenticationDtoConverter.dtoToAuthentication(authenticationDto)); boolean cleanupOldSshKey = true; if (StringUtils.isNotEmpty(authenticationDto.getPublicKey())) { cleanupOldSshKey = environmentResourceService.createAndUpdateSshKey(environment); } if (cleanupOldSshKey) { String oldSshKeyId = originalAuthentication.getPublicKeyId(); LOGGER.info("The '{}' of ssh key is replaced with {}", oldSshKeyId, environment.getAuthentication().getPublicKeyId()); if (originalAuthentication.isManagedKey()) { environmentResourceService.deletePublicKey(environment, oldSshKeyId); } } else { LOGGER.info("Create and update was not run successfully. We are reverting the authentication to the previous version"); environment.setAuthentication(originalAuthentication); } } } EnvironmentModificationService(EnvironmentDtoConverter environmentDtoConverter, EnvironmentService environmentService,
            CredentialService credentialService, NetworkService networkService, AuthenticationDtoConverter authenticationDtoConverter,
            ParametersService parametersService, EnvironmentFlowValidatorService environmentFlowValidatorService,
            EnvironmentResourceService environmentResourceService); }
EnvironmentModificationService { @VisibleForTesting void editAuthenticationIfChanged(EnvironmentEditDto editDto, Environment environment) { AuthenticationDto authenticationDto = editDto.getAuthentication(); if (authenticationDto != null) { EnvironmentValidatorService validatorService = environmentService.getValidatorService(); ValidationResult validationResult = validatorService.validateAuthenticationModification(editDto, environment); if (validationResult.hasError()) { throw new BadRequestException(validationResult.getFormattedErrors()); } EnvironmentAuthentication originalAuthentication = environment.getAuthentication(); environment.setAuthentication(authenticationDtoConverter.dtoToAuthentication(authenticationDto)); boolean cleanupOldSshKey = true; if (StringUtils.isNotEmpty(authenticationDto.getPublicKey())) { cleanupOldSshKey = environmentResourceService.createAndUpdateSshKey(environment); } if (cleanupOldSshKey) { String oldSshKeyId = originalAuthentication.getPublicKeyId(); LOGGER.info("The '{}' of ssh key is replaced with {}", oldSshKeyId, environment.getAuthentication().getPublicKeyId()); if (originalAuthentication.isManagedKey()) { environmentResourceService.deletePublicKey(environment, oldSshKeyId); } } else { LOGGER.info("Create and update was not run successfully. We are reverting the authentication to the previous version"); environment.setAuthentication(originalAuthentication); } } } EnvironmentModificationService(EnvironmentDtoConverter environmentDtoConverter, EnvironmentService environmentService,
            CredentialService credentialService, NetworkService networkService, AuthenticationDtoConverter authenticationDtoConverter,
            ParametersService parametersService, EnvironmentFlowValidatorService environmentFlowValidatorService,
            EnvironmentResourceService environmentResourceService); EnvironmentDto editByName(String environmentName, EnvironmentEditDto editDto); EnvironmentDto editByCrn(String crn, EnvironmentEditDto editDto); EnvironmentDto changeCredentialByEnvironmentName(String accountId, String environmentName, EnvironmentChangeCredentialDto dto); EnvironmentDto changeCredentialByEnvironmentCrn(String accountId, String crn, EnvironmentChangeCredentialDto dto); EnvironmentDto changeTelemetryFeaturesByEnvironmentName(String accountId, String environmentName,
            EnvironmentFeatures features); EnvironmentDto changeTelemetryFeaturesByEnvironmentCrn(String accountId, String crn,
            EnvironmentFeatures features); }
EnvironmentModificationService { @VisibleForTesting void editAuthenticationIfChanged(EnvironmentEditDto editDto, Environment environment) { AuthenticationDto authenticationDto = editDto.getAuthentication(); if (authenticationDto != null) { EnvironmentValidatorService validatorService = environmentService.getValidatorService(); ValidationResult validationResult = validatorService.validateAuthenticationModification(editDto, environment); if (validationResult.hasError()) { throw new BadRequestException(validationResult.getFormattedErrors()); } EnvironmentAuthentication originalAuthentication = environment.getAuthentication(); environment.setAuthentication(authenticationDtoConverter.dtoToAuthentication(authenticationDto)); boolean cleanupOldSshKey = true; if (StringUtils.isNotEmpty(authenticationDto.getPublicKey())) { cleanupOldSshKey = environmentResourceService.createAndUpdateSshKey(environment); } if (cleanupOldSshKey) { String oldSshKeyId = originalAuthentication.getPublicKeyId(); LOGGER.info("The '{}' of ssh key is replaced with {}", oldSshKeyId, environment.getAuthentication().getPublicKeyId()); if (originalAuthentication.isManagedKey()) { environmentResourceService.deletePublicKey(environment, oldSshKeyId); } } else { LOGGER.info("Create and update was not run successfully. We are reverting the authentication to the previous version"); environment.setAuthentication(originalAuthentication); } } } EnvironmentModificationService(EnvironmentDtoConverter environmentDtoConverter, EnvironmentService environmentService,
            CredentialService credentialService, NetworkService networkService, AuthenticationDtoConverter authenticationDtoConverter,
            ParametersService parametersService, EnvironmentFlowValidatorService environmentFlowValidatorService,
            EnvironmentResourceService environmentResourceService); EnvironmentDto editByName(String environmentName, EnvironmentEditDto editDto); EnvironmentDto editByCrn(String crn, EnvironmentEditDto editDto); EnvironmentDto changeCredentialByEnvironmentName(String accountId, String environmentName, EnvironmentChangeCredentialDto dto); EnvironmentDto changeCredentialByEnvironmentCrn(String accountId, String crn, EnvironmentChangeCredentialDto dto); EnvironmentDto changeTelemetryFeaturesByEnvironmentName(String accountId, String environmentName,
            EnvironmentFeatures features); EnvironmentDto changeTelemetryFeaturesByEnvironmentCrn(String accountId, String crn,
            EnvironmentFeatures features);  }