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.")));
}
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")));
}
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();
}
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());
}
}
}
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());
}
Aggregations