Search in sources :

Example 11 with ValidationError

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

the class ArtifactExtensionTestBase method shouldValidateFetchArtifactConfig.

@Test
public void shouldValidateFetchArtifactConfig() {
    String responseBody = "[{\"message\":\"Filename must not be blank.\",\"key\":\"Filename\"}]";
    when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ARTIFACT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(new DefaultGoPluginApiResponse(SUCCESS_RESPONSE_CODE, responseBody));
    ValidationResult validationResult = artifactExtension.validateFetchArtifactConfig(PLUGIN_ID, Collections.singletonMap("Filename", ""));
    final GoPluginApiRequest request = requestArgumentCaptor.getValue();
    assertThat(request.extension(), is(ARTIFACT_EXTENSION));
    assertThat(request.requestName(), is(REQUEST_FETCH_ARTIFACT_VALIDATE));
    assertThat(request.requestBody(), is("{\"Filename\":\"\"}"));
    assertThat(validationResult.isSuccessful(), is(false));
    assertThat(validationResult.getErrors(), containsInAnyOrder(new ValidationError("Filename", "Filename must not be blank.")));
}
Also used : GoPluginApiRequest(com.thoughtworks.go.plugin.api.request.GoPluginApiRequest) DefaultGoPluginApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 12 with ValidationError

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

the class ArtifactMessageConverterV1Test method getConfigurationValidationResultFromResponseBody_shouldDeserializeJsonToValidationResult.

@Test
public void getConfigurationValidationResultFromResponseBody_shouldDeserializeJsonToValidationResult() {
    final ArtifactMessageConverterV1 converter = new ArtifactMessageConverterV1();
    String responseBody = "[{\"message\":\"Url must not be blank.\",\"key\":\"Url\"},{\"message\":\"SearchBase must not be blank.\",\"key\":\"SearchBase\"}]";
    ValidationResult validationResult = converter.getConfigurationValidationResultFromResponseBody(responseBody);
    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.")));
}
Also used : ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 13 with ValidationError

use of com.thoughtworks.go.plugin.api.response.validation.ValidationError 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(StringUtils.join(exceptions, ", "));
        }
        return validationResult;
    } catch (Exception e) {
        LOGGER.error("Error occurred while converting the Json to Validation Result. Error: {}. The Json received was '{}'.", e.getMessage(), responseBody);
        throw new RuntimeException(String.format("Error occurred while converting the Json to Validation Result. Error: %s.", e.getMessage()));
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with ValidationError

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

the class PluggableScmServiceTest method isValidShouldMapPluginValidationErrorsToPluggableSCMConfigurations.

@Test
public void isValidShouldMapPluginValidationErrorsToPluggableSCMConfigurations() {
    PluginConfiguration pluginConfiguration = new PluginConfiguration("plugin_id", "version");
    Configuration configuration = new Configuration();
    configuration.add(ConfigurationPropertyMother.create("url", false, "url"));
    configuration.add(ConfigurationPropertyMother.create("username", false, "admin"));
    ValidationResult validationResult = new ValidationResult();
    validationResult.addError(new ValidationError("url", "invalid"));
    validationResult.addError(new ValidationError("username", "invalid"));
    SCM scmConfig = mock(SCM.class);
    when(scmConfig.doesPluginExist()).thenReturn(true);
    when(scmConfig.getPluginConfiguration()).thenReturn(pluginConfiguration);
    when(scmConfig.getConfiguration()).thenReturn(configuration);
    when(scmExtension.isSCMConfigurationValid(any(String.class), any(SCMPropertyConfiguration.class))).thenReturn(validationResult);
    assertFalse(pluggableScmService.isValid(scmConfig));
    assertThat(configuration.getProperty("url").errors().get("url").get(0), is("invalid"));
    assertThat(configuration.getProperty("username").errors().get("username").get(0), is("invalid"));
}
Also used : Configuration(com.thoughtworks.go.domain.config.Configuration) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) SCM(com.thoughtworks.go.domain.scm.SCM) Test(org.junit.jupiter.api.Test)

Example 15 with ValidationError

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

the class PluggableScmServiceTest method shouldValidateMandatoryFieldsForSCM.

@Test
public void shouldValidateMandatoryFieldsForSCM() {
    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"));
    SCM modifiedSCM = new SCM("scm-id", new PluginConfiguration(pluginId, "1"), configuration);
    ValidationResult validationResult = new ValidationResult();
    when(scmExtension.isSCMConfigurationValid(eq(modifiedSCM.getPluginConfiguration().getId()), any(SCMPropertyConfiguration.class))).thenReturn(validationResult);
    pluggableScmService.validate(modifiedSCM);
    final List<ValidationError> validationErrors = validationResult.getErrors();
    assertFalse(validationErrors.isEmpty());
    final ValidationError validationError = getValidationErrorFor(validationErrors, "KEY1");
    assertNotNull(validationError);
    assertThat(validationError.getMessage(), is("This field is required"));
}
Also used : Configuration(com.thoughtworks.go.domain.config.Configuration) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) PluginConfiguration(com.thoughtworks.go.domain.config.PluginConfiguration) ValidationError(com.thoughtworks.go.plugin.api.response.validation.ValidationError) SCM(com.thoughtworks.go.domain.scm.SCM) ValidationResult(com.thoughtworks.go.plugin.api.response.validation.ValidationResult) Test(org.junit.jupiter.api.Test)

Aggregations

ValidationError (com.thoughtworks.go.plugin.api.response.validation.ValidationError)63 ValidationResult (com.thoughtworks.go.plugin.api.response.validation.ValidationResult)61 Test (org.junit.jupiter.api.Test)35 Configuration (com.thoughtworks.go.domain.config.Configuration)15 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)15 Test (org.junit.Test)14 PluginConfiguration (com.thoughtworks.go.domain.config.PluginConfiguration)13 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)9 ConfigurationValue (com.thoughtworks.go.domain.config.ConfigurationValue)7 SCM (com.thoughtworks.go.domain.scm.SCM)7 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)7 GoPluginApiRequest (com.thoughtworks.go.plugin.api.request.GoPluginApiRequest)6 TaskConfig (com.thoughtworks.go.plugin.api.task.TaskConfig)6 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)5 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)5 ConfigurationKey (com.thoughtworks.go.domain.config.ConfigurationKey)5 PluginSettingsConfiguration (com.thoughtworks.go.plugin.access.common.settings.PluginSettingsConfiguration)5 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)4 PluginSettings (com.thoughtworks.go.server.domain.PluginSettings)4 GoConfigService (com.thoughtworks.go.server.service.GoConfigService)4