Skip to content

Commit 51dc735

Browse files
authored
0.4.1 - Improve error handling. Return 422 if parse exception occurs (#9)
* Throw XmlInvalidException if a SAXParse error occurs and return 422
1 parent efdc7be commit 51dc735

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

src/main/java/io/github/easybill/Services/XMLSanitizer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.easybill.Services;
22

3+
import io.github.easybill.Exceptions.InvalidXmlException;
34
import io.github.easybill.Exceptions.XmlSanitizationException;
45
import java.io.*;
56
import java.nio.charset.Charset;
@@ -18,6 +19,7 @@
1819
import org.w3c.dom.Document;
1920
import org.xml.sax.InputSource;
2021
import org.xml.sax.SAXException;
22+
import org.xml.sax.SAXParseException;
2123

2224
public final class XMLSanitizer {
2325

@@ -30,6 +32,8 @@ public final class XMLSanitizer {
3032
removeInvalidCharsFromProlog(removeBOM(xml)),
3133
charset
3234
);
35+
} catch (SAXParseException parseException) {
36+
throw new InvalidXmlException(parseException);
3337
} catch (Exception exception) {
3438
throw new XmlSanitizationException(exception);
3539
}

src/test/java/io/github/easybill/ValidationControllerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ void testValidationEndpointWithEmptyPayload() throws IOException {
4141
.statusCode(422);
4242
}
4343

44+
@Test
45+
void testValidationEndpointWithPayloadWithMissingNamespaces()
46+
throws IOException {
47+
given()
48+
.body(loadFixtureFileAsStream("Invalid/CII_missing_namespace.xml"))
49+
.contentType(ContentType.XML)
50+
.when()
51+
.post("/validation")
52+
.then()
53+
.statusCode(422);
54+
}
55+
4456
@Test
4557
void testValidationEndpointWithPayloadIncludingBOM() throws IOException {
4658
given()
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
3+
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<rsm:ExchangedDocumentContext>
6+
<ram:GuidelineSpecifiedDocumentContextParameter>
7+
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
8+
</ram:GuidelineSpecifiedDocumentContextParameter>
9+
</rsm:ExchangedDocumentContext>
10+
11+
<rsm:ExchangedDocument>
12+
<ram:ID>20240001</ram:ID>
13+
<ram:TypeCode>380</ram:TypeCode>
14+
<ram:IssueDateTime>
15+
<udt:DateTimeString format="102">20240211</udt:DateTimeString>
16+
</ram:IssueDateTime>
17+
</rsm:ExchangedDocument>
18+
19+
<rsm:SupplyChainTradeTransaction>
20+
<ram:ApplicableHeaderTradeAgreement>
21+
<ram:SellerTradeParty>
22+
<ram:Name>Beispiel GmbH</ram:Name>
23+
<ram:PostalTradeAddress>
24+
<ram:StreetName>Musterstraße</ram:StreetName>
25+
<ram:CityName>Berlin</ram:CityName>
26+
<ram:PostcodeCode>10115</ram:PostcodeCode>
27+
<ram:CountryID>DE</ram:CountryID>
28+
</ram:PostalTradeAddress>
29+
<ram:SpecifiedTaxRegistration>
30+
<ram:ID schemeID="VAT">DE123456789</ram:ID>
31+
</ram:SpecifiedTaxRegistration>
32+
</ram:SellerTradeParty>
33+
34+
<ram:BuyerTradeParty>
35+
<ram:Name>Öffentliche Behörde</ram:Name>
36+
<ram:PostalTradeAddress>
37+
<ram:StreetName>Amtsplatz</ram:StreetName>
38+
<ram:CityName>Hamburg</ram:CityName>
39+
<ram:PostcodeCode>20095</ram:PostcodeCode>
40+
<ram:CountryID>DE</ram:CountryID>
41+
</ram:PostalTradeAddress>
42+
<ram:SpecifiedTaxRegistration>
43+
<ram:ID schemeID="VAT">DE987654321</ram:ID>
44+
</ram:SpecifiedTaxRegistration>
45+
</ram:BuyerTradeParty>
46+
</ram:ApplicableHeaderTradeAgreement>
47+
48+
<ram:ApplicableHeaderTradeSettlement>
49+
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
50+
<ram:SpecifiedTradeSettlementPaymentMeans>
51+
<ram:TypeCode>58</ram:TypeCode>
52+
<ram:PayeePartyCreditorFinancialAccount>
53+
<ram:IBANID>DE44500105175407324931</ram:IBANID>
54+
<ram:AccountName>Beispiel GmbH</ram:AccountName>
55+
</ram:PayeePartyCreditorFinancialAccount>
56+
</ram:SpecifiedTradeSettlementPaymentMeans>
57+
58+
<ram:ApplicableTradeTax>
59+
<ram:TypeCode>VAT</ram:TypeCode>
60+
<ram:CategoryCode>S</ram:CategoryCode>
61+
<ram:RateApplicablePercent>19</ram:RateApplicablePercent>
62+
</ram:ApplicableTradeTax>
63+
64+
<ram:SpecifiedTradeSettlementMonetarySummation>
65+
<ram:LineTotalAmount currencyID="EUR">1000.00</ram:LineTotalAmount>
66+
<ram:ChargeTotalAmount currencyID="EUR">0.00</ram:ChargeTotalAmount>
67+
<ram:TaxBasisTotalAmount currencyID="EUR">1000.00</ram:TaxBasisTotalAmount>
68+
<ram:TaxTotalAmount currencyID="EUR">190.00</ram:TaxTotalAmount>
69+
<ram:GrandTotalAmount currencyID="EUR">1190.00</ram:GrandTotalAmount>
70+
<ram:DuePayableAmount currencyID="EUR">1190.00</ram:DuePayableAmount>
71+
</ram:SpecifiedTradeSettlementMonetarySummation>
72+
</ram:ApplicableHeaderTradeSettlement>
73+
74+
<ram:IncludedSupplyChainTradeLineItem>
75+
<ram:AssociatedDocumentLineDocument>
76+
<ram:LineID>1</ram:LineID>
77+
</ram:AssociatedDocumentLineDocument>
78+
<ram:SpecifiedTradeProduct>
79+
<ram:Name>IT-Dienstleistung</ram:Name>
80+
<ram:Description>Software-Entwicklung und Support</ram:Description>
81+
</ram:SpecifiedTradeProduct>
82+
<ram:SpecifiedLineTradeAgreement>
83+
<ram:GrossPriceProductTradePrice>
84+
<ram:ChargeAmount currencyID="EUR">1000.00</ram:ChargeAmount>
85+
</ram:GrossPriceProductTradePrice>
86+
</ram:SpecifiedLineTradeAgreement>
87+
<ram:SpecifiedLineTradeDelivery>
88+
<ram:BilledQuantity unitCode="HUR">10</ram:BilledQuantity>
89+
</ram:SpecifiedLineTradeDelivery>
90+
<ram:SpecifiedLineTradeSettlement>
91+
<ram:ApplicableTradeTax>
92+
<ram:TypeCode>VAT</ram:TypeCode>
93+
<ram:CategoryCode>S</ram:CategoryCode>
94+
<ram:RateApplicablePercent>19</ram:RateApplicablePercent>
95+
</ram:ApplicableTradeTax>
96+
<ram:SpecifiedTradeSettlementLineMonetarySummation>
97+
<ram:TotalAmount currencyID="EUR">1000.00</ram:TotalAmount>
98+
</ram:SpecifiedTradeSettlementLineMonetarySummation>
99+
</ram:SpecifiedLineTradeSettlement>
100+
</ram:IncludedSupplyChainTradeLineItem>
101+
</rsm:SupplyChainTradeTransaction>
102+
</rsm:CrossIndustryInvoice>

0 commit comments

Comments
 (0)