Skip to content

Commit c2201bd

Browse files
committed
Add UT Issue/Stamp V4
1 parent 44aea7d commit c2201bd

File tree

7 files changed

+528
-18
lines changed

7 files changed

+528
-18
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"**/.settings": true,
77
"**/.factorypath": true
88
},
9-
"java.compile.nullAnalysis.mode": "automatic"
9+
"java.compile.nullAnalysis.mode": "automatic",
10+
"java.debug.settings.onBuildFailureProceed": true
1011
}

resources/pdfresult.pdf

3.75 KB
Binary file not shown.

src/main/java/mx/com/sw/helpers/GeneralValidations.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package mx.com.sw.helpers;
22

3+
import java.util.List;
4+
import java.util.Arrays;
5+
import java.util.regex.Matcher;
6+
import java.util.regex.Pattern;
7+
8+
import mx.com.sw.exceptions.GeneralException;
39
import mx.com.sw.exceptions.ServicesException;
410

511
/**
@@ -66,4 +72,39 @@ private void validateToken(String tokenValidate) throws ServicesException {
6672
throw new ServicesException("Token Mal Formado");
6773
}
6874
}
75+
76+
/**
77+
* Este método valida el CustomId proporcionado.
78+
* @param customId
79+
* @throws ServicesException
80+
*/
81+
protected void validateCustomId(String customId) throws ServicesException{
82+
if (customId.length() <= 0 || customId.length() > 150) {
83+
throw new ServicesException("El CustomId no es válido o viene vacío.");
84+
}
85+
}
86+
87+
/**
88+
* Este método valida el o los emails proporcionados.
89+
* @param emails
90+
* @throws ServicesException
91+
*/
92+
protected boolean validateEmails(String emails) throws ServicesException {
93+
List<String> emailList = Arrays.asList(emails.split("\\s*,\\s*"));
94+
if (emailList.size() > 0 && emailList.size() <= 5) {
95+
for (int i = 0; i <= emailList.size() - 1; i++) {
96+
Pattern pattern = Pattern
97+
.compile("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$");
98+
Matcher matcher = pattern.matcher(emailList.get(i));
99+
if (!matcher.matches()) {
100+
throw new ServicesException(
101+
"El listado de correos no contiene un formato válido o alguno de los correos es inválido."
102+
);
103+
}
104+
}
105+
} else {
106+
throw new ServicesException("El listado de correos está vacío o contiene más de 5 correos.");
107+
}
108+
return true;
109+
}
69110
}

src/test/java/mx/com/sw/services/cfdi40/helpers/StampV4.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public StampV4(Boolean isToken) throws ServicesException {
3737
public StampResponseV1 stampResponseV1(String fileName, String stampVersion, boolean signed,
3838
boolean isBase64) throws ServicesException {
3939
return stamp.timbrarV1(getSettings().getCFDI(fileName, true, "4.0", isBase64),
40-
getSettings().getEmail(), isBase64);
40+
getSettings().getEmail(), null, false, isBase64);
4141
}
4242

