Skip to content

Commit

Permalink
rebased and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sambish5 committed Sep 19, 2024
1 parent 9d132c8 commit 8efa6fd
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/test/java/emissary/test/core/junit5/ExtractionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ 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 @@ -78,7 +81,7 @@ public static Stream<? extends Arguments> data() {

/**
* Allow overriding the initial form in extensions to this test.
*
*
* By default, get the initial form from the filename in the form {@code [email protected]} where {@code INITIAL_FORM}
* will be the initial form.
*
Expand Down Expand Up @@ -173,6 +176,18 @@ protected void checkAnswers(Element el, IBaseDataObject payload, @Nullable List<
throws DataConversionException {

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);
}
}
}
if (numAtt > -1) {
assertEquals(numAtt, attachments != null ? attachments.size() : 0, "Number of attachments in " + tname);
}
Expand Down Expand Up @@ -280,15 +295,29 @@ protected void checkAnswers(Element el, IBaseDataObject payload, @Nullable List<


// Check each extract
String extractCountStr = el.getChildTextTrim("extractCount");
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();

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

int attNum = 1;
Expand All @@ -300,9 +329,9 @@ protected void checkAnswers(Element el, IBaseDataObject payload, @Nullable List<
attNum++;
}
} else {
if (extractCountStr != null) {
assertEquals(0, Integer.parseInt(extractCountStr),
String.format("No extracted children in '%s' when expecting %s", tname, extractCountStr));
if (extractCount > -1) {
assertEquals(0, extractCount,
String.format("No extracted children in '%s' when expecting %s", tname, extractCount));
}
}
}
Expand Down Expand Up @@ -419,4 +448,13 @@ 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 8efa6fd

Please sign in to comment.