-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathparser_cii_test.go
More file actions
633 lines (588 loc) · 27.4 KB
/
Copy pathparser_cii_test.go
File metadata and controls
633 lines (588 loc) · 27.4 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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package einvoice
import (
"bytes"
"os"
"strings"
"testing"
)
func TestSimple(t *testing.T) {
t.Parallel()
inv, err := ParseXMLFile("testdata/cii/en16931/zugferd_2p0_EN16931_1_Teilrechnung.xml")
if err != nil {
t.Fatal(err)
}
expected := Invoice{
InvoiceNumber: "471102",
}
if got := inv.InvoiceNumber; got != expected.InvoiceNumber {
t.Errorf("invoice number got %s, expected %s\n", got, expected.InvoiceNumber)
}
}
func TestInvalidDecimalValue(t *testing.T) {
t.Parallel()
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty><ram:Name>Buyer</ram:Name></ram:BuyerTradeParty>
<ram:SellerTradeParty><ram:Name>Seller</ram:Name></ram:SellerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>INVALID</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
_, err := ParseReader(strings.NewReader(xml))
if err == nil {
t.Error("expected error for invalid decimal value, got nil")
}
if !strings.Contains(err.Error(), "invalid decimal value") {
t.Errorf("expected error message to contain 'invalid decimal value', got: %v", err)
}
}
func TestCountrySubDivisionNameParsing(t *testing.T) {
t.Parallel()
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty>
<ram:Name>Buyer Company</ram:Name>
<ram:PostalTradeAddress>
<ram:PostcodeCode>12345</ram:PostcodeCode>
<ram:LineOne>123 Main St</ram:LineOne>
<ram:CityName>Berlin</ram:CityName>
<ram:CountryID>DE</ram:CountryID>
<ram:CountrySubDivisionName>Brandenburg</ram:CountrySubDivisionName>
</ram:PostalTradeAddress>
</ram:BuyerTradeParty>
<ram:SellerTradeParty>
<ram:Name>Seller Company</ram:Name>
<ram:PostalTradeAddress>
<ram:PostcodeCode>54321</ram:PostcodeCode>
<ram:LineOne>456 Oak Ave</ram:LineOne>
<ram:CityName>Munich</ram:CityName>
<ram:CountryID>DE</ram:CountryID>
<ram:CountrySubDivisionName>Bavaria</ram:CountrySubDivisionName>
</ram:PostalTradeAddress>
</ram:SellerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
inv, err := ParseReader(strings.NewReader(xml))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if inv.Buyer.PostalAddress == nil {
t.Fatal("buyer postal address is nil")
}
if got := inv.Buyer.PostalAddress.CountrySubDivisionName; got != "Brandenburg" {
t.Errorf("buyer CountrySubDivisionName: got %q, want %q", got, "Brandenburg")
}
if inv.Seller.PostalAddress == nil {
t.Fatal("seller postal address is nil")
}
if got := inv.Seller.PostalAddress.CountrySubDivisionName; got != "Bavaria" {
t.Errorf("seller CountrySubDivisionName: got %q, want %q", got, "Bavaria")
}
}
// TestCIIDateParsingValid tests that valid CII dates (Format 102: YYYYMMDD) parse successfully
func TestCIIDateParsingValid(t *testing.T) {
t.Parallel()
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100"
xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240315</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty><ram:Name>Buyer</ram:Name></ram:BuyerTradeParty>
<ram:SellerTradeParty><ram:Name>Seller</ram:Name></ram:SellerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery>
<ram:ActualDeliverySupplyChainEvent>
<ram:OccurrenceDateTime><udt:DateTimeString format="102">20240310</udt:DateTimeString></ram:OccurrenceDateTime>
</ram:ActualDeliverySupplyChainEvent>
</ram:ApplicableHeaderTradeDelivery>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:BillingSpecifiedPeriod>
<ram:StartDateTime><udt:DateTimeString format="102">20240201</udt:DateTimeString></ram:StartDateTime>
<ram:EndDateTime><udt:DateTimeString format="102">20240229</udt:DateTimeString></ram:EndDateTime>
</ram:BillingSpecifiedPeriod>
<ram:InvoiceReferencedDocument>
<ram:IssuerAssignedID>PREV-001</ram:IssuerAssignedID>
<ram:FormattedIssueDateTime><qdt:DateTimeString format="102">20240101</qdt:DateTimeString></ram:FormattedIssueDateTime>
</ram:InvoiceReferencedDocument>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
<ram:IncludedSupplyChainTradeLineItem>
<ram:AssociatedDocumentLineDocument>
<ram:LineID>1</ram:LineID>
</ram:AssociatedDocumentLineDocument>
<ram:SpecifiedTradeProduct><ram:Name>Test Item</ram:Name></ram:SpecifiedTradeProduct>
<ram:SpecifiedLineTradeAgreement>
<ram:NetPriceProductTradePrice><ram:ChargeAmount>10.00</ram:ChargeAmount></ram:NetPriceProductTradePrice>
</ram:SpecifiedLineTradeAgreement>
<ram:SpecifiedLineTradeDelivery>
<ram:BilledQuantity unitCode="C62">10</ram:BilledQuantity>
</ram:SpecifiedLineTradeDelivery>
<ram:SpecifiedLineTradeSettlement>
<ram:BillingSpecifiedPeriod>
<ram:StartDateTime><udt:DateTimeString format="102">20240201</udt:DateTimeString></ram:StartDateTime>
<ram:EndDateTime><udt:DateTimeString format="102">20240210</udt:DateTimeString></ram:EndDateTime>
</ram:BillingSpecifiedPeriod>
<ram:ApplicableTradeTax>
<ram:TypeCode>VAT</ram:TypeCode>
<ram:CategoryCode>S</ram:CategoryCode>
<ram:RateApplicablePercent>19.00</ram:RateApplicablePercent>
</ram:ApplicableTradeTax>
<ram:SpecifiedTradeSettlementLineMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
</ram:SpecifiedTradeSettlementLineMonetarySummation>
</ram:SpecifiedLineTradeSettlement>
</ram:IncludedSupplyChainTradeLineItem>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
inv, err := ParseReader(strings.NewReader(xml))
if err != nil {
t.Fatalf("unexpected error parsing valid CII dates: %v", err)
}
// Verify invoice date
if inv.InvoiceDate.Format("20060102") != "20240315" {
t.Errorf("InvoiceDate: got %s, want 20240315", inv.InvoiceDate.Format("20060102"))
}
// Verify occurrence date time (BT-72)
if inv.OccurrenceDateTime.Format("20060102") != "20240310" {
t.Errorf("OccurrenceDateTime: got %s, want 20240310", inv.OccurrenceDateTime.Format("20060102"))
}
// Verify billing period (BT-73, BT-74)
if inv.BillingSpecifiedPeriodStart.Format("20060102") != "20240201" {
t.Errorf("BillingSpecifiedPeriodStart: got %s, want 20240201", inv.BillingSpecifiedPeriodStart.Format("20060102"))
}
if inv.BillingSpecifiedPeriodEnd.Format("20060102") != "20240229" {
t.Errorf("BillingSpecifiedPeriodEnd: got %s, want 20240229", inv.BillingSpecifiedPeriodEnd.Format("20060102"))
}
// Verify referenced document date
if len(inv.InvoiceReferencedDocument) != 1 {
t.Fatalf("expected 1 referenced document, got %d", len(inv.InvoiceReferencedDocument))
}
if inv.InvoiceReferencedDocument[0].Date.Format("20060102") != "20240101" {
t.Errorf("InvoiceReferencedDocument[0].Date: got %s, want 20240101", inv.InvoiceReferencedDocument[0].Date.Format("20060102"))
}
// Verify line billing period (BT-134, BT-135)
if len(inv.InvoiceLines) != 1 {
t.Fatalf("expected 1 invoice line, got %d", len(inv.InvoiceLines))
}
if inv.InvoiceLines[0].BillingSpecifiedPeriodStart.Format("20060102") != "20240201" {
t.Errorf("Line BillingSpecifiedPeriodStart: got %s, want 20240201", inv.InvoiceLines[0].BillingSpecifiedPeriodStart.Format("20060102"))
}
if inv.InvoiceLines[0].BillingSpecifiedPeriodEnd.Format("20060102") != "20240210" {
t.Errorf("Line BillingSpecifiedPeriodEnd: got %s, want 20240210", inv.InvoiceLines[0].BillingSpecifiedPeriodEnd.Format("20060102"))
}
}
// TestCIIDateParsingInvalidFormat tests that invalid date formats return errors
func TestCIIDateParsingInvalidFormat(t *testing.T) {
t.Parallel()
tests := []struct {
name string
dateElement string
wantErrMsg string
}{
{
name: "invalid invoice date format",
dateElement: `<ram:IssueDateTime><udt:DateTimeString format="102">2024-03-15</udt:DateTimeString></ram:IssueDateTime>`,
wantErrMsg: "parsing time",
},
{
name: "invalid occurrence date",
dateElement: `<ram:ActualDeliverySupplyChainEvent><ram:OccurrenceDateTime><udt:DateTimeString format="102">INVALID</udt:DateTimeString></ram:OccurrenceDateTime></ram:ActualDeliverySupplyChainEvent>`,
wantErrMsg: "invalid occurrence date time",
},
{
name: "invalid billing period start",
dateElement: `<ram:BillingSpecifiedPeriod><ram:StartDateTime><udt:DateTimeString format="102">99999999</udt:DateTimeString></ram:StartDateTime></ram:BillingSpecifiedPeriod>`,
wantErrMsg: "invalid billing period start date",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var xml string
if strings.Contains(tt.dateElement, "IssueDateTime") {
xml = `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
` + tt.dateElement + `
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty><ram:Name>Buyer</ram:Name></ram:BuyerTradeParty>
<ram:SellerTradeParty><ram:Name>Seller</ram:Name></ram:SellerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
} else {
xml = `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty><ram:Name>Buyer</ram:Name></ram:BuyerTradeParty>
<ram:SellerTradeParty><ram:Name>Seller</ram:Name></ram:SellerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery>` + tt.dateElement + `</ram:ApplicableHeaderTradeDelivery>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
` + (func() string {
if strings.Contains(tt.dateElement, "BillingSpecifiedPeriod") {
return tt.dateElement
}
return ""
}()) + `
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
}
_, err := ParseReader(strings.NewReader(xml))
if err == nil {
t.Errorf("%s: expected error, got nil", tt.name)
return
}
if !strings.Contains(err.Error(), tt.wantErrMsg) {
t.Errorf("%s: error message %q does not contain %q", tt.name, err.Error(), tt.wantErrMsg)
}
})
}
}
// TestCIILineDateParsingInvalid tests that invalid line billing period dates return errors
func TestCIILineDateParsingInvalid(t *testing.T) {
t.Parallel()
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty><ram:Name>Buyer</ram:Name></ram:BuyerTradeParty>
<ram:SellerTradeParty><ram:Name>Seller</ram:Name></ram:SellerTradeParty>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
<ram:IncludedSupplyChainTradeLineItem>
<ram:AssociatedDocumentLineDocument>
<ram:LineID>LINE-001</ram:LineID>
</ram:AssociatedDocumentLineDocument>
<ram:SpecifiedTradeProduct><ram:Name>Test</ram:Name></ram:SpecifiedTradeProduct>
<ram:SpecifiedLineTradeAgreement>
<ram:NetPriceProductTradePrice><ram:ChargeAmount>10.00</ram:ChargeAmount></ram:NetPriceProductTradePrice>
</ram:SpecifiedLineTradeAgreement>
<ram:SpecifiedLineTradeDelivery>
<ram:BilledQuantity unitCode="C62">10</ram:BilledQuantity>
</ram:SpecifiedLineTradeDelivery>
<ram:SpecifiedLineTradeSettlement>
<ram:BillingSpecifiedPeriod>
<ram:StartDateTime><udt:DateTimeString format="102">BADDATE</udt:DateTimeString></ram:StartDateTime>
</ram:BillingSpecifiedPeriod>
<ram:ApplicableTradeTax>
<ram:TypeCode>VAT</ram:TypeCode>
<ram:CategoryCode>S</ram:CategoryCode>
<ram:RateApplicablePercent>19.00</ram:RateApplicablePercent>
</ram:ApplicableTradeTax>
<ram:SpecifiedTradeSettlementLineMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
</ram:SpecifiedTradeSettlementLineMonetarySummation>
</ram:SpecifiedLineTradeSettlement>
</ram:IncludedSupplyChainTradeLineItem>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
_, err := ParseReader(strings.NewReader(xml))
if err == nil {
t.Error("expected error for invalid line billing period date, got nil")
return
}
if !strings.Contains(err.Error(), "invalid line billing period start date for line LINE-001") {
t.Errorf("error message should contain line ID, got: %v", err)
}
}
// TestCIIAttachmentBase64Encoding tests that binary attachments are properly encoded/decoded
func TestCIIAttachmentBase64Encoding(t *testing.T) {
t.Parallel()
// Base64 encoded "Hello, World!"
testData := "SGVsbG8sIFdvcmxkIQ=="
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty><ram:Name>Buyer</ram:Name></ram:BuyerTradeParty>
<ram:SellerTradeParty><ram:Name>Seller</ram:Name></ram:SellerTradeParty>
<ram:AdditionalReferencedDocument>
<ram:IssuerAssignedID>DOC-001</ram:IssuerAssignedID>
<ram:TypeCode>130</ram:TypeCode>
<ram:Name>Test Document</ram:Name>
<ram:AttachmentBinaryObject mimeCode="application/pdf" filename="test.pdf">` + testData + `</ram:AttachmentBinaryObject>
</ram:AdditionalReferencedDocument>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
inv, err := ParseReader(strings.NewReader(xml))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(inv.AdditionalReferencedDocument) != 1 {
t.Fatalf("expected 1 additional document, got %d", len(inv.AdditionalReferencedDocument))
}
doc := inv.AdditionalReferencedDocument[0]
if doc.AttachmentMimeCode != "application/pdf" {
t.Errorf("AttachmentMimeCode: got %q, want %q", doc.AttachmentMimeCode, "application/pdf")
}
if doc.AttachmentFilename != "test.pdf" {
t.Errorf("AttachmentFilename: got %q, want %q", doc.AttachmentFilename, "test.pdf")
}
if string(doc.AttachmentBinaryObject) != "Hello, World!" {
t.Errorf("AttachmentBinaryObject: got %q, want %q", string(doc.AttachmentBinaryObject), "Hello, World!")
}
}
// TestCIIAttachmentInvalidBase64 tests that invalid base64 data returns an error
func TestCIIAttachmentInvalidBase64(t *testing.T) {
t.Parallel()
xml := `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:udt="urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:cen.eu:en16931:2017</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
<rsm:ExchangedDocument>
<ram:ID>INV-001</ram:ID>
<ram:TypeCode>380</ram:TypeCode>
<ram:IssueDateTime><udt:DateTimeString format="102">20240101</udt:DateTimeString></ram:IssueDateTime>
</rsm:ExchangedDocument>
<rsm:SupplyChainTradeTransaction>
<ram:ApplicableHeaderTradeAgreement>
<ram:BuyerTradeParty><ram:Name>Buyer</ram:Name></ram:BuyerTradeParty>
<ram:SellerTradeParty><ram:Name>Seller</ram:Name></ram:SellerTradeParty>
<ram:AdditionalReferencedDocument>
<ram:IssuerAssignedID>DOC-001</ram:IssuerAssignedID>
<ram:TypeCode>130</ram:TypeCode>
<ram:AttachmentBinaryObject mimeCode="application/pdf">INVALID!!!BASE64</ram:AttachmentBinaryObject>
</ram:AdditionalReferencedDocument>
</ram:ApplicableHeaderTradeAgreement>
<ram:ApplicableHeaderTradeDelivery/>
<ram:ApplicableHeaderTradeSettlement>
<ram:InvoiceCurrencyCode>EUR</ram:InvoiceCurrencyCode>
<ram:SpecifiedTradeSettlementHeaderMonetarySummation>
<ram:LineTotalAmount>100.00</ram:LineTotalAmount>
<ram:TaxBasisTotalAmount>100.00</ram:TaxBasisTotalAmount>
<ram:TaxTotalAmount>19.00</ram:TaxTotalAmount>
<ram:GrandTotalAmount>119.00</ram:GrandTotalAmount>
<ram:DuePayableAmount>119.00</ram:DuePayableAmount>
</ram:SpecifiedTradeSettlementHeaderMonetarySummation>
</ram:ApplicableHeaderTradeSettlement>
</rsm:SupplyChainTradeTransaction>
</rsm:CrossIndustryInvoice>`
_, err := ParseReader(strings.NewReader(xml))
if err == nil {
t.Error("expected error for invalid base64 data, got nil")
return
}
if !strings.Contains(err.Error(), "cannot decode attachment") {
t.Errorf("error message should contain 'cannot decode attachment', got: %v", err)
}
}
// Benchmark tests for parser performance across different profiles
// BenchmarkParseCII benchmarks CII parsing performance across all profiles
func BenchmarkParseCII(b *testing.B) {
benchmarks := []struct {
name string
file string
}{
{"Minimum", "testdata/cii/minimum/zugferd-minimum-rechnung.xml"},
{"BasicWL", "testdata/cii/basicwl/zugferd-basicwl-rechnung.xml"},
{"Basic", "testdata/cii/basic/zugferd-basic-rechnung.xml"},
{"EN16931", "testdata/cii/en16931/zugferd_2p3_EN16931_1.xml"},
{"Extended", "testdata/cii/extended/zugferd-extended-rechnung.xml"},
{"XRechnung", "testdata/cii/xrechnung/zugferd-xrechnung-einfach.xml"},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
data, err := os.ReadFile(bm.file)
if err != nil {
b.Skipf("File not found: %s", bm.file)
}
b.SetBytes(int64(len(data)))
b.ReportAllocs()
b.ResetTimer()
for b.Loop() {
_, err := ParseReader(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
}
})
}
}
// Fuzz tests for parser robustness
// FuzzParseCII fuzzes the CII parser to find crashes and panics
func FuzzParseCII(f *testing.F) {
// Seed corpus with valid CII XML files from different profiles
seeds := []string{
"testdata/cii/minimum/zugferd-minimum-rechnung.xml",
"testdata/cii/basicwl/zugferd-basicwl-rechnung.xml",
"testdata/cii/basic/zugferd-basic-rechnung.xml",
"testdata/cii/en16931/zugferd_2p3_EN16931_1.xml",
"testdata/cii/extended/zugferd-extended-rechnung.xml",
"testdata/cii/xrechnung/zugferd-xrechnung-einfach.xml",
}
for _, seed := range seeds {
data, err := os.ReadFile(seed)
if err == nil {
f.Add(data)
}
}
f.Fuzz(func(t *testing.T, data []byte) {
// Parser should never panic, even with invalid input
_, err := ParseReader(bytes.NewReader(data))
_ = err // Error is expected for invalid inputs
})
}