1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from robot.api.deco import keyword
17 from deepdiff import DeepDiff
18 from six import string_types
21 class JSONKeywords(object):
22 """JSON is common resource for simple json helper keywords.
26 super(JSONKeywords, self).__init__()
28 def _json_compare(self, left, right, cmp):
29 """_json_compare takes two strings or JSON objects and checks their DeepDiff using cmp function."""
30 if isinstance(left, string_types):
31 left_json = json.loads(left)
34 if isinstance(right, string_types):
35 right_json = json.loads(right)
39 ddiff = DeepDiff(left_json, right_json, ignore_order=True)
43 def json_equals(self, left, right):
44 """JSON Equals takes in two strings or json objects, converts them into json if needed and then compares them,
45 returning if they are equal or not."""
46 return self._json_compare(left, right, lambda ddiff: ddiff == {})
49 def json_should_contain_sub_json(self, left, right):
50 """JSON Should Contain Sub JSON fails unless all items in right are found in left."""
52 # following could have been really long lambda but readability counts
53 def _is_subset(ddiff):
56 if len(ddiff.keys()) == 1 and 'dictionary_item_removed' in ddiff.keys():
60 return self._json_compare(left, right, _is_subset)
63 def make_list_into_dict(self, dict_list, key):
64 """ Converts a list of dicts that contains a field that has a unique key into a dict of dicts """
66 if isinstance(dict_list, list):
67 for thisDict in dict_list:
73 def find_element_in_array(self, searched_array, key, value):
74 """ Takes in an array and a key value, it will return the items in the array that has a key and value that
75 matches what you pass in """
77 for item in searched_array:
79 if item[key] == value: