Search in sources :

Example 6 with ValidationError

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

the class ArtifactExtensionTest method shouldValidatePluggableArtifactConfig.

@Test
public void shouldValidatePluggableArtifactConfig() {
    String responseBody = "[{\"message\":\"Filename must not be blank.\",\"key\":\"Filename\"}]";
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ARTIFACT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
    ValidationResult validationResult = artifactExtension.validatePluggableArtifactConfig(PLUGIN_ID, Collections.singletonMap("Filename", ""));
    final GoPluginApiRequest request = requestArgumentCaptor.getValue();
    assertThat(request.extension(), is(ARTIFACT_EXTENSION));
    assertThat(request.requestName(), is(REQUEST_PUBLISH_ARTIFACT_VALIDATE));
    assertThat(request.requestBody(), is("{\"Filename\":\"\"}"));
    assertThat(validationResult.isSuccessful(), is(false));
    assertThat(validationResult.getErrors(), containsInAnyOrder(new ValidationError("Filename", "Filename must not be blank.")));
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) DefaultGoPluginApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 7 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError 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 8 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError 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 9 with ValidationError

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

the class PluginService method validatePluginSettingsFor.

public void validatePluginSettingsFor(PluginSettings pluginSettings) {
    String pluginId = pluginSettings.getPluginId();
    PluginSettingsConfiguration configuration = pluginSettings.toPluginSettingsConfiguration();
    GoPluginExtension extension = findExtensionWhichCanHandleSettingsFor(pluginId);
    if (extension == null)
        throw new IllegalArgumentException(String.format("Plugin '%s' does not exist or does not implement settings validation.", pluginId));
    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 10 with ValidationError

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

the class PluggableTaskServiceTest method shouldPassValidationIfAllRequiredFieldsHaveValues.

@Test
public void shouldPassValidationIfAllRequiredFieldsHaveValues() {
    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"));
    configuration.getProperty("KEY1").setConfigurationValue(new ConfigurationValue("junk"));
    PluggableTask modifiedTask = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration);
    ValidationResult validationResult = new ValidationResult();
    when(taskExtension.validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class))).thenReturn(validationResult);
    pluggableTaskService.validate(modifiedTask);
    final List<ValidationError> validationErrors = validationResult.getErrors();
    assertTrue(validationErrors.isEmpty());
}
Also used : ConfigurationValue(com.thoughtworks.go.domain.config.ConfigurationValue) Configuration(com.thoughtworks.go.domain.config.Configuration) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) Test(org.junit.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