Skip to content

Commit 40ca159

Browse files
committed
Adding RESTLibrary
1 parent 94cf52a commit 40ca159

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3328
-2
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,4 @@
198198
distributed under the License is distributed on an "AS IS" BASIS,
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201-
limitations under the License.
201+
limitations under the License.

README.md

+522-1
Large diffs are not rendered by default.

pyproject.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=39.1.0",
4+
"wheel",
5+
"robotframework>=3.1.2",
6+
"requests>=2.25.1",
7+
"jsonpath-ng>=1.4.3",
8+
"jsonschema>=3.2.0"
9+
]
10+
build-backend = "setuptools.build_meta"

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
robotframework>=3.1.2
2+
requests>=2.25.1
3+
jsonpath-ng>=1.5.0
4+
jsonschema>=3.2.0
5+
setuptools>=39.1.0

setup.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import setuptools
2+
3+
with open("README.md", "r", encoding="utf-8") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="robotframework-restlibrary",
8+
version="1.0",
9+
author="Deepak Chourasia",
10+
author_email="[email protected]",
11+
description="A REST API Automation Library for Robot Framework",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://gitlab.sas.com/sindec/rf-rest",
15+
project_urls={
16+
"Bug Tracker": "https://gitlab.sas.com/sindec/rf-rest",
17+
},
18+
classifiers=[
19+
"Programming Language :: Python :: 3",
20+
"License :: OSI Approved :: Apache Software License",
21+
"Intended Audience :: Developers",
22+
"Natural Language :: English",
23+
"Topic :: Software Development :: Testing",
24+
"Development Status :: 5 - Production/Stable",
25+
"Framework :: Robot Framework",
26+
],
27+
package_dir={"": "src"},
28+
packages=setuptools.find_packages(where="src"),
29+
python_requires=">=3.6",
30+
)
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import json
2+
import jsonschema
3+
import sys
4+
from .libcommons import libcommons
5+
6+
class JSONSchemaValidator:
7+
'''
8+
JSONSchemaValidator validates json schema against the json data file provided as parameter.
9+
'''
10+
11+
schema_file_path = ''
12+
data_file_path = ''
13+
14+
def get_node_path(path):
15+
'''
16+
get_node_path is function used to extract the json node path.
17+
'''
18+
str_path = ''
19+
for i in path:
20+
str_path = str_path + '/' + str(i)
21+
return str_path
22+
23+
def ValidateSchema(schema, data):
24+
'''
25+
ValidateSchema is a function which validates schema file against the json data file provided as parameter to this function.
26+
It looks for the schema file in the same directory.
27+
'''
28+
__schema_str = None
29+
__data_str = None
30+
__flag = False
31+
__schema_flag = True
32+
__json_flag = True
33+
__err_arr = []
34+
try:
35+
if libcommons.path_exists(schema):
36+
with open(schema) as schema_file:
37+
__schema_str = json.load(schema_file)
38+
elif type(schema) is str:
39+
__schema_str = json.loads(schema)
40+
elif schema.__class__.__name__ in ('dict', 'dotdict'):
41+
__schema_str = schema
42+
except Exception as e:
43+
__json_flag = False
44+
#print("JSON Data file not found", '-->', e.filename)
45+
print(str(e))
46+
try:
47+
if libcommons.path_exists(data):
48+
with open(data) as data_file:
49+
__data_str = json.load(data_file)
50+
elif type(data) is str:
51+
__data_str = json.loads(data)
52+
elif data.__class__.__name__ in ('dict', 'dotdict'):
53+
__data_str = data
54+
except Exception as e:
55+
__schema_flag = False
56+
#print("JSON Schema file not found", '-->', se.filename)
57+
print(str(e))
58+
59+
flag = __schema_flag and __json_flag
60+
if flag:
61+
try:
62+
error_count = 1
63+
validation = jsonschema.Draft7Validator(__schema_str)
64+
for error in sorted(validation.iter_errors(__data_str), key=str):
65+
msg_str = ''
66+
if error_count == 1:
67+
#print('Schema Verification failed, following are the details:')
68+
error_count = error_count + 1
69+
if 'required' in error.message:
70+
msg_str = 'Msg: Following node has a property missing from Schema.'
71+
if msg_str == '':
72+
__err_arr.append(
73+
{'type': 'Schema Mismatch', 'path': JSONSchemaValidator.get_node_path(error.absolute_path),
74+
'error': ''.join(error.message)})
75+
else:
76+
__err_arr.append(
77+
{'type': 'Node missing', 'path': JSONSchemaValidator.get_node_path(error.absolute_path),
78+
'error': ''.join(error.message)})
79+
80+
#__err_arr = json.dumps(__err_arr, indent=4)
81+
return __err_arr
82+
except jsonschema.SchemaError as sExp:
83+
print('Bad Definition - Schema Definition Exception', sExp)

0 commit comments

Comments
 (0)