<commit_before>package com.sequenceiq.provisioning.controller.validation;

import java.util.ArrayList;
import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.sequenceiq.provisioning.controller.json.ProvisionRequest;
import com.sequenceiq.provisioning.controller.json.RequiredAWSRequestParam;

/**
 * Validates a provision request. Because different parameters belong to
 * different cloud platforms, they must be validated accordingly. This class
 * checks if the required parameters are present and if they match the required
 * format.
 */
public class ProvisionParametersValidator implements ConstraintValidator<ValidProvisionRequest, ProvisionRequest> {

    private List<String> requiredAWSParams = new ArrayList<>();
    private List<String> requiredAzureParams = new ArrayList<>();

    @Override
    public void initialize(ValidProvisionRequest constraintAnnotation) {
        for (RequiredAWSRequestParam param : RequiredAWSRequestParam.values()) {
            requiredAWSParams.add(param.getName());
        }
    }

    @Override
    public boolean isValid(ProvisionRequest request, ConstraintValidatorContext context) {
        boolean valid = true;
        switch (request.getCloudPlatform()) {
        case AWS:
            valid = validateAWSParams(request, context);
            break;
        case AZURE:
            valid = validateAzureParams(request, context);
            break;
        default:
            break;
        }
        return valid;

    }

    private boolean validateAzureParams(ProvisionRequest request, ConstraintValidatorContext context) {
        boolean valid = true;
        for (String param : requiredAzureParams) {
            if (!request.getParameters().containsKey(param)) {
                addParameterConstraintViolation(context, param, String.format("%s is required.", param));
                valid = false;
            }
        }
        return valid;
    }

    private boolean validateAWSParams(ProvisionRequest request, ConstraintValidatorContext context) {
        boolean valid = true;
        for (String param : requiredAWSParams) {
            if (!request.getParameters().containsKey(param)) {
                addParameterConstraintViolation(context, param, String.format("%s is required.", param));
                valid = false;
            }
        }
        // TODO: validate instanceType, sshLocation, etc.. with regex
        return valid;
    }

    private void addParameterConstraintViolation(ConstraintValidatorContext context, String key, String message) {
        context.buildConstraintViolationWithTemplate(message)
                .addPropertyNode("parameters")
                .addBeanNode().inIterable().atKey(key)
                .addConstraintViolation();
    }
}
<commit_msg>code duplication removed<commit_after>package com.sequenceiq.provisioning.controller.validation;

import java.util.ArrayList;
import java.util.List;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.sequenceiq.provisioning.controller.json.ProvisionRequest;
import com.sequenceiq.provisioning.controller.json.RequiredAWSRequestParam;

/**
 * Validates a provision request. Because different parameters belong to
 * different cloud platforms, they must be validated accordingly. This class
 * checks if the required parameters are present and if they match the required
 * format.
 */
public class ProvisionParametersValidator implements ConstraintValidator<ValidProvisionRequest, ProvisionRequest> {

    private List<String> requiredAWSParams = new ArrayList<>();
    private List<String> requiredAzureParams = new ArrayList<>();

    @Override
    public void initialize(ValidProvisionRequest constraintAnnotation) {
        for (RequiredAWSRequestParam param : RequiredAWSRequestParam.values()) {
            requiredAWSParams.add(param.getName());
        }
    }

    @Override
    public boolean isValid(ProvisionRequest request, ConstraintValidatorContext context) {
        boolean valid = true;
        switch (request.getCloudPlatform()) {
        case AWS:
            valid = validateAWSParams(request, context);
            break;
        case AZURE:
            valid = validateAzureParams(request, context);
            break;
        default:
            break;
        }
        return valid;

    }

    private boolean validateAzureParams(ProvisionRequest request, ConstraintValidatorContext context) {
        return validateRequiredParameters(request, context, requiredAzureParams);
    }

    private boolean validateAWSParams(ProvisionRequest request, ConstraintValidatorContext context) {
        // TODO: validate instanceType, sshLocation, etc.. with regex
        return validateRequiredParameters(request, context, requiredAWSParams);
    }

    private boolean validateRequiredParameters(ProvisionRequest request, ConstraintValidatorContext context, List<String> requiredParams) {
        boolean valid = true;
        for (String param : requiredParams) {
            if (!request.getParameters().containsKey(param)) {
                addParameterConstraintViolation(context, param, String.format("%s is required.", param));
                valid = false;
            }
        }
        return valid;
    }

    private void addParameterConstraintViolation(ConstraintValidatorContext context, String key, String message) {
        context.buildConstraintViolationWithTemplate(message)
                .addPropertyNode("parameters")
                .addBeanNode().inIterable().atKey(key)
                .addConstraintViolation();
    }
}

{'fraction_non_alphanumeric': 0.07836216025414755, 'fraction_numerical': 0.0, 'mean_word_length': 3.154692082111437, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3115351', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1291, 'n_words': 339}