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");
}
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");
}
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);
}
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));
}
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"));
}
Aggregations