Commit ff12b969 authored by Marco Cavalli's avatar Marco Cavalli
Browse files

feat: allow scripts to automatic detect established registrations from the code

parent c1c0ebfc
Loading
Loading
Loading
Loading
+51 −3
Original line number Diff line number Diff line
from os.path import dirname
from os.path import basename, dirname
from robot.api import TestSuiteBuilder
from analysis.parserobotfile import ParseRobotFile
from analysis.parseapiutilsfile import ParseApiUtilsFile
@@ -647,9 +647,12 @@ class GenerateRobotData:
        version = 'v1.3.1'
        tp_id = self.generate_name_iop()
        reference, clauses = self.generate_reference(version=version)
        test_doc = {}
        # Add test case documentation
        if self.robot.test_case_names:
            test_doc = self.robot.get_iop_documentation_data(test_name=self.robot.test_case_names[0])
            test_name = self.robot.test_case_names[0]
            test_doc = self.robot.get_iop_documentation_data(test_name=test_name)
            test_doc['registrations_established'] = self.generate_iop_registrations(test_name=test_name)

        self.test_suite = {
            'tp_id': tp_id,
@@ -665,6 +668,52 @@ class GenerateRobotData:
            'test_cases': list()
        }

    def generate_iop_registrations(self, test_name: str) -> str:
        """Generate the registration description from the test setup."""
        test = next((item for item in self.suite.tests if item.name == test_name), None)
        if test is None:
            raise ValueError(f"IOP test '{test_name}' was not found")

        setup_name = test.setup.name
        setup = next((item for item in self.suite.resource.keywords if item.name == setup_name), None)
        if setup is None:
            raise ValueError(f"IOP setup keyword '{setup_name}' was not found")

        modes = {'inclusive', 'exclusive', 'redirect', 'auxiliary'}
        registrations = []

        for keyword in setup.body:
            if keyword.name != 'Create List' or len(keyword.args) != 5:
                continue

            mode = str(keyword.args[2]).lower()
            if mode not in modes:
                continue

            payload_variable = str(keyword.args[1])
            try:
                payload_path = self.robot.variables[payload_variable]
            except KeyError:
                raise ValueError(
                    f"Registration payload variable '{payload_variable}' is not defined"
                )

            brokers = []
            for broker_variable in (str(keyword.args[4]), str(keyword.args[3])):
                broker_match = match(r'^\$\{(b\d+)_url\}$', broker_variable)
                if broker_match is None:
                    raise ValueError(
                        f"Registration broker variable '{broker_variable}' is invalid"
                    )
                brokers.append(broker_match.group(1))

            registrations.append(
                f"{mode.capitalize()} from {brokers[0]} to {brokers[1]} "
                f"(Figure {basename(payload_path)})"
            )

        return '. '.join(registrations)

    def visit_test_iop(self, test_name: str, suite_setup: str = "", suite_teardown: str = ""):
        """Process a single IOP test case"""
        # Get test information from the parsed robot file
@@ -715,4 +764,3 @@ class GenerateRobotData:
        
        # Fallback if pattern not found
        return f"IOP_TP/NGSI-LD/{self.robot.test_suite}"
    
 No newline at end of file