aai should not check status code
[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 import json
15
16 from robot.api.deco import keyword
17 from deepdiff import DeepDiff
18
19
20 class JSONKeywords(object):
21     """JSON is common resource for simple json helper keywords.
22     """
23
24     def __init__(self):
25         super(JSONKeywords, self).__init__()
26
27     @keyword
28     def json_equals(self, left, right):
29         """JSON Equals takes in two strings or json objects, converts them into json if needed and then compares them,
30         returning if they are equal or not."""
31         if isinstance(left, str) or isinstance(left, unicode):
32             left_json = json.loads(left)
33         else:
34             left_json = left
35         if isinstance(right, str) or isinstance(right, unicode):
36             right_json = json.loads(right)
37         else:
38             right_json = right
39
40         ddiff = DeepDiff(left_json, right_json, ignore_order=True)
41         if ddiff == {}:
42             return True
43         else:
44             return False
45
46     @keyword
47     def make_list_into_dict(self, dict_list, key):
48         """ Converts a list of dicts that contains a field that has a unique key into a dict of dicts """
49         d = {}
50         if isinstance(dict_list, list):
51             for thisDict in dict_list:
52                 v = thisDict[key]
53                 d[v] = thisDict
54         return d
55
56     @keyword
57     def find_element_in_array(self, searched_array, key, value):
58         """ Takes in an array and a key value, it will return the items in the array that has a key and value that
59         matches what you pass in """
60         elements = []
61         for item in searched_array:
62             if key in item:
63                 if item[key] == value:
64                     elements.append(item)
65         return elements