Skip to content

Commit

Permalink
Enhance Fault error code detection (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 authored Dec 19, 2022
1 parent d30d1cc commit 0fda533
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@

import javax.xml.namespace.QName;
import java.util.Collections;
import java.util.regex.Pattern;

public class SunatErrorResponseProcessor implements Processor {
private static Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");

public boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
return pattern.matcher(strNum).matches();
}

@Override
public void process(Exchange exchange) throws Exception {
Expand All @@ -36,10 +45,18 @@ public void process(Exchange exchange) throws Exception {

QName qName = fault.getFaultCode();
String localPart = qName.getLocalPart();
String errorCode = localPart

int errorCodeInt;
String errorCodeString = localPart
.replaceAll("Client.", "")
.replaceAll("Server.", "");
int errorCodeInt = Integer.parseInt(errorCode);
if (isNumeric(errorCodeString)) {
errorCodeInt = Integer.parseInt(errorCodeString);
} else if (isNumeric(message)) {
errorCodeInt = Integer.parseInt(message);
} else {
throw new IllegalStateException("Could not extract sunat error code", fault);
}

Metadata metadata = Metadata.builder()
.notes(Collections.emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -55,7 +56,7 @@ public class CxfTest {
CompanyURLs companyURLs = CompanyURLs
.builder()
.invoice("https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService")
.despatch("https://e-beta.sunat.gob.pe/ol-ti-itemision-otroscpe-gem-beta/billService")
.despatch("https://e-beta.sunat.gob.pe/ol-ti-itemision-guia-gem-beta/billService")
.perceptionRetention("https://e-beta.sunat.gob.pe/ol-ti-itemision-otroscpe-gem-beta/billService")
.build();

Expand Down Expand Up @@ -173,6 +174,29 @@ public void billService_sendBill_retention() throws Exception {
assertEquals("El Comprobante numero R001-00000001 ha sido aceptado", sunatResponse.getMetadata().getDescription());
}

@Test
public void billService_sendBill_guiaRemision() throws Exception {
InputStream xmlFileIS = Thread
.currentThread()
.getContextClassLoader()
.getResourceAsStream("ubl/20494637074-T001-00007394.xml");
BillServiceFileAnalyzer fileAnalyzer = new BillServiceXMLFileAnalyzer(xmlFileIS, companyURLs);

ZipFile zipFile = fileAnalyzer.getZipFile();
BillServiceDestination destination = fileAnalyzer.getSendFileDestination();
CamelData camelData = getBillServiceCamelData(zipFile, destination, credentials);

SunatResponse sunatResponse = camelContext
.createProducerTemplate()
.requestBodyAndHeaders(Constants.XSENDER_BILL_SERVICE_URI, camelData.getBody(), camelData.getHeaders(), SunatResponse.class);

assertNotNull(sunatResponse);
assertEquals(Status.EXCEPCION, sunatResponse.getStatus());
assertEquals(1085, sunatResponse.getMetadata().getResponseCode()); // Debe enviar las guias de remisión por el nuevo sistema de recepción de guias electronicas
}

// TODO verify when GET STATUS get back to work
@Disabled
@Test
public void billService_getStatus() throws Exception {
InputStream xmlFileIS = Thread
Expand Down
153 changes: 153 additions & 0 deletions core/src/test/resources/ubl/20494637074-T001-00007394.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?xml version="1.0" encoding="ISO-8859-1"?><DespatchAdvice xmlns="urn:oasis:names:specification:ubl:schema:xsd:DespatchAdvice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="PROJECT-OPENUBL"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><ds:Reference URI=""><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><ds:DigestValue>0foeDunivtTPD7730tmzX+HK7Vo=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>DAXH8YvHOEMz9rPrQsa3ds3/8WWaEOOab49TwabJmLKl0hlXpenSZwiDZoVCJkQnfCu/W/OWu6s6&#13;
1nwpn3OBXccVPhY2R14sl2LANsPJF97j83ECK/YUH3kq9aGulpbhcCunOVdeZqQYfWD8e7uNUaj3&#13;
Rz3bFj3Pn5S0js8EgIy21P7GLy76gjNYRsAZo2UVDKQw8o8TaUCcKmlIHa8K3pGXiGuEEI8qjV3w&#13;
lEhc77Ggq+gpcXQ5HiUE/QoAW/Qvs5jm3PkogMM/NdOulDBgcLB1sTvPrOIsFjx9cEcyzMsQNKUV&#13;
xqbJWUONEkvIHrdEBVJMFdjBE9KmEehNBQ8p+Q==</ds:SignatureValue><ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIIE+DCCA+CgAwIBAgIJALURz7AYmJ5+MA0GCSqGSIb3DQEBBQUAMIIBDTEbMBkGCgmSJomT8ixk&#13;
ARkWC0xMQU1BLlBFIFNBMQswCQYDVQQGEwJQRTENMAsGA1UECAwETElNQTENMAsGA1UEBwwETElN&#13;
QTEYMBYGA1UECgwPVFUgRU1QUkVTQSBTLkEuMUUwQwYDVQQLDDxETkkgOTk5OTk5OSBSVUMgMTA0&#13;
Njc3OTM1NDkgLSBDRVJUSUZJQ0FETyBQQVJBIERFTU9TVFJBQ0nDk04xRDBCBgNVBAMMO05PTUJS&#13;
RSBSRVBSRVNFTlRBTlRFIExFR0FMIC0gQ0VSVElGSUNBRE8gUEFSQSBERU1PU1RSQUNJw5NOMRww&#13;
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RU3BgvOVV3if8xYAaZT99n5PsvyBiZ3Gh6VpAW9ezoe25ZNSqGMmGfq+R4mEuiqK4h&#13;
6jDJp0fN47IwRhWjttB9dwpxIDEkWW7zPdueGx+BY8EuyfNDWR/C7GPfu6azSHiapzeKC57AAZ8x&#13;
o8kDdhXxDy8hTqNGolkqnc6QutW8cYPeonqyhi2THN163lZ3Cx5OV8vGFQ3ou2msF0klY9qXopI9&#13;
i8wQjEeOG6bCvVxdID9ZjTbuGbO9pAN9hH7hZ41XEG/CspSWbFf1/Wbnlfusne9v9NgRj0MM+LAH&#13;
M7AO5/7j1XwRq4x+U9TSVPgpoU9l5Q==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature></ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>1.0</cbc:CustomizationID>
<cbc:ID>T001-00007394</cbc:ID>
<cbc:IssueDate>2022-12-15</cbc:IssueDate>
<cbc:DespatchAdviceTypeCode>09</cbc:DespatchAdviceTypeCode>
<cbc:Note>observacion</cbc:Note>
<cac:Signature>
<cbc:ID>12345678912</cbc:ID>
<cac:SignatoryParty>
<cac:PartyIdentification>
<cbc:ID>12345678912</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name>Softgreen S.A.C</cbc:Name>
</cac:PartyName>
</cac:SignatoryParty>
<cac:DigitalSignatureAttachment>
<cac:ExternalReference>
<cbc:URI>#PROJECT-OPENUBL-SIGN</cbc:URI>
</cac:ExternalReference>
</cac:DigitalSignatureAttachment>
</cac:Signature>
<cac:DespatchSupplierParty>
<cbc:CustomerAssignedAccountID schemeID="6">12345678912</cbc:CustomerAssignedAccountID>
<cac:Party>
<cac:PostalAddress>
<cbc:ID>0000</cbc:ID>
<cbc:StreetName>AV.BUENA VISTA 123</cbc:StreetName>
<cbc:CitySubdivisionName>-</cbc:CitySubdivisionName>
<cbc:CityName>HUAMANGA</cbc:CityName>
<cbc:CountrySubentity>AYACUCHO</cbc:CountrySubentity>
<cbc:District>AYACUCHO</cbc:District>
<cac:Country>
<cbc:IdentificationCode>PE</cbc:IdentificationCode>
</cac:Country>
</cac:PostalAddress>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Softgreen S.A.C</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:DespatchSupplierParty>
<cac:DeliveryCustomerParty>
<cbc:CustomerAssignedAccountID schemeID="6">12345678912</cbc:CustomerAssignedAccountID>
<cac:Party>
<cac:PartyLegalEntity>
<cbc:RegistrationName>Softgreen S.A.C</cbc:RegistrationName>
</cac:PartyLegalEntity>
</cac:Party>
</cac:DeliveryCustomerParty>
<cac:Shipment>
<cbc:ID>1</cbc:ID>
<cbc:HandlingCode>13</cbc:HandlingCode>
<cbc:Information>TRASLADO A OBRA</cbc:Information>
<cbc:GrossWeightMeasure unitCode="KGM">26.000</cbc:GrossWeightMeasure>
<cbc:SplitConsignmentIndicator>false</cbc:SplitConsignmentIndicator>
<cac:ShipmentStage>
<cbc:TransportModeCode>02</cbc:TransportModeCode>
<cac:TransitPeriod>
<cbc:StartDate>2022-12-15</cbc:StartDate>
</cac:TransitPeriod>
<cac:CarrierParty>
<cac:PartyIdentification>
<cbc:ID schemeID="6">12345678912</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name>Softgreen S.A.C</cbc:Name>
</cac:PartyName>
</cac:CarrierParty>
<cac:TransportMeans>
<cac:RoadTransport>
<cbc:LicensePlateID>BBA-803</cbc:LicensePlateID>
</cac:RoadTransport>
</cac:TransportMeans>
<cac:DriverPerson>
<cbc:ID schemeID="1">71988046</cbc:ID>
</cac:DriverPerson>
</cac:ShipmentStage>
<cac:Delivery>
<cac:DeliveryAddress>
<cbc:ID>080910</cbc:ID>
<cbc:StreetName>AV. AVIACION</cbc:StreetName>
<cac:Country>
<cbc:IdentificationCode>PE</cbc:IdentificationCode>
</cac:Country>
</cac:DeliveryAddress>
</cac:Delivery>
<cac:OriginAddress>
<cbc:ID>080910</cbc:ID>
<cbc:StreetName>S/N CENTRO POB. PICHARI</cbc:StreetName>
<cac:Country>
<cbc:IdentificationCode>PE</cbc:IdentificationCode>
</cac:Country>
</cac:OriginAddress>
</cac:Shipment>
<cac:DespatchLine>
<cbc:ID>1</cbc:ID>
<cbc:DeliveredQuantity unitCode="NIU">3</cbc:DeliveredQuantity>
<cac:OrderLineReference>
<cbc:LineID>1</cbc:LineID>
</cac:OrderLineReference>
<cac:Item>
<cbc:Name>ESPATULA DE 3""</cbc:Name>
<cac:SellersItemIdentification>
<cbc:ID>3121190200070403</cbc:ID>
</cac:SellersItemIdentification>
</cac:Item>
</cac:DespatchLine>
<cac:DespatchLine>
<cbc:ID>2</cbc:ID>
<cbc:DeliveredQuantity unitCode="BG">1</cbc:DeliveredQuantity>
<cac:OrderLineReference>
<cbc:LineID>1</cbc:LineID>
</cac:OrderLineReference>
<cac:Item>
<cbc:Name>TEMPLE PATO BLANCO X 25 KG</cbc:Name>
<cac:SellersItemIdentification>
<cbc:ID>250811</cbc:ID>
</cac:SellersItemIdentification>
</cac:Item>
</cac:DespatchLine>
</DespatchAdvice>
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class QuarkusXSenderResource {
CompanyURLs companyURLs = CompanyURLs
.builder()
.invoice("https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService")
.despatch("https://e-beta.sunat.gob.pe/ol-ti-itemision-otroscpe-gem-beta/billService")
.despatch("https://e-beta.sunat.gob.pe/ol-ti-itemision-guia-gem-beta/billService")
.perceptionRetention("https://e-beta.sunat.gob.pe/ol-ti-itemision-guia-gem-beta/billService")
.build();

Expand Down

0 comments on commit 0fda533

Please sign in to comment.