-
Notifications
You must be signed in to change notification settings - Fork 1
/
translator_test.go
241 lines (193 loc) · 5.61 KB
/
translator_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
package ews
import (
diff "github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
"github.com/pkg/errors"
"github.com/sergi/go-diff/diffmatchpatch"
"bufio"
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)
// https://stackoverflow.com/questions/5884154/read-text-file-into-string-array-and-write
func readXfail(fname string) (ret map[string]bool) {
ret = make(map[string]bool)
file, err := os.Open(fname)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if text := strings.TrimSpace(scanner.Text()); len(text) != 0 {
ret[text] = true
}
}
return
}
func shouldFail(xfail map[string]bool, fname string) (ret bool) {
if _, ok := xfail[filepath.Base(fname)]; ok {
return true
}
return false
}
// borrowed from https://github.com/yudai/gojsondiff/blob/master/jd/main.go
// .. why isn't there a utility function for this?
// .. returns true on error/modified, false otherwise
func diffJson(a []byte, b []byte) (diffString string, err error) {
// the diff library seems to be buggy (but still useful for showing
// the output), so use Reflect instead
var aj map[string]interface{}
var bj map[string]interface{}
if err = json.Unmarshal(a, &aj); err != nil {
return "", errors.Wrap(err, "A json error")
}
if err = json.Unmarshal(b, &bj); err != nil {
return "", errors.Wrap(err, "B json error")
}
if reflect.DeepEqual(aj, bj) {
return "", nil
}
differ := diff.New()
d, err := differ.Compare(a, b)
if err != nil {
return "", errors.Wrap(err, "json diff failed")
}
if d.Modified() {
config := formatter.AsciiFormatterConfig{
ShowArrayIndex: true,
Coloring: true,
}
dformat := formatter.NewAsciiFormatter(aj, config)
diffString, err = dformat.Format(d)
if err != nil {
return "", errors.Wrap(err, "json diff format failed")
}
}
err = errors.New("A and B are different")
return
}
type TestFunc func(string) (string, error)
func testRunner(t *testing.T, globpath string, fn TestFunc) {
var testfiles []string
// for debugging only
envTestfile := os.Getenv("EWS_TESTFILE")
if envTestfile != "" {
testfiles = []string{envTestfile}
} else {
// normally get them from the testdata directory
var err error
testfiles, err = filepath.Glob(globpath)
if err != nil {
t.Fatal(err)
}
}
xfailMap := readXfail(filepath.Join(filepath.Dir(globpath), "xfail"))
sort.Strings(testfiles)
passed := 0
for _, testfile := range testfiles {
xfail := shouldFail(xfailMap, testfile)
xfailStr := ""
if xfail {
xfailStr = " (should fail)"
}
t.Logf("Now testing %s%s", testfile, xfailStr)
diffString, err := fn(testfile)
if err != nil {
if xfail {
passed++
t.Log(err)
if diffString != "" {
t.Log(diffString)
}
} else {
t.Errorf("Failed: %s", err)
if diffString != "" {
t.Error(diffString)
}
}
} else {
if xfail {
t.Errorf("Expected failure (did not fail)")
} else {
passed++
}
}
}
t.Logf("%d/%d tests passed", passed, len(testfiles))
if passed == 0 {
t.Fail()
}
}
func testSoapToJsonSingle(testfile string) (diffstring string, err error) {
xmlReader, err := os.Open(testfile)
if err != nil {
return "", errors.Wrapf(err, "opening %s", testfile)
}
defer xmlReader.Close()
data, _, err := SOAP2JSON(xmlReader)
if err != nil {
return "", errors.Wrapf(err, "parse failed %s", testfile)
}
// if you need the contents of the file
//ioutil.WriteFile(testfile + ".gen.json", data, 0700)
// load the correct output from a file
buf, err := ioutil.ReadFile(testfile + ".json")
if err != nil {
return "", errors.Wrapf(err, "%s.json load failed %s", testfile)
}
return diffJson(buf, data)
}
func TestSOAP2JSON(t *testing.T) {
testRunner(t, filepath.Join("testdata", "requests", "*.xml"), testSoapToJsonSingle)
}
func testJson2SoapSingle(testfile string) (diffstring string, err error) {
dmp := diffmatchpatch.New()
jsonReader, err := os.Open(testfile)
if err != nil {
return "", errors.Wrapf(err, "Opening %s", testfile)
}
defer jsonReader.Close()
// in order to process a response, we have to know what operation it is,
// so we encode it as the first part of the filename
opname := strings.Split(strings.Split(filepath.Base(testfile), ".")[0], "_")[0]
op := EwsOperations[opname]
if op == nil {
return "", errors.Errorf("unknown EWS operation `%s` in `%s`", opname, testfile)
}
buf := new(bytes.Buffer)
err = JSON2SOAP(jsonReader, op, buf, true)
if err != nil {
return "", errors.Wrapf(err, "parsing `%s` failed", testfile)
}
// if you need the contents of the file
//ioutil.WriteFile(testfile + ".gen.xml", buf.Bytes(), 0700)
// we're cheating here -- just going to do a text comparison of the XML,
// since the output should be fairly deterministic. It would be nice to
// do a logical comparison instead... but we need something for now
// load the correct output from a file
correctBuf, err := ioutil.ReadFile(testfile + ".xml")
if err != nil {
return "", errors.Wrapf(err, "loading `%s.xml` failed", testfile)
}
// if they match, then we're good to go
if bytes.Compare(buf.Bytes(), correctBuf) == 0 {
return "", nil
} else {
// display a diff
// TODO: this diff ignores whitespace, which happens to
// be really annoying if the outputs only differ by whitespace
diffs := dmp.DiffMain(string(correctBuf), string(buf.Bytes()), true)
diffText := dmp.DiffPrettyText(diffs)
return diffText, errors.New("outputs are different")
}
}
func TestJSON2SOAP(t *testing.T) {
testRunner(t, filepath.Join("testdata", "responses", "*.json"), testJson2SoapSingle)
}