Search in sources :

Example 41 with ValidationError

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

the class PluggableTaskServiceTest method shouldValidateTask.

@Test
public void shouldValidateTask() {
    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"));
    PluggableTask modifiedTask = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration);
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("KEY1", "error message"));
    when(taskExtension.validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class))).thenReturn(validationResult);
    pluggableTaskService.validate(modifiedTask);
    assertThat(modifiedTask.getConfiguration().getProperty("KEY1").errors().isEmpty(), is(false));
    assertThat(modifiedTask.getConfiguration().getProperty("KEY1").errors().firstError(), is("error message"));
    verify(taskExtension).validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class));
}
Also used : 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)

Example 42 with ValidationError

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

the class ExternalArtifactsServiceTest method isValidShouldMapPluginValidationErrorsToExternalArtifactForMissingConfigurations.

@Test
public void isValidShouldMapPluginValidationErrorsToExternalArtifactForMissingConfigurations() {
    pluggableArtifactConfig.getConfiguration().clear();
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("configuration", "Either Image or BuildFile is required"));
    ArtifactStore artifactStore = mock(ArtifactStore.class);
    when(artifactStore.getPluginId()).thenReturn(pluginId);
    when(artifactExtension.validatePluggableArtifactConfig(any(), any())).thenReturn(validationResult);
    externalArtifactsService.validateExternalArtifactConfig(pluggableArtifactConfig, artifactStore, true);
    assertThat(pluggableArtifactConfig.errors().getAllOn("configuration").get(0), is("Either Image or BuildFile is required"));
}
Also used : ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 43 with ValidationError

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

the class PluginServiceTest method createPluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationError.

@Test
public void createPluginSettings_shouldValidatePluginSettingUsingExtensionAndReturnUnprocessableEntityWhenThereIsAValidationError() {
    setUpElasticPluginForTheTest(true);
    when(pluginDao.findPlugin(elasticAgentPluginId)).thenReturn(new NullPlugin());
    final ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("key-1", "error-message"));
    when(pluginSettings.hasErrors()).thenReturn(true);
    pluginService.createPluginSettings(pluginSettings, currentUser, result);
    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.");
    verifyNoMoreInteractions(result);
}
Also used : NullPlugin(com.thoughtworks.go.domain.NullPlugin) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 44 with ValidationError

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

the class PluginServiceTest method validatePluginSettingsFor_shouldPopulateValidationErrorsInPluginSettingsObject.

@Test
public void validatePluginSettingsFor_shouldPopulateValidationErrorsInPluginSettingsObject() {
    final ElasticAgentPluginInfo elasticAgentPluginInfo = mockElasticAgentPluginInfo(getPluggableInstanceSettings());
    final ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("key-1", "m1"));
    validationResult.addError(new ValidationError("key-3", "m3"));
    addPluginSettingsMetadataToStore(elasticAgentPluginId, ELASTIC_AGENT_EXTENSION, getPluginSettingsConfiguration());
    when(elasticAgentExtension.extensionName()).thenReturn(ELASTIC_AGENT_EXTENSION);
    when(elasticAgentExtension.canHandlePlugin(elasticAgentPluginId)).thenReturn(true);
    when(elasticAgentExtension.validatePluginSettings(eq(elasticAgentPluginId), any(PluginSettingsConfiguration.class))).thenReturn(validationResult);
    PluginSettings pluginSettings = PluginSettings.from(getPlugin(elasticAgentPluginId), elasticAgentPluginInfo);
    pluginService.validatePluginSettings(pluginSettings);
    assertThat(pluginSettings.hasErrors(), is(true));
    assertThat(pluginSettings.getErrorFor("key-1"), is(Arrays.asList("m1")));
    assertThat(pluginSettings.getErrorFor("key-3"), is(Arrays.asList("m3")));
}
Also used : ElasticAgentPluginInfo(com.thoughtworks.go.plugin.domain.elastic.ElasticAgentPluginInfo) 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) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 45 with ValidationError

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

the class RoleConfigurationValidatorTest method shouldAddConfigurationAndMapErrorsInAbsenceOfConfiguration.

@Test
public void shouldAddConfigurationAndMapErrorsInAbsenceOfConfiguration() throws Exception {
    ConfigurationProperty property = new ConfigurationProperty(new ConfigurationKey("username"), new ConfigurationValue("view"));
    PluginRoleConfig roleConfig = new PluginRoleConfig("admin", "auth_id", property);
    ValidationResult result = new ValidationResult();
    result.addError(new ValidationError("password", "password is required"));
    when(extension.validateRoleConfiguration("pluginId", Collections.singletonMap("username", "view"))).thenReturn(result);
    validator.validate(roleConfig, "pluginId");
    assertTrue(roleConfig.hasErrors());
    assertThat(roleConfig.getProperty("password").errors().get("password").get(0), is("password is required"));
    assertNull(roleConfig.getProperty("password").getValue());
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) ConfigurationValue(com.thoughtworks.go.domain.config.ConfigurationValue) ConfigurationKey(com.thoughtworks.go.domain.config.ConfigurationKey) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) PluginRoleConfig(com.thoughtworks.go.config.PluginRoleConfig) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.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