com.neotys.neoload.model.validation.validator.Validator Maven / Gradle / Ivy
package com.neotys.neoload.model.validation.validator;
import com.neotys.neoload.model.validation.naming.LoadPolicyStrategy;
import com.neotys.neoload.model.validation.naming.PropertyNamingStrategy;
import com.neotys.neoload.model.validation.naming.RampupStrategy;
import com.neotys.neoload.model.validation.naming.SnakeCaseStrategy;
import javax.validation.Configuration;
import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.validation.Path.Node;
import javax.validation.ValidatorFactory;
import java.util.*;
/**
* @deprecated As of v3, replaced by an associated class from v3 version.
*/
@Deprecated
public final class Validator {
private static final String PROPERTY_PATH_SEPARATOR = ".";
private static final String LINE_SEPARATOR = System.lineSeparator();
private static final Map PROPERTY_NAMING_STRATEGIES;
static {
PROPERTY_NAMING_STRATEGIES = new HashMap<>();
PROPERTY_NAMING_STRATEGIES.put("loadPolicy", new LoadPolicyStrategy());
PROPERTY_NAMING_STRATEGIES.put("rampup", new RampupStrategy());
}
private static final PropertyNamingStrategy defaultNamingStrategy = new SnakeCaseStrategy();
public Validator() {
super();
}
public Validation validate(final T object, Class>... groups) {
// validate the specified object
final Configuration> configuration = javax.validation.Validation.byDefaultProvider().configure();
configuration.messageInterpolator(new ResourceBundleMessageInEnglishInterpolator());
final ValidatorFactory factory = configuration.buildValidatorFactory();
final javax.validation.Validator validator = factory.getValidator();
final Set> violations = validator.validate(object, groups);
// Manages the results
final int size = violations.size();
if (size != 0) {
final StringBuilder sb = new StringBuilder();
sb.append("Data Model is invalid. Violation Number: ").append(size).append(".");
sb.append(LINE_SEPARATOR);
int count = 1;
final List errors = normalizeErrors(violations);
for (final String error : errors) {
sb.append("Violation " + count + " - " + error);
sb.append(LINE_SEPARATOR);
count = count + 1;
}
return Validation.builder()
.message(sb.toString())
.isValid(false)
.build();
}
return Validation.builder()
.isValid(true)
.build();
}
protected List normalizeErrors(final Set> violations) {
final List errors = new ArrayList<>();
for (final ConstraintViolation violation : violations) {
errors.add("Incorrect value for '"+ normalizePath(violation.getPropertyPath()) + "': " + violation.getMessage());
}
Collections.sort(errors);
return errors;
}
protected String normalizePath(final Path path) {
if (path == null) {
return "N/A";
}
StringBuilder builder = new StringBuilder();
boolean first = true;
for (final Node node : path) {
final String input = node.toString();
if (input.isEmpty()) {
continue;
}
if (!first) {
builder.append(PROPERTY_PATH_SEPARATOR);
}
PropertyNamingStrategy namingStrategy = PROPERTY_NAMING_STRATEGIES.get(input);
if (namingStrategy == null) {
namingStrategy = defaultNamingStrategy;
}
final String output = namingStrategy.apply(node);
builder.append(output);
first = false;
}
return builder.toString();
}
}