attachments

This commit is contained in:
jstaerk
2024-08-06 17:35:16 +02:00
parent 543774967b
commit 63e0ecb42d
5 changed files with 155 additions and 62 deletions

View File

@@ -27,12 +27,16 @@ import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import javax.xml.xpath.XPathExpressionException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import static org.xmlunit.assertj.XmlAssert.assertThat;
@@ -41,6 +45,7 @@ import static org.xmlunit.assertj.XmlAssert.assertThat;
public class XRTest extends TestCase {
final String TARGET_XML = "./target/testout-XR.xml";
final String TARGET_EDGE_XML = "./target/testout-XR-Edge.xml";
public void testXRExport() {
// the writing part
@@ -54,13 +59,13 @@ public class XRTest extends TestCase {
String theXML = new String(zf2p.getXML(), StandardCharsets.UTF_8);
assertTrue(theXML.contains("<rsm:CrossIndustryInvoice"));
assertThat(theXML).valueByXPath("count(//*[local-name()='IncludedSupplyChainTradeLineItem'])")
.asInt()
.isEqualTo(1); //2 errors are OK because there is a known bug
.asInt()
.isEqualTo(1); //2 errors are OK because there is a known bug
assertThat(theXML).valueByXPath("//*[local-name()='DuePayableAmount']")
.asDouble()
.isEqualTo(1);
.asDouble()
.isEqualTo(1);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(TARGET_XML));
writer.write(theXML);
@@ -80,17 +85,17 @@ public class XRTest extends TestCase {
String amountStr = "1.00";
BigDecimal amount = new BigDecimal(amountStr);
byte[] b = {12, 13};
FileAttachment fe1=new FileAttachment("one.pdf", "application/pdf", "Alternative", b);
FileAttachment fe1 = new FileAttachment("one.pdf", "application/pdf", "Alternative", b);
Invoice i = new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date())
.setSender(new TradeParty(orgname,"teststr","55232","teststadt","DE").setEmail("sender@example.com").addTaxID("DE4711").addVATID("DE0815").setContact(new Contact("Hans Test","+49123456789","test@example.org")).addBankDetails(new BankDetails("DE12500105170648489890","COBADEFXXX")))
.setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE").setEmail("recipient@sample.org"))
.addCashDiscount(new CashDiscount(new BigDecimal(2),7))
.addCashDiscount(new CashDiscount(new BigDecimal(3),14))
.setReferenceNumber("991-01484-64")//leitweg-id
// not using any VAT, this is also a test of zero-rated goods:
.setNumber(number).addItem(new Item(new Product("Testprodukt", "", "C62", BigDecimal.ZERO), amount, new BigDecimal(1.0)))
.embedFileInXML(fe1);
.setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").setEmail("sender@example.com").addTaxID("DE4711").addVATID("DE0815").setContact(new Contact("Hans Test", "+49123456789", "test@example.org")).addBankDetails(new BankDetails("DE12500105170648489890", "COBADEFXXX")))
.setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE").setEmail("recipient@sample.org"))
.addCashDiscount(new CashDiscount(new BigDecimal(2), 7))
.addCashDiscount(new CashDiscount(new BigDecimal(3), 14))
.setReferenceNumber("991-01484-64")//leitweg-id
// not using any VAT, this is also a test of zero-rated goods:
.setNumber(number).addItem(new Item(new Product("Testprodukt", "", "C62", BigDecimal.ZERO), amount, new BigDecimal(1.0)))
.embedFileInXML(fe1);
ZUGFeRD2PullProvider zf2p = new ZUGFeRD2PullProvider();
@@ -101,13 +106,13 @@ public class XRTest extends TestCase {
assertTrue(theXML.contains("<rsm:CrossIndustryInvoice"));
assertTrue(theXML.contains("#SKONTO#"));
assertThat(theXML).valueByXPath("count(//*[local-name()='IncludedSupplyChainTradeLineItem'])")
.asInt()
.isEqualTo(1); //2 errors are OK because there is a known bug
.asInt()
.isEqualTo(1); //2 errors are OK because there is a known bug
assertThat(theXML).valueByXPath("//*[local-name()='DuePayableAmount']")
.asDouble()
.isEqualTo(1);
.asDouble()
.isEqualTo(1);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(TARGET_EDGE_XML));
writer.write(theXML);
@@ -116,6 +121,25 @@ public class XRTest extends TestCase {
e.printStackTrace();
}
Invoice readInvoice = new Invoice();
ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter();
try {
zii.setRawXML(zf2p.getXML());
zii.extractInto(readInvoice);
} catch (ParseException | XPathExpressionException xp) {
fail("Exception not expected");
} catch (IOException e) {
throw new RuntimeException(e);
}
List<FileAttachment> attachedFiles=zii.getFileAttachments();
assertNotNull(attachedFiles);
assertEquals(attachedFiles.size(), 1);
assertTrue(Arrays.equals(attachedFiles.get(0).getData(), b));
}
public void testXRExportWithoutStreet() {
@@ -130,13 +154,13 @@ public class XRTest extends TestCase {
String theXML = new String(zf2p.getXML(), StandardCharsets.UTF_8);
assertTrue(theXML.contains("<rsm:CrossIndustryInvoice"));
assertThat(theXML).valueByXPath("count(//*[local-name()='IncludedSupplyChainTradeLineItem'])")
.asInt()
.isEqualTo(1); //2 errors are OK because there is a known bug
.asInt()
.isEqualTo(1); //2 errors are OK because there is a known bug
assertThat(theXML).valueByXPath("//*[local-name()='DuePayableAmount']")
.asDouble()
.isEqualTo(1);
.asDouble()
.isEqualTo(1);
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(TARGET_XML));
writer.write(theXML);
@@ -153,11 +177,11 @@ public class XRTest extends TestCase {
String amountStr = "1.00";
BigDecimal amount = new BigDecimal(amountStr);
return new Invoice().setDueDate(new java.util.Date()).setIssueDate(new java.util.Date()).setDeliveryDate(new java.util.Date())
.setSender(new TradeParty(orgname,"teststr","55232","teststadt","DE").addTaxID("DE4711").addVATID("DE0815").setEmail("info@example.org").setContact(new org.mustangproject.Contact("Hans Test","+49123456789","test@example.org")).addBankDetails(new org.mustangproject.BankDetails("DE12500105170648489890","COBADEFXXX")))
.setRecipient(recipient)
.setReferenceNumber("991-01484-64")//leitweg-id
// not using any VAT, this is also a test of zero-rated goods:
.setNumber(number).addItem(new org.mustangproject.Item(new org.mustangproject.Product("Testprodukt", "", "C62", java.math.BigDecimal.ZERO), amount, new java.math.BigDecimal(1.0)));
.setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").addTaxID("DE4711").addVATID("DE0815").setEmail("info@example.org").setContact(new org.mustangproject.Contact("Hans Test", "+49123456789", "test@example.org")).addBankDetails(new org.mustangproject.BankDetails("DE12500105170648489890", "COBADEFXXX")))
.setRecipient(recipient)
.setReferenceNumber("991-01484-64")//leitweg-id
// not using any VAT, this is also a test of zero-rated goods:
.setNumber(number).addItem(new org.mustangproject.Item(new org.mustangproject.Product("Testprodukt", "", "C62", java.math.BigDecimal.ZERO), amount, new java.math.BigDecimal(1.0)));
}
}

View File

@@ -31,6 +31,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
/***
@@ -285,5 +286,33 @@ public class ZF2ZInvoiceImporterTest extends ResourceCase {
}
/**
* testing if other files embedded in pdf additionally to the invoice can be read correctly
* */
public void testDetach() {
boolean hasExceptions = false;
byte[] fileA=null;
byte[] fileB=null;
ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter("./target/testout-ZF2PushAttachments.pdf");
for (String filename:zii.getEmbeddedFilenames()
) {
if (filename.equals("one.pdf")) {
fileA=zii.getEmbeddedFile(filename);
} else if (filename.equals("two.pdf")) {
fileB=zii.getEmbeddedFile(filename);
}
}
byte[] b = {12, 13}; // the sample data that was used to write the files
assertTrue(Arrays.equals(fileA, b));
assertEquals(fileA.length, 2);
assertTrue(Arrays.equals(fileB, b));
assertEquals(fileB.length, 2);
}
}