Search in sources :

Example 41 with ValidationResult

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

the class RoleConfigurationValidator method validate.

public void validate(PluginRoleConfig role, String pluginId) {
    try {
        ValidationResult result = authorizationExtension.validateRoleConfiguration(pluginId, role.getConfigurationAsMap(true));
        if (!result.isSuccessful()) {
            for (ValidationError error : result.getErrors()) {
                ConfigurationProperty property = role.getProperty(error.getKey());
                if (property == null) {
                    role.addNewConfiguration(error.getKey(), false);
                    property = role.getProperty(error.getKey());
                }
                property.addError(error.getKey(), error.getMessage());
            }
        }
    } catch (PluginNotFoundException e) {
        role.addError("pluginRole", String.format("Unable to validate `pluginRole` configuration, missing plugin: %s", pluginId));
    }
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) PluginNotFoundException(com.thoughtworks.go.plugin.access.PluginNotFoundException) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Example 42 with ValidationResult

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

the class PluggableTaskService method validate.

public boolean validate(final PluggableTask modifiedTask) {
    final TaskConfig configuration = new TaskConfig();
    for (ConfigurationProperty configurationProperty : modifiedTask.getConfiguration()) {
        configuration.add(new TaskConfigProperty(configurationProperty.getConfigurationKey().getName(), configurationProperty.getValue()));
    }
    final String pluginId = modifiedTask.getPluginConfiguration().getId();
    ValidationResult validationResult = taskExtension.validate(pluginId, configuration);
    final TaskPreference preference = PluggableTaskConfigStore.store().preferenceFor(pluginId);
    if (PluggableTaskConfigStore.store().hasPreferenceFor(pluginId)) {
        for (ConfigurationProperty configurationProperty : modifiedTask.getConfiguration()) {
            String key = configurationProperty.getConfigurationKey().getName();
            final Property property = preference.getConfig().get(key);
            if (property != null) {
                final Boolean required = property.getOption(Property.REQUIRED);
                if (required && StringUtils.isBlank(configurationProperty.getConfigValue()))
                    validationResult.addError(new ValidationError(property.getKey(), localizer.localize("MANDATORY_CONFIGURATION_FIELD")));
            }
        }
    }
    for (ValidationError validationError : validationResult.getErrors()) {
        modifiedTask.getConfiguration().getProperty(validationError.getKey()).addError(validationError.getKey(), validationError.getMessage());
    }
    return validationResult.isSuccessful();
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) Property(com.thoughtworks.go.plugin.api.config.Property) ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) TaskPreference(com.thoughtworks.go.plugin.access.pluggabletask.TaskPreference)

Example 43 with ValidationResult

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

the class RoleConfigCommandTest method shouldPassValidationForValidRole.

@Test
public void shouldPassValidationForValidRole() {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    PluginRoleConfig pluginRoleConfig = new PluginRoleConfig("foo", "ldap");
    cruiseConfig.server().security().addRole(pluginRoleConfig);
    cruiseConfig.server().security().securityAuthConfigs().add(new SecurityAuthConfig("ldap", "cd.go.ldap"));
    when(extension.validateRoleConfiguration(eq("cd.go.ldap"), Matchers.<Map<String, String>>any())).thenReturn(new ValidationResult());
    RoleConfigCommand command = new StubCommand(goConfigService, pluginRoleConfig, extension, currentUser, result);
    boolean isValid = command.isValid(cruiseConfig);
    assertTrue(isValid);
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 44 with ValidationResult

use of com.thoughtworks.go.plugin.api.response.validation.ValidationResult 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"), Matchers.<Map<String, String>>any())).thenReturn(validationResult);
    ElasticProfile newProfile = new ElasticProfile("foo", "aws", new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("val")));
    PluginProfileCommand command = new ElasticAgentProfileCreateCommand(mock(GoConfigService.class), newProfile, extension, null, new HttpLocalizedOperationResult());
    BasicCruiseConfig cruiseConfig = new BasicCruiseConfig();
    thrown.expect(PluginProfileNotFoundException.class);
    thrown.expectMessage("Elastic agent profile `foo` does not exist.");
    command.isValid(cruiseConfig);
    command.update(cruiseConfig);
    assertThat(newProfile.first().errors().size(), is(1));
    assertThat(newProfile.first().errors().asString(), is("error"));
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) ConfigurationValue(com.thoughtworks.go.domain.config.ConfigurationValue) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) ConfigurationKey(com.thoughtworks.go.domain.config.ConfigurationKey) 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.Test)

Example 45 with ValidationResult

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

the class PluginProfilesService method validatePluginProperties.

private void validatePluginProperties(PluginProfileCommand command, PluginProfile newPluginProfile) {
    try {
        ValidationResult result = command.validateUsingExtension(newPluginProfile.getPluginId(), newPluginProfile.getConfigurationAsMap(true));
        addErrorsToConfiguration(result, newPluginProfile);
    } catch (PluginNotFoundException e) {
        newPluginProfile.addError("pluginId", String.format("Plugin with id `%s` is not found.", newPluginProfile.getPluginId()));
    } catch (Exception e) {
    // Ignore - it will be the invalid cipher text exception for an encrypted value. This will be validated later during entity update
    }
}
Also used : PluginNotFoundException(com.thoughtworks.go.plugin.access.PluginNotFoundException) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) PluginNotFoundException(com.thoughtworks.go.plugin.access.PluginNotFoundException)

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