From 07758fc26d3a5c4257d8f27a7d39b3b1abe615c7 Mon Sep 17 00:00:00 2001 From: Adrian-Devries Date: Thu, 19 Dec 2024 19:21:28 +0100 Subject: [PATCH] Fix local path issue Replace local absolute path by resource --- .../mustangproject/ZUGFeRD/ZF2PushTest.java | 1654 +++++++++-------- 1 file changed, 828 insertions(+), 826 deletions(-) diff --git a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java index 0d6f2cb7..48daa626 100644 --- a/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java +++ b/library/src/test/java/org/mustangproject/ZUGFeRD/ZF2PushTest.java @@ -1,826 +1,828 @@ - -/** - * ********************************************************************* - *

- * 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 java.io.ByteArrayInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.math.BigDecimal; -import java.nio.charset.StandardCharsets; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; - -import org.mustangproject.*; -import org.junit.FixMethodOrder; -import org.junit.runners.MethodSorters; - -import junit.framework.TestCase; - -import org.mustangproject.ZUGFeRD.model.DocumentCodeTypeConstants; -import org.mustangproject.ZUGFeRD.model.EventTimeCodeTypeConstants; - -import javax.xml.xpath.XPathExpressionException; - -import static org.xmlunit.assertj.XmlAssert.assertThat; - - -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class ZF2PushTest extends TestCase { - final String TARGET_PDF = "./target/testout-MustangGnuaccountingBeispielRE-20201121_508.pdf"; - final String TARGET_ALLOWANCESPDF = "./target/testout-ZF2PushAllowances.pdf"; - final String TARGET_CREDITNOTEPDF = "./target/testout-ZF2PushCreditNote.pdf"; - final String TARGET_CORRECTIONPDF = "./target/testout-ZF2PushCorrection.pdf"; - final String TARGET_ITEMCHARGESALLOWANCESPDF = "./target/testout-ZF2PushItemChargesAllowances.pdf"; - final String TARGET_CHARGESALLOWANCESPDF = "./target/testout-ZF2PushChargesAllowances.pdf"; - final String TARGET_RELATIVECHARGESALLOWANCESPDF = "./target/testout-ZF2PushRelativeChargesAllowances.pdf"; - final String TARGET_ATTACHMENTSPDF = "./target/testout-ZF2PushAttachments.pdf"; - final String TARGET_BANKPDF = "./target/testout-ZF2PushBank.pdf"; - final String TARGET_PUSHEDGE = "./target/testout-ZF2PushEdge.pdf"; - final String TARGET_INTRACOMMUNITYSUPPLYMANUALPDF = "./target/testout-ZF2PushIntraCommunitySupplyManual.pdf"; - final String TARGET_INTRACOMMUNITYSUPPLYPDF = "./target/testout-ZF2PushIntraCommunitySupply.pdf"; - 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?) - */ - // the writing part - String orgname = "Bei Spiel GmbH"; - String number = "RE-20201121/508"; - String priceStr = "160.00"; - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - - BigDecimal price = new BigDecimal(priceStr); - try { - InputStream SOURCE_PDF = this.getClass() - .getResourceAsStream("/MustangGnuaccountingBeispielRE-20201121_508blanko.pdf"); - - ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1(); - ze.ignorePDFAErrors(); - ze.load(SOURCE_PDF); - ze.setProducer("My Application").setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2); - ze.setTransaction(new Invoice().setDueDate(sdf.parse("2020-12-12")).setIssueDate(sdf.parse("2020-11-21")).setDeliveryDate(sdf.parse("2020-11-10")) - .setSender(new TradeParty(orgname, "Ecke 12", "12345", "Stadthausen", "DE").addBankDetails(new BankDetails("DE88200800000970375700", "COBADEFFXXX").setAccountName("Max Mustermann")).addVATID("DE136695976")) - .setRecipient(new TradeParty("Theodor Est", "Bahnstr. 42", "88802", "Spielkreis", "DE") - .setContact(new Contact("Ingmar N. Fo", "(555) 23 78-23", "info@localhost.local")).setID("2")) - .setNumber(number) - .setReferenceNumber("AB321") - .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); - } catch (IOException | ParseException e) { - 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) - ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); - assertTrue(zi.getUTF8().contains("DE88200800000970375700")); //the iban - assertTrue(zi.getUTF8().contains("Max Mustermann")); //account holder - assertTrue(zi.getUTF8().contains("DueDateDateTime")); //account holder - assertTrue(zi.getUTF8().contains("20201212")); //account holder - - assertTrue(zi.getUTF8().contains("")); // maybe add a direct debit mandate to the class in the future then this would fail - assertTrue(zi.getUTF8().contains("Hinterhaus")); - assertThat(zi.getUTF8()).valueByXPath("//*[local-name()='BuyerTradeParty']/*[local-name()='GlobalID'][@schemeID=0088]").asString().isEqualTo("4304171000002"); - assertThat(zi.getUTF8()).valueByXPath("//*[local-name()='SpecifiedTradeProduct']/*[local-name()='GlobalID'][@schemeID=0160]").asString().isEqualTo("2001015001325"); - assertTrue(zi.getUTF8().contains("2001015001325")); - assertTrue(zi.getUTF8().contains("4304171000002")); - assertTrue(zi.getUTF8().contains("0088")); - assertTrue(zi.getUTF8().contains(orgID)); - assertTrue(zi.getUTF8().contains("ram:BuyerOrderReferencedDocument")); - 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 - - assertTrue(zi.getUTF8().contains("item level 1/1")); - assertTrue(zi.getUTF8().contains("DE4711")); // the VAT ID should be there... - assertFalse(zi.getUTF8().contains("DE47110")); // but not the VAT ID of the shiptotradeparty - assertTrue(zi.getUTF8().contains("document level 2/2")); - assertTrue(zi.getUTF8().contains("++49555123456")); - assertTrue(zi.getUTF8().contains("Cash Discount")); // default description for cash discounts - assertThat(zi.getUTF8()).valueByXPath("//*[local-name()='ApplicableTradeTax']/*[local-name()='DueDateTypeCode']").asString().isEqualTo(EventTimeCodeTypeConstants.PAYMENT_DATE); - - ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(TARGET_PUSHEDGE); - try { - Invoice i = zii.extractInvoice(); - - assertEquals("abc123", i.getInvoiceReferencedDocumentID()); - assertEquals("4304171000002", i.getRecipient().getGlobalID()); - assertEquals("2001015001325", i.getZFItems()[0].getProduct().getGlobalID()); - assertEquals(orgID, i.getSender().getID()); - - } catch (XPathExpressionException e) { - fail("XPathExpressionException should not be raised"); - } catch (ParseException e) { - fail("ParseException should not be raised"); - /* a parseException would also be fired if the calculated grand total does not - match the read grand total */ - } - } - - public void testAllowancesExport() { - - String orgname = "Test company"; - String number = "123"; - BigDecimal qty = new BigDecimal("20"); - try { - InputStream SOURCE_PDF = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf"); - - ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1(); - ze.ignorePDFAErrors().load(SOURCE_PDF); - - ze.setProducer("My Application").setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2).setProfile(Profiles.getByName("en16931")); - - ze.setTransaction(new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date()) - .setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").addTaxID("4711").addVATID("DE0815")) - .setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE")) - .setNumber(number) - .addItem(new Item(new Product("Testprodukt", "", "C62", new BigDecimal(19)), new BigDecimal(500.0), qty).addAllowance(new Allowance(new BigDecimal(300)).setTaxPercent(new BigDecimal(19)))) - .addAllowance(new Allowance(new BigDecimal(600)).setTaxPercent(new BigDecimal(19))) - ); - String theXML = new String(ze.getProvider().getXML()); - assertTrue(theXML.contains(" + * 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 java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +import org.mustangproject.*; +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +import junit.framework.TestCase; + +import org.mustangproject.ZUGFeRD.model.DocumentCodeTypeConstants; +import org.mustangproject.ZUGFeRD.model.EventTimeCodeTypeConstants; + +import javax.xml.xpath.XPathExpressionException; + +import static org.xmlunit.assertj.XmlAssert.assertThat; + + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ZF2PushTest extends TestCase { + final String TARGET_PDF = "./target/testout-MustangGnuaccountingBeispielRE-20201121_508.pdf"; + final String TARGET_ALLOWANCESPDF = "./target/testout-ZF2PushAllowances.pdf"; + final String TARGET_CREDITNOTEPDF = "./target/testout-ZF2PushCreditNote.pdf"; + final String TARGET_CORRECTIONPDF = "./target/testout-ZF2PushCorrection.pdf"; + final String TARGET_ITEMCHARGESALLOWANCESPDF = "./target/testout-ZF2PushItemChargesAllowances.pdf"; + final String TARGET_CHARGESALLOWANCESPDF = "./target/testout-ZF2PushChargesAllowances.pdf"; + final String TARGET_RELATIVECHARGESALLOWANCESPDF = "./target/testout-ZF2PushRelativeChargesAllowances.pdf"; + final String TARGET_ATTACHMENTSPDF = "./target/testout-ZF2PushAttachments.pdf"; + final String TARGET_BANKPDF = "./target/testout-ZF2PushBank.pdf"; + final String TARGET_PUSHEDGE = "./target/testout-ZF2PushEdge.pdf"; + final String TARGET_INTRACOMMUNITYSUPPLYMANUALPDF = "./target/testout-ZF2PushIntraCommunitySupplyManual.pdf"; + final String TARGET_INTRACOMMUNITYSUPPLYPDF = "./target/testout-ZF2PushIntraCommunitySupply.pdf"; + 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?) + */ + // the writing part + String orgname = "Bei Spiel GmbH"; + String number = "RE-20201121/508"; + String priceStr = "160.00"; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + BigDecimal price = new BigDecimal(priceStr); + try { + InputStream SOURCE_PDF = this.getClass() + .getResourceAsStream("/MustangGnuaccountingBeispielRE-20201121_508blanko.pdf"); + + ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1(); + ze.ignorePDFAErrors(); + ze.load(SOURCE_PDF); + ze.setProducer("My Application").setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2); + ze.setTransaction(new Invoice().setDueDate(sdf.parse("2020-12-12")).setIssueDate(sdf.parse("2020-11-21")).setDeliveryDate(sdf.parse("2020-11-10")) + .setSender(new TradeParty(orgname, "Ecke 12", "12345", "Stadthausen", "DE").addBankDetails(new BankDetails("DE88200800000970375700", "COBADEFFXXX").setAccountName("Max Mustermann")).addVATID("DE136695976")) + .setRecipient(new TradeParty("Theodor Est", "Bahnstr. 42", "88802", "Spielkreis", "DE") + .setContact(new Contact("Ingmar N. Fo", "(555) 23 78-23", "info@localhost.local")).setID("2")) + .setNumber(number) + .setReferenceNumber("AB321") + .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); + } catch (IOException | ParseException e) { + 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) + ZUGFeRDImporter zi = new ZUGFeRDImporter(TARGET_PDF); + assertTrue(zi.getUTF8().contains("DE88200800000970375700")); //the iban + assertTrue(zi.getUTF8().contains("Max Mustermann")); //account holder + assertTrue(zi.getUTF8().contains("DueDateDateTime")); //account holder + assertTrue(zi.getUTF8().contains("20201212")); //account holder + + assertTrue(zi.getUTF8().contains("")); // maybe add a direct debit mandate to the class in the future then this would fail + assertTrue(zi.getUTF8().contains("Hinterhaus")); + assertThat(zi.getUTF8()).valueByXPath("//*[local-name()='BuyerTradeParty']/*[local-name()='GlobalID'][@schemeID=0088]").asString().isEqualTo("4304171000002"); + assertThat(zi.getUTF8()).valueByXPath("//*[local-name()='SpecifiedTradeProduct']/*[local-name()='GlobalID'][@schemeID=0160]").asString().isEqualTo("2001015001325"); + assertTrue(zi.getUTF8().contains("2001015001325")); + assertTrue(zi.getUTF8().contains("4304171000002")); + assertTrue(zi.getUTF8().contains("0088")); + assertTrue(zi.getUTF8().contains(orgID)); + assertTrue(zi.getUTF8().contains("ram:BuyerOrderReferencedDocument")); + 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 + + assertTrue(zi.getUTF8().contains("item level 1/1")); + assertTrue(zi.getUTF8().contains("DE4711")); // the VAT ID should be there... + assertFalse(zi.getUTF8().contains("DE47110")); // but not the VAT ID of the shiptotradeparty + assertTrue(zi.getUTF8().contains("document level 2/2")); + assertTrue(zi.getUTF8().contains("++49555123456")); + assertTrue(zi.getUTF8().contains("Cash Discount")); // default description for cash discounts + assertThat(zi.getUTF8()).valueByXPath("//*[local-name()='ApplicableTradeTax']/*[local-name()='DueDateTypeCode']").asString().isEqualTo(EventTimeCodeTypeConstants.PAYMENT_DATE); + + ZUGFeRDInvoiceImporter zii = new ZUGFeRDInvoiceImporter(TARGET_PUSHEDGE); + try { + Invoice i = zii.extractInvoice(); + + assertEquals("abc123", i.getInvoiceReferencedDocumentID()); + assertEquals("4304171000002", i.getRecipient().getGlobalID()); + assertEquals("2001015001325", i.getZFItems()[0].getProduct().getGlobalID()); + assertEquals(orgID, i.getSender().getID()); + + } catch (XPathExpressionException e) { + fail("XPathExpressionException should not be raised"); + } catch (ParseException e) { + fail("ParseException should not be raised"); + /* a parseException would also be fired if the calculated grand total does not + match the read grand total */ + } + } + + public void testAllowancesExport() { + + String orgname = "Test company"; + String number = "123"; + BigDecimal qty = new BigDecimal("20"); + try { + InputStream SOURCE_PDF = this.getClass().getResourceAsStream("/MustangGnuaccountingBeispielRE-20170509_505blanko.pdf"); + + ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1(); + ze.ignorePDFAErrors().load(SOURCE_PDF); + + ze.setProducer("My Application").setCreator(System.getProperty("user.name")).setZUGFeRDVersion(2).setProfile(Profiles.getByName("en16931")); + + ze.setTransaction(new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date()) + .setSender(new TradeParty(orgname, "teststr", "55232", "teststadt", "DE").addTaxID("4711").addVATID("DE0815")) + .setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE")) + .setNumber(number) + .addItem(new Item(new Product("Testprodukt", "", "C62", new BigDecimal(19)), new BigDecimal(500.0), qty).addAllowance(new Allowance(new BigDecimal(300)).setTaxPercent(new BigDecimal(19)))) + .addAllowance(new Allowance(new BigDecimal(600)).setTaxPercent(new BigDecimal(19))) + ); + String theXML = new String(ze.getProvider().getXML()); + assertTrue(theXML.contains("