Java Code Examples for com.thoughtworks.go.plugin.api.response.validation.ValidationResult#getErrors()

The following examples show how to use com.thoughtworks.go.plugin.api.response.validation.ValidationResult#getErrors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PackageRepositoryService.java    From gocd with Apache License 2.0 6 votes vote down vote up
void performPluginValidationsFor(final PackageRepository packageRepository) {
    if (!validatePluginId(packageRepository)) {
        return;
    }
    for (ConfigurationProperty configurationProperty : packageRepository.getConfiguration()) {
        String key = configurationProperty.getConfigurationKey().getName();
        String pluginId = packageRepository.getPluginConfiguration().getId();
        if (repositoryMetadataStore.hasOption(pluginId, key, PackageConfiguration.REQUIRED)) {
            if (configurationProperty.getValue().isEmpty()) {
                configurationProperty.addErrorAgainstConfigurationValue("This field is required");
            }
        }
    }

    ValidationResult validationResult = packageRepositoryExtension.isRepositoryConfigurationValid(packageRepository.getPluginConfiguration().getId(), populateConfiguration(packageRepository.getConfiguration()));
    for (ValidationError error : validationResult.getErrors()) {
        packageRepository.addConfigurationErrorFor(error.getKey(), error.getMessage());
    }
}
 
Example 2
Source File: RoleConfigurationValidator.java    From gocd with Apache License 2.0 6 votes vote down vote up
public void validate(PluginRoleConfig role, String pluginId) {
    try {
        ValidationResult result = authorizationExtension.validateRoleConfiguration(pluginId, role.getConfigurationAsMap(true));

        if (!result.isSuccessful()) {
            for (ValidationError error : result.getErrors()) {
                ConfigurationProperty property = role.getProperty(error.getKey());

                if (property == null) {
                    role.addNewConfiguration(error.getKey(), false);
                    property = role.getProperty(error.getKey());
                }
                property.addError(error.getKey(), error.getMessage());
            }
        }
    } catch (RecordNotFoundException e) {
        role.addError("pluginRole", String.format("Unable to validate `pluginRole` configuration, missing plugin: %s", pluginId));
    }
}
 
Example 3
Source File: ElasticAgentProfileConfigurationValidator.java    From gocd with Apache License 2.0 6 votes vote down vote up
public void validate(ElasticProfile elasticAgentProfile, String pluginId) {
    try {
        ValidationResult result = elasticAgentExtension.validate(pluginId, elasticAgentProfile.getConfigurationAsMap(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));
    }
}
 
Example 4
Source File: PluginService.java    From gocd with Apache License 2.0 6 votes vote down vote up
void validatePluginSettings(PluginSettings pluginSettings) {
    pluginSettings.validateTree();
    if (pluginSettings.hasErrors()) {
        return;
    }

    final String pluginId = pluginSettings.getPluginId();
    final PluginSettingsConfiguration configuration = pluginSettings.toPluginSettingsConfiguration();

    final GoPluginExtension extension = findExtensionWhichCanHandleSettingsFor(pluginId);
    if (extension == null) {
        throw new IllegalArgumentException(String.format("Plugin '%s' does not exist or does not implement settings validation.", pluginId));
    }

    final ValidationResult result = extension.validatePluginSettings(pluginId, configuration);
    if (!result.isSuccessful()) {
        for (ValidationError error : result.getErrors()) {
            pluginSettings.populateErrorMessageFor(error.getKey(), error.getMessage());
        }
    }
}
 
