Skip to content

Add xsd to tests #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions core/src/test/java/e2e/renderer/XMLAssertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@
import io.github.project.openubl.xsender.sunat.BillServiceDestination;
import org.apache.camel.CamelContext;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.Diff;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -55,6 +64,14 @@

public class XMLAssertUtils {

public static final String INVOICE_XSD = "xsd/2.1/maindoc/UBL-Invoice-2.1.xsd";
public static final String CREDIT_NOTE_XSD = "xsd/2.1/maindoc/UBL-CreditNote-2.1.xsd";
public static final String DEBIT_NOTE_XSD = "xsd/2.1/maindoc/UBL-DebitNote-2.1.xsd";
public static final String VOIDED_DOCUMENTS_XSD = "xsd/2.0/maindoc/UBLPE-VoidedDocuments-1.0.xsd";
public static final String SUMMARY_DOCUMENTS_XSD = "xsd/2.0/maindoc/UBLPE-SummaryDocuments-1.0.xsd";
public static final String PERCEPTION_XSD = "xsd/2.0/maindoc/UBLPE-Perception-1.0.xsd";
public static final String RETENTION_XSD = "xsd/2.0/maindoc/UBLPE-Retention-1.0.xsd";

public static final CompanyURLs companyURLs = CompanyURLs
.builder()
.invoice("https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService")
Expand Down Expand Up @@ -82,7 +99,7 @@ public class XMLAssertUtils {
}
}

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

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

