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