diff --git a/data_converters_validator.py b/data_converters_validator.py index 46e7405..c7926d6 100644 --- a/data_converters_validator.py +++ b/data_converters_validator.py @@ -1,6 +1,7 @@ import os import json import base64 +import time from tb_rest_client.rest_client_pe import RestClientPE import re @@ -13,21 +14,25 @@ def find_payload_and_result_pairs(directory): - payloads = sorted([f for f in os.listdir(directory) if re.match(r'payload_\d+\.json', f)]) - results = sorted([f for f in os.listdir(directory) if re.match(r'result_\d+\.json', f)]) + payloads = sorted([f for f in os.listdir(directory) if re.match(r'payload(_\d+)?\.json', f)]) + results = sorted([f for f in os.listdir(directory) if re.match(r'result(_\d+)?\.json', f)]) pairs = [] + if 'payload.json' in payloads and 'result.json' in results: + pairs.append(('payload.json', 'result.json')) + for payload_file in payloads: - suffix = re.search(r'_(\d+)\.json', payload_file).group(1) # Extract the number from the filename - result_file = f"result_{suffix}.json" - if result_file in results: - pairs.append((payload_file, result_file)) + if re.match(r'payload_\d+\.json', payload_file): + suffix = re.search(r'_(\d+)\.json', payload_file).group(1) + result_file = f"result_{suffix}.json" + if result_file in results: + pairs.append((payload_file, result_file)) return pairs -def validate_uplink_downlink(directory, client): +def validate_uplink_downlink(directory): converter_file = os.path.join(directory, 'converter.json') metadata_file = os.path.join(directory, 'metadata.json') @@ -44,7 +49,7 @@ def validate_uplink_downlink(directory, client): } payload_result_pairs = find_payload_and_result_pairs(directory) - success = True # Boolean flag to track if all validations are successful + success = True for payload_file, result_file in payload_result_pairs: with open(os.path.join(directory, payload_file)) as pf: @@ -57,18 +62,16 @@ def validate_uplink_downlink(directory, client): encoded_payload = base64.b64encode(json.dumps(payload).encode('utf-8')).decode('utf-8') request["decoder"] = decoder request["payload"] = encoded_payload - actual_result = client.converter_controller.test_up_link_converter_using_post(async_req='false', - body=request, - script_lang=script_lang) + actual_result = client.converter_controller.test_up_link_converter_using_post( + async_req='false', body=request, script_lang=script_lang) elif 'downlink' in directory: encoder = configuration.get("encoder") if script_lang == "JS" else configuration.get("tbelEncoder") request["encoder"] = encoder request["msg"] = json.dumps(payload) request["msgType"] = "POST_TELEMETRY_REQUEST" request["integrationMetadata"] = {} - actual_result = client.converter_controller.test_down_link_converter_using_post(async_req='false', - body=request, - script_lang=script_lang) + actual_result = client.converter_controller.test_down_link_converter_using_post( + async_req='false', body=request, script_lang=script_lang) else: raise ValueError(f"Directory '{directory}' is not recognized as 'uplink' or 'downlink'.") @@ -77,34 +80,36 @@ def validate_uplink_downlink(directory, client): error = result_value.get('error') if error != '': - success = False # Set the flag to False if any validation fails + success = False result_message = f"Validation failed for {directory} with payload {payload_file} and result {result_file} with error: {error}\n" elif json.loads(output) == expected_result: result_message = f"Validation passed for {directory} with payload {payload_file} and result {result_file}\n" else: - success = False # Set the flag to False if output does not match expected result + success = False result_message = f"Validation failed for {directory} with payload {payload_file} and result {result_file}. Expected output does not match.\n" print(result_message) - return success # Return whether this directory's validation was successful + time.sleep(1) + + return success -def walk_vendors_directory(root_dir, client): - all_success = True # Overall success flag +def walk_vendors_directory(root_dir): + all_success = True for root, dirs, files in os.walk(root_dir): if 'converter.json' in files and 'metadata.json' in files: - success = validate_uplink_downlink(root, client) + success = validate_uplink_downlink(root) if not success: - all_success = False # Set overall flag to False if any validation fails + all_success = False return all_success if __name__ == "__main__": root_directory = "VENDORS" - all_success = walk_vendors_directory(root_directory, client) + all_success = walk_vendors_directory(root_directory) if all_success: print("All converters data validated successfully.")