-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
schema_test.go
167 lines (157 loc) · 4.95 KB
/
schema_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
package omniparser
import (
"errors"
"io"
"strings"
"testing"
"github.com/jf-tech/go-corelib/testlib"
"github.com/stretchr/testify/assert"
"github.com/jf-tech/omniparser/customfuncs"
"github.com/jf-tech/omniparser/errs"
"github.com/jf-tech/omniparser/header"
"github.com/jf-tech/omniparser/schemahandler"
"github.com/jf-tech/omniparser/transformctx"
)
func TestNewSchema(t *testing.T) {
for _, test := range []struct {
name string
schema string
exts []Extension
err string
}{
{
name: "fail to read out schema content",
schema: "",
exts: nil,
err: "unable to read schema 'test-schema': mock reading failure",
},
{
name: "fail to unmarshal schema header",
schema: "[invalid",
exts: nil,
err: "unable to perform schema validation: invalid character 'i' looking for beginning of value",
},
{
name: "json schema validation for header failed",
schema: `{"parser_settings": {"versionx": "9999", "file_format_type": "exe" }}`,
exts: nil,
err: "schema 'test-schema' validation failed:\nparser_settings: Additional property versionx is not allowed\nparser_settings: version is required",
},
{
name: "no supported schema handler",
schema: `{"parser_settings": {"version": "9999", "file_format_type": "exe" }}`,
exts: []Extension{
{}, // Empty to test if we skip empty 3rd party handler config properly or not.
{
CreateSchemaHandler: func(_ *schemahandler.CreateCtx) (schemahandler.SchemaHandler, error) {
return nil, errs.ErrSchemaNotSupported
},
},
},
err: errs.ErrSchemaNotSupported.Error(),
},
{
name: "supported schema handler found, but schema validation failed",
schema: `{"parser_settings": {"version": "9999", "file_format_type": "exe" }}`,
exts: []Extension{
{
CreateSchemaHandler: func(_ *schemahandler.CreateCtx) (schemahandler.SchemaHandler, error) {
return nil, errors.New("invalid schema")
},
},
},
err: "invalid schema",
},
{
name: "supported schema handler found, schema parsing successful",
schema: `{"parser_settings": {"version": "9999", "file_format_type": "exe" }}`,
exts: []Extension{
{
CustomFuncs: customfuncs.CustomFuncs{
"upper": func() {},
"lower": func() {},
},
CreateSchemaHandler: func(ctx *schemahandler.CreateCtx) (schemahandler.SchemaHandler, error) {
assert.Equal(t, 2, len(ctx.CustomFuncs))
assert.NotNil(t, ctx.CustomFuncs["upper"])
assert.NotNil(t, ctx.CustomFuncs["lower"])
// make sure create params are passed in correctly.
assert.Equal(t, 13, ctx.CreateParams.(int))
return nil, nil
},
CreateSchemaHandlerParams: 13,
},
},
err: "",
},
} {
t.Run(test.name, func(t *testing.T) {
var schemaReader io.Reader
if test.schema == "" {
schemaReader = testlib.NewMockReadCloser("mock reading failure", nil)
} else {
schemaReader = strings.NewReader(test.schema)
}
schema, err := NewSchema("test-schema", schemaReader, test.exts...)
if test.err != "" {
assert.Error(t, err)
assert.Equal(t, test.err, err.Error())
assert.Nil(t, schema)
} else {
assert.NoError(t, err)
assert.NotNil(t, schema)
assert.Equal(t, test.schema, string(schema.Content()))
}
})
}
}
func TestSchema_NewTransform_StripBOMFailure(t *testing.T) {
s := &schema{
header: header.Header{
ParserSettings: header.ParserSettings{Version: "999", FileFormatType: "exe"},
},
}
op, err := s.NewTransform("test input", testlib.NewMockReadCloser("bom read failure", nil), nil)
assert.Error(t, err)
assert.Equal(t, "bom read failure", err.Error())
assert.Nil(t, op)
}
type testSchemaHandler struct {
newIngesterErr error
}
func (t testSchemaHandler) NewIngester(_ *transformctx.Ctx, _ io.Reader) (schemahandler.Ingester, error) {
if t.newIngesterErr != nil {
return nil, t.newIngesterErr
}
return &testIngester{}, nil
}
func TestSchema_NewTransform_NewIngesterFailure(t *testing.T) {
p := &schema{
header: header.Header{
ParserSettings: header.ParserSettings{Version: "999", FileFormatType: "exe"},
},
handler: testSchemaHandler{newIngesterErr: errors.New("test failure")},
}
transform, err := p.NewTransform("test input", strings.NewReader("something"), &transformctx.Ctx{})
assert.Error(t, err)
assert.Equal(t, "test failure", err.Error())
assert.Nil(t, transform)
}
func TestSchema_NewTransform_NameAndCtxAwareErrOverwrite(t *testing.T) {
h := header.Header{
ParserSettings: header.ParserSettings{Version: "999", FileFormatType: "exe"},
}
s := &schema{
header: h,
content: []byte("test schema content"),
handler: testSchemaHandler{},
}
ctx := &transformctx.Ctx{}
transform, err := s.NewTransform("test input", strings.NewReader("something"), ctx)
assert.NoError(t, err)
assert.NotNil(t, transform)
assert.Equal(t, "test input", ctx.InputName)
assert.NotNil(t, ctx.CtxAwareErr)
assert.Equal(t, h, s.Header())
assert.Equal(t, "test schema content", string(s.Content()))
}