Example 5
Source File: PackageDefinitionService.java    From gocd with Apache License 2.0 6 votes vote down vote up
public void performPluginValidationsFor(final PackageDefinition packageDefinition) {
    String pluginId = packageDefinition.getRepository().getPluginConfiguration().getId();


    ValidationResult validationResult = packageRepositoryExtension.isPackageConfigurationValid(pluginId, buildPackageConfigurations(packageDefinition), buildRepositoryConfigurations(packageDefinition.getRepository()));
    for (ValidationError error : validationResult.getErrors()) {
        packageDefinition.addConfigurationErrorFor(error.getKey(), error.getMessage());
    }
    for (ConfigurationProperty configurationProperty : packageDefinition.getConfiguration()) {
        String key = configurationProperty.getConfigurationKey().getName();
        if (PackageMetadataStore.getInstance().hasOption(packageDefinition.getRepository().getPluginConfiguration().getId(), key, PackageConfiguration.REQUIRED)) {
            if (configurationProperty.getValue().isEmpty() && configurationProperty.doesNotHaveErrorsAgainstConfigurationValue()) {
                configurationProperty.addErrorAgainstConfigurationValue("Field: '" + configurationProperty.getConfigurationKey().getName() + "' is required");
            }
        }
    }
}
 
Example 6
Source File: PluggableScmServiceTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldValidateMandatoryAndSecureFieldsForSCM() {
    SCMConfiguration scmConfig = new SCMConfiguration(new SCMProperty("KEY2").with(Property.REQUIRED, true).with(Property.SECURE, true));
    scmConfigurations.add(scmConfig);

    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"), ConfigurationPropertyMother.create("KEY2", true, ""));
    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 validationErrorForKey1 = getValidationErrorFor(validationErrors, "KEY1");
    assertNotNull(validationErrorForKey1);
    assertThat(validationErrorForKey1.getMessage(), is("This field is required"));
    final ValidationError validationErrorForKey2 = getValidationErrorFor(validationErrors, "KEY2");
    assertNotNull(validationErrorForKey2);
    assertThat(validationErrorForKey2.getMessage(), is("This field is required"));
}
 
Example 7
Source File: PluggableTaskServiceTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldValidateMandatoryFields() {
    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"));
    PluggableTask modifiedTask = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration);
    ValidationResult validationResult = new ValidationResult();
    when(taskExtension.validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class))).thenReturn(validationResult);

    pluggableTaskService.validate(modifiedTask);

    final List<ValidationError> validationErrors = validationResult.getErrors();
    assertFalse(validationErrors.isEmpty());
    final ValidationError validationError = validationErrors.stream().filter(new Predicate<ValidationError>() {
        @Override
        public boolean test(ValidationError item) {
            return ((ValidationError) item).getKey().equals("KEY1");
        }
    }).findFirst().orElse(null);
    assertNotNull(validationError);
    assertThat(validationError.getMessage(), is("This field is required"));
}
 
Example 8
Source File: PluggableTaskService.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void mapErrorsToConfiguration(ValidationResult result, PluggableTask task) {
    for (ValidationError validationError : result.getErrors()) {
        ConfigurationProperty property = task.getConfiguration().getProperty(validationError.getKey());

        if (property != null) {
            property.addError(validationError.getKey(), validationError.getMessage());
        } else {
            task.addError(validationError.getKey(), validationError.getMessage());
        }
    }
}
 
Example 9
Source File: PackageDefinitionService.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void addErrorsToConfiguration(ValidationResult validationResult, PackageDefinition packageDefinition) {
    for (ValidationError validationError : validationResult.getErrors()) {
        ConfigurationProperty property = packageDefinition.getConfiguration().getProperty(validationError.getKey());

        if (property != null) {
            property.addError(validationError.getKey(), validationError.getMessage());
        } else {
            String validationErrorKey = StringUtils.isBlank(validationError.getKey()) ? PackageDefinition.CONFIGURATION : validationError.getKey();
            packageDefinition.addError(validationErrorKey, validationError.getMessage());
        }
    }
}
 
Example 10
Source File: GenericArtifactoryPoller.java    From go-generic-artifactory-poller with Apache License 2.0 5 votes vote down vote up
private void validateConfig(RepositoryConfiguration repoConfig, PackageConfiguration packageConfig) {
    ValidationResult errors = new PluginConfig().isRepositoryConfigurationValid(repoConfig);

    errors.addErrors(new PluginConfig().isPackageConfigurationValid(packageConfig, repoConfig).getErrors());

    if (!errors.isSuccessful()) {
        StringBuilder stringBuilder = new StringBuilder();
        for (ValidationError validationError : errors.getErrors()) {
            stringBuilder.append(validationError.getMessage()).append("; ");
        }
        String errorString = stringBuilder.toString();
        throw new RuntimeException(errorString.substring(0, errorString.length() - 2));
    }
}
 
