diff --git a/api/src/main/java/peppol/bis/invoice3/api/PeppolBillingApi.java b/api/src/main/java/peppol/bis/invoice3/api/PeppolBillingApi.java index e702bd7..6bf57e3 100644 --- a/api/src/main/java/peppol/bis/invoice3/api/PeppolBillingApi.java +++ b/api/src/main/java/peppol/bis/invoice3/api/PeppolBillingApi.java @@ -16,6 +16,7 @@ package peppol.bis.invoice3.api; import org.eaxy.Document; +import org.eaxy.Element; import peppol.bis.invoice3.domain.BillingCommon; import peppol.bis.invoice3.domain.CreditNote; import peppol.bis.invoice3.domain.Invoice; @@ -71,6 +72,28 @@ public boolean isInvoice() { return false; } + public String getSupplierCountryIdentifier() { + if (this.object instanceof Document) { + return ((Document) this.object).find("AccountingSupplierParty", "Party", "PostalAddress", "Country", "IdentificationCode").single().text().trim(); + } + throw new RuntimeException("Mandatory property missing in document: AccountingSupplierParty -> Party -> PostalAddress -> Country -> IdentificationCode"); + } + + public String getCustomerCountryIdentifier() { + if (this.object instanceof Document) { + return ((Document) this.object).find("AccountingCustomerParty", "Party", "PostalAddress", "Country", "IdentificationCode").single().text().trim(); + } + throw new RuntimeException("Mandatory property missing in document: AccountingCustomerParty -> Party -> PostalAddress -> Country -> IdentificationCode"); + } + + public String getSupplierEndpointID() { + if (this.object instanceof Document) { + Element element = ((Document) this.object).find("AccountingSupplierParty", "Party", "EndpointID").single(); + return element.attr("schemeID").trim() + ":" + element.text().trim(); + } + throw new RuntimeException("Mandatory property missing in document: AccountingSupplierParty -> Party -> EndpointID"); + } + public String prettyPrint() { return XML_FIRST_LINE + (this.object instanceof BillingCommon ? ((BillingCommon) this.object).xmlRoot() : ((Document) this.object).getRootElement()).toIndentedXML(); } diff --git a/api/src/test/java/peppol/bis/invoice3/api/PeppolBillingApiTest.java b/api/src/test/java/peppol/bis/invoice3/api/PeppolBillingApiTest.java new file mode 100644 index 0000000..9470ceb --- /dev/null +++ b/api/src/test/java/peppol/bis/invoice3/api/PeppolBillingApiTest.java @@ -0,0 +1,42 @@ +/** + * Copyright (C) Posten Norge AS + * + * 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 peppol.bis.invoice3.api; + +import org.eaxy.Document; +import org.eaxy.Xml; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +class PeppolBillingApiTest { + + @Test + public void test() throws IOException { + try (InputStream inputStream = PeppolBillingApiTest.class.getResourceAsStream("/norwegian-example.xml")) { + assertNotNull(inputStream); + Document document = Xml.xml(new String(inputStream.readAllBytes())); + PeppolBillingApi peppolBillingApi = new PeppolBillingApi<>(document); + assertEquals("NO", peppolBillingApi.getCustomerCountryIdentifier()); + assertEquals("NO", peppolBillingApi.getSupplierCountryIdentifier()); + assertEquals("0192:123456785", peppolBillingApi.getSupplierEndpointID()); + } + } + +} diff --git a/api/src/test/resources/generated_norwegian-exmple.xml b/api/src/test/resources/generated_norwegian-example.xml similarity index 100% rename from api/src/test/resources/generated_norwegian-exmple.xml rename to api/src/test/resources/generated_norwegian-example.xml