* Copyright 2019 Jochen Staerk - * + *
* Use is subject to license terms. - * + *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0. - * + *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * + *
* See the License for the specific language governing permissions and * limitations under the License. - * - *********************************************************************** */ + *
+ * ********************************************************************** + */ package org.mustangproject.ZUGFeRD; -import org.apache.commons.io.IOUtils; import org.junit.FixMethodOrder; import org.junit.runners.MethodSorters; import org.mustangproject.util.ByteArraySearcher; import org.xml.sax.SAXException; import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.*; -import java.io.*; +import javax.xml.transform.TransformerException; +import java.io.File; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; @@ -51,31 +52,83 @@ public class VisualizationTest extends ResourceCase { /* remove file endings so that tests can also pass after checking out from git with arbitrary options (which may include CSRF changes) */ - result = zvi.visualize(CIIinputFile.getAbsolutePath(),ZUGFeRDVisualizer.Language.FR).replace("\r","").replace("\n",""); + result = zvi.visualize(CIIinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.FR) + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); - File expectedResult=getResourceAsFile("factur-x-vis.fr.html"); - expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r","").replace("\n",""); + File expectedResult = getResourceAsFile("factur-x-vis.fr.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8) + .replace("\r", "") + .replace("\n", "") + .replace("\t", "") + .replace(" ", ""); // remove linebreaks as well... } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: "+e.getMessage()); + fail("UnsupportedOperationException should not happen: " + e.getMessage()); } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: "+e.getMessage()); + fail("IllegalArgumentException should not happen: " + e.getMessage()); } catch (TransformerException e) { - fail("TransformerException should not happen: "+e.getMessage()); + fail("TransformerException should not happen: " + e.getMessage()); } catch (IOException e) { - fail("IOException should not happen: "+e.getMessage()); + fail("IOException should not happen: " + e.getMessage()); } catch (ParserConfigurationException e) { - fail("ParserConfigurationException should not happen: "+e.getMessage()); - } catch (SAXException e) { - fail("SAXException should not happen: "+e.getMessage()); - } + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); + } - assertNotNull(result); + assertNotNull(result); // Reading ZUGFeRD assertEquals(expected, result); } + + public void testCIIVisualizationExtended() { + + // the writing part + String sourceFilename = "factur-x-extended.xml"; + File CIIinputFile = getResourceAsFile(sourceFilename); + + String expected = null; + String result = null; + try { + ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); + /* remove file endings so that tests can also pass after checking + out from git with arbitrary options (which may include CSRF changes) + */ + result = zvi.visualize(CIIinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.DE).replace("\r", "").replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + + File expectedResult = getResourceAsFile("factur-x-vis-extended.de.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r", "").replace("\n", "") + .replace("\t", "") + .replace(" ", ""); + // remove linebreaks as well... + + } catch (UnsupportedOperationException e) { + fail("UnsupportedOperationException should not happen: " + e.getMessage()); + } catch (IllegalArgumentException e) { + fail("IllegalArgumentException should not happen: " + e.getMessage()); + } catch (TransformerException e) { + fail("TransformerException should not happen: " + e.getMessage()); + } catch (IOException e) { + fail("IOException should not happen: " + e.getMessage()); + } catch (ParserConfigurationException e) { + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); + } + + + assertNotNull(result); + // Reading ZUGFeRD + assertEquals(expected, result); + } + public void testUBLCreditNoteVisualizationBasic() { // the writing part @@ -88,31 +141,33 @@ public class VisualizationTest extends ResourceCase { /* remove file endings so that tests can also pass after checking out from git with arbitrary options (which may include CSRF changes) */ - result = zvi.visualize(UBLinputFile.getAbsolutePath(),ZUGFeRDVisualizer.Language.EN).replace("\r","").replace("\n","").replace(" ","").replace("\t",""); + result = zvi.visualize(UBLinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.EN).replace("\r", "").replace("\n", "").replace("\t", "").replace(" ", ""); - File expectedResult=getResourceAsFile("factur-x-vis-ubl-creditnote.en.html"); - expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r","").replace("\n","").replace(" ","").replace("\t",""); + File expectedResult = getResourceAsFile("factur-x-vis-ubl-creditnote.en.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r", "").replace("\n", "").replace("\t", "").replace(" ", ""); // remove linebreaks as well... } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: "+e.getMessage()); + fail("UnsupportedOperationException should not happen: " + e.getMessage()); } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: "+e.getMessage()); + fail("IllegalArgumentException should not happen: " + e.getMessage()); } catch (TransformerException e) { - fail("TransformerException should not happen: "+e.getMessage()); + fail("TransformerException should not happen: " + e.getMessage()); } catch (IOException e) { - fail("IOException should not happen: "+e.getMessage()); + fail("IOException should not happen: " + e.getMessage()); } catch (ParserConfigurationException e) { - fail("ParserConfigurationException should not happen: "+e.getMessage()); - } catch (SAXException e) { - fail("SAXException should not happen: "+e.getMessage()); + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); } - assertNotNull(result); + assertNotNull(result); // Reading ZUGFeRD assertEquals(expected, result); } + + public void testUBLVisualizationBasic() { // the writing part @@ -125,28 +180,32 @@ public class VisualizationTest extends ResourceCase { /* remove file endings so that tests can also pass after checking out from git with arbitrary options (which may include CSRF changes) */ - result = zvi.visualize(UBLinputFile.getAbsolutePath(),ZUGFeRDVisualizer.Language.EN).replace("\r","").replace("\n",""); + result = zvi.visualize(UBLinputFile.getAbsolutePath(), ZUGFeRDVisualizer.Language.EN).replace("\r", "").replace("\n", "") + .replace("\t", "") + .replace(" ", ""); - File expectedResult=getResourceAsFile("factur-x-vis-ubl.en.html"); - expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r","").replace("\n",""); + File expectedResult = getResourceAsFile("factur-x-vis-ubl.en.html"); + expected = new String(Files.readAllBytes(expectedResult.toPath()), StandardCharsets.UTF_8).replace("\r", "").replace("\n", "") + .replace("\t", "") + .replace(" ", ""); // remove linebreaks as well... } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: "+e.getMessage()); + fail("UnsupportedOperationException should not happen: " + e.getMessage()); } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: "+e.getMessage()); + fail("IllegalArgumentException should not happen: " + e.getMessage()); } catch (TransformerException e) { - fail("TransformerException should not happen: "+e.getMessage()); + fail("TransformerException should not happen: " + e.getMessage()); } catch (IOException e) { - fail("IOException should not happen: "+e.getMessage()); + fail("IOException should not happen: " + e.getMessage()); } catch (ParserConfigurationException e) { - fail("ParserConfigurationException should not happen: "+e.getMessage()); - } catch (SAXException e) { - fail("SAXException should not happen: "+e.getMessage()); - } + fail("ParserConfigurationException should not happen: " + e.getMessage()); + } catch (SAXException e) { + fail("SAXException should not happen: " + e.getMessage()); + } - assertNotNull(result); + assertNotNull(result); // Reading ZUGFeRD assertEquals(expected, result); } @@ -163,9 +222,9 @@ public class VisualizationTest extends ResourceCase { ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); zvi.toPDF(CIIinputFile.getAbsolutePath(), TARGET_PDF_CII); } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: "+e.getMessage()); + fail("UnsupportedOperationException should not happen: " + e.getMessage()); } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: "+e.getMessage()); + fail("IllegalArgumentException should not happen: " + e.getMessage()); } try { @@ -188,9 +247,9 @@ public class VisualizationTest extends ResourceCase { ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); zvi.toPDF(UBLinputFile.getAbsolutePath(), TARGET_PDF_UBL); } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: "+e.getMessage()); + fail("UnsupportedOperationException should not happen: " + e.getMessage()); } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: "+e.getMessage()); + fail("IllegalArgumentException should not happen: " + e.getMessage()); } @@ -214,9 +273,9 @@ public class VisualizationTest extends ResourceCase { ZUGFeRDVisualizer zvi = new ZUGFeRDVisualizer(); zvi.toPDF(UBLinputFile.getAbsolutePath(), TARGET_PDF_UBL); } catch (UnsupportedOperationException e) { - fail("UnsupportedOperationException should not happen: "+e.getMessage()); + fail("UnsupportedOperationException should not happen: " + e.getMessage()); } catch (IllegalArgumentException e) { - fail("IllegalArgumentException should not happen: "+e.getMessage()); + fail("IllegalArgumentException should not happen: " + e.getMessage()); } diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java index be949c34..24c73e0f 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java @@ -61,9 +61,9 @@ public class ZF2PushTest extends TestCase { final String TARGET_REVERSECHARGEPDF = "./target/testout-ZF2PushReverseCharge.pdf"; public void testPushExport() { - /*** - * This writes to a filename like an official sample, please consider when changing (probably better not?) - */ + /*** + * This writes to a filename like an official sample, please consider when changing (probably better not?) + */ // the writing part String orgname = "Bei Spiel GmbH"; String number = "RE-20201121/508"; @@ -88,6 +88,7 @@ public class ZF2PushTest extends TestCase { .addItem(new Item(new Product("Design (hours)", "Of a sample invoice", "HUR", new BigDecimal(7)), price, new BigDecimal(1.0))) .addItem(new Item(new Product("Ballons", "various colors, ~2000ml", "H87", new BigDecimal(19)), new BigDecimal("0.79"), new BigDecimal(400.0))) .addItem(new Item(new Product("Hot air „heiße Luft“ (litres)", "", "LTR", new BigDecimal(19)), new BigDecimal("0.025"), new BigDecimal(800.0))) + .setRoundingAmount(new BigDecimal("1")) ); ze.export(TARGET_PDF); @@ -95,6 +96,15 @@ public class ZF2PushTest extends TestCase { fail("Exception should not be raised"); } + ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(TARGET_PDF); + Invoice i = new Invoice(); + try { + zii.extractInto(i); + } catch (XPathExpressionException e) { + throw new RuntimeException(e); + } catch (ParseException e) { + throw new RuntimeException(e); + } // now check the contents (like MustangReaderTest) @@ -154,7 +164,7 @@ public class ZF2PushTest extends TestCase { fail("IOException should not be raised"); } ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(TARGET_ATTACHMENTSPDF); - Invoice i= null; + Invoice i = null; try { i = zii.extractInvoice(); } catch (XPathExpressionException e) { @@ -162,7 +172,7 @@ public class ZF2PushTest extends TestCase { } catch (ParseException e) { throw new RuntimeException(e); } - assertEquals(senderDescription,i.getSender().getDescription()); + assertEquals(senderDescription, i.getSender().getDescription()); // now check the contents (like MustangReaderTest) ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_ATTACHMENTSPDF); @@ -292,7 +302,7 @@ public class ZF2PushTest extends TestCase { // .addItem(new Item(new Product("Testprodukt", "", "C62", new BigDecimal(19)), amount, new BigDecimal(1.0)).addAllowance(new Allowance().setPercent(new BigDecimal(50))))); ze.setTransaction(new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date()) - .setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").addVATID("DE0815").addTaxID ("4711")) + .setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").addVATID("DE0815").addTaxID("4711")) .setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE").addVATID("DE0816") .setContact(new Contact("contact testname", "123456", "contact.testemail@example.org").setFax("0911623562"))) .setDeliveryAddress(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE").addVATID("DE0816")) @@ -362,7 +372,7 @@ public class ZF2PushTest extends TestCase { fail("IOException should not be raised"); } ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(TARGET_INTRACOMMUNITYSUPPLYMANUALPDF); - Invoice i= null; + Invoice i = null; try { i = zii.extractInvoice(); } catch (XPathExpressionException e) { @@ -520,7 +530,7 @@ public class ZF2PushTest extends TestCase { .setContractReferencedDocument(contractID) .setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE").addGlobalID(gln).setEmail("recipient@test.org").addVATID("DE4711") .setContact(new Contact("Franz Müller", "01779999999", "franz@mueller.de", "teststr. 12", "55232", "Entenhausen", "DE").setFax("++49555123456")).setAdditionalAddress("Hinterhaus 3")) - .addItem(new Item(new Product("Testprodukt", "", "C62", new BigDecimal(16)).addGlobalID(gtin).setSellerAssignedID("4711"), price, new BigDecimal(1.0)).addReferencedLineID("xxx").addNote("item level 1/1").addAllowance(new Allowance(new BigDecimal(0.02)).setReason("item discount").setTaxPercent(new BigDecimal(16))).setDetailedDeliveryPeriod(sdf.parse("2020-01-13"), sdf.parse("2020-01-15"))) + .addItem(new Item(new Product("Testprodukt", "", "H87", new BigDecimal(16)).addGlobalID(gtin).setSellerAssignedID("4711"), price, new BigDecimal(1.0)).setId("a123").addReferencedLineID("xxx").addNote("item level 1/1").addAllowance(new Allowance(new BigDecimal(0.02)).setReason("item discount").setTaxPercent(new BigDecimal(16))).setDetailedDeliveryPeriod(sdf.parse("2020-01-13"), sdf.parse("2020-01-15"))) .addCharge(new Charge(new BigDecimal(0.5)).setReason("quick delivery charge").setTaxPercent(new BigDecimal(16))) .addAllowance(new Allowance(new BigDecimal(0.2)).setReason("discount").setTaxPercent(new BigDecimal(16))) .addCashDiscount(new CashDiscount(new BigDecimal(2), 14)) @@ -552,7 +562,7 @@ public class ZF2PushTest extends TestCase { assertTrue(zi.getUTF8().contains(occurrenceFrom)); assertTrue(zi.getUTF8().contains(occurrenceTo)); assertTrue(zi.getUTF8().contains(contractID)); - + assertEquals(zi.importedInvoice.getZFItems()[0].getId(), "a123"); assertTrue(zi.getUTF8().contains("20200113")); // to contain item delivery periods assertTrue(zi.getUTF8().contains("20200115")); // to contain item delivery periods diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java index 40ee39de..a0b6ad16 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2ZInvoiceImporterTest.java @@ -1,4 +1,3 @@ - /** * ********************************************************************* *
@@ -23,10 +22,14 @@ package org.mustangproject.ZUGFeRD;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
import org.mustangproject.*;
import javax.xml.xpath.XPathExpressionException;
-import java.io.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -35,6 +38,9 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
/***
@@ -304,7 +310,7 @@ public class ZF2ZInvoiceImporterTest extends ResourceCase {
assertTrue(invoice.getTradeSettlement().length==1);
assertTrue(invoice.getTradeSettlement()[0] instanceof IZUGFeRDTradeSettlementPayment);
- IZUGFeRDTradeSettlementPayment paym=(IZUGFeRDTradeSettlementPayment)invoice.getTradeSettlement()[0];
+ IZUGFeRDTradeSettlementPayment paym = (IZUGFeRDTradeSettlementPayment) invoice.getTradeSettlement()[0];
assertEquals("DE12500105170648489890", paym.getOwnIBAN());
assertEquals("COBADEFXXX", paym.getOwnBIC());
@@ -315,19 +321,19 @@ 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;
+ byte[] fileA = null;
+ byte[] fileB = null;
ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter("./target/testout-ZF2PushAttachments.pdf");
- for (FileAttachment fa:zii.getFileAttachmentsPDF()) {
+ for (FileAttachment fa : zii.getFileAttachmentsPDF()) {
if (fa.getFilename().equals("one.pdf")) {
- fileA=fa.getData();
+ fileA = fa.getData();
} else if (fa.getFilename().equals("two.pdf")) {
- fileB=fa.getData();
+ fileB = fa.getData();
}
}
byte[] b = {12, 13}; // the sample data that was used to write the files
@@ -339,19 +345,18 @@ public class ZF2ZInvoiceImporterTest extends ResourceCase {
}
-
public void testImportDebit() {
File CIIinputFile = getResourceAsFile("cii/minimalDebit.xml");
try {
ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(new FileInputStream(CIIinputFile));
- Invoice i=zii.extractInvoice();
+ Invoice i = zii.extractInvoice();
assertEquals("DE21860000000086001055", i.getSender().getBankDetails().get(0).getIBAN());
ObjectMapper mapper = new ObjectMapper();
String jsonArray = mapper.writeValueAsString(i);
- // assertEquals("",jsonArray);
+ // assertEquals("",jsonArray);
} catch (IOException e) {
fail("IOException not expected");
@@ -363,14 +368,14 @@ public class ZF2ZInvoiceImporterTest extends ResourceCase {
}
-
- public void testImportMinimum() {
+
+ public void testImportMinimum() {
File CIIinputFile = getResourceAsFile("cii/facturFrMinimum.xml");
try {
ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(new FileInputStream(CIIinputFile));
- CalculatedInvoice i=new CalculatedInvoice();
+ CalculatedInvoice i = new CalculatedInvoice();
zii.extractInto(i);
assertEquals("671.15", i.getGrandTotal().toString());
@@ -441,4 +446,49 @@ this would test if for all elements/attributes
}
+ @Test
+ public void testImportPrepaid() throws XPathExpressionException, ParseException {
+ InputStream inputStream = this.getClass()
+ .getResourceAsStream("/EN16931_1_Teilrechnung.pdf");
+ ZUGFeRDInvoiceImporter importer = new ZUGFeRDInvoiceImporter();
+ importer.doIgnoreCalculationErrors();
+ importer.setInputStream(inputStream);
+
+ CalculatedInvoice invoice = new CalculatedInvoice();
+ importer.extractInto(invoice);
+
+ boolean isBD=invoice.getTotalPrepaidAmount() instanceof BigDecimal;
+ assertTrue(isBD);
+ BigDecimal expectedPrepaid=new BigDecimal(50);
+ BigDecimal expectedLineTotal=new BigDecimal("180.76");
+ if (isBD) {
+ BigDecimal amread=invoice.getTotalPrepaidAmount();
+ BigDecimal amline=invoice.getLineTotalAmount();
+ assertTrue(amread.compareTo(expectedPrepaid) == 0);
+ assertTrue(amline.compareTo(expectedLineTotal) == 0);
+ }
+
+ }
+
+
+ @Test
+ public void testImportIncludedNotes() throws XPathExpressionException, ParseException {
+ InputStream inputStream = this.getClass()
+ .getResourceAsStream("/EN16931_Einfach.pdf");
+ ZUGFeRDInvoiceImporter importer = new ZUGFeRDInvoiceImporter(inputStream);
+ Invoice invoice = importer.extractInvoice();
+ ListAbrechnungszeitraum:
+ Vorausgegangene Rechnungen:
+ Abrechnungszeitraum:
+ Eigenschaften des Artikels:
+ Abrechnungszeitraum:
+ Eigenschaften des Artikels:
Abrechnungszeitraum:
+ Eigenschaften des Artikels:
Abrechnungszeitraum:
+ Eigenschaften des Artikels:
Abrechnungszeitraum:
+ Eigenschaften des Artikels:
+