Update INFO.yaml with new PTL
[testsuite/python-testing-utils.git] / robotframework-onap / ONAPLibrary / JSONKeywords.py
1 # Copyright 2019 AT&T Intellectual Property. All rights reserved.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import json
16 from robot.api.deco import keyword
17 from deepdiff import DeepDiff
18 from six import string_types
19
20
21 class JSONKeywords(object):
22     """JSON is common resource for simple json helper keywords.
23     """
24
25     def __init__(self):
26         super(JSONKeywords, self).__init__()
27
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)
32         else:
33             left_json = left
34         if isinstance(right, string_types):
35             right_json = json.loads(right)
36         else:
37             right_json = right
38
39         ddiff = DeepDiff(left_json, right_json, ignore_order=True)
40         return cmp(ddiff)
41
42     @keyword
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 == {})
47
48     @keyword
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."""
51
52         # following could have been really long lambda but readability counts
53         def _is_subset(ddiff):
54             if ddiff == {}:
55                 return True
56             if len(ddiff.keys()) == 1 and 'dictionary_item_removed' in ddiff.keys():
57                 return True
58             return False
59
60         return self._json_compare(left, right, _is_subset)
61
62     @keyword
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 """
65         d = {}
66         if isinstance(dict_list, list):
67             for thisDict in dict_list:
68                 v = thisDict[key]
69                 d[v] = thisDict
70         return d
71
72     @keyword
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 """
76         elements = []
77         for item in searched_array:
78             if key in item:
79                 if item[key] == value:
80                     elements.append(item)
81         return elements