com.rosetta.jptelections.model.validation.SomeElectionWrapperValidator Maven / Gradle / Ivy
package com.rosetta.jptelections.model.validation;
import com.google.common.collect.Lists;
import com.rosetta.jptelections.model.Election;
import com.rosetta.jptelections.model.SomeElectionWrapper;
import com.rosetta.model.lib.expression.ComparisonResult;
import com.rosetta.model.lib.path.RosettaPath;
import com.rosetta.model.lib.validation.ValidationResult;
import com.rosetta.model.lib.validation.ValidationResult.ValidationType;
import com.rosetta.model.lib.validation.Validator;
import java.util.List;
import static com.google.common.base.Strings.isNullOrEmpty;
import static com.rosetta.model.lib.expression.ExpressionOperators.checkCardinality;
import static com.rosetta.model.lib.validation.ValidationResult.failure;
import static com.rosetta.model.lib.validation.ValidationResult.success;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class SomeElectionWrapperValidator implements Validator {
private List getComparisonResults(SomeElectionWrapper o) {
return Lists.newArrayList(
checkCardinality("name", (String) o.getName() != null ? 1 : 0, 1, 1),
checkCardinality("concreteElection", (List extends Election>) o.getConcreteElection() == null ? 0 : ((List extends Election>) o.getConcreteElection()).size(), 2, 2)
);
}
@Override
public ValidationResult validate(RosettaPath path, SomeElectionWrapper o) {
String error = getComparisonResults(o)
.stream()
.filter(res -> !res.get())
.map(res -> res.getError())
.collect(joining("; "));
if (!isNullOrEmpty(error)) {
return failure("SomeElectionWrapper", ValidationType.CARDINALITY, "SomeElectionWrapper", path, "", error);
}
return success("SomeElectionWrapper", ValidationType.CARDINALITY, "SomeElectionWrapper", path, "");
}
@Override
public List> getValidationResults(RosettaPath path, SomeElectionWrapper o) {
return getComparisonResults(o)
.stream()
.map(res -> {
if (!isNullOrEmpty(res.getError())) {
return failure("SomeElectionWrapper", ValidationType.CARDINALITY, "SomeElectionWrapper", path, "", res.getError());
}
return success("SomeElectionWrapper", ValidationType.CARDINALITY, "SomeElectionWrapper", path, "");
})
.collect(toList());
}
}