diff options
author | Pawel Wieczorek <p.wieczorek2@samsung.com> | 2019-12-12 11:07:10 +0100 |
---|---|---|
committer | Pawel Wieczorek <p.wieczorek2@samsung.com> | 2019-12-12 11:37:41 +0100 |
commit | dc6b6c7d9d0cf9e2d0d8f69ba2dd5fca8e010b2e (patch) | |
tree | 05098ad8c6f6485360848b878490a08463c2776a /robotframework-onap/ONAPLibrary/JSONKeywords.py | |
parent | 78600c90d81d85ae0e1183c0610625bb28a6cc5e (diff) |
Add JSON Should Contain Sub JSON Keyword
This patch provides functionality of "Dictionary Should Contain Sub
Dictionary" Keyword (from Robot Framework "Collections" library) for
JSON. It supports both objects and strings containing JSON.
Added tests were intended to verify no regressions were introduced as
well as corner cases are handled properly.
Issue-ID: SECCOM-261
Change-Id: Id7581d79b93ce0403e97e1bbf47c10e0e166f2ec
Signed-off-by: Pawel Wieczorek <p.wieczorek2@samsung.com>
Diffstat (limited to 'robotframework-onap/ONAPLibrary/JSONKeywords.py')
-rw-r--r-- | robotframework-onap/ONAPLibrary/JSONKeywords.py | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/robotframework-onap/ONAPLibrary/JSONKeywords.py b/robotframework-onap/ONAPLibrary/JSONKeywords.py index 5b9e24e..70fa2c0 100644 --- a/robotframework-onap/ONAPLibrary/JSONKeywords.py +++ b/robotframework-onap/ONAPLibrary/JSONKeywords.py @@ -25,10 +25,8 @@ class JSONKeywords(object): def __init__(self): super(JSONKeywords, self).__init__() - @keyword - def json_equals(self, left, right): - """JSON Equals takes in two strings or json objects, converts them into json if needed and then compares them, - returning if they are equal or not.""" + def _json_compare(self, left, right, cmp): + """_json_compare takes two strings or JSON objects and checks their DeepDiff using cmp function.""" if isinstance(left, string_types): left_json = json.loads(left) else: @@ -39,11 +37,28 @@ class JSONKeywords(object): right_json = right ddiff = DeepDiff(left_json, right_json, ignore_order=True) - if ddiff == {}: - return True - else: + return cmp(ddiff) + + @keyword + def json_equals(self, left, right): + """JSON Equals takes in two strings or json objects, converts them into json if needed and then compares them, + returning if they are equal or not.""" + return self._json_compare(left, right, lambda ddiff: ddiff == {}) + + @keyword + def json_should_contain_sub_json(self, left, right): + """JSON Should Contain Sub JSON fails unless all items in right are found in left.""" + + # following could have been really long lambda but readability counts + def _is_subset(ddiff): + if ddiff == {}: + return True + if len(ddiff.keys()) == 1 and 'dictionary_item_removed' in ddiff.keys(): + return True return False + return self._json_compare(left, right, _is_subset) + @keyword def make_list_into_dict(self, dict_list, key): """ Converts a list of dicts that contains a field that has a unique key into a dict of dicts """ |