Search in sources :

Example 71 with ValidationResult

use of com.thoughtworks.go.plugin.api.response.validation.ValidationResult 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 72 with ValidationResult

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

the class PluginServiceTest method shouldNotifyTheExtensionWhichHandlesSettingsInAPluginWithMultipleExtensions_WhenPluginSettingsHaveChanged.

@Test
public void shouldNotifyTheExtensionWhichHandlesSettingsInAPluginWithMultipleExtensions_WhenPluginSettingsHaveChanged() {
    String pluginId = "plugin-id-2";
    Map<String, String> parameterMap = m("p2-k1", "v1");
    PluginSettings pluginSettings = new PluginSettings(pluginId).populateSettingsMap(parameterMap);
    Username currentUser = new Username("admin");
    when(securityService.isUserAdmin(currentUser)).thenReturn(true);
    when(configRepoExtension.canHandlePlugin(pluginId)).thenReturn(true);
    when(configRepoExtension.validatePluginSettings(eq(pluginId), any(PluginSettingsConfiguration.class))).thenReturn(new ValidationResult());
    pluginService.savePluginSettings(currentUser, new HttpLocalizedOperationResult(), pluginSettings);
    verify(configRepoExtension).notifyPluginSettingsChange(pluginId, pluginSettings.getSettingsAsKeyValuePair());
    verify(scmExtension, never()).canHandlePlugin(pluginId);
    verify(scmExtension, never()).notifyPluginSettingsChange(pluginId, pluginSettings.getSettingsAsKeyValuePair());
    verify(taskExtension, never()).notifyPluginSettingsChange(pluginId, pluginSettings.getSettingsAsKeyValuePair());
    verify(notificationExtension, never()).notifyPluginSettingsChange(pluginId, pluginSettings.getSettingsAsKeyValuePair());
    verify(packageRepositoryExtension, never()).notifyPluginSettingsChange(pluginId, pluginSettings.getSettingsAsKeyValuePair());
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) PluginSettings(com.thoughtworks.go.server.domain.PluginSettings) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 73 with ValidationResult

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

the class PluginServiceTest method shouldNotifyPluginThatPluginSettingsHaveChangedAfterSaving.

@Test
public void shouldNotifyPluginThatPluginSettingsHaveChangedAfterSaving() {
    String pluginId = "plugin-id-2";
    Map<String, String> parameterMap = m("p2-k1", "v1");
    PluginSettings pluginSettings = new PluginSettings(pluginId).populateSettingsMap(parameterMap);
    Username currentUser = new Username("admin");
    when(securityService.isUserAdmin(currentUser)).thenReturn(true);
    when(configRepoExtension.canHandlePlugin(pluginId)).thenReturn(true);
    when(configRepoExtension.validatePluginSettings(eq(pluginId), any(PluginSettingsConfiguration.class))).thenReturn(new ValidationResult());
    pluginService.savePluginSettings(currentUser, new HttpLocalizedOperationResult(), pluginSettings);
    Plugin plugin = new Plugin(pluginId, toJSON(parameterMap));
    verify(pluginDao).saveOrUpdate(plugin);
    verify(configRepoExtension).notifyPluginSettingsChange(pluginId, pluginSettings.getSettingsAsKeyValuePair());
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) PluginSettings(com.thoughtworks.go.server.domain.PluginSettings) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) NullPlugin(com.thoughtworks.go.domain.NullPlugin) Plugin(com.thoughtworks.go.domain.Plugin) Test(org.junit.Test)

Example 74 with ValidationResult

use of com.thoughtworks.go.plugin.api.response.validation.ValidationResult 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 75 with ValidationResult

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

the class PluginServiceTest method shouldCallValidationOnPlugin.

@Test
public void shouldCallValidationOnPlugin() throws Exception {
    for (GoPluginExtension extension : extensions) {
        String pluginId = UUID.randomUUID().toString();
        PluginSettingsMetadataStore.getInstance().addMetadataFor(pluginId, extension.extensionName(), null, null);
        when(extension.canHandlePlugin(pluginId)).thenReturn(true);
        when(extension.validatePluginSettings(eq(pluginId), any(PluginSettingsConfiguration.class))).thenReturn(new ValidationResult());
        PluginSettings pluginSettings = new PluginSettings(pluginId);
        pluginService.validatePluginSettingsFor(pluginSettings);
        verify(extension).validatePluginSettings(eq(pluginId), any(PluginSettingsConfiguration.class));
    }
}
Also used : GoPluginExtension(com.thoughtworks.go.plugin.access.common.settings.GoPluginExtension) PluginSettings(com.thoughtworks.go.server.domain.PluginSettings) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PluginSettingsConfiguration(com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Aggregations

ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)85 Test (org.junit.Test)69 ValidationError (com.thoughtworks.go.plugin.api.response.validation.ValidationError)43 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)17 Configuration (com.thoughtworks.go.domain.config.Configuration)14 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)11 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)11 PluginSettingsConfiguration (com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration)10 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)10 PluginSettings (com.thoughtworks.go.server.domain.PluginSettings)9 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)7 ConfigurationValue (com.thoughtworks.go.domain.config.ConfigurationValue)7 SCM (com.thoughtworks.go.domain.scm.SCM)7 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 RepositoryConfiguration (com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration)5 GoPluginApiRequest (com.thoughtworks.go.plugin.api.request.GoPluginApiRequest)5 Username (com.thoughtworks.go.server.domain.Username)5 ConfigurationKey (com.thoughtworks.go.domain.config.ConfigurationKey)4 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)3