diff --git a/asserts/json/json.go b/asserts/json/json.go index a18b3fe..7da1634 100644 --- a/asserts/json/json.go +++ b/asserts/json/json.go @@ -63,6 +63,22 @@ func NotEqual(expression string, expect interface{}) cute.AssertBody { } } +// 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 { diff --git a/asserts/json/json_test.go b/asserts/json/json_test.go index 5b3cb30..07036a5 100644 --- a/asserts/json/json_test.go +++ b/asserts/json/json_test.go @@ -698,6 +698,196 @@ func TestNotEqual(t *testing.T) { } } +func TestEqualJSON(t *testing.T) { + tests := []jsonTest{ + { + caseName: "valid json", + data: `{"first": 777, "second": [{"key_1": "some_key", "value": "some_value"}]}`, + expression: "$.second[0].value", + expect: `"some_value"`, + IsNilErr: true, + }, + { + caseName: "not valid json", + data: "{not_valid_json}", + }, + { + caseName: "3rd party key", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.l", + }, + { + caseName: "not array", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.b[bs]", + }, + { + caseName: "valid array", + data: `{"arr": ["one","two"]}`, + expression: "$.arr", + expect: `["one", "two"]`, + IsNilErr: true, + }, + { + caseName: "check equal map", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.b", + expect: `{"bs": "sb"}`, + IsNilErr: true, + }, + { + caseName: "check equal string", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.a", + expect: `"as"`, + IsNilErr: true, + }, + { + caseName: "check equal not correct string", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.a", + expect: `"not_correct"`, + }, + { + caseName: "check deep equal", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$", + expect: `{ "b": {"bs": "sb"}, "a":"as" }`, + IsNilErr: true, + }, + { + caseName: "check deep equal not correct", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$", + expect: `{ "b": {"sb": "bs"}, "a":"as" }`, + }, + { + caseName: "check 186135434", + data: `{"a":186135434, "b":{"bs":"sb"}}`, + expression: "$.a", + expect: "186135434", + IsNilErr: true, + }, + { + caseName: "check float", + data: `{"a":1.0000001, "b":{"bs":"sb"}}`, + expression: "$.a", + expect: "1.0000001", + IsNilErr: true, + }, + { + caseName: "check float 2", + data: `{"a":999.0000001, "b":{"bs":"sb"}}`, + expression: "$.a", + expect: "999.0000001", + IsNilErr: true, + }, + } + + for _, test := range tests { + exp, _ := test.expect.(string) + err := EqualJSON(test.expression, []byte(exp))([]byte(test.data)) + + if test.IsNilErr { + require.NoError(t, err, "failed test %v", test.caseName) + } else { + require.Error(t, err, "failed test %v", test.caseName) + } + } +} + +func TestNotEqualJSON(t *testing.T) { + tests := []jsonTest{ + { + caseName: "valid json", + data: `{"first": 777, "second": [{"key_1": "some_key", "value": "some_value"}]}`, + expression: "$.second[0].value", + expect: `"some_value"`, + }, + { + caseName: "not valid json", + data: "{not_valid_json}", + }, + { + caseName: "3rd party key", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.l", + }, + { + caseName: "not array", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.b[bs]", + }, + { + caseName: "valid array", + data: `{"arr": ["one","two"]}`, + expression: "$.arr", + expect: `["one", "two"]`, + }, + { + caseName: "check equal map", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.b", + expect: `{"bs": "sb"}`, + }, + { + caseName: "check equal string", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.a", + expect: `"as"`, + }, + { + caseName: "check equal not correct string", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$.a", + expect: `"not_correct"`, + IsNilErr: true, + }, + { + caseName: "check deep equal", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$", + expect: `{ "b": {"bs": "sb"}, "a":"as" }`, + }, + { + caseName: "check deep equal not correct", + data: `{"a":"as", "b":{"bs":"sb"}}`, + expression: "$", + expect: `{ "b": {"sb": "bs"}, "a":"as" }`, + IsNilErr: true, + }, + { + caseName: "check 186135434", + data: `{"a":186135434, "b":{"bs":"sb"}}`, + expression: "$.a", + expect: "186135434", + }, + { + caseName: "check float", + data: `{"a":1.0000001, "b":{"bs":"sb"}}`, + expression: "$.a", + expect: "1.0000001", + }, + { + caseName: "check float 2", + data: `{"a":999.0000001, "b":{"bs":"sb"}}`, + expression: "$.a", + expect: "999.0000001", + }, + } + + for _, test := range tests { + exp, _ := test.expect.(string) + err := NotEqualJSON(test.expression, []byte(exp))([]byte(test.data)) + + if test.IsNilErr { + require.NoError(t, err, "failed test %v", test.caseName) + } else { + require.Error(t, err, "failed test %v", test.caseName) + } + } +} + func TestGetValueFromJSON(t *testing.T) { testCases := []struct { name string diff --git a/asserts/json/util.go b/asserts/json/util.go index 7c8dffa..ed2c8d1 100644 --- a/asserts/json/util.go +++ b/asserts/json/util.go @@ -6,6 +6,7 @@ import ( "reflect" "strings" + "github.com/ohler55/ojg/oj" "github.com/ozontech/cute/errors" ) @@ -32,9 +33,7 @@ func contains(data []byte, expression string, expect interface{}) error { return nil } -// 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 { +func equalAbstract(data []byte, expression string, expect interface{}, name string) error { values, err := GetValueFromJSON(data, expression) if err != nil { return err @@ -42,16 +41,14 @@ func equal(data []byte, expression string, expect interface{}) error { 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 errors.NewAssertError(name, fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect) } } return nil } -// 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 { +func notEqualAbstract(data []byte, expression string, expect interface{}, name string) error { values, err := GetValueFromJSON(data, expression) if err != nil { return err @@ -59,13 +56,47 @@ func notEqual(data []byte, expression string, expect interface{}) error { 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 errors.NewAssertError(name, fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect) } } return nil } +// 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 { + return equalAbstract(data, expression, expect, "Equal") +} + +// 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 { + return notEqualAbstract(data, expression, expect, "NotEqual") +} + +// EqualJSON is a function to check json path expression value is equal to given json +// About expression - https://goessner.net/articles/JsonPath/ +func equalJSON(data []byte, expression string, expect []byte) error { + obj, err := oj.Parse(expect) + if err != nil { + return fmt.Errorf("could not parse json in EqualJSON error: '%s'", err) + } + + return equalAbstract(data, expression, obj, "EqualJSON") +} + +// 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(data []byte, expression string, expect []byte) error { + obj, err := oj.Parse(expect) + if err != nil { + return fmt.Errorf("could not parse json in NotEqualJSON error: '%s'", err) + } + + return notEqualAbstract(data, expression, obj, "NotEqualJSON") +} + // 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 {