support json path searching 47/90747/2
authorDR695H <dr695h@att.com>
Mon, 1 Jul 2019 18:33:37 +0000 (14:33 -0400)
committerDR695H <dr695h@att.com>
Mon, 1 Jul 2019 18:39:14 +0000 (14:39 -0400)
Issue-ID: TEST-172
Change-Id: Id5a355ca0148efb97065c0a5aacd53ddfd11818d
Signed-off-by: DR695H <dr695h@att.com>
robotframework-onap/ONAPLibrary/JSON.py
robotframework-onap/ONAPLibrary/JSONPathKeywords.py [new file with mode: 0644]
robotframework-onap/setup.py

index 6eb1e65..1cb67dd 100644 (file)
@@ -14,6 +14,7 @@
 
 from ONAPLibrary.robotlibcore import HybridCore
 from ONAPLibrary.JSONKeywords import JSONKeywords
+from ONAPLibrary.JSONPathKeywords import JSONPathKeywords
 
 
 class JSON(HybridCore):
@@ -22,6 +23,7 @@ class JSON(HybridCore):
 
     def __init__(self):
         self.keyword_implementors = [
-            JSONKeywords()
+            JSONKeywords(),
+            JSONPathKeywords()
         ]
         HybridCore.__init__(self, self.keyword_implementors)
diff --git a/robotframework-onap/ONAPLibrary/JSONPathKeywords.py b/robotframework-onap/ONAPLibrary/JSONPathKeywords.py
new file mode 100644 (file)
index 0000000..22e5e04
--- /dev/null
@@ -0,0 +1,39 @@
+# Copyright 2019 AT&T Intellectual Property. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+import json
+
+from robot.api.deco import keyword
+from jsonpath_rw import parse
+
+
+class JSONPathKeywords(object):
+    """JSONPATH is common resource for json path keywords.
+    """
+
+    def __init__(self):
+        super(JSONPathKeywords, self).__init__()
+
+    @keyword
+    def json_search(self, expression, target):
+        """JSON Search takes in two params, the first is the jsonpath expression and the second is the json target
+        which is converted into string if needed and then compares them, returning the matches."""
+
+        jsonpath_expr = parse(expression)
+        if isinstance(target, str) or isinstance(target, unicode):
+            search_json = json.dumps(target)
+        else:
+            search_json = target
+
+        results = jsonpath_expr.find(search_json)
+        return results
index 192e171..89b72d5 100644 (file)
@@ -38,7 +38,8 @@ setup(
         'future',
         'robotframework-requests',
         'kafka-python',
-        'urllib3'
+        'urllib3',
+        'jsonpath-rw'
     ],  # what we need
     packages=['loadtest', 'vcpeutils', 'ONAPLibrary'],       # The name of your scripts package
     package_dir={