4343
/**
@@ -47,7 +47,7 @@ public StampResponseV1 stampResponseV1(String fileName, String stampVersion, boo
4747
public StampResponseV2 stampResponseV2(String fileName, String stampVersion, boolean signed,
4848
boolean isBase64) throws ServicesException {
4949
return stamp.timbrarV2(getSettings().getCFDI(fileName, true, "4.0", isBase64),
50-
getSettings().getEmail(), isBase64);
50+
getSettings().getEmail(), null, false, isBase64);
5151
}
5252

5353
/**
@@ -57,7 +57,7 @@ public StampResponseV2 stampResponseV2(String fileName, String stampVersion, boo
5757
public StampResponseV3 stampResponseV3(String fileName, String stampVersion, boolean signed,
5858
boolean isBase64) throws ServicesException {
5959
return stamp.timbrarV3(getSettings().getCFDI(fileName, true, "4.0", isBase64),
60-
getSettings().getEmail(), isBase64);
60+
getSettings().getEmail(), null, false, isBase64);
6161
}
6262

6363
/**
@@ -67,6 +67,6 @@ public StampResponseV3 stampResponseV3(String fileName, String stampVersion, boo
6767
public StampResponseV4 stampResponseV4(String fileName, String stampVersion, boolean signed,
6868
boolean isBase64) throws ServicesException {
6969
return stamp.timbrarV4(getSettings().getCFDI(fileName, true, "4.0", isBase64),
70-
getSettings().getEmail(), isBase64);
70+
getSettings().getEmail(), null, false, isBase64);
7171
}
7272
}

src/test/java/mx/com/sw/services/issue/IssueJsonTest.java

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
import mx.com.sw.services.stamp.responses.StampResponseV2;
77
import mx.com.sw.services.stamp.responses.StampResponseV3;
88
import mx.com.sw.services.stamp.responses.StampResponseV4;
9+
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.UUID;
14+
import java.util.stream.Collectors;
15+
916
import org.junit.jupiter.api.Assertions;
1017
import org.junit.jupiter.api.Test;
1118

@@ -190,7 +197,129 @@ public void testV4StampV1() {
190197
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
191198
null, 0);
192199
String json = settings.getJsonCFDI();
193-
StampResponseV1 response = stamp.timbrarV1(json, settings.getEmail());
200+
StampResponseV1 response = stamp.timbrarV1(json, settings.getEmail(), null, false);
201+
Assertions.assertNotNull(response);
202+
Assertions.assertNotNull(response.getData());
203+
Assertions.assertNotNull(response.getStatus());
204+
Assertions.assertNotNull(response.getData().getTFD());
205+
Assertions.assertTrue("success".equalsIgnoreCase(response.getStatus()));
206+
} catch (ServicesException ex) {
207+
Assertions.assertNotNull(ex);
208+
}
209+
}
210+
211+
/**
212+
* Método de UT timbrado versión 1 (con usuario y password y envio de email erroneo).
213+
*/
214+
@Test
215+
public void testV4StampV1_errorEmail() {
216+
try {
217+
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
218+
null, 0);
219+
String json = settings.getJsonCFDI();
220+
StampResponseV1 response = stamp.timbrarV1(json, "correotest.com.mx", null, false);
221+
String messageExpect = "El listado de correos no contiene un formato válido o alguno de los correos es inválido.";
222+
Assertions.assertNotNull(response);
223+
Assertions.assertTrue("error".equalsIgnoreCase(response.getStatus()));
224+
Assertions.assertTrue(messageExpect .equalsIgnoreCase(response.getMessage()));
225+
} catch (ServicesException ex) {
226+
Assertions.assertNotNull(ex);
227+
}
228+
}
229+
230+
/**
231+
* Método de UT timbrado versión 1 (con usuario y password y envio de 6 emails).
232+
*/
233+
@Test
234+
public void testV4StampV1_maxEmail() {
235+
try {
236+
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
237+
null, 0);
238+
String json = settings.getJsonCFDI();
239+
String emails = "correo@test.com.mx, correo@test2.com.mx, correo@test3.com.mx, correo@test4.com.mx, correo@test5.com.mx, correo@test6.com.mx";
240+
StampResponseV1 response = stamp.timbrarV1(json, emails, null, false);
241+
String messageExpect = "El listado de correos está vacío o contiene más de 5 correos.";
242+
Assertions.assertNotNull(response);
243+
Assertions.assertTrue("error".equalsIgnoreCase(response.getStatus()));
244+
Assertions.assertTrue(messageExpect .equalsIgnoreCase(response.getMessage()));
245+
} catch (ServicesException ex) {
246+
Assertions.assertNotNull(ex);
247+
}
248+
}
249+
250+
/**
251+
* Método de UT timbrado versión 1 (con usuario y password y envio de CustomId).
252+
*/
253+
@Test
254+
public void testV4StampV1_customId() {
255+
try {
256+
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
257+
null, 0);
258+
String json = settings.getJsonCFDI();
259+
String customId = UUID.randomUUID().toString();
260+
StampResponseV1 response = stamp.timbrarV1(json, null, customId, false);
261+
Assertions.assertNotNull(response);
262+
Assertions.assertNotNull(response.getData());
263+
Assertions.assertNotNull(response.getStatus());
264+
Assertions.assertNotNull(response.getData().getTFD());
265+
Assertions.assertTrue("success".equalsIgnoreCase(response.getStatus()));
266+
} catch (ServicesException ex) {
267+
Assertions.assertNotNull(ex);
268+
}
269+
}
270+
271+
/**
272+
* Método de UT timbrado versión 1 (con usuario y password y envio de CustomId duplicado).
273+
*/
274+
@Test
275+
public void testV4StampV1_duplicateCustomId() {
276+
try {
277+
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
278+
null, 0);
279+
String json = settings.getJsonCFDI();
280+
String customId = UUID.randomUUID().toString();
281+
StampResponseV1 response = stamp.timbrarV1(json, null, customId, false);
282+
Assertions.assertNotNull(response);
283+
Assertions.assertTrue("success".equalsIgnoreCase(response.getStatus()));
284+
response = stamp.timbrarV1(json, null, customId, false);
285+
Assertions.assertTrue("error".equalsIgnoreCase(response.getStatus()));
286+
Assertions.assertTrue("307. El comprobante contiene un timbre previo.".equalsIgnoreCase(response.getMessage()));
287+
} catch (ServicesException ex) {
288+
Assertions.assertNotNull(ex);
289+
}
290+
}
291+
292+
/**
293+
* Método de UT timbrado versión 1 (con usuario y password y envio de CustomId invalido).
294+
*/
295+
@Test
296+
public void testV4StampV1_invalidCustomId() {
297+
try {
298+
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
299+
null, 0);
300+
String json = settings.getJsonCFDI();
301+
String customId = UUID.randomUUID().toString();
302+
customId = Collections.nCopies(10, customId).stream().collect(Collectors.joining());
303+
StampResponseV1 response = stamp.timbrarV1(json, null, customId, false);
304+
String messageExpect = "El CustomId no es válido o viene vacío.";
305+
Assertions.assertNotNull(response);
306+
Assertions.assertTrue("error".equalsIgnoreCase(response.getStatus()));
307+
Assertions.assertTrue(messageExpect .equalsIgnoreCase(response.getMessage()));
308+
} catch (ServicesException ex) {
309+
Assertions.assertNotNull(ex);
310+
}
311+
}
312+
313+
/**
314+
* Método de UT timbrado versión 1 (con usuario y password y envio de parametro extra para crear PDF).
315+
*/
316+
@Test
317+
public void testV4StampV1_pdf() {
318+
try {
319+
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
320+
null, 0);
321+
String json = settings.getJsonCFDI();
322+
StampResponseV1 response = stamp.timbrarV1(json, null, null, true);
194323
Assertions.assertNotNull(response);
195324
Assertions.assertNotNull(response.getData());
196325
Assertions.assertNotNull(response.getStatus());
@@ -210,7 +339,7 @@ public void testV4StampV2() {
210339
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getUserSW(), settings.getPasswordSW(),
211340
null, 0);
212341
String json = settings.getJsonCFDI();
213-
StampResponseV2 response = stamp.timbrarV2(json, settings.getEmail());
342+
StampResponseV2 response = stamp.timbrarV2(json, settings.getEmail(), null, false);
214343
Assertions.assertNotNull(response);
215344
Assertions.assertNotNull(response.getData());
216345
Assertions.assertNotNull(response.getStatus());
@@ -229,7 +358,7 @@ public void testV4StampV3() {
229358
try {
230359
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getTokenSW(), null, 0);
231360
String json = settings.getJsonCFDI();
232-
StampResponseV3 response = stamp.timbrarV3(json, settings.getEmail());
361+
StampResponseV3 response = stamp.timbrarV3(json, settings.getEmail(), null, false);
233362
Assertions.assertNotNull(response);
234363
Assertions.assertNotNull(response.getData());
235364
Assertions.assertNotNull(response.getStatus());
@@ -248,7 +377,7 @@ public void testV4StampV4() {
248377
try {
249378
IssueJsonV4 stamp = new IssueJsonV4(settings.getUrlSW(), settings.getTokenSW(), null, 0);
250379
String json = settings.getJsonCFDI();
251-
StampResponseV4 response = stamp.timbrarV4(json, settings.getEmail());
380+
StampResponseV4 response = stamp.timbrarV4(json, settings.getEmail(), null, false);
252381
Assertions.assertNotNull(response);
253382
Assertions.assertNotNull(response.getData());
254383
Assertions.assertNotNull(response.getStatus());

0 commit comments

Comments
 (0)