-
Notifications
You must be signed in to change notification settings - Fork 3
/
validate2_test.go
196 lines (184 loc) · 4.43 KB
/
validate2_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
package checkjson
import (
"encoding/json"
"fmt"
"testing"
"time"
)
type struct1 struct {
This string
Is struct2
Not []struct2
But map[string]interface{}
anon int
}
type struct2 struct {
A string
Json string
}
func TestJdata(t *testing.T) {
fmt.Println("===================== TestJdata ...")
s := new(struct1)
json1 := []byte(`{
"this":"is",
"is":{"a":"simple","json":"object"},
"not":[
{"a":"simple"},
{"JSON":"object"},
{"a":"not so","json":"simple"}
],
"but": {"it":"is a", "little":"goofy"}}`)
if err := Validate(json1, s); err != nil {
t.Fatal(err)
}
// field doesn't exist for JSON key 'else'
json2 := []byte(`{
"this":"is",
"not":[
{"a":"simple"},
{"JSON":"object"}
],
"but": {"it":"is a", "little":"goofy"},
"else": false}`)
// json2 is valid encoding of struct1
if err := Validate(json2, s); err == nil {
t.Fatal("no error returned")
} else {
fmt.Println("err ok:", err)
}
// field doesn't exist for JSON key 'else' in member struct
json3 := []byte(`{
"this":"is",
"not":[
{"a":"simple"},
{"json":"object"},
{"else": false}
],
"but": {"it":"is a", "little":"goofy"}}`)
// json2 is valid encoding of struct1
if err := Validate(json3, s); err == nil {
t.Fatal("no error returned")
} else {
fmt.Println("err ok:", err)
}
json4 := []byte(`{
"this":"is",
"is":{"a":"simple","json":"object", "else":false},
"not":[
{"a":"simple"},
{"JSON":"object"},
{"a":"not so","json":"simple"}
],
"but": {"it":"is a", "little":"goofy"}}`)
if err := Validate(json4, s); err == nil {
t.Fatal("no error returned")
} else {
fmt.Println("err ok:", err)
}
json5 := []byte(`{
"this":"is",
"is":{"a":"simple","json":"object"},
"not": {"a":"simple"},
"but": {"it":"is a", "little":"goofy"}}`)
if err := Validate(json5, s); err == nil {
t.Fatal("no error returned")
} else {
fmt.Println("err ok:", err)
}
}
type VmonConns struct {
Display bool
Active bool
Conns map[string]bool
}
type VmonSpec struct {
topology string
Id string
Defined bool
View string
Passive bool
Status *VmonConns
OldestRecallTime string
EncodingMask string
KeyFilter json.RawMessage
BumpFilter json.RawMessage
SortBy json.RawMessage
BumpOtherVmons json.RawMessage
BumpItems bool
BumpDependents bool
}
type TopoSpec struct {
Id string
ConfigId string
Startup bool
StartTime time.Time
StopTime time.Time
Vmons []*VmonSpec
vmons map[string]*VmonSpec
}
func TestKdata(t *testing.T) {
fmt.Println("===================== TestKdata ...")
kdata1 := []byte(`
{
"config" : "topo",
"id" : "expos",
"startup" : false,
"vmons" : [
{
"id" : "expeditor_all",
"view" : "item",
"keyfilter" : {"child":false,"modifier":false},
"bumpfilter" : {"orderbumped":false},
"bumpdependents" : true,
"bumpothervmons" : ["grill_dt","fryer_dt","grill","fryer","expeditor","expeditor_dt"],
"sortby" : ["orderatime","priority","line"],
"encodingmask" : "QSRItem_Hierarchical"
},
{
"id" : "expeditor_order",
"view" : "order",
"keyfilter" : {"dest":["EAT IN","CARRY OUT", "DRIVE THRU"]},
"bumpfilter" : {"vmonbumped":false},
"bumpitems" : true,
"sortby" : ["atime"],
"encodingmask" : "QSROrder_Hierarchical"
}
]
}`)
tspec := new(TopoSpec)
if err := Validate(kdata1, tspec); err != nil {
t.Fatal(err)
}
kdata2 := []byte(`
{
"config" : "topo",
"id" : "expos",
"startup" : false,
"vmons" : [
{
"id" : "expeditor_all",
"view" : "item",
"keyfilter" : {"child":false,"modifier":false},
"bumpfilter" : {"orderbumped":false},
"bumpdependents" : true,
"bumpothervmons" : ["grill_dt","fryer_dt","grill","fryer","expeditor","expeditor_dt"],
"sortby" : ["orderatime","priority","line"],
"encodingmask" : "QSRItem_Hierarchical"
},
{
"not_id" : "expeditor_order",
"view" : "order",
"keyfilter" : {"dest":["EAT IN","CARRY OUT", "DRIVE THRU"]},
"bumpfilter" : {"vmonbumped":false},
"bumpitems" : true,
"sortby" : ["atime"],
"encodingmask" : "QSROrder_Hierarchical"
}
]
}`)
if err := Validate(kdata2, tspec); err == nil {
t.Fatal("no error reported")
} else {
fmt.Println("err ok:", err)
}
}