Search in sources :

Example 1 with ValidationResult

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

the class JSONResultMessageHandlerTest method shouldBuildValidationResultFromResponseBody.

@Test
public void shouldBuildValidationResultFromResponseBody() throws Exception {
    String responseBody = "[{\"key\":\"key-one\",\"message\":\"incorrect value\"},{\"message\":\"general error\"}]";
    ValidationResult validationResult = messageHandler.toValidationResult(responseBody);
    assertValidationError(validationResult.getErrors().get(0), "key-one", "incorrect value");
    assertValidationError(validationResult.getErrors().get(1), "", "general error");
}
Also used : ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 2 with ValidationResult

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

the class PluginSettingsJsonMessageHandler1_0Test method shouldBuildValidationResultFromCheckSCMConfigurationValidResponse.

@Test
public void shouldBuildValidationResultFromCheckSCMConfigurationValidResponse() throws Exception {
    String responseBody = "[{\"key\":\"key-one\",\"message\":\"incorrect value\"},{\"message\":\"general error\"}]";
    ValidationResult validationResult = messageHandler.responseMessageForPluginSettingsValidation(responseBody);
    assertValidationError(validationResult.getErrors().get(0), "key-one", "incorrect value");
    assertValidationError(validationResult.getErrors().get(1), "", "general error");
}
Also used : ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 3 with ValidationResult

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

the class JsonBasedTaskExtensionHandler_V1 method toValidationResult.

@Override
public ValidationResult toValidationResult(String responseBody) {
    ValidationResult validationResult = new ValidationResult();
    ArrayList<String> exceptions = new ArrayList<>();
    try {
        Map result = (Map) new GsonBuilder().create().fromJson(responseBody, Object.class);
        if (result == null)
            return validationResult;
        final Map<String, Object> errors = (Map<String, Object>) result.get("errors");
        if (errors != null) {
            for (Map.Entry<String, Object> entry : errors.entrySet()) {
                if (!(entry.getValue() instanceof String)) {
                    exceptions.add(String.format("Key: '%s' - The Json for Validation Request must contain a not-null error message of type String", entry.getKey()));
                } else {
                    validationResult.addError(new ValidationError(entry.getKey(), entry.getValue().toString()));
                }
            }
        }
        if (!exceptions.isEmpty()) {
            throw new RuntimeException(ListUtil.join(exceptions));
        }
        return validationResult;
    } catch (Exception e) {
        LOGGER.error(String.format("Error occurred while converting the Json to Validation Result. Error: %s. The Json received was '%s'.", e.getMessage(), responseBody));
        throw new RuntimeException(String.format("Error occurred while converting the Json to Validation Result. Error: %s.", e.getMessage()));
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ValidationResult

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

the class AuthenticationExtensionTest method shouldTalkToPluginToValidatePluginSettings.

@Test
public void shouldTalkToPluginToValidatePluginSettings() throws Exception {
    String requestBody = "expected-request";
    when(pluginSettingsJSONMessageHandler.requestMessageForPluginSettingsValidation(pluginSettingsConfiguration)).thenReturn(requestBody);
    ValidationResult deserializedResponse = new ValidationResult();
    when(pluginSettingsJSONMessageHandler.responseMessageForPluginSettingsValidation(RESPONSE_BODY)).thenReturn(deserializedResponse);
    ValidationResult response = authenticationExtension.validatePluginSettings(PLUGIN_ID, pluginSettingsConfiguration);
    assertRequest(requestArgumentCaptor.getValue(), AuthenticationExtension.EXTENSION_NAME, "1.0", PluginSettingsConstants.REQUEST_VALIDATE_PLUGIN_SETTINGS, requestBody);
    verify(pluginSettingsJSONMessageHandler).responseMessageForPluginSettingsValidation(RESPONSE_BODY);
    assertSame(response, deserializedResponse);
}
Also used : ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 5 with ValidationResult

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

the class AuthorizationExtensionTest method shouldTalkToPlugin_To_ValidatePluginConfiguration.

@Test
public void shouldTalkToPlugin_To_ValidatePluginConfiguration() throws Exception {
    String responseBody = "[{\"message\":\"Url must not be blank.\",\"key\":\"Url\"},{\"message\":\"SearchBase must not be blank.\",\"key\":\"SearchBase\"}]";
    when(pluginManager.submitTo(eq(PLUGIN_ID), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
    ValidationResult validationResult = authorizationExtension.validatePluginConfiguration(PLUGIN_ID, Collections.emptyMap());
    assertRequest(requestArgumentCaptor.getValue(), AuthorizationPluginConstants.EXTENSION_NAME, "1.0", REQUEST_VALIDATE_AUTH_CONFIG, "{}");
    assertThat(validationResult.isSuccessful(), is(false));
    assertThat(validationResult.getErrors(), containsInAnyOrder(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.Test)

Aggregations

ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)61 Test (org.junit.Test)48 ValidationError (com.thoughtworks.go.plugin.api.response.validation.ValidationError)30 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)17 Configuration (com.thoughtworks.go.domain.config.Configuration)14 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)10 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)8 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)7 SCM (com.thoughtworks.go.domain.scm.SCM)7 ConfigurationValue (com.thoughtworks.go.domain.config.ConfigurationValue)5 PluginSettingsConfiguration (com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration)5 RepositoryConfiguration (com.thoughtworks.go.plugin.api.material.packagerepository.RepositoryConfiguration)5 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)5 PluginSettings (com.thoughtworks.go.server.domain.PluginSettings)4 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)3 PackageRepository (com.thoughtworks.go.domain.packagerepository.PackageRepository)3 PackageConfiguration (com.thoughtworks.go.plugin.access.packagematerial.PackageConfiguration)3 PackageConfigurations (com.thoughtworks.go.plugin.access.packagematerial.PackageConfigurations)3 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)3 GoPluginDescriptor (com.thoughtworks.go.plugin.infra.plugininfo.GoPluginDescriptor)3