-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCarboneTest.java
More file actions
498 lines (383 loc) · 41.3 KB
/
CarboneTest.java
File metadata and controls
498 lines (383 loc) · 41.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
package io.carbone;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import feign.Request;
import feign.Response;
public class CarboneTest {
String CARBONE_URI = "https://api.carbone.io";
CarboneServicesFactory carboneServicesFactory;
ICarboneTemplateClient carboneTemplate = mock(CarboneTemplateClient.class);
ICarboneRenderClient carboneRender = mock(CarboneRenderClient.class);
ICarboneStatusClient carboneStatus = mock(CarboneStatusClient.class);
ICarboneServices carboneServices = CarboneServicesFactory.CARBONE_SERVICES_FACTORY_INSTANCE.create(carboneTemplate, carboneRender, carboneStatus);
CarboneException carboneException;
String directory = System.getProperty("user.dir");
@Test
public void Test_Add_Template_With_Path() throws CarboneException, IOException, InterruptedException {
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.templateId("fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987B")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
String filename = "/src/test/java/io/carbone/template3.odt";
String file = directory + filename;
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
when(carboneTemplate.addTemplate(fileBytes)).thenReturn(mockedResponse);
String resp = carboneServices.addTemplate(file);
assertEquals(resp, mockedResponse.getData().templateId);
}
@Test
public void Test_Add_Template_With_Array_Byte() throws CarboneException, IOException, InterruptedException {
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.templateId("fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987B")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
when(carboneTemplate.addTemplate(fileBytes)).thenReturn(mockedResponse);
String resp = carboneServices.addTemplate(Files.readAllBytes(filePath));
assertEquals(resp, mockedResponse.getData().templateId);
}
@Test
public void Test_Add_Template_Error_Path() throws CarboneException, IOException
{
String filename = "/src/test/java/io/carbone/template.odt";
Path filePath = Paths.get(filename);
try{
carboneTemplate.addTemplate(Files.readAllBytes(filePath));
}
catch(NoSuchFileException e)
{
assertEquals(filename, e.getMessage());
}
}
@Test
public void Test_Delete_Template() throws CarboneException
{
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.error(null)
.build();
when(carboneTemplate.deleteTemplate("fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987a")).thenReturn(mockedResponse);
boolean resp = carboneServices.deleteTemplate("fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987a");
assertEquals(resp, mockedResponse.isSuccess());
}
@Test
public void Test_Delete_Template_Error_Already_Deleted() throws CarboneException
{
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(false)
.error("Error: Cannot remove template, does it exist ?")
.build();
when(carboneTemplate.deleteTemplate("fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987a")).thenReturn(mockedResponse);
boolean resp = carboneServices.deleteTemplate("fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987a");
assertEquals(resp, mockedResponse.isSuccess());
}
@Test
public void Test_Generate_TemplateId()
{
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
String templateId = carboneServices.generateTemplateId(filePath.toString());
assertEquals(templateId, "fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987a");
}
@Test
public void Test_Generate_TemplateId_With_A_Wrong_Path() throws NoSuchFileException
{
String filename = "";
String templateId = carboneServices.generateTemplateId(filename);
assertNull(templateId);
}
@Test
public void Test_Get_Template() throws IOException, CarboneException {
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
String templateId = carboneServices.generateTemplateId(filePath.toString());
when(carboneTemplate.getTemplate(anyString())).thenReturn(new CarboneFileResponse(fileBytes));
byte[] resp = carboneServices.getTemplate(templateId);
assertArrayEquals(fileBytes, resp);
}
@Test
public void Test_Render_Repport_With_Template_Id() throws CarboneException
{
String templateId = "fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987B";
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
when(carboneRender.renderReport(json, templateId)).thenReturn(mockedResponse);
String resp = carboneServices.renderReport(json, templateId);
assertEquals(resp, "MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf");
}
@Test
public void Test_Render_Repport_With_Path() throws CarboneException
{
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
String filename = directory + "/src/test/java/io/carbone/template3.odt";
String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
String templateID = carboneServices.generateTemplateId(filename);
when(carboneRender.renderReport(json, templateID)).thenReturn(mockedResponse);
String resp = carboneServices.renderReport(json, filename);
assertEquals(resp, "MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf");
}
@Test
public void Test_Get_Report() throws IOException, CarboneException
{
String renderId = "MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf";
String filename = "/src/test/java/io/carbone/template3.odt";
String name = "test.pdf";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
when(carboneRender.getReport(anyString())).thenReturn(new CarboneDocument(fileBytes, name));
CarboneDocument resp = carboneRender.getReport(renderId);
assertArrayEquals(fileBytes, resp.getFileContent());
assertEquals(resp.getName(), name);
}
@Test
public void Test_Render_Template_Id() throws CarboneException
{
String fakeTemplateId = "FakeTemplateId";
String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
when(carboneRender.renderReport(json, fakeTemplateId)).thenThrow(new CarboneException("Carbone SDK render error: Error while rendering template Error: 404 Not Found"));
try {
carboneServices.render(json, fakeTemplateId);
} catch (CarboneException e) {
assertEquals("Carbone SDK render error: Error while rendering template Error: 404 Not Found", e.getMessage());
}
}
@Test
public void Test_Render_Error_From_Directory() throws CarboneException, NoSuchFileException
{
String filename = "/src/test/java/io/carbone/template.odt";
String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
when(carboneRender.renderReport(json, filename)).thenThrow(new CarboneException("Carbone SDK render error: failled to generate the template id"));
try {
carboneServices.render(json, filename);
} catch (CarboneException e) {
assertEquals("Carbone SDK render error: failled to generate the template id", e.getMessage());
}
}
@Test
public void Test_Render_A_Report_From_An_Null_Template_Id() throws CarboneException, IOException {
final String templateId = "";
final String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
final String name = "test.pdf";
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
when(carboneRender.renderReport(json, templateId)).thenReturn(mockedResponse);
when(carboneRender.getReport(mockedResponse.getData().renderId)).thenReturn(new CarboneDocument(fileBytes, name));
try{
carboneServices.render(json, templateId);
}
catch(CarboneException e){
assertEquals(e.getMessage(), "Carbone SDK render error: argument is missing: file_or_template_id");
}
}
@Test
public void Test_Render_A_Report_From_An_Null_Json() throws CarboneException, IOException {
final String templateId = "fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987B";
final String json = "";
final String name = "test.pdf";
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
when(carboneRender.renderReport(json, templateId)).thenReturn(mockedResponse);
when(carboneRender.getReport(mockedResponse.getData().renderId)).thenReturn(new CarboneDocument(fileBytes, name));
try{
carboneServices.render(json, templateId);
}
catch(CarboneException e){
assertEquals(e.getMessage(), "Carbone SDK render error: argument is missing: json_data");
}
}
@Test
public void Test_Render_A_Report_From_An_Existing_Template_Id() throws CarboneException, IOException {
final String templateId = "fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987B";
final String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
final String name = "test.pdf";
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
when(carboneRender.renderReport(json, templateId)).thenReturn(mockedResponse);
when(carboneRender.getReport(mockedResponse.getData().renderId)).thenReturn(new CarboneDocument(fileBytes, name));
CarboneDocument resp = carboneServices.render(json, templateId);
assertArrayEquals(fileBytes, resp.getFileContent());
assertEquals(resp.getName(), name);
}
@Test
public void Test_Render_From_A_Template_Already_Upload() throws IOException, CarboneException{
final String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
final String name = "test.pdf";
final String templateId = "d13876946291b722c3071c8ed2787037bbb3902e2503f955783c6f5912c5b1d4";
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
when(carboneRender.renderReport(json, templateId)).thenReturn(mockedResponse);
when(carboneRender.getReport(mockedResponse.getData().renderId)).thenReturn(new CarboneDocument(fileBytes, name));
CarboneDocument resp = carboneServices.render(json, templateId);
assertArrayEquals(fileBytes, resp.getFileContent());
assertEquals(resp.getName(), name);
}
@Test
public void Test_Render_Something_Wrong() throws IOException, CarboneException{
final String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
final String name = "test.pdf";
String filename = "/src/test/java/io/carbone/Template.html";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
when(carboneRender.renderReport(json, directory + filename)).thenReturn(mockedResponse);
when(carboneRender.getReport(mockedResponse.getData().renderId)).thenReturn(new CarboneDocument(fileBytes, name));
try{
carboneServices.render(json, directory + filename);
}
catch(CarboneException e){
assertEquals(e.getMessage(), "Carbone SDK render error: something went wrong");
}
}
@Test
public void Test_Render_From_A_Generate_Template_Id() throws IOException, CarboneException{
final String json = "{ \"data\": { \"invoiceId\": \"FR-2023-781\", \"invoiceDate\": \"2023-01-17\", \"name\": \"Quentin Le Forestier\", \"phone\": \"+33 (0)2 23 12 33 24\", \"email\": \"tim+serverpro@carbone.io\", \"partner\": { \"name\": \"Corp\", \"phone\": \"+33 (0)6 27 89 01 23\", \"email\": \"quentinleforestierleforestier0@gmail.com\", \"vat\": \"FR45899106785\", \"siren\": \"899106785\", \"address\": { \"street\": \"12 Doctor Street\", \"postalcode\": 34920, \"city\": \"Anger\", \"country\": \"France\" } }, \"note\": \"Our team is confident that <b>this cutting-edge technology</b> will enhance your operations and take your business to the next level. Should you have any questions or concerns, please don't hesitate to reach out to us.\", \"subscriptionFromDate\": \"2023-02-02T07:28:05+00:00\", \"subscriptionToDate\": \"2024-04-18T07:28:05+00:00\", \"products\": [ { \"name\": \"<b>Rackmount case</b> (With bays for multiple 3.5-inch drives and room for the motherboard and other components.)\", \"exTaxTotal\": 1000, \"unit\": 1000, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>Motherboard</b> (has the necessary number of SATA ports for your storage needs)\", \"exTaxTotal\": 150, \"unit\": 150, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>3.5-inch SATA hard drives</b> (20TO)\", \"exTaxTotal\": 1000, \"unit\": 100, \"vat\": 20, \"qty\": 10 }, { \"name\": \"<b>Processor</b> ( A low-power consumption processor)\", \"exTaxTotal\": 100, \"unit\": 100, \"vat\": 20, \"qty\": 1 }, { \"name\": \"<b>4GB DDR5 RAM</b>\", \"exTaxTotal\": 120, \"unit\": 30, \"vat\": 20, \"qty\": 4 } ], \"exTaxTotal\": 1870, \"taxTotal\": 374, \"inTaxTotal\": 2244, \"amountRemaining\": 1044, \"invoicePaymentList\": [ { \"paymentDate\": \"2023-02-03T07:28:05+00:00\", \"typeSelect\": \"Paiement\", \"amount\": 1200 } ], \"payment\": { \"name\": \"SEPA Transfer\", \"typeSelect\": \"SEPAtransfer\", \"condition\": \"Upon receipt of invoice\", \"bankLabel\": \"Revolut\", \"bankBIC\": \"REVOGB2L\", \"bankIBAN\": \"FR2112739000409352423869N75\" } }, \"convertTo\" : { \"formatName\" : \"pdf\" } }";
final String name = "test.pdf";
String filename = directory + "/src/test/java/io/carbone/Template.html";
Path filePath = Paths.get(filename);
byte[] fileBytes = Files.readAllBytes(filePath);
String templateId = carboneServices.generateTemplateId(filename);
CarboneResponse.CarboneResponseData responseDataRender = CarboneResponse.CarboneResponseData.builder()
.renderId("MTAuMjAuMTEuNDAgICAgCRLu7jNNEe84ubAa82ofaAcmVwb3J0.pdf")
.build();
CarboneResponse mockedResponseRender = CarboneResponse.builder()
.success(true)
.data(responseDataRender)
.build();
CarboneResponse.CarboneResponseData responseData = CarboneResponse.CarboneResponseData.builder()
.templateId(templateId)
.build();
CarboneResponse mockedResponse = CarboneResponse.builder()
.success(true)
.data(responseData)
.build();
CarboneResponse mocked = CarboneResponse.builder()
.success(false)
.build();
CarboneException mockException = CarboneException.builder()
.carboneResponse(mocked)
.httpStatus(404)
.build();
when(carboneRender.renderReport(json, templateId)).thenThrow(mockException).thenReturn(mockedResponseRender);
when(carboneTemplate.addTemplate(fileBytes)).thenReturn(mockedResponse);
when(carboneRender.getReport(mockedResponseRender.getData().renderId)).thenReturn(new CarboneDocument(fileBytes, name));
CarboneDocument resp = carboneServices.render(json, filename);
assertArrayEquals(fileBytes, resp.getFileContent());
assertEquals(resp.getName(), name);
}
@Test
public void Test_Get_Status() throws CarboneException {
Map<String, Collection<String>> headers = new HashMap<>();
headers.put("Content-Type", Collections.singletonList("application/json"));
Request originalRequest = Request.create(Request.HttpMethod.GET, CARBONE_URI + "/status", headers, null, StandardCharsets.UTF_8, null);
Response response = Response.builder()
.status(200)
.request(originalRequest)
.headers(Collections.emptyMap())
.body("{\"success\":true,\"code\":200,\"message\":\"OK\",\"version\":\"4.22.8\"}", StandardCharsets.UTF_8)
.build();
when(carboneStatus.getStatus()).thenReturn(response);
String result = carboneServices.getStatus();
assertEquals("{\"success\":true,\"code\":200,\"message\":\"OK\",\"version\":\"4.22.8\"}", result);
}
@Test
public void Test_Set_Carbon_Uri() throws CarboneException{
CarboneServicesFactory.CARBONE_SERVICES_FACTORY_INSTANCE.SetCarboneUrl(directory);
assertEquals(CarboneServicesFactory.CARBONE_SERVICES_FACTORY_INSTANCE.GetCarboneUrl(), directory);
}
@Test
public void Test_Carbon_Service_Fectory() throws CarboneException{
ICarboneServices carboneService = CarboneServicesFactory.CARBONE_SERVICES_FACTORY_INSTANCE.create("token", "4");
String status = carboneService.getStatus();
assertEquals(status.contains("\"success\":true,\"code\":200"), true);
}
@Test
public void Test_Carbon_Service_Fectory_With_No_Argument() throws CarboneException{
ICarboneServices carboneService = CarboneServicesFactory.CARBONE_SERVICES_FACTORY_INSTANCE.create("");
String status = carboneService.getStatus();
assertEquals(status.contains("\"success\":true,\"code\":200"), true);
}
@Test
public void Test_Generate_Template_Id_From_Byte_Array() throws CarboneException, IOException
{
String filename = "/src/test/java/io/carbone/template3.odt";
Path filePath = Paths.get(directory, filename);
byte[] fileBytes = Files.readAllBytes(filePath);
String templateId = carboneServices.generateTemplateId(fileBytes);
assertEquals(templateId, "fb9241ea2218ffd8f974110e539386384620244618c2efbf182b7bd47242987a");
}
}