Skip to content

Commit

Permalink
added new validation types
Browse files Browse the repository at this point in the history
  • Loading branch information
rsehr committed Dec 3, 2024
1 parent 7d24cc8 commit b27ef9f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private void readChecks() {
break;
}
}

}

//ADD NEW CHECKS HERE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.text.StringSubstitutor;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
Expand All @@ -27,8 +28,9 @@ public class TifValidationSimpleXpathCheck implements TifValidationCheck {
private String errorMessage;
private Map<String, String> replaceMap;

// possible values: equals, greater, lesser, exists, not exists, same
// possible values: equals, greater, lesser, exists, not exists, same, multiple
private String checkType = "equals";
private String otherXpath;

public TifValidationSimpleXpathCheck(Set<Namespace> namespaces, String xpath, String expectedValue, String errorMessage) {
super();
Expand Down Expand Up @@ -69,11 +71,65 @@ public boolean check(Document doc) {
value = value.toString();
}
this.replaceMap.put("found", (String) value);

String val = (String) value;
switch (checkType) {
case "exists":
return true;

case "greater":

if (NumberUtils.isCreatable(val)) {
if (val.contains(".")) {
double expected = Double.parseDouble(expectedValue);
double d = Double.parseDouble(val);
return expected <= d;
} else {
int expected = Integer.parseInt(expectedValue);
int i = Integer.parseInt(val);
return expected <= i;
}
}
// no number
return false;
case "lesser":
if (NumberUtils.isCreatable(val)) {
if (val.contains(".")) {
double expected = Double.parseDouble(expectedValue);
double d = Double.parseDouble(val);
return expected >= d;
} else {
int expected = Integer.parseInt(expectedValue);
int i = Integer.parseInt(val);
return expected >= i;
}
}
// no number
return false;
case "same":
XPathExpression<Object> o = xFactory.compile(otherXpath, Filters.fpassthrough(), null, namespaces);
Object otherValue = o.evaluateFirst(doc);
if (otherValue instanceof Element e) {
otherValue = e.getTextTrim();
} else if (otherValue instanceof Attribute a) {
otherValue = a.getValue();
} else if (otherValue instanceof Text t) {
otherValue = t.getText();
} else if (!(otherValue instanceof String)) {
otherValue = otherValue.toString();
}
return value.equals(otherValue);
case "multiple":
if (NumberUtils.isCreatable(val)) {
int expexted = Integer.parseInt(expectedValue);
int actual;
if (val.contains(".")) {
actual = (int) Double.parseDouble(val);
} else {
actual = Integer.parseInt(val);
}
return actual % expexted == 0;
}
// no number
return false;
case "equals": {
return this.expectedValue.equals(value) || (value instanceof String s && s.matches(this.expectedValue));
}
Expand All @@ -86,7 +142,7 @@ public boolean check(Document doc) {
@Override
public String getFormattedError(String imageName) {
this.replaceMap.put("image", imageName);
return StrSubstitutor.replace(errorMessage, replaceMap);
return StringSubstitutor.replace(errorMessage, replaceMap);
}

}

0 comments on commit b27ef9f

Please sign in to comment.