Search in sources :

Example 36 with ValidationError

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

the class ElasticAgentExtensionV3Test method shouldValidateProfile.

@Test
public void shouldValidateProfile() throws JSONException {
    String responseBody = "[{\"message\":\"Url must not be blank.\",\"key\":\"Url\"},{\"message\":\"SearchBase must not be blank.\",\"key\":\"SearchBase\"}]";
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody));
    final ValidationResult result = extensionV3.validateElasticProfile(PLUGIN_ID, Collections.emptyMap());
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.getErrors(), containsInAnyOrder(new ValidationError("Url", "Url must not be blank."), new ValidationError("SearchBase", "SearchBase must not be blank.")));
    assertExtensionRequest("3.0", REQUEST_VALIDATE_PROFILE, "{}");
}
Also used : ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 37 with ValidationError

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

the class ArtifactExtensionTest method shouldValidateArtifactStoreConfig.

@Test
public void shouldValidateArtifactStoreConfig() {
    String responseBody = "[{\"message\":\"ACCESS_KEY must not be blank.\",\"key\":\"ACCESS_KEY\"}]";
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ARTIFACT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
    ValidationResult validationResult = artifactExtension.validateArtifactStoreConfig(PLUGIN_ID, Collections.singletonMap("ACCESS_KEY", ""));
    final GoPluginApiRequest request = requestArgumentCaptor.getValue();
    assertThat(request.extension(), is(ARTIFACT_EXTENSION));
    assertThat(request.requestName(), is(REQUEST_STORE_CONFIG_VALIDATE));
    assertThat(request.requestBody(), is("{\"ACCESS_KEY\":\"\"}"));
    assertThat(validationResult.isSuccessful(), is(false));
    assertThat(validationResult.getErrors(), containsInAnyOrder(new ValidationError("ACCESS_KEY", "ACCESS_KEY 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 38 with ValidationError

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

the class ArtifactExtensionTest method shouldValidateFetchArtifactConfig.

@Test
public void shouldValidateFetchArtifactConfig() {
    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.validateFetchArtifactConfig(PLUGIN_ID, Collections.singletonMap("Filename", ""));
    final GoPluginApiRequest request = requestArgumentCaptor.getValue();
    assertThat(request.extension(), is(ARTIFACT_EXTENSION));
    assertThat(request.requestName(), is(REQUEST_FETCH_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 39 with ValidationError

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

the class PluginServiceTest method shouldNotSavePluginSettingsIfPluginReturnsValidationErrors.

@Test
public void shouldNotSavePluginSettingsIfPluginReturnsValidationErrors() {
    PluginSettingsMetadataStore.getInstance().addMetadataFor("some-plugin", PluginConstants.CONFIG_REPO_EXTENSION, null, null);
    PluginSettings pluginSettings = new PluginSettings("some-plugin");
    Username currentUser = new Username("admin");
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    when(securityService.isUserAdmin(currentUser)).thenReturn(true);
    when(configRepoExtension.canHandlePlugin("some-plugin")).thenReturn(true);
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("foo", "foo is a required field"));
    when(configRepoExtension.validatePluginSettings(eq("some-plugin"), any(PluginSettingsConfiguration.class))).thenReturn(validationResult);
    pluginService.savePluginSettings(currentUser, result, pluginSettings);
    assertThat(result.httpCode(), is(422));
    assertThat(pluginSettings.errors().size(), is(1));
    assertThat(pluginSettings.getErrorFor("foo"), is(Arrays.asList("foo is a required field")));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) 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.Test)

Example 40 with ValidationError

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

the class PluggableTaskServiceTest method shouldValidateMandatoryFields.

@Test
public void shouldValidateMandatoryFields() {
    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"));
    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);
    when(localizer.localize("MANDATORY_CONFIGURATION_FIELD")).thenReturn("MANDATORY_CONFIGURATION_FIELD");
    pluggableTaskService.validate(modifiedTask);
    final List<ValidationError> validationErrors = validationResult.getErrors();
    assertFalse(validationErrors.isEmpty());
    final ValidationError validationError = validationErrors.stream().filter(new Predicate<ValidationError>() {

        @Override
        public boolean test(ValidationError item) {
            return ((ValidationError) item).getKey().equals("KEY1");
        }
    }).findFirst().orElse(null);
    assertNotNull(validationError);
    assertThat(validationError.getMessage(), is("MANDATORY_CONFIGURATION_FIELD"));
}
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)

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