Example 11
Source File: PackageRepositoryService.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void addErrorsToConfiguration(ValidationResult validationResult, PackageRepository packageRepository) {
    for (ValidationError validationError : validationResult.getErrors()) {
        ConfigurationProperty property = packageRepository.getConfiguration().getProperty(validationError.getKey());

        if (property != null) {
            property.addError(validationError.getKey(), validationError.getMessage());
        } else {
            String validationErrorKey = StringUtils.isBlank(validationError.getKey()) ? PackageRepository.CONFIGURATION : validationError.getKey();
            packageRepository.addError(validationErrorKey, validationError.getMessage());
        }
    }
}
 
Example 12
Source File: PluggableScmService.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void addErrorsToConfiguration(ValidationResult validationResult, SCM scmConfig) {
    for (ValidationError validationError : validationResult.getErrors()) {
        ConfigurationProperty property = scmConfig.getConfiguration().getProperty(validationError.getKey());

        if (property != null) {
            property.addError(validationError.getKey(), validationError.getMessage());
        } else {
            scmConfig.addError(validationError.getKey(), validationError.getMessage());
        }
    }
}
 
Example 13
Source File: ExternalArtifactsService.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void mapErrorsToConfiguration(ValidationResult result, Configuration configuration, Validatable validatableConfig) {
    for (ValidationError validationError : result.getErrors()) {
        ConfigurationProperty property = configuration.getProperty(validationError.getKey());

        if (property == null) {
            validatableConfig.addError(validationError.getKey(), validationError.getMessage());
        } else {
            property.addError(validationError.getKey(), validationError.getMessage());
        }
    }
}
 
Example 14
Source File: RuleAwarePluginProfileService.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void addErrorsToConfiguration(ValidationResult result, RuleAwarePluginProfile newSecurityAuthConfig) {
    if (!result.isSuccessful()) {
        for (com.thoughtworks.go.plugin.api.response.validation.ValidationError validationError : result.getErrors()) {
            ConfigurationProperty property = newSecurityAuthConfig.getConfiguration().getProperty(validationError.getKey());

            if (property != null) {
                property.addError(validationError.getKey(), validationError.getMessage());
            } else {
                newSecurityAuthConfig.addError(validationError.getKey(), validationError.getMessage());
            }
        }
    }
}
 
Example 15
Source File: PluginProfilesService.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void addErrorsToConfiguration(ValidationResult result, PluginProfile newSecurityAuthConfig) {
    if (!result.isSuccessful()) {
        for (com.thoughtworks.go.plugin.api.response.validation.ValidationError validationError : result.getErrors()) {
            ConfigurationProperty property = newSecurityAuthConfig.getProperty(validationError.getKey());

            if (property != null) {
                property.addError(validationError.getKey(), validationError.getMessage());
            } else {
                newSecurityAuthConfig.addError(validationError.getKey(), validationError.getMessage());
            }
        }
    }
}
 
Example 16
Source File: PluggableScmServiceTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example 17
Source File: PluggableTaskServiceTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPassValidationIfAllRequiredFieldsHaveValues() {
    Configuration configuration = new Configuration(ConfigurationPropertyMother.create("KEY1"));
    configuration.getProperty("KEY1").setConfigurationValue(new ConfigurationValue("junk"));
    PluggableTask modifiedTask = new PluggableTask(new PluginConfiguration(pluginId, "1"), configuration);
    ValidationResult validationResult = new ValidationResult();
    when(taskExtension.validate(eq(modifiedTask.getPluginConfiguration().getId()), any(TaskConfig.class))).thenReturn(validationResult);

    pluggableTaskService.validate(modifiedTask);

    final List<ValidationError> validationErrors = validationResult.getErrors();
    assertTrue(validationErrors.isEmpty());
}