use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.
the class PluginService method validatePluginSettings.
void validatePluginSettings(PluginSettings pluginSettings) {
pluginSettings.validateTree();
if (pluginSettings.hasErrors()) {
return;
}
final String pluginId = pluginSettings.getPluginId();
final PluginSettingsConfiguration configuration = pluginSettings.toPluginSettingsConfiguration();
final GoPluginExtension extension = findExtensionWhichCanHandleSettingsFor(pluginId);
if (extension == null) {
throw new IllegalArgumentException(String.format("Plugin '%s' does not exist or does not implement settings validation.", pluginId));
}
final 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 PackageDefinitionServiceTest method shouldPerformPluginValidationsBeforeValidationsByGoAndGoDoesNotAddErrorIfAlreadyPresent.
@Test
void shouldPerformPluginValidationsBeforeValidationsByGoAndGoDoesNotAddErrorIfAlreadyPresent() throws Exception {
Configuration configuration = new Configuration();
configuration.add(ConfigurationPropertyMother.create("required-field", false, ""));
PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", configuration, packageRepository);
ValidationResult expectedValidationResult = new ValidationResult();
expectedValidationResult.addError(new ValidationError("required-field", "error-one"));
expectedValidationResult.addError(new ValidationError("required-field", "error-two"));
when(packageRepositoryExtension.isPackageConfigurationValid(eq(packageRepository.getPluginConfiguration().getId()), any(com.thoughtworks.go.plugin.api.material.packagerepository.PackageConfiguration.class), any(RepositoryConfiguration.class))).thenReturn(expectedValidationResult);
service.performPluginValidationsFor(packageDefinition);
assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value").size()).isEqualTo(2);
assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value")).contains("error-one", "error-two");
}
use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.
the class PackageDefinitionServiceTest method shouldPerformPluginValidationsUsingMetaDataBeforeSavingPackageRepository.
@Test
void shouldPerformPluginValidationsUsingMetaDataBeforeSavingPackageRepository() throws Exception {
Configuration configuration = new Configuration();
configuration.add(ConfigurationPropertyMother.create("required", false, ""));
configuration.add(ConfigurationPropertyMother.create("required_secure", true, ""));
configuration.add(ConfigurationPropertyMother.create("secure", true, ""));
configuration.add(ConfigurationPropertyMother.create("not_required_not_secure", false, ""));
configuration.add(ConfigurationPropertyMother.create("spec", false, "xyz?"));
PackageDefinition packageDefinition = PackageDefinitionMother.create("1", "name", configuration, packageRepository);
ValidationResult expectedValidationResult = new ValidationResult();
expectedValidationResult.addError(new ValidationError("spec", "invalid spec"));
when(packageRepositoryExtension.isPackageConfigurationValid(eq(packageRepository.getPluginConfiguration().getId()), any(com.thoughtworks.go.plugin.api.material.packagerepository.PackageConfiguration.class), any(RepositoryConfiguration.class))).thenReturn(expectedValidationResult);
service.performPluginValidationsFor(packageDefinition);
assertThat(packageDefinition.getConfiguration().get(0).getConfigurationValue().errors().getAllOn("value")).isEqualTo(Arrays.asList("Field: 'required' is required"));
assertThat(packageDefinition.getConfiguration().get(1).getEncryptedConfigurationValue().errors().getAllOn("value")).isEqualTo(Arrays.asList("Field: 'required_secure' is required"));
assertThat(packageDefinition.getConfiguration().get(2).getEncryptedConfigurationValue().errors().isEmpty()).isTrue();
assertThat(packageDefinition.getConfiguration().get(3).getConfigurationValue().errors().isEmpty()).isTrue();
assertThat(packageDefinition.getConfiguration().get(4).getConfigurationValue().errors().getAllOn("value")).isEqualTo(Arrays.asList("invalid spec"));
}
use of com.thoughtworks.go.plugin.api.response.validation.ValidationError 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"), anyMap())).thenReturn(validationResult);
ElasticProfile newProfile = new ElasticProfile("foo", "prod-cluster", new ConfigurationProperty(new ConfigurationKey("key"), new ConfigurationValue("val")));
EntityConfigUpdateCommand command = new ElasticAgentProfileCreateCommand(mock(GoConfigService.class), newProfile, extension, null, new HttpLocalizedOperationResult());
BasicCruiseConfig cruiseConfig = new BasicCruiseConfig();
assertThatThrownBy(() -> command.isValid(cruiseConfig)).isInstanceOf(RecordNotFoundException.class).hasMessageContaining(EntityType.ElasticProfile.notFoundMessage(newProfile.getId()));
command.update(cruiseConfig);
}
use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.
the class PluginServiceTest method updatePluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationFailure.
@Test
public void updatePluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationFailure() {
setUpElasticPluginForTheTest(true);
when(pluginDao.findPlugin(elasticAgentPluginId)).thenReturn(getPlugin(elasticAgentPluginId));
final ValidationResult validationResult = new ValidationResult();
validationResult.addError(new ValidationError("key-1", "error-message"));
when(pluginSettings.hasErrors()).thenReturn(true);
when(entityHashingService.hashForEntity(any(PluginSettings.class))).thenReturn("foo");
when(result.isSuccessful()).thenReturn(false);
pluginService.updatePluginSettings(pluginSettings, currentUser, result, "foo");
verify(pluginDao, times(0)).saveOrUpdate(any());
verify(elasticAgentExtension, times(0)).notifyPluginSettingsChange(eq(elasticAgentPluginId), anyMap());
verify(result, times(1)).unprocessableEntity("Save failed. There are errors in the plugin settings. Please fix them and resubmit.");
}
Aggregations