|
16 | 16 | import io.facturapi.enums.Taxability; |
17 | 17 | import io.facturapi.http.FacturapiConfig; |
18 | 18 | import io.facturapi.models.Customer; |
| 19 | +import io.facturapi.models.InvoiceItem; |
19 | 20 | import java.io.InputStream; |
20 | 21 | import java.nio.charset.StandardCharsets; |
21 | 22 | import java.time.Instant; |
22 | 23 | import java.time.LocalDate; |
| 24 | +import java.util.List; |
23 | 25 | import java.util.Map; |
24 | 26 | import org.junit.jupiter.api.Test; |
25 | 27 |
|
@@ -206,6 +208,98 @@ void retentionDraftCancelAndListUseExpectedPaths() { |
206 | 208 | assertEquals("/v2/retentions?status=draft", listRequest.uri().getPath() + "?" + listRequest.uri().getQuery()); |
207 | 209 | } |
208 | 210 |
|
| 211 | + @Test |
| 212 | + void invoiceZipRequestCanBeCreated() { |
| 213 | + StubHttpClient httpClient = new StubHttpClient(); |
| 214 | + httpClient.enqueueJson(200, "{\"id\":\"zip_1\",\"status\":\"pending\"}"); |
| 215 | + |
| 216 | + Facturapi sdk = new Facturapi( |
| 217 | + FacturapiConfig.builder("sk_test") |
| 218 | + .httpClient(httpClient.client()) |
| 219 | + .build() |
| 220 | + ); |
| 221 | + |
| 222 | + var response = sdk.invoices().createZipRequest( |
| 223 | + Map.of( |
| 224 | + "year", 2025, |
| 225 | + "month", 3, |
| 226 | + "issuer_type", "issuing", |
| 227 | + "invoice_types", java.util.List.of("I", "E") |
| 228 | + ) |
| 229 | + ); |
| 230 | + |
| 231 | + assertEquals("zip_1", response.get("id")); |
| 232 | + var request = httpClient.requests().get(0); |
| 233 | + assertEquals("POST", request.method()); |
| 234 | + assertEquals("/v2/invoices/zip-requests", request.uri().getPath()); |
| 235 | + assertTrue(request.bodyUtf8().contains("\"issuer_type\":\"issuing\"")); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + void invoiceZipRequestsCanBeListed() { |
| 240 | + StubHttpClient httpClient = new StubHttpClient(); |
| 241 | + httpClient.enqueueJson( |
| 242 | + 200, |
| 243 | + "{\"page\":1,\"total_pages\":1,\"total_results\":1,\"data\":[{\"id\":\"zip_1\"}]}" |
| 244 | + ); |
| 245 | + |
| 246 | + Facturapi sdk = new Facturapi( |
| 247 | + FacturapiConfig.builder("sk_test") |
| 248 | + .httpClient(httpClient.client()) |
| 249 | + .build() |
| 250 | + ); |
| 251 | + |
| 252 | + var response = sdk.invoices().listZipRequests( |
| 253 | + Map.of("year", 2025, "month", 3, "status", "finished", "limit", 20, "page", 1) |
| 254 | + ); |
| 255 | + |
| 256 | + assertEquals("zip_1", response.getData().get(0).get("id")); |
| 257 | + var request = httpClient.requests().get(0); |
| 258 | + assertEquals("GET", request.method()); |
| 259 | + assertEquals("/v2/invoices/zip-requests", request.uri().getPath()); |
| 260 | + assertTrue(request.uri().getQuery().contains("year=2025")); |
| 261 | + assertTrue(request.uri().getQuery().contains("status=finished")); |
| 262 | + } |
| 263 | + |
| 264 | + @Test |
| 265 | + void invoiceZipRequestCanBeRetrieved() { |
| 266 | + StubHttpClient httpClient = new StubHttpClient(); |
| 267 | + httpClient.enqueueJson(200, "{\"id\":\"zip_1\",\"status\":\"finished\"}"); |
| 268 | + |
| 269 | + Facturapi sdk = new Facturapi( |
| 270 | + FacturapiConfig.builder("sk_test") |
| 271 | + .httpClient(httpClient.client()) |
| 272 | + .build() |
| 273 | + ); |
| 274 | + |
| 275 | + var response = sdk.invoices().retrieveZipRequest("zip_1"); |
| 276 | + |
| 277 | + assertEquals("finished", response.get("status")); |
| 278 | + var request = httpClient.requests().get(0); |
| 279 | + assertEquals("GET", request.method()); |
| 280 | + assertEquals("/v2/invoices/zip-requests/zip_1", request.uri().getPath()); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + void invoiceZipRequestCanBeDownloaded() throws Exception { |
| 285 | + StubHttpClient httpClient = new StubHttpClient(); |
| 286 | + httpClient.enqueueBinary(200, "ZIP-CONTENT".getBytes(StandardCharsets.UTF_8), "application/zip"); |
| 287 | + |
| 288 | + Facturapi sdk = new Facturapi( |
| 289 | + FacturapiConfig.builder("sk_test") |
| 290 | + .httpClient(httpClient.client()) |
| 291 | + .build() |
| 292 | + ); |
| 293 | + |
| 294 | + try (InputStream stream = sdk.invoices().downloadZipRequestStream("zip_1")) { |
| 295 | + assertEquals("ZIP-CONTENT", new String(stream.readAllBytes(), StandardCharsets.UTF_8)); |
| 296 | + } |
| 297 | + |
| 298 | + var request = httpClient.requests().get(0); |
| 299 | + assertEquals("GET", request.method()); |
| 300 | + assertEquals("/v2/invoices/zip-requests/zip_1/zip", request.uri().getPath()); |
| 301 | + } |
| 302 | + |
209 | 303 | @Test |
210 | 304 | void organizationUploadsAcceptBytes() { |
211 | 305 | StubHttpClient httpClient = new StubHttpClient(); |
@@ -414,4 +508,18 @@ void objectMapperDeserializesCodeEnums() throws Exception { |
414 | 508 | assertEquals(TaxType.IEPS, tax.getType()); |
415 | 509 | assertEquals(TaxFactor.EXENTO, tax.getFactor()); |
416 | 510 | } |
| 511 | + |
| 512 | + @Test |
| 513 | + void objectMapperDeserializesPropertyTaxAccountsAsArrays() throws Exception { |
| 514 | + var mapper = FacturapiConfig.builder("sk_test").build().getObjectMapper(); |
| 515 | + |
| 516 | + var empty = mapper.readValue("{\"property_tax_account\":[]}", InvoiceItem.class); |
| 517 | + var accounts = mapper.readValue( |
| 518 | + "{\"property_tax_account\":[\"0102030405\"]}", |
| 519 | + InvoiceItem.class |
| 520 | + ); |
| 521 | + |
| 522 | + assertEquals(List.of(), empty.getPropertyTaxAccounts()); |
| 523 | + assertEquals(List.of("0102030405"), accounts.getPropertyTaxAccounts()); |
| 524 | + } |
417 | 525 | } |
0 commit comments