Search in sources :

Example 46 with ValidationError

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

the class ElasticAgentProfileConfigurationValidator method validate.

public void validate(ElasticProfile elasticAgentProfile, String pluginId) {
    try {
        ValidationResult result = elasticAgentExtension.validate(pluginId, elasticAgentProfile.getConfigurationAsMap(true, true));
        if (!result.isSuccessful()) {
            for (ValidationError error : result.getErrors()) {
                ConfigurationProperty property = elasticAgentProfile.getProperty(error.getKey());
                if (property == null) {
                    elasticAgentProfile.addNewConfiguration(error.getKey(), false);
                    property = elasticAgentProfile.getProperty(error.getKey());
                }
                property.addError(error.getKey(), error.getMessage());
            }
        }
    } catch (RecordNotFoundException e) {
        elasticAgentProfile.addError("pluginId", String.format("Unable to validate Elastic Agent Profile configuration, missing plugin: %s", pluginId));
    }
}
Also used : ConfigurationProperty(com.thoughtworks.go.domain.config.ConfigurationProperty) RecordNotFoundException(com.thoughtworks.go.config.exceptions.RecordNotFoundException) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult)

Example 47 with ValidationError

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

the class JSONResultMessageHandler method toValidationResult.

public ValidationResult toValidationResult(String responseBody) {
    try {
        ValidationResult validationResult = new ValidationResult();
        if (isEmpty(responseBody))
            return validationResult;
        List errors;
        try {
            errors = (List<Map>) new GsonBuilder().create().fromJson(responseBody, Object.class);
        } catch (Exception e) {
            throw new RuntimeException("Validation errors should be returned as list or errors, with each error represented as a map");
        }
        for (Object errorObj : errors) {
            if (!(errorObj instanceof Map)) {
                throw new RuntimeException("Each validation error should be represented as a map");
            }
            Map errorMap = (Map) errorObj;
            String key;
            try {
                key = (String) errorMap.get("key");
            } catch (Exception e) {
                throw new RuntimeException("Validation error key should be of type string");
            }
            String message;
            try {
                message = (String) errorMap.get("message");
            } catch (Exception e) {
                throw new RuntimeException("Validation message should be of type string");
            }
            if (isEmpty(key)) {
                validationResult.addError(new ValidationError(message));
            } else {
                validationResult.addError(new ValidationError(key, message));
            }
        }
        return validationResult;
    } catch (Exception e) {
        throw new RuntimeException(format("Unable to de-serialize json response. %s", e.getMessage()));
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 48 with ValidationError

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

the class AuthorizationExtensionTest method shouldTalkToPlugin_To_ValidateRoleConfiguration.

@Test
void shouldTalkToPlugin_To_ValidateRoleConfiguration() {
    String responseBody = "[{\"message\":\"memberOf must not be blank.\",\"key\":\"memberOf\"}]";
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(AUTHORIZATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
    ValidationResult validationResult = authorizationExtension.validateRoleConfiguration(PLUGIN_ID, Collections.emptyMap());
    assertRequest(requestArgumentCaptor.getValue(), AUTHORIZATION_EXTENSION, "1.0", REQUEST_VALIDATE_ROLE_CONFIG, "{}");
    assertThat(validationResult.isSuccessful()).isEqualTo(false);
    assertThat(validationResult.getErrors()).contains(new ValidationError("memberOf", "memberOf must not be blank."));
}
Also used : 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.jupiter.api.Test)

Example 49 with ValidationError

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

the class AuthorizationExtensionTest method shouldTalkToPlugin_To_ValidateAuthConfig.

@Test
void shouldTalkToPlugin_To_ValidateAuthConfig() {
    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(AUTHORIZATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
    ValidationResult validationResult = authorizationExtension.validateAuthConfig(PLUGIN_ID, Collections.emptyMap());
    assertRequest(requestArgumentCaptor.getValue(), AUTHORIZATION_EXTENSION, "1.0", REQUEST_VALIDATE_AUTH_CONFIG, "{}");
    assertThat(validationResult.isSuccessful()).isEqualTo(false);
    assertThat(validationResult.getErrors()).hasSize(2).contains(new ValidationError("Url", "Url must not be blank."), new ValidationError("SearchBase", "SearchBase must not be blank."));
}
Also used : 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.jupiter.api.Test)

Example 50 with ValidationError

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

the class ArtifactExtensionTestBase 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.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