Search in sources :

Example 56 with ValidationResult

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

the class PackageRepositoryExtensionTest method shouldTalkToPluginToCheckIfPackageConfigurationIsValid.

@Test
public void shouldTalkToPluginToCheckIfPackageConfigurationIsValid() throws Exception {
    String expectedRequestBody = "{\"repository-configuration\":{\"key-one\":{\"value\":\"value-one\"},\"key-two\":{\"value\":\"value-two\"}}," + "\"package-configuration\":{\"key-three\":{\"value\":\"value-three\"},\"key-four\":{\"value\":\"value-four\"}}}";
    String expectedResponseBody = "[{\"key\":\"key-one\",\"message\":\"incorrect value\"},{\"message\":\"general error\"}]";
    when(pluginManager.isPluginOfType(PACKAGE_MATERIAL_EXTENSION, PLUGIN_ID)).thenReturn(true);
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(PACKAGE_MATERIAL_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(expectedResponseBody));
    ValidationResult validationResult = extension.isPackageConfigurationValid(PLUGIN_ID, packageConfiguration, repositoryConfiguration);
    assertRequest(requestArgumentCaptor.getValue(), PACKAGE_MATERIAL_EXTENSION, "1.0", PackageRepositoryExtension.REQUEST_VALIDATE_PACKAGE_CONFIGURATION, expectedRequestBody);
    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 57 with ValidationResult

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

the class PackageRepositoryExtensionTest method shouldTalkToPluginToCheckIfRepositoryConfigurationIsValid.

@Test
public void shouldTalkToPluginToCheckIfRepositoryConfigurationIsValid() throws Exception {
    String expectedRequestBody = "{\"repository-configuration\":{\"key-one\":{\"value\":\"value-one\"},\"key-two\":{\"value\":\"value-two\"}}}";
    String expectedResponseBody = "[{\"key\":\"key-one\",\"message\":\"incorrect value\"},{\"message\":\"general error\"}]";
    when(pluginManager.isPluginOfType(PACKAGE_MATERIAL_EXTENSION, PLUGIN_ID)).thenReturn(true);
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(PACKAGE_MATERIAL_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(expectedResponseBody));
    ValidationResult validationResult = extension.isRepositoryConfigurationValid(PLUGIN_ID, repositoryConfiguration);
    assertRequest(requestArgumentCaptor.getValue(), PACKAGE_MATERIAL_EXTENSION, "1.0", PackageRepositoryExtension.REQUEST_VALIDATE_REPOSITORY_CONFIGURATION, expectedRequestBody);
    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 58 with ValidationResult

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

the class PackageRepositoryExtensionTest method shouldTalkToPluginToValidatePluginSettings.

@Test
public void shouldTalkToPluginToValidatePluginSettings() throws Exception {
    extension.registerHandler("1.0", pluginSettingsJSONMessageHandler);
    extension.messageHandlerMap.put("1.0", jsonMessageHandler);
    String requestBody = "expected-request";
    when(pluginSettingsJSONMessageHandler.requestMessageForPluginSettingsValidation(pluginSettingsConfiguration)).thenReturn(requestBody);
    String responseBody = "expected-response";
    ValidationResult deserializedResponse = new ValidationResult();
    when(pluginSettingsJSONMessageHandler.responseMessageForPluginSettingsValidation(responseBody)).thenReturn(deserializedResponse);
    when(pluginManager.isPluginOfType(PACKAGE_MATERIAL_EXTENSION, PLUGIN_ID)).thenReturn(true);
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(PACKAGE_MATERIAL_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody));
    ValidationResult response = extension.validatePluginSettings(PLUGIN_ID, pluginSettingsConfiguration);
    assertRequest(requestArgumentCaptor.getValue(), PACKAGE_MATERIAL_EXTENSION, "1.0", PluginSettingsConstants.REQUEST_VALIDATE_PLUGIN_SETTINGS, requestBody);
    verify(pluginSettingsJSONMessageHandler).responseMessageForPluginSettingsValidation(responseBody);
    assertSame(response, deserializedResponse);
}
Also used : ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 59 with ValidationResult

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

the class JsonBasedPluggableTaskTest method shouldValidateTaskConfig.

@Test
public void shouldValidateTaskConfig() {
    String jsonResponse = "{\"errors\":{\"key1\":\"err1\",\"key2\":\"err3\"}}";
    String config = "{\"URL\":{\"secure\":false,\"value\":\"http://foo\",\"required\":true}}";
    when(goPluginApiResponse.responseBody()).thenReturn(jsonResponse);
    TaskConfig configuration = new TaskConfig();
    final TaskConfigProperty property = new TaskConfigProperty("URL", "http://foo");
    property.with(Property.SECURE, false);
    property.with(Property.REQUIRED, true);
    configuration.add(property);
    ValidationResult result = task.validate(configuration);
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.getErrors().get(0).getKey(), is("key1"));
    assertThat(result.getErrors().get(0).getMessage(), is("err1"));
    assertThat(result.getErrors().get(1).getKey(), is("key2"));
    assertThat(result.getErrors().get(1).getMessage(), is("err3"));
    ArgumentCaptor<GoPluginApiRequest> argument = ArgumentCaptor.forClass(GoPluginApiRequest.class);
    verify(pluginManager).submitTo(eq(pluginId), eq(PLUGGABLE_TASK_EXTENSION), argument.capture());
    assertThat(argument.getValue().requestBody(), is(config));
    MatcherAssert.assertThat(argument.getValue().extension(), Matchers.is(PLUGGABLE_TASK_EXTENSION));
    MatcherAssert.assertThat(argument.getValue().extensionVersion(), Matchers.is(JsonBasedTaskExtensionHandler_V1.VERSION));
    MatcherAssert.assertThat(argument.getValue().requestName(), Matchers.is(TaskExtension.VALIDATION_REQUEST));
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) TaskConfig(com.thoughtworks.go.plugin.api.task.TaskConfig) TaskConfigProperty(com.thoughtworks.go.plugin.api.task.TaskConfigProperty) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.Test)

Example 60 with ValidationResult

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

the class JsonBasedTaskExtensionHandler_V1Test method shouldConvertJsonResponseToValidationResultWhenValidationFails.

@Test
public void shouldConvertJsonResponseToValidationResultWhenValidationFails() {
    String jsonResponse = "{\"errors\":{\"key1\":\"err1\",\"key2\":\"err2\"}}";
    TaskConfig configuration = new TaskConfig();
    TaskConfigProperty property = new TaskConfigProperty("URL", "http://foo");
    property.with(Property.SECURE, false);
    property.with(Property.REQUIRED, true);
    configuration.add(property);
    ValidationResult result = new JsonBasedTaskExtensionHandler_V1().toValidationResult(jsonResponse);
    Assert.assertThat(result.isSuccessful(), CoreMatchers.is(false));
    ValidationError error1 = result.getErrors().get(0);
    ValidationError error2 = result.getErrors().get(1);
    Assert.assertThat(error1.getKey(), CoreMatchers.is("key1"));
    Assert.assertThat(error1.getMessage(), CoreMatchers.is("err1"));
    Assert.assertThat(error2.getKey(), CoreMatchers.is("key2"));
    Assert.assertThat(error2.getMessage(), CoreMatchers.is("err2"));
}
Also used : 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)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