From 03dadb9b9ce5c8386079bd47c54cde17f39382c4 Mon Sep 17 00:00:00 2001 From: DR695H Date: Mon, 1 Jul 2019 14:33:37 -0400 Subject: support json path searching Issue-ID: TEST-172 Change-Id: Id5a355ca0148efb97065c0a5aacd53ddfd11818d Signed-off-by: DR695H --- robotframework-onap/ONAPLibrary/JSON.py | 4 ++- .../ONAPLibrary/JSONPathKeywords.py | 39 ++++++++++++++++++++++ robotframework-onap/setup.py | 3 +- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 robotframework-onap/ONAPLibrary/JSONPathKeywords.py diff --git a/robotframework-onap/ONAPLibrary/JSON.py b/robotframework-onap/ONAPLibrary/JSON.py index 6eb1e65..1cb67dd 100644 --- a/robotframework-onap/ONAPLibrary/JSON.py +++ b/robotframework-onap/ONAPLibrary/JSON.py @@ -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 index 0000000..22e5e04 --- /dev/null +++ b/robotframework-onap/ONAPLibrary/JSONPathKeywords.py @@ -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 diff --git a/robotframework-onap/setup.py b/robotframework-onap/setup.py index 192e171..89b72d5 100644 --- a/robotframework-onap/setup.py +++ b/robotframework-onap/setup.py @@ -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={ -- cgit 1.2.3-korg