Skip to content

Commit 9d1146d

Browse files
Add xsd to tests (#166)
1 parent 1cb4962 commit 9d1146d

File tree

124 files changed

+126018
-41
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+126018
-41
lines changed

core/src/test/java/e2e/renderer/XMLAssertUtils.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,19 @@
3636
import io.github.project.openubl.xsender.sunat.BillServiceDestination;
3737
import org.apache.camel.CamelContext;
3838
import org.w3c.dom.Document;
39+
import org.xml.sax.SAXException;
3940
import org.xmlunit.builder.DiffBuilder;
4041
import org.xmlunit.diff.Diff;
4142

43+
import javax.xml.XMLConstants;
44+
import javax.xml.transform.stream.StreamSource;
45+
import javax.xml.validation.Schema;
46+
import javax.xml.validation.SchemaFactory;
47+
import javax.xml.validation.Validator;
48+
import java.io.ByteArrayInputStream;
49+
import java.io.IOException;
4250
import java.io.InputStream;
51+
import java.net.URL;
4352
import java.nio.file.Files;
4453
import java.nio.file.Path;
4554
import java.nio.file.Paths;
@@ -55,6 +64,14 @@
5564

5665
public class XMLAssertUtils {
5766

67+
public static final String INVOICE_XSD = "xsd/2.1/maindoc/UBL-Invoice-2.1.xsd";
68+
public static final String CREDIT_NOTE_XSD = "xsd/2.1/maindoc/UBL-CreditNote-2.1.xsd";
69+
public static final String DEBIT_NOTE_XSD = "xsd/2.1/maindoc/UBL-DebitNote-2.1.xsd";
70+
public static final String VOIDED_DOCUMENTS_XSD = "xsd/2.0/maindoc/UBLPE-VoidedDocuments-1.0.xsd";
71+
public static final String SUMMARY_DOCUMENTS_XSD = "xsd/2.0/maindoc/UBLPE-SummaryDocuments-1.0.xsd";
72+
public static final String PERCEPTION_XSD = "xsd/2.0/maindoc/UBLPE-Perception-1.0.xsd";
73+
public static final String RETENTION_XSD = "xsd/2.0/maindoc/UBLPE-Retention-1.0.xsd";
74+
5875
public static final CompanyURLs companyURLs = CompanyURLs
5976
.builder()
6077
.invoice("https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService")
@@ -82,7 +99,7 @@ public class XMLAssertUtils {
8299
}
83100
}
84101

85-
public static void assertSnapshot(String expected, Class<?> clasz, String snapshotFile) {
102+
public static void assertSnapshot(String expected, Class<?> clasz, String snapshotFile) throws SAXException {
86103
String rootDir = clasz.getName().replaceAll("\\.", "/");
87104

88105
// Update snapshots and if updated do not verify since it doesn't make sense anymore
@@ -117,7 +134,7 @@ public static void assertSnapshot(String expected, Class<?> clasz, String snapsh
117134
assertFalse(myDiff.hasDifferences(), expected + "\n" + myDiff);
118135
}
119136

120-
public static void assertSendSunat(String xmlWithoutSignature, String... allowedNotes) throws Exception {
137+
public static void assertSendSunat(String xmlWithoutSignature, String xsdSchema, String... allowedNotes) throws Exception {
121138
String skipSunat = System.getProperty("skipSunat", "false");
122139
if (skipSunat != null && skipSunat.equals("false")) {
123140
Document signedXML = XMLSigner.signXML(
@@ -127,6 +144,22 @@ public static void assertSendSunat(String xmlWithoutSignature, String... allowed
127144
CERTIFICATE.getPrivateKey()
128145
);
129146
sendFileToSunat(signedXML, xmlWithoutSignature, allowedNotes);
147+
isCompliantWithXsd(xsdSchema, signedXML);
148+
}
149+
}
150+
151+
private static void isCompliantWithXsd(String xsdSchema, Document signedXML) throws Exception {
152+
// Assert XSD
153+
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
154+
URL xsd = Thread.currentThread().getContextClassLoader().getResource(xsdSchema);
155+
Schema schema = factory.newSchema(xsd);
156+
Validator validator = schema.newValidator();
157+
try {
158+
byte[] bytesFromDocument = XmlSignatureHelper.getBytesFromDocument(signedXML);
159+
InputStream is = new ByteArrayInputStream(bytesFromDocument);
160+
validator.validate(new StreamSource(is));
161+
} catch (IOException e) {
162+
throw new RuntimeException(e);
130163
}
131164
}
132165

core/src/test/java/e2e/renderer/creditnote/CreditNoteOrdenDeCompraTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package e2e.renderer.creditnote;
1818

1919
import e2e.AbstractTest;
20+
import e2e.renderer.XMLAssertUtils;
2021
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
2122
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
2223
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
@@ -80,6 +81,6 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {
8081

8182
// Then
8283
assertSnapshot(xml, getClass(), "ordenDeCompra.xml");
83-
assertSendSunat(xml);
84+
assertSendSunat(xml, XMLAssertUtils.CREDIT_NOTE_XSD);
8485
}
8586
}

core/src/test/java/e2e/renderer/creditnote/CreditNoteTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package e2e.renderer.creditnote;
1818

1919
import e2e.AbstractTest;
20+
import e2e.renderer.XMLAssertUtils;
2021
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
2122
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
2223
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
@@ -76,6 +77,6 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {
7677

7778
// Then
7879
assertSnapshot(xml, getClass(), "MinData_RUC.xml");
79-
assertSendSunat(xml);
80+
assertSendSunat(xml, XMLAssertUtils.CREDIT_NOTE_XSD);
8081
}
8182
}

core/src/test/java/e2e/renderer/debitnote/DebitNoteTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package e2e.renderer.debitnote;
1818

1919
import e2e.AbstractTest;
20+
import e2e.renderer.XMLAssertUtils;
2021
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
2122
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
2223
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
@@ -76,6 +77,6 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {
7677

7778
// Then
7879
assertSnapshot(xml, getClass(), "MinData_RUC.xml");
79-
assertSendSunat(xml);
80+
assertSendSunat(xml, XMLAssertUtils.DEBIT_NOTE_XSD);
8081
}
8182
}

core/src/test/java/e2e/renderer/invoice/InvoiceAnticiposTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ public void testFechaVencimiento() throws Exception {
7777

7878
// Then
7979
XMLAssertUtils.assertSnapshot(xml, getClass(), "minAnticipos.xml");
80-
XMLAssertUtils.assertSendSunat(xml);
80+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8181
}
8282
}

core/src/test/java/e2e/renderer/invoice/InvoiceDetraccionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ public void testFechaVencimiento() throws Exception {
7979

8080
// Then
8181
XMLAssertUtils.assertSnapshot(xml, getClass(), "detraccion.xml");
82-
XMLAssertUtils.assertSendSunat(xml);
82+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8383
}
8484
}

