Search in sources :

Example 1 with AlertFieldStatus

use of com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus in project blackduck-alert by blackducksoftware.

the class BlackDuckDistributionFieldModelTestAction method testConfig.

@Override
public MessageResult testConfig(String configId, FieldModel fieldModel, FieldUtility registeredFieldValues) throws IntegrationException {
    ArrayList<AlertFieldStatus> fieldStatuses = new ArrayList<>();
    Optional<Long> optionalProviderConfigId = registeredFieldValues.getLong(ProviderDescriptor.KEY_PROVIDER_CONFIG_ID);
    if (optionalProviderConfigId.isPresent()) {
        Long providerConfigId = optionalProviderConfigId.get();
        boolean filterByProjects = registeredFieldValues.getBoolean(ProviderDescriptor.KEY_FILTER_BY_PROJECT).orElse(false);
        if (filterByProjects) {
            Collection<String> configuredProjects = registeredFieldValues.getAllStrings(ProviderDescriptor.KEY_CONFIGURED_PROJECT);
            validateSelectedProjectExists(providerConfigId, configuredProjects).ifPresent(fieldStatuses::add);
            Optional<String> optionalProjectNamePattern = registeredFieldValues.getString(ProviderDescriptor.KEY_PROJECT_NAME_PATTERN);
            optionalProjectNamePattern.flatMap(projectNamePattern -> validatePatternMatchesProject(providerConfigId, projectNamePattern)).ifPresent(fieldStatuses::add);
            registeredFieldValues.getString(ProviderDescriptor.KEY_PROJECT_VERSION_NAME_PATTERN).flatMap(projectVersionNamePattern -> validatePatternMatchesProjectVersion(providerConfigId, projectVersionNamePattern, optionalProjectNamePattern.orElse(null), configuredProjects)).ifPresent(fieldStatuses::add);
        }
        BlackDuckProperties blackDuckProperties = null;
        Optional<ConfigurationModel> providerConfigurationOptional = configurationModelConfigurationAccessor.getConfigurationById(providerConfigId);
        if (providerConfigurationOptional.isPresent()) {
            ConfigurationModel providerConfiguration = providerConfigurationOptional.get();
            StatefulProvider statefulProvider = blackDuckProvider.createStatefulProvider(providerConfiguration);
            blackDuckProperties = (BlackDuckProperties) statefulProvider.getProperties();
        }
        if (null != blackDuckProperties) {
            BlackDuckApiTokenValidator blackDuckAPITokenValidator = new BlackDuckApiTokenValidator(blackDuckProperties);
            if (!blackDuckAPITokenValidator.isApiTokenValid()) {
                fieldStatuses.add(AlertFieldStatus.error(ProviderDescriptor.KEY_PROVIDER_CONFIG_ID, "User permission failed, cannot read notifications from Black Duck."));
            }
        }
    } else {
        fieldStatuses.add(AlertFieldStatus.error(ProviderDescriptor.KEY_PROVIDER_CONFIG_ID, "A provider configuration is required"));
    }
    if (MessageResult.hasFieldStatusBySeverity(fieldStatuses, FieldStatusSeverity.ERROR)) {
        return new MessageResult("There were errors with the BlackDuck provider fields", fieldStatuses);
    }
    return new MessageResult("Successfully tested BlackDuck provider fields", fieldStatuses);
}
Also used : BlackDuckProvider(com.synopsys.integration.alert.provider.blackduck.BlackDuckProvider) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) ProviderDataAccessor(com.synopsys.integration.alert.common.persistence.accessor.ProviderDataAccessor) IntegrationException(com.synopsys.integration.exception.IntegrationException) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) AlertFieldStatus(com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus) LoggerFactory(org.slf4j.LoggerFactory) FieldStatusSeverity(com.synopsys.integration.alert.api.common.model.errors.FieldStatusSeverity) Autowired(org.springframework.beans.factory.annotation.Autowired) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Nullable(org.springframework.lang.Nullable) ProviderDescriptor(com.synopsys.integration.alert.api.provider.ProviderDescriptor) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) BlackDuckApiTokenValidator(com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckApiTokenValidator) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) AlertPagedModel(com.synopsys.integration.alert.common.rest.model.AlertPagedModel) Collectors(java.util.stream.Collectors) StatefulProvider(com.synopsys.integration.alert.api.provider.state.StatefulProvider) ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) BlackDuckProperties(com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties) FieldUtility(com.synopsys.integration.alert.common.persistence.accessor.FieldUtility) ProviderProject(com.synopsys.integration.alert.common.persistence.model.ProviderProject) List(java.util.List) Component(org.springframework.stereotype.Component) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) FieldModelTestAction(com.synopsys.integration.alert.common.action.FieldModelTestAction) ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) BlackDuckProperties(com.synopsys.integration.alert.provider.blackduck.BlackDuckProperties) ArrayList(java.util.ArrayList) AlertFieldStatus(com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) StatefulProvider(com.synopsys.integration.alert.api.provider.state.StatefulProvider) BlackDuckApiTokenValidator(com.synopsys.integration.alert.provider.blackduck.validator.BlackDuckApiTokenValidator)

