Search in sources :

Example 36 with ValidationResult

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

the class PluginServiceTest method shouldUpdatePluginSettingsWithErrorsIfExists.

@Test
public void shouldUpdatePluginSettingsWithErrorsIfExists() {
    PluginSettingsMetadataStore.getInstance().addMetadataFor("plugin-id-4", PluginConstants.NOTIFICATION_EXTENSION, null, null);
    when(notificationExtension.canHandlePlugin("plugin-id-4")).thenReturn(true);
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("p4-k1", "m1"));
    validationResult.addError(new ValidationError("p4-k3", "m3"));
    when(notificationExtension.validatePluginSettings(eq("plugin-id-4"), any(PluginSettingsConfiguration.class))).thenReturn(validationResult);
    Map<String, String> parameterMap = new HashMap<>();
    parameterMap.put("p4-k1", "v1");
    parameterMap.put("p4-k2", "v2");
    parameterMap.put("p4-k3", "v3");
    PluginSettings pluginSettings = new PluginSettings("plugin-id-4");
    pluginSettings.populateSettingsMap(parameterMap);
    pluginService.validatePluginSettingsFor(pluginSettings);
    assertThat(pluginSettings.hasErrors(), is(true));
    assertThat(pluginSettings.getErrorFor("p4-k1"), is(Arrays.asList("m1")));
    assertThat(pluginSettings.getErrorFor("p4-k3"), is(Arrays.asList("m3")));
}
Also used : PluginSettings(com.thoughtworks.go.server.domain.PluginSettings) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 37 with ValidationResult

use of com.thoughtworks.go.plugin.api.response.validation.ValidationResult 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(localizer.localize("MANDATORY_CONFIGURATION_FIELD_WITH_NAME", configurationProperty.getConfigurationKey().getName()));
            }
        }
    }
}
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 38 with ValidationResult

use of com.thoughtworks.go.plugin.api.response.validation.ValidationResult 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(localizer.localize("MANDATORY_CONFIGURATION_FIELD"));
            }
        }
    }
    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 39 with ValidationResult

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

the class PluggableScmService method validate.

public void validate(final SCM 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, localizer.localize("MANDATORY_CONFIGURATION_FIELD")));
            }
        }
    }
    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)

Example 40 with ValidationResult

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

the class PluggableScmService method isValid.

public boolean isValid(final SCM scmConfig) {
    if (!scmConfig.doesPluginExist()) {
        throw new RuntimeException(String.format("Plugin with id '%s' is not found.", scmConfig.getPluginConfiguration().getId()));
    }
    ValidationResult validationResult = scmExtension.isSCMConfigurationValid(scmConfig.getPluginConfiguration().getId(), getScmPropertyConfiguration(scmConfig));
    addErrorsToConfiguration(validationResult, scmConfig);
    return validationResult.isSuccessful();
}
Also used : ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Aggregations

ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)85 Test (org.junit.Test)69 ValidationError (com.thoughtworks.go.plugin.api.response.validation.ValidationError)43 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)17 Configuration (com.thoughtworks.go.domain.config.Configuration)14 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)11 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)11 PluginSettingsConfiguration (com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration)10 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)10 PluginSettings (com.thoughtworks.go.server.domain.PluginSettings)9 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)7 ConfigurationValue (com.thoughtworks.go.domain.config.ConfigurationValue)7 SCM (com.thoughtworks.go.domain.scm.SCM)7 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 RepositoryConfiguration (com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration)5 GoPluginApiRequest (com.thoughtworks.go.plugin.api.request.GoPluginApiRequest)5 Username (com.thoughtworks.go.server.domain.Username)5 ConfigurationKey (com.thoughtworks.go.domain.config.ConfigurationKey)4 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)3