use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.
the class ElasticAgentProfileConfigurationValidator method validate.
public void validate(ElasticProfile elasticAgentProfile, String pluginId) {
try {
ValidationResult result = elasticAgentExtension.validate(pluginId, elasticAgentProfile.getConfigurationAsMap(true, true));
if (!result.isSuccessful()) {
for (ValidationError error : result.getErrors()) {
ConfigurationProperty property = elasticAgentProfile.getProperty(error.getKey());
if (property == null) {
elasticAgentProfile.addNewConfiguration(error.getKey(), false);
property = elasticAgentProfile.getProperty(error.getKey());
}
property.addError(error.getKey(), error.getMessage());
}
}
} catch (RecordNotFoundException e) {
elasticAgentProfile.addError("pluginId", String.format("Unable to validate Elastic Agent Profile configuration, missing plugin: %s", pluginId));
}
}
use of com.thoughtworks.go.plugin.api.response.validation.ValidationError 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.ValidationError in project gocd by gocd.
the class AuthorizationExtensionTest method shouldTalkToPlugin_To_ValidateRoleConfiguration.
@Test
void shouldTalkToPlugin_To_ValidateRoleConfiguration() {
String responseBody = "[{\"message\":\"memberOf must not be blank.\",\"key\":\"memberOf\"}]";
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(AUTHORIZATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
ValidationResult validationResult = authorizationExtension.validateRoleConfiguration(PLUGIN_ID, Collections.emptyMap());
assertRequest(requestArgumentCaptor.getValue(), AUTHORIZATION_EXTENSION, "1.0", REQUEST_VALIDATE_ROLE_CONFIG, "{}");
assertThat(validationResult.isSuccessful()).isEqualTo(false);
assertThat(validationResult.getErrors()).contains(new ValidationError("memberOf", "memberOf must not be blank."));
}
use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.
the class AuthorizationExtensionTest method shouldTalkToPlugin_To_ValidateAuthConfig.
@Test
void shouldTalkToPlugin_To_ValidateAuthConfig() {
String responseBody = "[{\"message\":\"Url must not be blank.\",\"key\":\"Url\"},{\"message\":\"SearchBase must not be blank.\",\"key\":\"SearchBase\"}]";
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(AUTHORIZATION_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
ValidationResult validationResult = authorizationExtension.validateAuthConfig(PLUGIN_ID, Collections.emptyMap());
assertRequest(requestArgumentCaptor.getValue(), AUTHORIZATION_EXTENSION, "1.0", REQUEST_VALIDATE_AUTH_CONFIG, "{}");
assertThat(validationResult.isSuccessful()).isEqualTo(false);
assertThat(validationResult.getErrors()).hasSize(2).contains(new ValidationError("Url", "Url must not be blank."), new ValidationError("SearchBase", "SearchBase must not be blank."));
}
use of com.thoughtworks.go.plugin.api.response.validation.ValidationError in project gocd by gocd.
the class ArtifactExtensionTestBase method shouldValidateArtifactStoreConfig.
@Test
public void shouldValidateArtifactStoreConfig() {
String responseBody = "[{\"message\":\"ACCESS_KEY must not be blank.\",\"key\":\"ACCESS_KEY\"}]";
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ARTIFACT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
ValidationResult validationResult = artifactExtension.validateArtifactStoreConfig(PLUGIN_ID, Collections.singletonMap("ACCESS_KEY", ""));
final GoPluginApiRequest request = requestArgumentCaptor.getValue();
assertThat(request.extension(), is(ARTIFACT_EXTENSION));
assertThat(request.requestName(), is(REQUEST_STORE_CONFIG_VALIDATE));
assertThat(request.requestBody(), is("{\"ACCESS_KEY\":\"\"}"));
assertThat(validationResult.isSuccessful(), is(false));
assertThat(validationResult.getErrors(), containsInAnyOrder(new ValidationError("ACCESS_KEY", "ACCESS_KEY must not be blank.")));
}
Aggregations