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