Search in sources :

Example 21 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.

the class PluginService method validatePluginSettings.

void validatePluginSettings(PluginSettings pluginSettings) {
    pluginSettings.validateTree();
    if (pluginSettings.hasErrors()) {
        return;
    }
    final String pluginId = pluginSettings.getPluginId();
    final PluginSettingsConfiguration configuration = pluginSettings.toPluginSettingsConfiguration();
    final GoPluginExtension extension = findExtensionWhichCanHandleSettingsFor(pluginId);
    if (extension == null) {
        throw new IllegalArgumentException(String.format("Plugin '%s' does not exist or does not implement settings validation.", pluginId));
    }
    final ValidationResult result = extension.validatePluginSettings(pluginId, configuration);
    if (!result.isSuccessful()) {
        for (ValidationError error : result.getErrors()) {
            pluginSettings.populateErrorMessageFor(error.getKey(), error.getMessage());
        }
    }
}
Also used : GoPluginExtension(com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Example 22 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.

the class PackageDefinitionServiceTest method shouldPerformPluginValidationsBeforeValidationsByGoAndGoDoesNotAddErrorIfAlreadyPresent.

@Test
void shouldPerformPluginValidationsBeforeValidationsByGoAndGoDoesNotAddErrorIfAlreadyPresent() throws Exception {
    Configuration configuration = new Configuration();
    configuration.add(ConfigurationPropertyMother.create("required-field", false, ""));
    PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", configuration, packageRepository);
    ValidationResult expectedValidationResult = new ValidationResult();
    expectedValidationResult.addError(new ValidationError("required-field", "error-one"));
    expectedValidationResult.addError(new ValidationError("required-field", "error-two"));
    when(packageRepositoryExtension.isPackageConfigurationValid(eq(packageRepository.getPluginConfiguration().getId()), any(com.thoughtworks.go.plugin.api.material.packagerepository.PackageConfiguration.class), any(RepositoryConfiguration.class))).thenReturn(expectedValidationResult);
    service.performPluginValidationsFor(packageDefinition);
    assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value").size()).isEqualTo(2);
    assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value")).contains("error-one", "error-two");
}
Also used : Configuration(com.thoughtworks.go.domain.config.Configuration) PackageMaterialTestHelper.assertPackageConfiguration(com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration) RepositoryConfiguration(com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration) com.thoughtworks.go.plugin.access.packagematerial(com.thoughtworks.go.plugin.access.packagematerial) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) PackageMaterialTestHelper.assertPackageConfiguration(com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration) RepositoryConfiguration(com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration) Test(org.junit.jupiter.api.Test)

Example 23 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.

the class PackageDefinitionServiceTest method shouldPerformPluginValidationsUsingMetaDataBeforeSavingPackageRepository.

@Test
void shouldPerformPluginValidationsUsingMetaDataBeforeSavingPackageRepository() throws Exception {
    Configuration configuration = new Configuration();
    configuration.add(ConfigurationPropertyMother.create("required", false, ""));
    configuration.add(ConfigurationPropertyMother.create("required_secure", true, ""));
    configuration.add(ConfigurationPropertyMother.create("secure", true, ""));
    configuration.add(ConfigurationPropertyMother.create("not_required_not_secure", false, ""));
    configuration.add(ConfigurationPropertyMother.create("spec", false, "xyz?"));
    PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", configuration, packageRepository);
    ValidationResult expectedValidationResult = new ValidationResult();
    expectedValidationResult.addError(new ValidationError("spec", "invalid spec"));
    when(packageRepositoryExtension.isPackageConfigurationValid(eq(packageRepository.getPluginConfiguration().getId()), any(com.thoughtworks.go.plugin.api.material.packagerepository.PackageConfiguration.class), any(RepositoryConfiguration.class))).thenReturn(expectedValidationResult);
    service.performPluginValidationsFor(packageDefinition);
    assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value")).isEqualTo(Arrays.asList("Field: 'required' is required"));
    assertThat(packageDefinition.getConfiguration().get(1).getEncryptedConfigurationValue().errors().getAllOn("value")).isEqualTo(Arrays.asList("Field: 'required_secure' is required"));
    assertThat(packageDefinition.getConfiguration().get(2).getEncryptedConfigurationValue().errors().isEmpty()).isTrue();
    assertThat(packageDefinition.getConfiguration().get(3).getConfigurationValue().errors().isEmpty()).isTrue();
    assertThat(packageDefinition.getConfiguration().get(4).getConfigurationValue().errors().getAllOn("value")).isEqualTo(Arrays.asList("invalid spec"));
}
Also used : Configuration(com.thoughtworks.go.domain.config.Configuration) PackageMaterialTestHelper.assertPackageConfiguration(com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration) RepositoryConfiguration(com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration) com.thoughtworks.go.plugin.access.packagematerial(com.thoughtworks.go.plugin.access.packagematerial) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) PackageMaterialTestHelper.assertPackageConfiguration(com.thoughtworks.go.server.service.materials.PackageMaterialTestHelper.assertPackageConfiguration) RepositoryConfiguration(com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration) Test(org.junit.jupiter.api.Test)

