Skip to content

Commit

Permalink
Reworking pr to only log when there are count differences
Browse files Browse the repository at this point in the history
  • Loading branch information
sambish5 committed Feb 16, 2024
1 parent 176a5c7 commit 85c96bb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 156 deletions.
137 changes: 22 additions & 115 deletions src/test/java/emissary/test/core/junit5/ExtractionTest.java
Expand Up @@ -171,17 +171,14 @@ protected void checkAnswers(Document answers, IBaseDataObject payload, List<IBas
}

protected void checkAnswers(Element el, IBaseDataObject payload, List<IBaseDataObject> attachments, String tname) throws DataConversionException {
// see if <skipAttExtCountCheck> exists in answer file and if the text != true.
String skipAttExtCountCheck = el.getChildTextTrim("skipAttExtCountCheck");
if (skipAttExtCountCheck == null || !skipAttExtCountCheck.equalsIgnoreCase("true")) {
// see if payload has extracted records or see if attachments is not null
if (attachments != null || payload.hasExtractedRecords()) {
// implement check on IBaseDataObject payload and attachments to compare expected v found counts
checkAttachmentAndExtractedRecordCount(el, payload, attachments, tname);
} else {
// implement base check of resource answer file
checkAttachmentAndExtractedRecordCount(el, tname);
}
int numAtt = JDOMUtil.getChildIntValue(el, "numAttachments");
long numAttElements = el.getChildren().stream().filter(c -> c.getName().startsWith("att")).count();
if (Math.toIntExact(numAttElements) != numAtt && !(numAttElements <= 0 && numAtt == -1)) {
logger.warn("Expected numAttachments and actual <att#> count in {} not equal. Expected: {} Actual: {}", tname, Math.max(numAtt, 0),
numAttElements);
}
if (numAtt > -1) {
assertEquals(numAtt, attachments != null ? attachments.size() : 0, "Number of attachments in " + tname);
}

for (Element currentForm : el.getChildren("currentForm")) {
Expand Down Expand Up @@ -286,27 +283,25 @@ protected void checkAnswers(Element el, IBaseDataObject payload, List<IBaseDataO
byte[] viewData = payload.getAlternateView(viewName);
assertNull(viewData, String.format("Alternate View '%s' is present, but should not be, in %s", viewName, tname));
}
}

public void checkAttachmentAndExtractedRecordCount(Element el, IBaseDataObject payload, List<IBaseDataObject> attachments, String tname)
throws DataConversionException {
// check List<IBaseDataObject> attachments
int numAtt = JDOMUtil.getChildIntValue(el, "numAttachments");
if (numAtt > -1) {
assertEquals(numAtt, attachments != null ? attachments.size() : 0, "Number of attachments in " + tname);
// Check each extract
int extractCount = JDOMUtil.getChildIntValue(el, "extractCount");
long numExtractElements =
el.getChildren().stream().filter(c -> c.getName().startsWith("extract") && !c.getName().startsWith("extractCount")).count();
if (Math.toIntExact(numExtractElements) != extractCount && !(numExtractElements <= 0 && extractCount == -1)) {
logger.warn("Expected extractCount and actual <extract#> count in {} not equal. Expected: {} Actual: {}", tname,
Math.max(extractCount, 0),
numExtractElements);
}

// Check each extract based on payload
String extractCountStr = el.getChildTextTrim("extractCount");

if (payload.hasExtractedRecords()) {
List<IBaseDataObject> extractedChildren = payload.getExtractedRecords();
int foundCount = extractedChildren.size();

if (extractCountStr != null) {
assertEquals(Integer.parseInt(extractCountStr), foundCount,
if (extractCount > -1) {
assertEquals(extractCount, foundCount,
String.format("Number of extracted children in '%s' is %s, not expected %s", tname, foundCount,
extractCountStr));
extractCount));
}

int attNum = 1;
Expand All @@ -318,98 +313,10 @@ public void checkAttachmentAndExtractedRecordCount(Element el, IBaseDataObject p
attNum++;
}
} else {
if (extractCountStr != null) {
assertEquals(Integer.parseInt(extractCountStr), 0,
String.format("No extracted children in '%s' when expecting %s", tname, extractCountStr));
}
}

// check resource file
checkAttachmentAndExtractedRecordCount(el, tname);
}

public void checkAttachmentAndExtractedRecordCount(Element parent, String tname) {
// initialize attachment/extracted record counts to 0
int attExpectedCount = 0;
int attFoundCount = 0;
int extExpectedCount = 0;
int extFoundCount = 0;

// initialize att and ext expected to false, if found, then will be true
boolean attExpected = false;
boolean extExpected = false;

// used to see if number is present between <numAttachments> and <extractCount>
// assumed true until child is found with defining number not present, then false
boolean missingNumAttachments = true;
boolean missingExtCount = true;

// go through children list to find 4 possibilities:
// expected counts of <numAttachments> & <extractCount>
// found counts of <att> and <extract>
List<Element> children = parent.getChildren();
for (Element child : children) {
String currentChild = child.toString();

if (currentChild.contains("numAttachments")) {
String attCountStr = parent.getChildTextTrim("numAttachments");
// sees if number is present between <numAttachments> tag
if (!attCountStr.equals("")) {
attExpectedCount = Integer.parseInt(attCountStr);
attExpected = true;
} else {
missingNumAttachments = false;
}
}
if (currentChild.contains("att")) {
attFoundCount += 1;
}

if (currentChild.contains("extractCount")) {
String extractCountStr = parent.getChildTextTrim("extractCount");
if (!extractCountStr.equals("")) {
extExpectedCount = Integer.parseInt(extractCountStr);
extExpected = true;
} else {
missingExtCount = false;
}
}
if (currentChild.contains("extract") && !currentChild.contains("extractCount")) {
extFoundCount += 1;
}
}

// first check if <numAttachments> and <extractCount> have int between tags
// check if <numAttachments> and <extractCount> are missing from file and <att> and <extract> still exist
// make sure expected = found count
// formats fail string output
if (attExpectedCount != attFoundCount || extExpectedCount != extFoundCount || !missingNumAttachments || !missingExtCount) {
String failResult = "Errors in " + tname + ": ";
if (!missingNumAttachments) {
failResult += "<numAttachments> missing int between tags. ";
} else {
if (!attExpected && (attFoundCount > 0)) {
failResult += "<numAttachments> missing from answer file, but attachments found: <" + attFoundCount + ">. ";
} else {
if (attExpectedCount != attFoundCount) {
failResult +=
"<numAttachments> expected:<" + attExpectedCount + "> but found: <" + attFoundCount + "> in answer file. ";
}
}
}
if (!missingExtCount) {
failResult += "<extractCount> missing int between tags. ";
} else {
if (!extExpected && (extFoundCount > 0)) {
failResult += "<extractCount> missing from answer file, but extracts found: <" + extFoundCount + ">. ";
} else {
if (extExpectedCount != extFoundCount) {
failResult += "<extractCount> expected:<" + extExpectedCount + "> but found: <"
+ extFoundCount + "> in answer file. ";
}
}
if (extractCount > -1) {
assertEquals(0, extractCount,
String.format("No extracted children in '%s' when expecting %s", tname, extractCount));
}
fail(failResult);
}
}

Expand Down

This file was deleted.

16 changes: 0 additions & 16 deletions src/test/resources/emissary/test/core/TestExtractionTest.xml

This file was deleted.

0 comments on commit 85c96bb

Please sign in to comment.