forked from invopop/gobl.ubl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
445 lines (374 loc) · 11.5 KB
/
examples_test.go
File metadata and controls
445 lines (374 loc) · 11.5 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
package ubl_test
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/invopop/gobl"
ubl "github.com/invopop/gobl.ubl"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl/uuid"
"github.com/invopop/phive"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/lestrrat-go/libxml2"
"github.com/lestrrat-go/libxml2/xsd"
)
const (
xmlPattern = "*.xml"
jsonPattern = "*.json"
staticUUID uuid.UUID = "0195ce71-dc9c-72c8-bf2c-9890a4a9f0a2"
)
// updateOut is a flag that can be set to update example files
var updateOut = flag.Bool("update", false, "Update the example files in test/data")
// validate is a flag that enables Phive validation
var validate = flag.Bool("validate", false, "Run Phive validation on generated XML")
func TestConvertToInvoice(t *testing.T) {
var pc phive.ValidationServiceClient
// Only connect to Phive if validation is requested
if *validate {
conn, err := grpc.NewClient(
"127.0.0.1:9091",
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
require.NoError(t, err)
defer conn.Close() //nolint:errcheck
pc = phive.NewValidationServiceClient(conn)
}
// Define contexts to test
contexts := []struct {
name string
context ubl.Context
dir string
}{
{"EN16931", ubl.ContextEN16931, "en16931"},
{"Peppol", ubl.ContextPeppol, "peppol"},
{"PeppolSelfBilled", ubl.ContextPeppolSelfBilled, "peppol-self-billed"},
{"XRechnung", ubl.ContextXRechnung, "xrechnung"},
{"FranceCIUS", ubl.ContextPeppolFranceCIUS, "france-cius"},
{"FranceExtended", ubl.ContextPeppolFranceExtended, "france-extended"},
{"OIOUBL", ubl.ContextOIOUBL, "oioubl"},
{"OIOUBL21", ubl.ContextOIOUBL21, "oioubl21"},
}
for _, ctx := range contexts {
t.Run(ctx.name, func(t *testing.T) {
examples, err := filepath.Glob(filepath.Join(getConvertPath(), ctx.dir, jsonPattern))
require.NoError(t, err)
if len(examples) == 0 {
t.Skip("No examples found for context")
}
for _, example := range examples {
inName := filepath.Base(example)
outName := strings.Replace(inName, ".json", ".xml", 1)
t.Run(inName, func(t *testing.T) {
doc, err := testInvoiceFromContext(filepath.Join(ctx.dir, inName), ctx.context)
require.NoError(t, err)
data, err := ubl.Bytes(doc)
require.NoError(t, err)
outPath := filepath.Join(getConvertPath(), ctx.dir, "out", outName)
if *updateOut {
err = os.WriteFile(outPath, data, 0644)
require.NoError(t, err)
}
// Run Phive validation if requested
if *validate {
// Determine VESID based on document type
env, err := loadTestEnvelopeFromPath(example)
require.NoError(t, err)
inv, ok := env.Extract().(*bill.Invoice)
require.True(t, ok, "Document should be an invoice")
vesid := ctx.context.GetVESID(inv)
resp, err := pc.ValidateXml(context.Background(), &phive.ValidateXmlRequest{
Vesid: vesid,
XmlContent: data,
})
require.NoError(t, err)
results, err := json.MarshalIndent(resp.Results, "", " ")
require.NoError(t, err)
require.True(t, resp.Success, "Generated XML should be valid for %s: %s", vesid, string(results))
}
output, err := os.ReadFile(outPath)
assert.NoError(t, err)
assert.Equal(t, string(output), string(data), "Output should match the expected XML. Update with --update flag.")
})
}
})
}
}
func TestParseInvoice(t *testing.T) {
// Define contexts to test
contexts := []struct {
name string
dir string
}{
{"EN16931", "en16931"},
{"Peppol", "peppol"},
{"PeppolSelfBilled", "peppol-self-billed"},
{"XRechnung", "xrechnung"},
{"FranceCIUS", "france-cius"},
{"FranceExtended", "france-extended"},
{"OIOUBL", "oioubl"},
{"OIOUBL21", "oioubl21"},
}
for _, ctx := range contexts {
t.Run(ctx.name, func(t *testing.T) {
examples, err := filepath.Glob(filepath.Join(getParsePath(), ctx.dir, xmlPattern))
require.NoError(t, err)
if len(examples) == 0 {
t.Skip("No examples found for context")
}
for _, example := range examples {
inName := filepath.Base(example)
outName := strings.Replace(inName, ".xml", ".json", 1)
t.Run(inName, func(t *testing.T) {
// Load XML data
xmlData, err := os.ReadFile(example)
require.NoError(t, err)
// Convert UBL XML to GOBL
doc, err := ubl.Parse(xmlData)
require.NoError(t, err)
inv, ok := doc.(*ubl.Invoice)
require.True(t, ok, "Document should be an invoice")
env, err := inv.Convert()
require.NoError(t, err)
// Unfortunately, the sample UBL documents have lots of errors, including
// missing exchange rate data and invalid Tax ID codes. We're disabling
// validation here, but periodically it'd be good to re-enable and check
// for any major issues.
// require.NoError(t, env.Validate())
env.Head.UUID = staticUUID
if inv, ok := env.Extract().(*bill.Invoice); ok {
inv.UUID = staticUUID
}
// Recalculate to ensure consistent digests
if err = env.Calculate(); err != nil {
require.NoError(t, err)
}
outPath := filepath.Join(getParsePath(), ctx.dir, "out", outName)
if *updateOut {
data, err := json.MarshalIndent(env, "", "\t")
if err != nil {
require.NoError(t, err)
}
if err := os.WriteFile(outPath, data, 0644); err != nil {
require.NoError(t, err)
}
}
// Extract the invoice from the envelope
invoice, ok := env.Extract().(*bill.Invoice)
require.True(t, ok, "Document should be an invoice")
// Marshal only the invoice
data, err := json.MarshalIndent(invoice, "", "\t")
require.NoError(t, err)
// Load the expected output
output, err := os.ReadFile(outPath)
assert.NoError(t, err)
// Parse the expected output to extract the invoice
var expectedEnv gobl.Envelope
err = json.Unmarshal(output, &expectedEnv)
require.NoError(t, err)
expectedInvoice, ok := expectedEnv.Extract().(*bill.Invoice)
require.True(t, ok, "Expected document should be an invoice")
// Marshal the expected invoice
expectedData, err := json.MarshalIndent(expectedInvoice, "", "\t")
require.NoError(t, err)
assert.JSONEq(t, string(expectedData), string(data), "Invoice should match the expected JSON. Update with --update flag.")
})
}
})
}
}
// testInvoiceFrom creates a UBL Invoice from a GOBL file in the `test/data` folder
func testInvoiceFrom(name string) (*ubl.Invoice, error) {
env, err := loadTestEnvelope(name)
if err != nil {
return nil, err
}
var opts []ubl.Option
switch {
case strings.HasPrefix(name, "oioubl21-"), strings.HasPrefix(name, "nemhandel21-"), strings.HasPrefix(name, "oioubl-2.1-"):
opts = append(opts, ubl.WithContext(ubl.ContextOIOUBL21))
case strings.HasPrefix(name, "oioubl30-"), strings.HasPrefix(name, "nemhandel-"), strings.HasPrefix(name, "oioubl-"):
opts = append(opts, ubl.WithContext(ubl.ContextOIOUBL))
default:
opts = append(opts, ubl.WithContext(ubl.ContextPeppol))
}
return ubl.ConvertInvoice(env, opts...)
}
// testInvoiceFromContext creates a UBL Invoice from a GOBL file with a specific context
func testInvoiceFromContext(name string, ctx ubl.Context) (*ubl.Invoice, error) {
env, err := loadTestEnvelopeFromPath(filepath.Join(getConvertPath(), name))
if err != nil {
return nil, err
}
return ubl.ConvertInvoice(env, ubl.WithContext(ctx))
}
// loadTestEnvelopeFromPath loads a GOBL envelope from a specific file path
func loadTestEnvelopeFromPath(path string) (*gobl.Envelope, error) {
src, err := os.Open(path)
if err != nil {
return nil, err
}
defer src.Close() //nolint:errcheck
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(src); err != nil {
return nil, err
}
env := new(gobl.Envelope)
if err := json.Unmarshal(buf.Bytes(), env); err != nil {
return nil, err
}
// Clear the IDs
env.Head.UUID = staticUUID
if inv, ok := env.Extract().(*bill.Invoice); ok {
inv.UUID = staticUUID
}
if err := env.Calculate(); err != nil {
panic(err)
}
if err := env.Validate(); err != nil {
panic(err)
}
// Make an update if requested
if *updateOut {
data, err := json.MarshalIndent(env, "", "\t")
if err != nil {
panic(err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
panic(err)
}
}
return env, nil
}
// testLoadXML provides the raw data of a test XML file
// The name parameter can include subdirectories (e.g., "en16931/ubl-example2.xml")
func testLoadXML(name string) ([]byte, error) {
src, err := os.Open(filepath.Join(getParsePath(), name))
if err != nil {
return nil, err
}
defer func() {
if cerr := src.Close(); cerr != nil && err == nil {
err = cerr
}
}()
return io.ReadAll(src)
}
// testParseInvoice takes the provided file and converts to a
// GOBL
func testParseInvoice(name string) (*gobl.Envelope, error) {
data, err := testLoadXML(name)
if err != nil {
return nil, err
}
doc, err := ubl.Parse(data)
if err != nil {
return nil, err
}
inv, ok := doc.(*ubl.Invoice)
if !ok {
return nil, fmt.Errorf("document is not an invoice")
}
return inv.Convert()
}
// loadTestEnvelope returns a GOBL Envelope from a file in the `test/data` folder
func loadTestEnvelope(name string) (*gobl.Envelope, error) {
path := filepath.Join(getConversionTypePath(jsonPattern), name)
src, _ := os.Open(path)
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(src); err != nil {
return nil, err
}
env := new(gobl.Envelope)
if err := json.Unmarshal(buf.Bytes(), env); err != nil {
return nil, err
}
// Clear the IDs
env.Head.UUID = staticUUID
if inv, ok := env.Extract().(*bill.Invoice); ok {
inv.UUID = staticUUID
}
if err := env.Calculate(); err != nil {
panic(err)
}
if err := env.Validate(); err != nil {
panic(err)
}
// Make an update if requested
writeEnvelope(path, env)
return env, nil
}
// loadOutputFile returns byte data from a file in the `test/data/out` folder
func writeEnvelope(path string, env *gobl.Envelope) {
if !*updateOut {
return
}
data, err := json.MarshalIndent(env, "", "\t")
if err != nil {
panic(err)
}
if err := os.WriteFile(path, data, 0644); err != nil {
panic(err)
}
}
// ValidateXML validates a XML document against a XSD Schema
func ValidateXML(schema *xsd.Schema, data []byte) error {
xmlDoc, err := libxml2.Parse(data)
if err != nil {
return err
}
err = schema.Validate(xmlDoc)
if err != nil {
return err.(xsd.SchemaValidationError).Errors()[0]
}
return nil
}
func getDataPath() string {
return filepath.Join(getTestPath(), "data")
}
func getConversionTypePath(pattern string) string {
if pattern == xmlPattern {
return filepath.Join(getDataPath(), "parse")
}
return filepath.Join(getDataPath(), "convert")
}
func getConvertPath() string {
return filepath.Join(getDataPath(), "convert")
}
func getParsePath() string {
return filepath.Join(getDataPath(), "parse")
}
func getTestPath() string {
return filepath.Join(getRootFolder(), "test")
}
// TODO: adapt to new folder structure
func getRootFolder() string {
cwd, _ := os.Getwd()
for !isRootFolder(cwd) {
cwd = removeLastEntry(cwd)
}
return cwd
}
func isRootFolder(dir string) bool {
files, _ := os.ReadDir(dir)
for _, file := range files {
if file.Name() == "go.mod" {
return true
}
}
return false
}
func removeLastEntry(dir string) string {
lastEntry := "/" + filepath.Base(dir)
i := strings.LastIndex(dir, lastEntry)
return dir[:i]
}