}
private static void validateMetric(MetricDto metric, Errors errors) {
- errors.check(isAlertable(metric), format("Metric '%s' cannot be used to define a condition.", metric.getKey()));
+ check(errors, isAlertable(metric), format("Metric '%s' cannot be used to define a condition.", metric.getKey()));
}
private static boolean isAlertable(MetricDto metric) {
private static void checkOperator(MetricDto metric, String operator, Errors errors) {
ValueType valueType = valueOf(metric.getValueType());
- errors.check(isOperatorAllowed(operator, valueType), format("Operator %s is not allowed for metric type %s.", operator, metric.getValueType()));
+ check(errors, isOperatorAllowed(operator, valueType), format("Operator %s is not allowed for metric type %s.", operator, metric.getValueType()));
}
private static void checkThresholds(@Nullable String warningThreshold, @Nullable String errorThreshold, Errors errors) {
- errors.check(warningThreshold != null || errorThreshold != null, "At least one threshold (warning, error) must be set.");
+ check(errors, warningThreshold != null || errorThreshold != null, "At least one threshold (warning, error) must be set.");
}
private static void checkPeriod(MetricDto metric, @Nullable Integer period, Errors errors) {
if (period == null) {
- errors.check(!metric.getKey().startsWith("new_"), "A period must be selected for differential metrics.");
+ check(errors, !metric.getKey().startsWith("new_"), "A period must be selected for differential metrics.");
} else {
- errors.check(period == 1, "The only valid quality gate period is 1, the leak period.");
+ check(errors, period == 1, "The only valid quality gate period is 1, the leak period.");
}
}
}
private static void checkRatingGreaterThanOperator(@Nullable String value, Errors errors) {
- errors.check(isNullOrEmpty(value) || !Objects.equals(toRating(value), E), format("There's no worse rating than E (%s)", value));
+ check(errors, isNullOrEmpty(value) || !Objects.equals(toRating(value), E), format("There's no worse rating than E (%s)", value));
}
private static Rating toRating(String value) {
private static boolean isValidRating(@Nullable String value) {
return isNullOrEmpty(value) || RATING_VALID_INT_VALUES.contains(value);
}
+
+ private static boolean check(Errors errors, boolean expression, String message) {
+ if (!expression) {
+ errors.add(Message.of(message));
+ }
+ return expression;
+ }
}
private void checkQgateNotAlreadyExists(@Nullable Long updatingQgateId, String name, Errors errors) {
QualityGateDto existingQgate = dao.selectByName(name);
boolean isModifyingCurrentQgate = updatingQgateId != null && existingQgate != null && existingQgate.getId().equals(updatingQgateId);
- errors.check(isModifyingCurrentQgate || existingQgate == null, Validation.IS_ALREADY_USED_MESSAGE, "Name");
+ if (!isModifyingCurrentQgate && existingQgate != null) {
+ errors.add(Message.of(Validation.IS_ALREADY_USED_MESSAGE, "Name"));
+ }
}
private void checkIsSystemAdministrator() {