Skip to content

Commit

Permalink
change validation process to be less burdensome donwstream
Browse files Browse the repository at this point in the history
  • Loading branch information
sambish5 committed Sep 26, 2024
1 parent 0e0e0c8 commit 4e39266
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions src/test/java/emissary/test/core/junit5/ExtractionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public abstract class ExtractionTest extends UnitTest {
private static final List<IBaseDataObject> NO_ATTACHMENTS = Collections.emptyList();
private static final byte[] INCORRECT_VIEW_MESSAGE = "This is the incorrect view, the place should not have processed this view".getBytes();

private boolean skipAttCountValidation = false;
private boolean skipExtractCountValidation = false;

protected KffDataObjectHandler kff =
new KffDataObjectHandler(KffDataObjectHandler.TRUNCATE_KNOWN_DATA, KffDataObjectHandler.SET_FORM_WHEN_KNOWN,
KffDataObjectHandler.SET_FILE_TYPE);
Expand Down Expand Up @@ -177,19 +174,25 @@ protected void checkAnswers(Element el, IBaseDataObject payload, @Nullable List<

int numAtt = JDOMUtil.getChildIntValue(el, "numAttachments");
long numAttElements = el.getChildren().stream().filter(c -> c.getName().startsWith("att")).count();
if (numAtt > -1 || numAttElements > 0) {
if (!skipAttCountValidation) {
assertEquals(Math.max(numAtt, 0), Math.toIntExact(numAttElements),
"Expected numAttachments in " + tname + " not equal to number of <att#> elements");
} else {
if (Math.toIntExact(numAttElements) != numAtt) {
logger.warn("Expected numAttachments and actual <att#> count in {} not equal. Expected: {} Actual: {}", tname,
Math.max(numAtt, 0), numAttElements);
}
// check attachments answer file count against payload count
if (numAtt > -1) {
assertEquals(numAtt, attachments != null ? attachments.size() : 0,
String.format("Expected <numAttachments> in %s not equal to number of att in payload.", tname));
} else if (numAtt == -1 && numAttElements > 0) {
assertEquals(numAttElements, attachments != null ? attachments.size() : 0,
String.format("Expected <att#> in %s not equal to number of att in payload.", tname));
} else {
if (attachments != null && !attachments.isEmpty()) {
fail(String.format("%d attachments in payload with no count in answer xml, add matching <numAttachments> count for %s",
attachments.size(), tname));
}
}
if (numAtt > -1) {
assertEquals(numAtt, attachments != null ? attachments.size() : 0, "Number of attachments in " + tname);
// log if <numAttachments> and <att#> don't match in answer file
if (numAtt > -1 || numAttElements > 0) {
if (Math.toIntExact(numAttElements) != numAtt) {
logger.warn("Expected and actual attachment counts in {} not equal. <numAttachments>: {} <att#>: {}", tname,
Math.max(numAtt, 0), numAttElements);
}
}

for (Element currentForm : el.getChildren("currentForm")) {
Expand Down Expand Up @@ -293,31 +296,23 @@ protected void checkAnswers(Element el, IBaseDataObject payload, @Nullable List<
assertNull(viewData, String.format("Alternate View '%s' is present, but should not be, in %s", viewName, 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 (extractCount > -1 || numExtractElements > 0) {
if (!skipExtractCountValidation) {
assertEquals(Math.max(extractCount, 0), Math.toIntExact(numExtractElements),
"Expected extractCount in " + tname + " not equal to number of <extract#> elements");
} else {
if (Math.toIntExact(numExtractElements) != extractCount) {
logger.warn("Expected extractCount and actual <extract#> count in {} not equal. Expected: {} Actual: {}", tname,
Math.max(extractCount, 0), numExtractElements);
}
}
}

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

// check extracted records answer file count against payload count
if (extractCount > -1) {
assertEquals(extractCount, foundCount,
String.format("Number of extracted children in '%s' is %s, not expected %s", tname, foundCount,
extractCount));
String.format("Expected <extractCount> in %s not equal to number of extracts in payload.", tname));
} else if (extractCount == -1 && numExtractElements > 0) {
assertEquals(numExtractElements, foundCount,
String.format("Expected <extract#> in %s not equal to number of extracts in payload.", tname));
} else {
fail(String.format("%d extracts in payload with no count in answer xml, add matching <extractCount> count for %s",
foundCount, tname));
}

int attNum = 1;
Expand All @@ -331,7 +326,17 @@ protected void checkAnswers(Element el, IBaseDataObject payload, @Nullable List<
} else {
if (extractCount > -1) {
assertEquals(0, extractCount,
String.format("No extracted children in '%s' when expecting %s", tname, extractCount));
String.format("No extracted children in '%s' when <extractCount> is %d", tname, extractCount));
} else if (numExtractElements > 0) {
assertEquals(0, numExtractElements,
String.format("No extracted children in '%s' when <extract#> is %d", tname, numExtractElements));
}
}
// log if <extractCount> and <extract#> don't match in answer file
if (extractCount > -1 || numExtractElements > 0) {
if (Math.toIntExact(numExtractElements) != extractCount) {
logger.warn("Expected and actual extract counts in {} not equal. <extractCount>: {} <extract#>: {}", tname,
Math.max(extractCount, 0), numExtractElements);
}
}
}
Expand Down Expand Up @@ -448,13 +453,4 @@ protected void setupPayload(IBaseDataObject payload, Document doc) {
payload.setFileType(payload.currentForm());
}
}

// allow the validation of att and ext counts to be skipped/not logged in tests
protected void setAttachmentCountValidation(boolean value) {
this.skipAttCountValidation = !value;
}

protected void setExtractCountValidation(boolean value) {
this.skipExtractCountValidation = !value;
}
}

0 comments on commit 4e39266

Please sign in to comment.