-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathubl_test.go
257 lines (207 loc) · 6.08 KB
/
ubl_test.go
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
package ubl
import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/invopop/gobl"
"github.com/invopop/gobl.ubl/document"
"github.com/invopop/gobl.ubl/internal/gtou"
"github.com/invopop/gobl.ubl/internal/utog"
"github.com/invopop/gobl/bill"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/lestrrat-go/libxml2"
"github.com/lestrrat-go/libxml2/xsd"
)
const (
xmlPattern = "*.xml"
jsonPattern = "*.json"
)
func TestGtoU(t *testing.T) {
schema, err := loadSchema("schema.xsd")
require.NoError(t, err)
examples, err := getDataGlob(jsonPattern)
require.NoError(t, err)
for _, example := range examples {
inName := filepath.Base(example)
outName := strings.Replace(inName, ".json", ".xml", 1)
t.Run(inName, func(t *testing.T) {
doc, err := NewDocumentFrom(inName)
require.NoError(t, err)
data, err := doc.Bytes()
require.NoError(t, err)
err = ValidateXML(schema, data)
require.NoError(t, err)
output, err := LoadOutputFile(outName)
assert.NoError(t, err)
assert.Equal(t, output, data, "Output should match the expected XML. Update with --update flag.")
})
}
}
func TestUtoG(t *testing.T) {
examples, err := getDataGlob(xmlPattern)
require.NoError(t, err)
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
goblEnv, err := utog.Convert(xmlData)
require.NoError(t, err)
// Extract the invoice from the envelope
invoice, ok := goblEnv.Extract().(*bill.Invoice)
require.True(t, ok, "Document should be an invoice")
// Remove UUID from the invoice
invoice.UUID = ""
// Marshal only the invoice
data, err := json.MarshalIndent(invoice, "", " ")
require.NoError(t, err)
// Load the expected output
output, err := LoadOutputFile(outName)
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")
// Remove UUID from the expected invoice
expectedInvoice.UUID = ""
// Marshal the expected invoice
expectedData, err := json.MarshalIndent(expectedInvoice, "", " ")
require.NoError(t, err)
assert.JSONEq(t, string(expectedData), string(data), "Invoice should match the expected JSON. Update with --update flag.")
})
}
}
// NewDocumentFrom creates a cii Document from a GOBL file in the `test/data` folder
func NewDocumentFrom(name string) (*document.Invoice, error) {
env, err := LoadTestEnvelope(name)
if err != nil {
return nil, err
}
return gtou.Convert(env)
}
// LoadTestXMLDoc returns a CII XMLDoc from a file in the test data folder
func LoadTestXMLDoc(name string) (*document.Invoice, error) {
src, err := os.Open(filepath.Join(getConversionTypePath(xmlPattern), name))
if err != nil {
return nil, err
}
defer func() {
if cerr := src.Close(); cerr != nil && err == nil {
err = cerr
}
}()
inData, err := io.ReadAll(src)
if err != nil {
return nil, err
}
doc := new(document.Invoice)
if err := xml.Unmarshal(inData, doc); err != nil {
return nil, err
}
return doc, err
}
// LoadTestInvoice returns a GOBL Invoice from a file in the `test/data` folder
func LoadTestInvoice(name string) (*bill.Invoice, error) {
env, err := LoadTestEnvelope(name)
if err != nil {
return nil, err
}
return env.Extract().(*bill.Invoice), nil
}
// LoadTestEnvelope returns a GOBL Envelope from a file in the `test/data` folder
func LoadTestEnvelope(name string) (*gobl.Envelope, error) {
src, _ := os.Open(filepath.Join(getConversionTypePath(jsonPattern), name))
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
}
return env, nil
}
// LoadOutputFile returns byte data from a file in the `test/data/out` folder
func LoadOutputFile(name string) ([]byte, error) {
var pattern string
if strings.HasSuffix(name, ".json") {
pattern = xmlPattern
} else {
pattern = jsonPattern
}
src, _ := os.Open(filepath.Join(getOutPath(pattern), name))
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(src); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func loadSchema(name string) (*xsd.Schema, error) {
return xsd.ParseFromFile(filepath.Join(getSchemaPath(name), name))
}
// 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 getDataGlob(pattern string) ([]string, error) {
return filepath.Glob(filepath.Join(getConversionTypePath(pattern), pattern))
}
func getSchemaPath(pattern string) string {
return filepath.Join(getConversionTypePath(pattern), "schema")
}
func getOutPath(pattern string) string {
return filepath.Join(getConversionTypePath(pattern), "out")
}
func getDataPath() string {
return filepath.Join(getTestPath(), "data")
}
func getConversionTypePath(pattern string) string {
if pattern == xmlPattern {
return filepath.Join(getDataPath(), "utog")
}
return filepath.Join(getDataPath(), "gtou")
}
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]
}