Example 2 with AlertFieldStatus

use of com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus in project blackduck-alert by blackducksoftware.

the class ProxySettingsEnvironmentHandlerFactory method updateFunction.

private EnvironmentProcessingResult updateFunction() {
    EnvironmentProcessingResult.Builder builder = new EnvironmentProcessingResult.Builder(PROXY_CONFIGURATION_KEYSET);
    SettingsProxyModel configModel = new SettingsProxyModel();
    configModel.setName(AlertRestConstants.DEFAULT_CONFIGURATION_NAME);
    configureProxySettings(configModel);
    // TODO: Refactor and remove duplicate code after 6.10.0 model package refactor
    ValidationResponseModel validationResponseModel = validator.validate(configModel);
    if (validationResponseModel.hasErrors()) {
        logger.error("Error inserting startup values: {}", validationResponseModel.getMessage());
        Map<String, AlertFieldStatus> errors = validationResponseModel.getErrors();
        for (Map.Entry<String, AlertFieldStatus> error : errors.entrySet()) {
            AlertFieldStatus status = error.getValue();
            logger.error("Field: '{}' failed with the error: {}", status.getFieldName(), status.getFieldMessage());
        }
        return EnvironmentProcessingResult.empty();
    }
    SettingsProxyModel obfuscatedModel = configModel.obfuscate();
    obfuscatedModel.getProxyHost().ifPresent(value -> builder.addVariableValue(PROXY_HOST_KEY, value));
    obfuscatedModel.getProxyPort().map(String::valueOf).ifPresent(value -> builder.addVariableValue(PROXY_PORT_KEY, value));
    obfuscatedModel.getProxyUsername().ifPresent(value -> builder.addVariableValue(PROXY_USERNAME_KEY, value));
    obfuscatedModel.getNonProxyHosts().map(String::valueOf).ifPresent(value -> builder.addVariableValue(PROXY_NON_PROXY_HOSTS_KEY, value));
    if (Boolean.TRUE.equals(obfuscatedModel.getIsProxyPasswordSet())) {
        builder.addVariableValue(PROXY_PASSWORD_KEY, AlertConstants.MASKED_VALUE);
    }
    EnvironmentProcessingResult result = builder.build();
    if (result.hasValues()) {
        try {
            configAccessor.createConfiguration(configModel);
        } catch (AlertConfigurationException ex) {
            logger.error("Error creating the configuration: {}", ex.getMessage());
        }
    }
    return result;
}
Also used : ValidationResponseModel(com.synopsys.integration.alert.api.common.model.ValidationResponseModel) SettingsProxyModel(com.synopsys.integration.alert.common.rest.model.SettingsProxyModel) AlertFieldStatus(com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus) EnvironmentProcessingResult(com.synopsys.integration.alert.environment.EnvironmentProcessingResult) Map(java.util.Map) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)

Example 3 with AlertFieldStatus

use of com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus in project hub-alert by blackducksoftware.

the class EmailGlobalConfigurationValidatorTest method verifyEmptyConfig.

