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
}

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

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

8181
// Then
8282
XMLAssertUtils.assertSnapshot(xml, getClass(), "guiaSerieT.xml");
83-
XMLAssertUtils.assertSendSunat(xml);
83+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8484
}
8585
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testInvoice_withPrecioUnitario() throws Exception {
6767

6868
// Then
6969
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitario.xml");
70-
XMLAssertUtils.assertSendSunat(xml);
70+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
7171
}
7272

7373
@Test
@@ -106,7 +106,7 @@ public void testInvoice_withPrecioConIgv() throws Exception {
106106

107107
// Then
108108
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitarioConImpuestos.xml");
109-
XMLAssertUtils.assertSendSunat(xml);
109+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
110110
}
111111

112112
@Test
@@ -145,7 +145,7 @@ public void testInvoice_withPrecioUnitario_andICB() throws Exception {
145145

146146
// Then
147147
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitario-ICB.xml");
148-
XMLAssertUtils.assertSendSunat(xml);
148+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
149149
}
150150

151151
@Test
@@ -185,6 +185,6 @@ public void testInvoice_withPrecioConIgv_andICB() throws Exception {
185185

186186
// Then
187187
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitario-conImpuestos-ICB.xml");
188-
XMLAssertUtils.assertSendSunat(xml);
188+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
189189
}
190190
}

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

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

7777
// Then
7878
XMLAssertUtils.assertSnapshot(xml, getClass(), "ordenDeCompra.xml");
79-
XMLAssertUtils.assertSendSunat(xml);
79+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8080
}
8181
}

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

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

7070
// Then
7171
XMLAssertUtils.assertSnapshot(xml, getClass(), "percepcion.xml");
72-
XMLAssertUtils.assertSendSunat(xml);
72+
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
7373
}
7474
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package e2e.renderer.invoice;
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.Contacto;
@@ -84,7 +85,7 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {
8485

8586
// Then
8687
assertSnapshot(xml, getClass(), "customUnidadMedida.xml");
87-
assertSendSunat(xml);
88+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
8889
}
8990

9091
@Test
@@ -135,7 +136,7 @@ public void testInvoiceWithCustomFechaEmision() throws Exception {
135136

136137
// Then
137138
assertSnapshot(xml, getClass(), "customFechaEmision.xml");
138-
assertSendSunat(xml);
139+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
139140
}
140141

141142
@Test
@@ -195,7 +196,7 @@ public void testInvoiceWithCustomClienteDireccionAndContacto() throws Exception
195196

196197
// Then
197198
assertSnapshot(xml, getClass(), "customClienteDireccionAndContacto.xml");
198-
assertSendSunat(xml);
199+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
199200
}
200201

201202
@Test
@@ -261,7 +262,7 @@ public void testInvoiceWithCustomProveedorDireccionAndContacto() throws Exceptio
261262

262263
// Then
263264
assertSnapshot(xml, getClass(), "customProveedorDireccionAndContacto.xml");
264-
assertSendSunat(xml);
265+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
265266
}
266267

267268
@Test
@@ -308,7 +309,7 @@ public void testInvoiceWithCustomFirmante() throws Exception {
308309

309310
// Then
310311
assertSnapshot(xml, getClass(), "customFirmante.xml");
311-
assertSendSunat(xml);
312+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
312313
}
313314

314315
@Test
@@ -356,7 +357,7 @@ public void testInvoiceWithICB_precioUnitario() throws Exception {
356357

357358
// Then
358359
assertSnapshot(xml, getClass(), "icb.xml");
359-
assertSendSunat(xml);
360+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
360361
}
361362

362363
@Test
@@ -406,7 +407,7 @@ public void testInvoiceWithICB_precioConIgv() throws Exception {
406407

407408
// Then
408409
assertSnapshot(xml, getClass(), "icb.xml");
409-
assertSendSunat(xml);
410+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
410411
}
411412

412413
@Test
@@ -459,6 +460,6 @@ public void testInvoiceWithCustomProveedor_direccionNotNullAndCodigoLocalNull()
459460

460461
// Then
461462
assertSnapshot(xml, getClass(), "customCodigoLocal.xml");
462-
assertSendSunat(xml);
463+
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
463464
}
464465
}

core/src/test/java/e2e/renderer/perception/PerceptionTest.java

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

1919
import e2e.AbstractTest;
20+
import e2e.renderer.XMLAssertUtils;
2021
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1;
2122
import io.github.project.openubl.xbuilder.content.catalogs.Catalog22;
2223
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
@@ -85,7 +86,7 @@ public void testSimplePerception() throws Exception {
8586

8687
// Then
8788
assertSnapshot(xml, getClass(), "perception_simple.xml");
88-
assertSendSunat(xml);
89+
assertSendSunat(xml, XMLAssertUtils.PERCEPTION_XSD);
8990
}
9091

9192
}

core/src/test/java/e2e/renderer/retention/RetentionTest.java

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

1919
import e2e.AbstractTest;
20+
import e2e.renderer.XMLAssertUtils;
2021
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1;
2122
import io.github.project.openubl.xbuilder.content.catalogs.Catalog22;
2223
import io.github.project.openubl.xbuilder.content.catalogs.Catalog23;
@@ -87,7 +88,7 @@ public void testSimplePerception() throws Exception {
8788

8889
// Then
8990
assertSnapshot(xml, getClass(), "retention_simple.xml");
90-
assertSendSunat(xml);
91+
assertSendSunat(xml, XMLAssertUtils.RETENTION_XSD);
9192
}
9293

9394
}

core/src/test/java/e2e/renderer/summarydocuments/SummaryDocumentsTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
package e2e.renderer.summarydocuments;
1818

1919
import e2e.AbstractTest;
20+
import e2e.renderer.XMLAssertUtils;
2021
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1;
2122
import io.github.project.openubl.xbuilder.content.catalogs.Catalog19;
2223
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1_Invoice;
2324
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
2425
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
2526
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
26-
import io.github.project.openubl.xbuilder.content.models.sunat.baja.VoidedDocuments;
27-
import io.github.project.openubl.xbuilder.content.models.sunat.baja.VoidedDocumentsItem;
2827
import io.github.project.openubl.xbuilder.content.models.sunat.resumen.Comprobante;
2928
import io.github.project.openubl.xbuilder.content.models.sunat.resumen.ComprobanteAfectado;
3029
import io.github.project.openubl.xbuilder.content.models.sunat.resumen.ComprobanteImpuestos;
@@ -37,7 +36,6 @@
3736
import org.junit.jupiter.api.Test;
3837

3938
import java.math.BigDecimal;
40-
import java.time.LocalDate;
4139

4240
import static e2e.renderer.XMLAssertUtils.assertSendSunat;
4341
import static e2e.renderer.XMLAssertUtils.assertSnapshot;
@@ -120,8 +118,7 @@ public void testMultipleVoidedDocuments() throws Exception {
120118

121119
// Then
122120
assertSnapshot(xml, getClass(), "summaryDocuments.xml");
123-
// TODO uncomment following test
124-
// assertSendSunat(xml);
121+
assertSendSunat(xml, XMLAssertUtils.SUMMARY_DOCUMENTS_XSD);
125122
}
126123

127124
}

0 commit comments

Comments
 (0)