Search in sources :

Example 26 with ValidationError

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

the class ElasticAgentExtensionV4Test method shouldValidateProfile.

@Test
public void shouldValidateProfile() {
    String responseBody = "[{\"message\":\"Url must not be blank.\",\"key\":\"Url\"},{\"message\":\"SearchBase must not be blank.\",\"key\":\"SearchBase\"}]";
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody));
    final ValidationResult result = extensionV4.validateElasticProfile(PLUGIN_ID, Collections.emptyMap());
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.getErrors(), containsInAnyOrder(new ValidationError("Url", "Url must not be blank."), new ValidationError("SearchBase", "SearchBase must not be blank.")));
    assertExtensionRequest("4.0", REQUEST_VALIDATE_PROFILE, "{}");
}
Also used : ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 27 with ValidationError

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

the class ExternalArtifactsServiceTest method shouldMapPluginValidationErrorsToConfigrationProperties.

@Test
public void shouldMapPluginValidationErrorsToConfigrationProperties() {
    Configuration configuration = new Configuration();
    configuration.add(ConfigurationPropertyMother.create("Image", false, "alpine"));
    configuration.add(ConfigurationPropertyMother.create("Tag", false, "fml"));
    pluggableArtifactConfig.setConfiguration(configuration);
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("Image", "invalid"));
    validationResult.addError(new ValidationError("Tag", "invalid"));
    ArtifactStore artifactStore = mock(ArtifactStore.class);
    when(artifactStore.getPluginId()).thenReturn(pluginId);
    when(artifactExtension.validatePluggableArtifactConfig(any(String.class), any())).thenReturn(validationResult);
    externalArtifactsService.validateExternalArtifactConfig(pluggableArtifactConfig, artifactStore, true);
    assertThat(configuration.getProperty("Image").errors().get("Image").get(0), is("invalid"));
    assertThat(configuration.getProperty("Tag").errors().get("Tag").get(0), is("invalid"));
}
Also used : Configuration(com.thoughtworks.go.domain.config.Configuration) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 28 with ValidationError

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

the class PackageDefinitionService method performPluginValidationsFor.

public void performPluginValidationsFor(final PackageDefinition packageDefinition) {
    String pluginId = packageDefinition.getRepository().getPluginConfiguration().getId();
    ValidationResult validationResult = packageRepositoryExtension.isPackageConfigurationValid(pluginId, buildPackageConfigurations(packageDefinition), buildRepositoryConfigurations(packageDefinition.getRepository()));
    for (ValidationError error : validationResult.getErrors()) {
        packageDefinition.addConfigurationErrorFor(error.getKey(), error.getMessage());
    }
    for (ConfigurationProperty configurationProperty : packageDefinition.getConfiguration()) {
        String key = configurationProperty.getConfigurationKey().getName();
        if (PackageMetadataStore.getInstance().hasOption(packageDefinition.getRepository().getPluginConfiguration().getId(), key, PackageConfiguration.REQUIRED)) {
            if (configurationProperty.getValue().isEmpty() && configurationProperty.doesNotHaveErrorsAgainstConfigurationValue()) {
                configurationProperty.addErrorAgainstConfigurationValue("Field: '" + configurationProperty.getConfigurationKey().getName() + "' is required");
            }
        }
    }
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Example 29 with ValidationError

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

the class PackageRepositoryService method performPluginValidationsFor.

void performPluginValidationsFor(final PackageRepository packageRepository) {
    if (!validatePluginId(packageRepository)) {
        return;
    }
    for (ConfigurationProperty configurationProperty : packageRepository.getConfiguration()) {
        String key = configurationProperty.getConfigurationKey().getName();
        String pluginId = packageRepository.getPluginConfiguration().getId();
        if (repositoryMetadataStore.hasOption(pluginId, key, PackageConfiguration.REQUIRED)) {
            if (configurationProperty.getValue().isEmpty()) {
                configurationProperty.addErrorAgainstConfigurationValue("This field is required");
            }
        }
    }
    ValidationResult validationResult = packageRepositoryExtension.isRepositoryConfigurationValid(packageRepository.getPluginConfiguration().getId(), populateConfiguration(packageRepository.getConfiguration()));
    for (ValidationError error : validationResult.getErrors()) {
        packageRepository.addConfigurationErrorFor(error.getKey(), error.getMessage());
    }
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Example 30 with ValidationError

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

the class PluggableScmService method validate.

public void validate(final SCM scmConfig) {
    secretParamResolver.resolve(scmConfig);
    final String pluginId = scmConfig.getPluginConfiguration().getId();
    final SCMPropertyConfiguration configuration = getScmPropertyConfiguration(scmConfig);
    ValidationResult validationResult = scmExtension.isSCMConfigurationValid(pluginId, configuration);
    if (SCMMetadataStore.getInstance().hasPreferenceFor(pluginId)) {
        SCMConfigurations configurationMetadata = SCMMetadataStore.getInstance().getConfigurationMetadata(pluginId);
        for (SCMConfiguration scmConfiguration : configurationMetadata.list()) {
            String key = scmConfiguration.getKey();
            boolean isRequired = SCMMetadataStore.getInstance().hasOption(pluginId, key, Property.REQUIRED);
            ConfigurationProperty property = scmConfig.getConfiguration().getProperty(key);
            String configValue = property == null ? null : property.getValue();
            if (isRequired && StringUtils.isBlank(configValue)) {
                validationResult.addError(new ValidationError(key, "This field is required"));
            }
        }
    }
    for (ValidationError validationError : validationResult.getErrors()) {
        ConfigurationProperty property = scmConfig.getConfiguration().getProperty(validationError.getKey());
        if (property != null) {
            property.addError(validationError.getKey(), validationError.getMessage());
        }
    }
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

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