Example 24 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.

the class ElasticAgentProfileCreateCommandTest method shouldInvokePluginValidationsBeforeSave.

@Test
public void shouldInvokePluginValidationsBeforeSave() throws Exception {
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("key", "error"));
    when(extension.validate(eq("aws"), anyMap())).thenReturn(validationResult);
    ElasticProfile newProfile = new ElasticProfile("foo", "prod-cluster", new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("val")));
    EntityConfigUpdateCommand command = new ElasticAgentProfileCreateCommand(mock(GoConfigService.class), newProfile, extension, null, new HttpLocalizedOperationResult());
    BasicCruiseConfig cruiseConfig = new BasicCruiseConfig();
    assertThatThrownBy(() -> command.isValid(cruiseConfig)).isInstanceOf(RecordNotFoundException.class).hasMessageContaining(EntityType.ElasticProfile.notFoundMessage(newProfile.getId()));
    command.update(cruiseConfig);
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) ConfigurationValue(com.thoughtworks.go.domain.config.ConfigurationValue) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) ConfigurationKey(com.thoughtworks.go.domain.config.ConfigurationKey) EntityConfigUpdateCommand(com.thoughtworks.go.config.commands.EntityConfigUpdateCommand) BasicCruiseConfig(com.thoughtworks.go.config.BasicCruiseConfig) ElasticProfile(com.thoughtworks.go.config.elastic.ElasticProfile) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) GoConfigService(com.thoughtworks.go.server.service.GoConfigService) Test(org.junit.jupiter.api.Test)

Example 25 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.

the class PluginServiceTest method updatePluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationFailure.

@Test
public void updatePluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationFailure() {
    setUpElasticPluginForTheTest(true);
    when(pluginDao.findPlugin(elasticAgentPluginId)).thenReturn(getPlugin(elasticAgentPluginId));
    final ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("key-1", "error-message"));
    when(pluginSettings.hasErrors()).thenReturn(true);
    when(entityHashingService.hashForEntity(any(PluginSettings.class))).thenReturn("foo");
    when(result.isSuccessful()).thenReturn(false);
    pluginService.updatePluginSettings(pluginSettings, currentUser, result, "foo");
    verify(pluginDao, times(0)).saveOrUpdate(any());
    verify(elasticAgentExtension, times(0)).notifyPluginSettingsChange(eq(elasticAgentPluginId), anyMap());
    verify(result, times(1)).unprocessableEntity("Save failed. There are errors in the plugin settings. Please fix them and resubmit.");
}
Also used : PluginSettings(com.thoughtworks.go.server.domain.PluginSettings) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationError (com.thoughtworks.go.plugin.api.response.validation.ValidationError)63 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)61 Test (org.junit.jupiter.api.Test)35 Configuration (com.thoughtworks.go.domain.config.Configuration)15 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)15 Test (org.junit.Test)14 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)13 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)9 ConfigurationValue (com.thoughtworks.go.domain.config.ConfigurationValue)7 SCM (com.thoughtworks.go.domain.scm.SCM)7 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)7 GoPluginApiRequest (com.thoughtworks.go.plugin.api.request.GoPluginApiRequest)6 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)6 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)5 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)5 ConfigurationKey (com.thoughtworks.go.domain.config.ConfigurationKey)5 PluginSettingsConfiguration (com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration)5 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)4 PluginSettings (com.thoughtworks.go.server.domain.PluginSettings)4 GoConfigService (com.thoughtworks.go.server.service.GoConfigService)4