Fixed Tests for PDFValidatior, switched to using ProcessingResults
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
package org.mustangproject.ZUGFeRD;
|
||||||
|
|
||||||
|
public class Version {
|
||||||
|
public static final String VERSION = "2.9.0";
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@ import org.verapdf.features.FeatureFactory;
|
|||||||
import org.verapdf.metadata.fixer.FixerFactory;
|
import org.verapdf.metadata.fixer.FixerFactory;
|
||||||
import org.verapdf.metadata.fixer.MetadataFixerConfig;
|
import org.verapdf.metadata.fixer.MetadataFixerConfig;
|
||||||
import org.verapdf.gf.foundry.VeraGreenfieldFoundryProvider;
|
import org.verapdf.gf.foundry.VeraGreenfieldFoundryProvider;
|
||||||
|
import org.verapdf.pdfa.flavours.PDFAFlavour;
|
||||||
import org.verapdf.pdfa.validation.validators.ValidatorConfig;
|
import org.verapdf.pdfa.validation.validators.ValidatorConfig;
|
||||||
import org.verapdf.pdfa.validation.validators.ValidatorFactory;
|
import org.verapdf.pdfa.validation.validators.ValidatorFactory;
|
||||||
import org.verapdf.processor.BatchProcessor;
|
import org.verapdf.processor.BatchProcessor;
|
||||||
@@ -42,6 +43,7 @@ import org.verapdf.processor.FormatOption;
|
|||||||
import org.verapdf.processor.ItemProcessor;
|
import org.verapdf.processor.ItemProcessor;
|
||||||
import org.verapdf.processor.ProcessorConfig;
|
import org.verapdf.processor.ProcessorConfig;
|
||||||
import org.verapdf.processor.ProcessorFactory;
|
import org.verapdf.processor.ProcessorFactory;
|
||||||
|
import org.verapdf.processor.ProcessorResult;
|
||||||
import org.verapdf.processor.TaskType;
|
import org.verapdf.processor.TaskType;
|
||||||
import org.verapdf.processor.plugins.PluginsCollectionConfig;
|
import org.verapdf.processor.plugins.PluginsCollectionConfig;
|
||||||
import org.verapdf.processor.reports.ItemDetails;
|
import org.verapdf.processor.reports.ItemDetails;
|
||||||
@@ -57,12 +59,14 @@ public class PDFValidator extends Validator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(PDFValidator.class.getCanonicalName()); // log output
|
private static final Logger LOGGER = LoggerFactory.getLogger(PDFValidator.class.getCanonicalName()); // log output
|
||||||
|
private static final PDFAFlavour[] PDF_A_3_FLAVOURS = {PDFAFlavour.PDFA_3_A, PDFAFlavour.PDFA_3_A, PDFAFlavour.PDFA_3_A};
|
||||||
|
|
||||||
private String pdfFilename;
|
private String pdfFilename;
|
||||||
|
|
||||||
private byte[] fileContents;
|
private byte[] fileContents;
|
||||||
|
|
||||||
private String pdfReport;
|
private String pdfReport;
|
||||||
|
private ProcessorResult processorResult = null;
|
||||||
|
|
||||||
private String Signature;
|
private String Signature;
|
||||||
|
|
||||||
@@ -113,8 +117,8 @@ public class PDFValidator extends Validator {
|
|||||||
// starting the processor
|
// starting the processor
|
||||||
ItemDetails itemDetails = ItemDetails.fromValues(pdfFilename);
|
ItemDetails itemDetails = ItemDetails.fromValues(pdfFilename);
|
||||||
inputStream.mark(Integer.MAX_VALUE);
|
inputStream.mark(Integer.MAX_VALUE);
|
||||||
processor.process(itemDetails, inputStream);
|
processorResult = processor.process(itemDetails, inputStream);
|
||||||
pdfReport = reportStream.toString("utf-8").replaceAll(
|
pdfReport = processorResult.getValidationResult().toString().replaceAll(
|
||||||
"<\\?xml version=\"1\\.0\" encoding=\"utf-8\"\\?>",
|
"<\\?xml version=\"1\\.0\" encoding=\"utf-8\"\\?>",
|
||||||
""
|
""
|
||||||
);
|
);
|
||||||
@@ -252,14 +256,7 @@ public class PDFValidator extends Validator {
|
|||||||
.setSection(16).setPart(EPart.pdf));
|
.setSection(16).setPart(EPart.pdf));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
} catch (final SAXException | IOException | ParserConfigurationException | XPathExpressionException e) {
|
||||||
} catch (final SAXException e) {
|
|
||||||
LOGGER.error(e.getMessage(), e);
|
|
||||||
} catch (final IOException e) {
|
|
||||||
LOGGER.error(e.getMessage(), e);
|
|
||||||
} catch (final ParserConfigurationException e) {
|
|
||||||
LOGGER.error(e.getMessage(), e);
|
|
||||||
} catch (final XPathExpressionException e) {
|
|
||||||
LOGGER.error(e.getMessage(), e);
|
LOGGER.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
zfXML = zi.getUTF8();
|
zfXML = zi.getUTF8();
|
||||||
@@ -308,10 +305,11 @@ public class PDFValidator extends Validator {
|
|||||||
//end
|
//end
|
||||||
|
|
||||||
final long endTime = Calendar.getInstance().getTimeInMillis();
|
final long endTime = Calendar.getInstance().getTimeInMillis();
|
||||||
if (!pdfReport.contains("validationReports compliant=\"1\"")) {
|
if (!processorResult.getValidationResult().isCompliant()) {
|
||||||
context.setInvalid();
|
context.setInvalid();
|
||||||
}
|
}
|
||||||
if (!pdfReport.contains("PDF/A-3")) {
|
if (Arrays.stream(PDF_A_3_FLAVOURS)
|
||||||
|
.anyMatch(pdfaFlavour -> processorResult.getValidationResult().getPDFAFlavour().equals(pdfaFlavour))) {
|
||||||
context.addResultItem(
|
context.addResultItem(
|
||||||
new ValidationResultItem(ESeverity.error, "Not a PDF/A-3").setSection(23).setPart(EPart.pdf));
|
new ValidationResultItem(ESeverity.error, "Not a PDF/A-3").setSection(23).setPart(EPart.pdf));
|
||||||
|
|
||||||
@@ -329,8 +327,9 @@ public class PDFValidator extends Validator {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFileContents(byte[] fileContents) {
|
public void setFileContents(byte[] filecontents) throws IrrecoverableValidationError {
|
||||||
this.fileContents = fileContents;
|
this.fileContents = filecontents;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRawXML() {
|
public String getRawXML() {
|
||||||
|
|||||||
@@ -55,15 +55,17 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
File tempFile = getResourceAsFile("XMLinvalidV2PDF.pdf");// need a more invalid file here
|
byte [] contents = getResourceAsByteArray("XMLinvalidV2PDF.pdf");// need a more invalid file here
|
||||||
|
|
||||||
pv.setFilename(tempFile.getAbsolutePath());
|
pv.setFilename("XMLinvalidV2PDF.pdf");
|
||||||
|
pv.setFileContents(contents);
|
||||||
pv.validate();
|
pv.validate();
|
||||||
// assertEquals("", pv.getXMLResult());
|
// assertEquals("", pv.getXMLResult());
|
||||||
|
|
||||||
//
|
//
|
||||||
tempFile = getResourceAsFile("Facture_F20180027.pdf");
|
contents = getResourceAsByteArray("Facture_F20180027.pdf");
|
||||||
pv.setFilename(tempFile.getAbsolutePath());
|
pv.setFilename("Facture_F20180027.pdf");
|
||||||
|
pv.setFileContents(contents);
|
||||||
pv.validate();
|
pv.validate();
|
||||||
String actual = pv.getXMLResult();
|
String actual = pv.getXMLResult();
|
||||||
assertEquals(true, actual.contains("summary status=\"valid"));
|
assertEquals(true, actual.contains("summary status=\"valid"));
|
||||||
@@ -74,10 +76,10 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
xv.validate();
|
xv.validate();
|
||||||
actual = vc.getXMLResult();
|
actual = vc.getXMLResult();
|
||||||
|
|
||||||
assertEquals(true, actual.contains("validationReport profileName=\"PDF/A-3"));
|
assertEquals(true, actual.contains("flavour=3u"));
|
||||||
assertEquals(true, actual.contains("batchSummary totalJobs=\"1\" failedToParse=\"0\" encrypted=\"0\""));
|
assertEquals(true, actual.contains("flavour=3b"));
|
||||||
assertEquals(true,
|
assertEquals(true,
|
||||||
actual.contains("validationReports compliant=\"1\" nonCompliant=\"0\" failedJobs=\"0\">"));
|
actual.contains("isCompliant=true"));
|
||||||
// test some xml
|
// test some xml
|
||||||
// assertEquals(true, actual.contains("<error
|
// assertEquals(true, actual.contains("<error
|
||||||
// location=\"/*:CrossIndustryInvoice[namespace-uri()='urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100'][1]/*:SupplyChainTradeTransaction[namespace-uri()='urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100'][1]/*:ApplicableHeaderTradeSettlement[namespace-uri()='urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'][1]/*:SpecifiedTradeSettlementHeaderMonetarySummation[namespace-uri()='urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'][1]/*:DuePayableAmount[namespace-uri()='urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'][1]\"
|
// location=\"/*:CrossIndustryInvoice[namespace-uri()='urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100'][1]/*:SupplyChainTradeTransaction[namespace-uri()='urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100'][1]/*:ApplicableHeaderTradeSettlement[namespace-uri()='urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'][1]/*:SpecifiedTradeSettlementHeaderMonetarySummation[namespace-uri()='urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'][1]/*:DuePayableAmount[namespace-uri()='urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100'][1]\"
|
||||||
@@ -87,16 +89,16 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
assertEquals(true, actual.contains("<version>2</version>"));
|
assertEquals(true, actual.contains("<version>2</version>"));
|
||||||
|
|
||||||
// valid one
|
// valid one
|
||||||
tempFile = getResourceAsFile("validV2PDF.pdf");
|
contents = getResourceAsByteArray("validV2PDF.pdf");
|
||||||
|
|
||||||
pv.setFilename(tempFile.getAbsolutePath());
|
pv.setFilename("validV2PDF.pdf");
|
||||||
|
pv.setFileContents(contents);
|
||||||
vc.clear();
|
vc.clear();
|
||||||
pv.validate();
|
pv.validate();
|
||||||
actual = pv.getXMLResult();
|
actual = pv.getXMLResult();
|
||||||
assertEquals(true, actual.contains("validationReport profileName=\"PDF/A-3"));
|
assertEquals(true, actual.contains("flavour=3u"));
|
||||||
assertEquals(true, actual.contains("batchSummary totalJobs=\"1\" failedToParse=\"0\" encrypted=\"0\""));
|
assertEquals(true, actual.contains("summary status=\"valid"));
|
||||||
assertEquals(true,
|
assertEquals(false, actual.contains("summary status=\"invalid"));
|
||||||
actual.contains("validationReports compliant=\"1\" nonCompliant=\"0\" failedJobs=\"0\">"));
|
|
||||||
|
|
||||||
assertEquals(false, actual.contains("<error"));
|
assertEquals(false, actual.contains("<error"));
|
||||||
} catch (final IrrecoverableValidationError e) {
|
} catch (final IrrecoverableValidationError e) {
|
||||||
@@ -109,11 +111,12 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
final ValidationContext vc = new ValidationContext(null);
|
final ValidationContext vc = new ValidationContext(null);
|
||||||
try {
|
try {
|
||||||
final PDFValidator pv = new PDFValidator(vc);
|
final PDFValidator pv = new PDFValidator(vc);
|
||||||
|
// need a more
|
||||||
|
// invalid file here
|
||||||
|
byte [] contents = getResourceAsByteArray("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");
|
||||||
|
|
||||||
File tempFile = getResourceAsFile("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");// need a more
|
pv.setFilename("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");
|
||||||
// invalid file here
|
pv.setFileContents(contents);
|
||||||
|
|
||||||
pv.setFilename(tempFile.getAbsolutePath());
|
|
||||||
pv.validate();
|
pv.validate();
|
||||||
String pdfvres = pv.getXMLResult();
|
String pdfvres = pv.getXMLResult();
|
||||||
|
|
||||||
@@ -127,9 +130,10 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
assertEquals(true, xmlvres.contains("invalid"));
|
assertEquals(true, xmlvres.contains("invalid"));
|
||||||
|
|
||||||
vc.clear();
|
vc.clear();
|
||||||
tempFile = getResourceAsFile("validV1WithAdditionalData.pdf");// need a more invalid file here
|
contents = getResourceAsByteArray("validV1WithAdditionalData.pdf");// need a more invalid file here
|
||||||
|
|
||||||
pv.setFilename(tempFile.getAbsolutePath());
|
pv.setFilename("validV1WithAdditionalData.pdf");
|
||||||
|
pv.setFileContents(contents);
|
||||||
pv.validate();
|
pv.validate();
|
||||||
pdfvres = pv.getXMLResult();
|
pdfvres = pv.getXMLResult();
|
||||||
|
|
||||||
@@ -151,10 +155,10 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
final ValidationContext vc = new ValidationContext(null);
|
final ValidationContext vc = new ValidationContext(null);
|
||||||
final PDFValidator pv = new PDFValidator(vc);
|
final PDFValidator pv = new PDFValidator(vc);
|
||||||
try {
|
try {
|
||||||
|
byte [] contents = getResourceAsByteArray("invalidXMP.pdf");
|
||||||
|
|
||||||
File tempFile = getResourceAsFile("invalidXMP.pdf");
|
pv.setFilename("invalidXMP.pdf");
|
||||||
|
pv.setFileContents(contents);
|
||||||
pv.setFilename(tempFile.getAbsolutePath());
|
|
||||||
vc.clear();
|
vc.clear();
|
||||||
pv.validate();
|
pv.validate();
|
||||||
String actual = pv.getXMLResult();
|
String actual = pv.getXMLResult();
|
||||||
@@ -162,9 +166,10 @@ public class PDFValidatorTest extends ResourceCase {
|
|||||||
assertEquals(true, actual
|
assertEquals(true, actual
|
||||||
.contains("<error type=\"12\">XMP Metadata: ConformanceLevel contains invalid value</error>"));
|
.contains("<error type=\"12\">XMP Metadata: ConformanceLevel contains invalid value</error>"));
|
||||||
|
|
||||||
tempFile = getResourceAsFile("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");
|
contents = getResourceAsByteArray("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");
|
||||||
|
|
||||||
pv.setFilename(tempFile.getAbsolutePath());
|
pv.setFilename("attributeBasedXMP_zugferd_2p0_EN16931_Einfach.pdf");
|
||||||
|
pv.setFileContents(contents);
|
||||||
vc.clear();
|
vc.clear();
|
||||||
pv.validate();
|
pv.validate();
|
||||||
actual = pv.getXMLResult();
|
actual = pv.getXMLResult();
|
||||||
|
|||||||
@@ -37,4 +37,18 @@ public class ResourceCase extends TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] getResourceAsByteArray(String resourcePath) {
|
||||||
|
try {
|
||||||
|
InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath);
|
||||||
|
if (in == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return in.readAllBytes();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.error(e.getMessage(), e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user