public static void assertSendSunat(String xmlWithoutSignature, String... allowedNotes) throws Exception {
public static void assertSendSunat(String xmlWithoutSignature, String xsdSchema, String... allowedNotes) throws Exception {
String skipSunat = System.getProperty("skipSunat", "false");
if (skipSunat != null && skipSunat.equals("false")) {
Document signedXML = XMLSigner.signXML(
Expand All @@ -127,6 +144,22 @@ public static void assertSendSunat(String xmlWithoutSignature, String... allowed
CERTIFICATE.getPrivateKey()
);
sendFileToSunat(signedXML, xmlWithoutSignature, allowedNotes);
isCompliantWithXsd(xsdSchema, signedXML);
}
}

private static void isCompliantWithXsd(String xsdSchema, Document signedXML) throws Exception {
// Assert XSD
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL xsd = Thread.currentThread().getContextClassLoader().getResource(xsdSchema);
Schema schema = factory.newSchema(xsd);
Validator validator = schema.newValidator();
try {
byte[] bytesFromDocument = XmlSignatureHelper.getBytesFromDocument(signedXML);
InputStream is = new ByteArrayInputStream(bytesFromDocument);
validator.validate(new StreamSource(is));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e.renderer.creditnote;

import e2e.AbstractTest;
import e2e.renderer.XMLAssertUtils;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
Expand Down Expand Up @@ -80,6 +81,6 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {

// Then
assertSnapshot(xml, getClass(), "ordenDeCompra.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.CREDIT_NOTE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e.renderer.creditnote;

import e2e.AbstractTest;
import e2e.renderer.XMLAssertUtils;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
Expand Down Expand Up @@ -76,6 +77,6 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {

// Then
assertSnapshot(xml, getClass(), "MinData_RUC.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.CREDIT_NOTE_XSD);
}
}
3 changes: 2 additions & 1 deletion core/src/test/java/e2e/renderer/debitnote/DebitNoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e.renderer.debitnote;

import e2e.AbstractTest;
import e2e.renderer.XMLAssertUtils;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
Expand Down Expand Up @@ -76,6 +77,6 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {

// Then
assertSnapshot(xml, getClass(), "MinData_RUC.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.DEBIT_NOTE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public void testFechaVencimiento() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "minAnticipos.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ public void testFechaVencimiento() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "detraccion.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testDireccionEntregaMin() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "direccionEntregaMin.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -136,6 +136,6 @@ public void testDireccionEntregaFull() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "direccionEntregaFull.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ public void testDocumentoRelacionado() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "documentoRelacionado.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ public void testFechaVencimiento() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "conFechaVencimiento.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testInvoiceWithFormaPagoContadoPorDefecto() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "sinFormaPago.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -144,6 +144,6 @@ public void testInvoiceWithFormaPagoCredito() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "conFormaPago.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ public void testGuiaConSerieT() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "guiaSerieT.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testInvoice_withPrecioUnitario() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitario.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -106,7 +106,7 @@ public void testInvoice_withPrecioConIgv() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitarioConImpuestos.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -145,7 +145,7 @@ public void testInvoice_withPrecioUnitario_andICB() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitario-ICB.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -185,6 +185,6 @@ public void testInvoice_withPrecioConIgv_andICB() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "with-precioUnitario-conImpuestos-ICB.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public void testFechaVencimiento() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "ordenDeCompra.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public void testFechaVencimiento() throws Exception {

// Then
XMLAssertUtils.assertSnapshot(xml, getClass(), "percepcion.xml");
XMLAssertUtils.assertSendSunat(xml);
XMLAssertUtils.assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
17 changes: 9 additions & 8 deletions core/src/test/java/e2e/renderer/invoice/InvoiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e.renderer.invoice;

import e2e.AbstractTest;
import e2e.renderer.XMLAssertUtils;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
import io.github.project.openubl.xbuilder.content.models.common.Contacto;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void testInvoiceWithCustomUnidadMedida() throws Exception {

// Then
assertSnapshot(xml, getClass(), "customUnidadMedida.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -135,7 +136,7 @@ public void testInvoiceWithCustomFechaEmision() throws Exception {

// Then
assertSnapshot(xml, getClass(), "customFechaEmision.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -195,7 +196,7 @@ public void testInvoiceWithCustomClienteDireccionAndContacto() throws Exception

// Then
assertSnapshot(xml, getClass(), "customClienteDireccionAndContacto.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -261,7 +262,7 @@ public void testInvoiceWithCustomProveedorDireccionAndContacto() throws Exceptio

// Then
assertSnapshot(xml, getClass(), "customProveedorDireccionAndContacto.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -308,7 +309,7 @@ public void testInvoiceWithCustomFirmante() throws Exception {

// Then
assertSnapshot(xml, getClass(), "customFirmante.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -356,7 +357,7 @@ public void testInvoiceWithICB_precioUnitario() throws Exception {

// Then
assertSnapshot(xml, getClass(), "icb.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -406,7 +407,7 @@ public void testInvoiceWithICB_precioConIgv() throws Exception {

// Then
assertSnapshot(xml, getClass(), "icb.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}

@Test
Expand Down Expand Up @@ -459,6 +460,6 @@ public void testInvoiceWithCustomProveedor_direccionNotNullAndCodigoLocalNull()

// Then
assertSnapshot(xml, getClass(), "customCodigoLocal.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.INVOICE_XSD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e.renderer.perception;

import e2e.AbstractTest;
import e2e.renderer.XMLAssertUtils;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog22;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void testSimplePerception() throws Exception {

// Then
assertSnapshot(xml, getClass(), "perception_simple.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.PERCEPTION_XSD);
}

}
3 changes: 2 additions & 1 deletion core/src/test/java/e2e/renderer/retention/RetentionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package e2e.renderer.retention;

import e2e.AbstractTest;
import e2e.renderer.XMLAssertUtils;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog22;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog23;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void testSimplePerception() throws Exception {

// Then
assertSnapshot(xml, getClass(), "retention_simple.xml");
assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.RETENTION_XSD);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
package e2e.renderer.summarydocuments;

import e2e.AbstractTest;
import e2e.renderer.XMLAssertUtils;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog19;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog1_Invoice;
import io.github.project.openubl.xbuilder.content.catalogs.Catalog6;
import io.github.project.openubl.xbuilder.content.models.common.Cliente;
import io.github.project.openubl.xbuilder.content.models.common.Proveedor;
import io.github.project.openubl.xbuilder.content.models.sunat.baja.VoidedDocuments;
import io.github.project.openubl.xbuilder.content.models.sunat.baja.VoidedDocumentsItem;
import io.github.project.openubl.xbuilder.content.models.sunat.resumen.Comprobante;
import io.github.project.openubl.xbuilder.content.models.sunat.resumen.ComprobanteAfectado;
import io.github.project.openubl.xbuilder.content.models.sunat.resumen.ComprobanteImpuestos;
Expand All @@ -37,7 +36,6 @@
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.time.LocalDate;

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

// Then
assertSnapshot(xml, getClass(), "summaryDocuments.xml");
// TODO uncomment following test
// assertSendSunat(xml);
assertSendSunat(xml, XMLAssertUtils.SUMMARY_DOCUMENTS_XSD);
}

}
Loading