-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjson.go
167 lines (142 loc) · 5.34 KB
/
json.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 json
import (
"fmt"
jd "github.com/josephburnett/jd/lib"
"github.com/ohler55/ojg/jp"
"github.com/ohler55/ojg/oj"
"github.com/ozontech/cute"
cuteErrors "github.com/ozontech/cute/errors"
)
// Diff is a function to compare two jsons
func Diff(original string) cute.AssertBody {
return func(body []byte) error {
originalJSON, err := jd.ReadJsonString(original)
if err != nil {
return fmt.Errorf("could not parse original json in Diff error: '%s'", err)
}
bodyJSON, err := jd.ReadJsonString(string(body))
if err != nil {
return fmt.Errorf("could not parse body json in Diff error: '%s'", err)
}
diff := originalJSON.Diff(bodyJSON).Render()
if diff != "" {
cErr := cuteErrors.NewEmptyAssertError("JSON Diff", "JSON is not the same")
cErr.PutAttachment(&cuteErrors.Attachment{
Name: "JSON diff",
MimeType: "text/plain",
Content: []byte(diff),
})
return cErr
}
return nil
}
}
// Contains is a function to assert that a jsonpath expression extracts a value in an array
// About expression - https://goessner.net/articles/JsonPath/
func Contains(expression string, expect interface{}) cute.AssertBody {
return func(body []byte) error {
return contains(body, expression, expect)
}
}
// Equal is a function to assert that a jsonpath expression matches the given value
// About expression - https://goessner.net/articles/JsonPath/
func Equal(expression string, expect interface{}) cute.AssertBody {
return func(body []byte) error {
return equal(body, expression, expect)
}
}
// NotEqual is a function to check json path expression value is not equal to given value
// About expression - https://goessner.net/articles/JsonPath/
func NotEqual(expression string, expect interface{}) cute.AssertBody {
return func(body []byte) error {
return notEqual(body, expression, expect)
}
}
// EqualJSON is a function to check json path expression value is equal to given json
// About expression - https://goessner.net/articles/JsonPath/
func EqualJSON(expression string, expect []byte) cute.AssertBody {
return func(body []byte) error {
return equalJSON(body, expression, expect)
}
}
// NotEqualJSON is a function to check json path expression value is not equal to given json
// About expression - https://goessner.net/articles/JsonPath/
func NotEqualJSON(expression string, expect []byte) cute.AssertBody {
return func(body []byte) error {
return notEqualJSON(body, expression, expect)
}
}
// Length is a function to asserts that value is the expected length
// About expression - https://goessner.net/articles/JsonPath/
func Length(expression string, expectLength int) cute.AssertBody {
return func(body []byte) error {
return length(body, expression, expectLength)
}
}
// LengthGreaterThan is a function to asserts that value is greater than the given length
// About expression - https://goessner.net/articles/JsonPath/
func LengthGreaterThan(expression string, minimumLength int) cute.AssertBody {
return func(body []byte) error {
return greaterThan(body, expression, minimumLength)
}
}
// LengthGreaterOrEqualThan is a function to asserts that value is greater or equal than the given length
// About expression - https://goessner.net/articles/JsonPath/
func LengthGreaterOrEqualThan(expression string, minimumLength int) cute.AssertBody {
return func(body []byte) error {
return greaterOrEqualThan(body, expression, minimumLength)
}
}
// LengthLessThan is a function to asserts that value is less than the given length
// About expression - https://goessner.net/articles/JsonPath/
func LengthLessThan(expression string, maximumLength int) cute.AssertBody {
return func(body []byte) error {
return lessThan(body, expression, maximumLength)
}
}
// LengthLessOrEqualThan is a function to asserts that value is less or equal than the given length
// About expression - https://goessner.net/articles/JsonPath/
func LengthLessOrEqualThan(expression string, maximumLength int) cute.AssertBody {
return func(body []byte) error {
return lessOrEqualThan(body, expression, maximumLength)
}
}
// Present is a function to asserts that value is present
// value can be nil or 0
// About expression - https://goessner.net/articles/JsonPath/
func Present(expression string) cute.AssertBody {
return func(body []byte) error {
return present(body, expression)
}
}
// NotEmpty is a function to asserts that value is present
// value can't be nil or 0
// About expression - https://goessner.net/articles/JsonPath/
func NotEmpty(expression string) cute.AssertBody {
return func(body []byte) error {
return notEmpty(body, expression)
}
}
// NotPresent is a function to asserts that value is not present
// About expression - https://goessner.net/articles/JsonPath/
func NotPresent(expression string) cute.AssertBody {
return func(body []byte) error {
return notPresent(body, expression)
}
}
// GetValueFromJSON is function for get value from json
func GetValueFromJSON(js []byte, expression string) ([]interface{}, error) {
obj, err := oj.Parse(js)
if err != nil {
return nil, fmt.Errorf("could not parse json in GetValueFromJSON error: '%s'", err)
}
jsonPath, err := jp.ParseString(expression)
if err != nil {
return nil, fmt.Errorf("could not parse path in GetValueFromJSON error: '%s'", err)
}
res := jsonPath.Get(obj)
if len(res) == 0 {
return nil, fmt.Errorf("could not find element by path %v in JSON", expression)
}
return res, nil
}