@Test
void verifyEmptyConfig() {
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailGlobalConfigModel model = new EmailGlobalConfigModel();
    ValidationResponseModel validationResponseModel = validator.validate(model);
    Collection<AlertFieldStatus> alertFieldStatuses = validationResponseModel.getErrors().values();
    assertEquals(3, alertFieldStatuses.size(), "Validation found more or fewer errors than expected.");
    for (AlertFieldStatus status : alertFieldStatuses) {
        assertEquals(ConfigurationFieldValidator.REQUIRED_FIELD_MISSING_MESSAGE, status.getFieldMessage(), "Validation had unexpected field message.");
    }
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) ValidationResponseModel(com.synopsys.integration.alert.api.common.model.ValidationResponseModel) AlertFieldStatus(com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus) Test(org.junit.jupiter.api.Test)

Example 4 with AlertFieldStatus

use of com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus in project hub-alert by blackducksoftware.

the class EmailGlobalConfigurationValidatorTest method verifyMissingAuth.

@Test
void verifyMissingAuth() {
    EmailGlobalConfigurationValidator validator = new EmailGlobalConfigurationValidator();
    EmailGlobalConfigModel model = new EmailGlobalConfigModel(null, AlertRestConstants.DEFAULT_CONFIGURATION_NAME, "from", "host");
    model.setSmtpAuth(true);
    ValidationResponseModel validationResponseModel = validator.validate(model);
    Collection<AlertFieldStatus> alertFieldStatuses = validationResponseModel.getErrors().values();
    assertEquals(2, alertFieldStatuses.size(), "Validation found more or fewer errors than expected.");
    for (AlertFieldStatus status : alertFieldStatuses) {
        assertEquals(EmailGlobalConfigurationValidator.REQUIRED_BECAUSE_AUTH, status.getFieldMessage(), "Validation had unexpected field message.");
    }
}
Also used : EmailGlobalConfigModel(com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel) ValidationResponseModel(com.synopsys.integration.alert.api.common.model.ValidationResponseModel) AlertFieldStatus(com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus) Test(org.junit.jupiter.api.Test)

Example 5 with AlertFieldStatus

use of com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus in project hub-alert by blackducksoftware.

the class ResponseBodyBuilder method putErrors.

public ResponseBodyBuilder putErrors(List<AlertFieldStatus> errors) {
    JsonObject element = new JsonObject();
    for (AlertFieldStatus alertFieldStatus : errors) {
        JsonObject statusObject = new JsonObject();
        statusObject.addProperty(PROPERTY_KEY_SEVERITY, alertFieldStatus.getSeverity().name());
        statusObject.addProperty(PROPERTY_KEY_FIELD_MESSAGE, alertFieldStatus.getFieldMessage());
        element.add(alertFieldStatus.getFieldName(), statusObject);
    }
    map.add(PROPERTY_KEY_ERRORS, element);
    return this;
}
Also used : JsonObject(com.google.gson.JsonObject) AlertFieldStatus(com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus)

Aggregations

AlertFieldStatus (com.synopsys.integration.alert.api.common.model.errors.AlertFieldStatus)108 ValidationResponseModel (com.synopsys.integration.alert.api.common.model.ValidationResponseModel)54 Test (org.junit.jupiter.api.Test)52 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)22 ArrayList (java.util.ArrayList)22 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)20 FieldValueModel (com.synopsys.integration.alert.common.rest.model.FieldValueModel)18 HashSet (java.util.HashSet)18 Set (java.util.Set)18 Autowired (org.springframework.beans.factory.annotation.Autowired)18 Component (org.springframework.stereotype.Component)18 JiraServerGlobalConfigAccessor (com.synopsys.integration.alert.channel.jira.server.database.accessor.JiraServerGlobalConfigAccessor)16 JiraServerGlobalConfigModel (com.synopsys.integration.alert.channel.jira.server.model.JiraServerGlobalConfigModel)16 AlertFieldException (com.synopsys.integration.alert.common.exception.AlertFieldException)16 IntegrationException (com.synopsys.integration.exception.IntegrationException)16 List (java.util.List)16 Optional (java.util.Optional)16 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)15 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)14 JobFieldModel (com.synopsys.integration.alert.common.rest.model.JobFieldModel)14