core/src/test/java/e2e/renderer/invoice/InvoiceDireccionEntregaTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testDireccionEntregaMin() throws Exception {
7777

7878
// Then
7979
XMLAssertUtils.assertSnapshot(xml, getClass(), "direccionEntregaMin.xml");
80-
XMLAssertUtils.assertSendSunat(xml);
80+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8181
}
8282

8383
@Test
@@ -136,6 +136,6 @@ public void testDireccionEntregaFull() throws Exception {
136136

137137
// Then
138138
XMLAssertUtils.assertSnapshot(xml, getClass(), "direccionEntregaFull.xml");
139-
XMLAssertUtils.assertSendSunat(xml);
139+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
140140
}
141141
}

core/src/test/java/e2e/renderer/invoice/InvoiceDocumentoRelacionadoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ public void testDocumentoRelacionado() throws Exception {
8585

8686
// Then
8787
XMLAssertUtils.assertSnapshot(xml, getClass(), "documentoRelacionado.xml");
88-
XMLAssertUtils.assertSendSunat(xml);
88+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8989
}
9090
}

core/src/test/java/e2e/renderer/invoice/InvoiceFechaVencimientoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ public void testFechaVencimiento() throws Exception {
7777

7878
// Then
7979
XMLAssertUtils.assertSnapshot(xml, getClass(), "conFechaVencimiento.xml");
80-
XMLAssertUtils.assertSendSunat(xml);
80+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8181
}
8282
}

core/src/test/java/e2e/renderer/invoice/InvoiceFormaPagoTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void testInvoiceWithFormaPagoContadoPorDefecto() throws Exception {
7979

8080
// Then
8181
XMLAssertUtils.assertSnapshot(xml, getClass(), "sinFormaPago.xml");
82-
XMLAssertUtils.assertSendSunat(xml);
82+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8383
}
8484

8585
@Test
@@ -144,6 +144,6 @@ public void testInvoiceWithFormaPagoCredito() throws Exception {
144144

145145
// Then
146146
XMLAssertUtils.assertSnapshot(xml, getClass(), "conFormaPago.xml");
147-
XMLAssertUtils.assertSendSunat(xml);
147+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
148148
}
149149
}

0 commit comments

Comments
 (0)