Skip to content

Commit

Permalink
[ISSUE-49] fix json validations
Browse files Browse the repository at this point in the history
  • Loading branch information
siller174 committed Nov 21, 2023
1 parent 8b574b1 commit ef8cfd8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 47 deletions.
4 changes: 2 additions & 2 deletions asserts/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func NotPresent(expression string) cute.AssertBody {
}

// GetValueFromJSON is function for get value from json
func GetValueFromJSON(js []byte, expression string) (interface{}, error) {
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)
Expand All @@ -147,5 +147,5 @@ func GetValueFromJSON(js []byte, expression string) (interface{}, error) {
return nil, fmt.Errorf("could not find element by path %v in JSON", expression)
}

return res[0], nil
return res, nil
}
11 changes: 6 additions & 5 deletions asserts/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,28 +703,28 @@ func TestGetValueFromJSON(t *testing.T) {
name string
inputJSON string
expression string
expectedValue interface{}
expectedValue []interface{}
expectedError string
}{
{
name: "ValidExpressionObject",
inputJSON: `{"key1": "value1", "key2": {"key3": "value3"}}`,
expression: "key2.key3",
expectedValue: "value3",
expectedValue: []interface{}{"value3"},
expectedError: "", // No error expected
},
{
name: "ValidExpressionArray",
inputJSON: `{"key1": "value1", "key2": [1, 2, 3]}`,
expression: "key2[1]",
expectedValue: int64(2),
expectedValue: []interface{}{int64(2)},
expectedError: "", // No error expected
},
{
name: "ValidExpressionMap",
inputJSON: `{"key1": "value1", "key2": {"subkey1": "subvalue1"}}`,
expression: "key2",
expectedValue: map[string]interface{}{"subkey1": "subvalue1"},
expectedValue: []interface{}{map[string]interface{}{"subkey1": "subvalue1"}},
expectedError: "", // No error expected
},
{
Expand Down Expand Up @@ -756,7 +756,8 @@ func TestGetValueFromJSON(t *testing.T) {
assert.Contains(t, err.Error(), testCase.expectedError)
}

// Check if the returned value matches the expected result
// Check if the returned value is an array and matches the expected result
assert.IsType(t, []interface{}{}, value)
assert.Equal(t, testCase.expectedValue, value)
})
}
Expand Down
106 changes: 66 additions & 40 deletions asserts/json/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ import (
// Given the response is {"first": 777, "second": [{"key_1": "some_key", "value": "some_value"}]}, we can assert on the result like so `$.second[? @.key_1=="some_key"].value`, "some_value"
// About expression - https://goessner.net/articles/JsonPath/
func contains(data []byte, expression string, expect interface{}) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

ok, found := insideArray(value, expect)
if !ok {
return errors.NewAssertError("Contains", fmt.Sprintf("on path %v. %v could not be applied builtin len()", expression, expect), nil, nil)
}
if !found {
return errors.NewAssertError("Contains", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
for _, value := range values {
ok, found := insideArray(value, expect)
if !ok {
return errors.NewAssertError("Contains", fmt.Sprintf("on path %v. %v could not be applied builtin len()", expression, expect), nil, nil)
}
if !found {

Check failure on line 26 in asserts/json/util.go

View workflow job for this annotation

GitHub Actions / golangci-lint

if statements should only be cuddled with assignments (wsl)
return errors.NewAssertError("Contains", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
}
}

return nil
Expand All @@ -32,13 +34,15 @@ func contains(data []byte, expression string, expect interface{}) error {
// Equal is a function to assert that a jsonpath expression matches the given value
// About expression - https://goessner.net/articles/JsonPath/
func equal(data []byte, expression string, expect interface{}) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

if !objectsAreEqual(value, expect) {
return errors.NewAssertError("Equal", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
for _, value := range values {
if !objectsAreEqual(value, expect) {
return errors.NewAssertError("Equal", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
}
}

return nil
Expand All @@ -47,13 +51,15 @@ func equal(data []byte, expression string, expect interface{}) error {
// 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(data []byte, expression string, expect interface{}) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

if objectsAreEqual(value, expect) {
return errors.NewAssertError("NotEqual", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
for _, value := range values {
if objectsAreEqual(value, expect) {
return errors.NewAssertError("NotEqual", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
}
}

return nil
Expand All @@ -62,14 +68,16 @@ func notEqual(data []byte, expression string, expect interface{}) error {
// Length is a function to asserts that value is the expected length
// About expression - https://goessner.net/articles/JsonPath/
func length(data []byte, expression string, expectLength int) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

v := reflect.ValueOf(value)
if v.Len() != expectLength {
return errors.NewAssertError("Length", fmt.Sprintf("on path %v. expect lenght %v, but actual %v", expression, expectLength, v.Len()), v.Len(), expectLength)
for _, value := range values {
v := reflect.ValueOf(value)
if v.Len() != expectLength {
return errors.NewAssertError("Length", fmt.Sprintf("on path %v. expect lenght %v, but actual %v", expression, expectLength, v.Len()), v.Len(), expectLength)
}
}

return nil
Expand All @@ -78,14 +86,16 @@ func length(data []byte, expression string, expectLength int) error {
// GreaterThan is a function to asserts that value is greater than the given length
// About expression - https://goessner.net/articles/JsonPath/
func greaterThan(data []byte, expression string, minimumLength int) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

v := reflect.ValueOf(value)
if v.Len() <= minimumLength {
return errors.NewAssertError("GreaterThan", fmt.Sprintf("on path %v. %v is greater than %v", expression, v.Len(), minimumLength), v.Len(), minimumLength)
for _, value := range values {
v := reflect.ValueOf(value)
if v.Len() <= minimumLength {
return errors.NewAssertError("GreaterThan", fmt.Sprintf("on path %v. %v is greater than %v", expression, v.Len(), minimumLength), v.Len(), minimumLength)
}
}

return nil
Expand All @@ -94,14 +104,16 @@ func greaterThan(data []byte, expression string, minimumLength int) error {
// GreaterOrEqualThan is a function to asserts that value is greater or equal than the given length
// About expression - https://goessner.net/articles/JsonPath/
func greaterOrEqualThan(data []byte, expression string, minimumLength int) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

v := reflect.ValueOf(value)
if v.Len() < minimumLength {
return errors.NewAssertError("GreaterOrEqualThan", fmt.Sprintf("on path %v. %v is greater or equal than %v", expression, v.Len(), minimumLength), v.Len(), minimumLength)
for _, value := range values {
v := reflect.ValueOf(value)
if v.Len() < minimumLength {
return errors.NewAssertError("GreaterOrEqualThan", fmt.Sprintf("on path %v. %v is greater or equal than %v", expression, v.Len(), minimumLength), v.Len(), minimumLength)
}
}

return nil
Expand All @@ -110,14 +122,16 @@ func greaterOrEqualThan(data []byte, expression string, minimumLength int) error
// LessThan is a function to asserts that value is less than the given length
// About expression - https://goessner.net/articles/JsonPath/
func lessThan(data []byte, expression string, maximumLength int) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

v := reflect.ValueOf(value)
if v.Len() >= maximumLength {
return errors.NewAssertError("LessThan", fmt.Sprintf("on path %v. %v is less than %v", expression, v.Len(), maximumLength), v.Len(), maximumLength)
for _, value := range values {
v := reflect.ValueOf(value)
if v.Len() >= maximumLength {
return errors.NewAssertError("LessThan", fmt.Sprintf("on path %v. %v is less than %v", expression, v.Len(), maximumLength), v.Len(), maximumLength)
}
}

return nil
Expand All @@ -126,14 +140,16 @@ func lessThan(data []byte, expression string, maximumLength int) error {
// LessOrEqualThan is a function to asserts that value is less or equal than the given length
// About expression - https://goessner.net/articles/JsonPath/
func lessOrEqualThan(data []byte, expression string, maximumLength int) error {
value, err := GetValueFromJSON(data, expression)
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

v := reflect.ValueOf(value)
if v.Len() > maximumLength {
return errors.NewAssertError("LessThan", fmt.Sprintf("on path %v. %v is less or equal than %v", expression, v.Len(), maximumLength), v.Len(), maximumLength)
for _, value := range values {
v := reflect.ValueOf(value)
if v.Len() > maximumLength {
return errors.NewAssertError("LessThan", fmt.Sprintf("on path %v. %v is less or equal than %v", expression, v.Len(), maximumLength), v.Len(), maximumLength)
}
}

return nil
Expand All @@ -142,20 +158,27 @@ func lessOrEqualThan(data []byte, expression string, maximumLength int) error {
// notEmpty is a function to asserts that value is not empty (!= 0, != null)
// About expression - https://goessner.net/articles/JsonPath/
func notEmpty(data []byte, expression string) error {
value, _ := GetValueFromJSON(data, expression)
if isEmpty(value) {
values, _ := GetValueFromJSON(data, expression)

if len(values) == 0 {
return errors.NewAssertError("NotEmpty", fmt.Sprintf("on path %v. value is not present", expression), nil, nil)
}

for _, value := range values {
if isEmpty(value) {
return errors.NewAssertError("NotEmpty", fmt.Sprintf("on path %v. value is not present", expression), nil, nil)
}
}

return nil
}

// Present is a function to asserts that value is present
// value can be 0 or null
// About expression - https://goessner.net/articles/JsonPath/
func present(data []byte, expression string) error {
_, err := GetValueFromJSON(data, expression)
if err != nil {
values, err := GetValueFromJSON(data, expression)
if err != nil || len(values) == 0 {
return errors.NewAssertError("Present", fmt.Sprintf("on path %v. value not present", expression), nil, nil)
}

Expand All @@ -165,9 +188,12 @@ func present(data []byte, expression string) error {
// NotPresent is a function to asserts that value is not present
// About expression - https://goessner.net/articles/JsonPath/
func notPresent(data []byte, expression string) error {
value, _ := GetValueFromJSON(data, expression)
if !isEmpty(value) {
return errors.NewAssertError("NotPresent", fmt.Sprintf("on path %v. value present", expression), nil, nil)
values, _ := GetValueFromJSON(data, expression)

for _, value := range values {
if !isEmpty(value) {
return errors.NewAssertError("NotPresent", fmt.Sprintf("on path %v. value present", expression), nil, nil)
}
}

return nil
Expand Down

0 comments on commit ef8cfd8

Please sign in to comment.