Commit 4051be3a authored by lopezaguilar's avatar lopezaguilar
Browse files

Managing the value of the check properties

parent 27269b27
Loading
Loading
Loading
Loading
+69 −38
Original line number Diff line number Diff line
@@ -70,24 +70,50 @@ class Checks:
                Checks.check_created_resource_set_to,
            'Check Updated Resource Set To':
                Checks.check_updated_resource_set_to,
            'Check Updated Resources Set To':
                Checks.check_updated_resources_set_to,
            'Check SUT Not Containing Resource':
                Checks.check_sut_not_containing_resource
                Checks.check_sut_not_containing_resource,
            'Check SUT Not Containing Resources':
                Checks.check_sut_not_containing_resources,
            'Check NotificationParams':
                Checks.check_notificationparams
        }

        self.args = {
            'Check Response Body Type When Using Session Request': [2],
            'Check Response Body Containing ProblemDetails Element Containing Type Element set to': [2],
            'Check RL Response Body Containing ProblemDetails Element Containing Type Element set to': [2],
            'Check JSON Value In Response Body': [1, 2],
            'Check Pagination Prev And Next Headers': [2, 3],
            'Check SUT Not Containing Resource': [1]
            'Check Response Status Code': {
                'params': ['status_code'],
                'position': [1]
            },
            'Check Response Body Type When Using Session Request': {
                'position': [2],
            },
            'Check Response Body Containing ProblemDetails Element Containing Type Element set to': {
                'params': ['type'],
                'position': [2],
            },
            'Check RL Response Body Containing ProblemDetails Element Containing Type Element set to': {
                'position': [2],
            },
            'Check JSON Value In Response Body': {
                'position': [1, 2],
            },
            'Check Pagination Prev And Next Headers': {
                'position': [2, 3],
            },
            'Check SUT Not Containing Resource': {
                'position': [1],
            }
        }

    @staticmethod
    def check_response_status_code(kwargs: list) -> str:
        if "status_code" in kwargs:
            status_code = kwargs['status_code']
            try:
                return f'Response Status Code set to {status_code} ({HTTPStatus(status_code).phrase})'
            except ValueError:
                return f'Response Status Code set to {status_code}'
        else:
            raise Exception(f'ERROR, Expected status_code parameter but received: {kwargs}')

@@ -309,6 +335,11 @@ class Checks:
    def check_updated_resource_set_to(kwargs: list) -> str:
            return "Updated Entity set to ${entity}"

    def check_updated_resources_set_to(kwargs: list) -> str:
        if 'number_entities' in kwargs:
            number_entities = kwargs['number_entities']
            return f"Updated Entities set to '{number_entities}' valid entities"

    def check_sut_not_containing_resource(kwargs: list) -> str:
        if "status_code" in kwargs:
            status_code = kwargs['status_code']
@@ -316,6 +347,27 @@ class Checks:
        else:
            raise Exception(f'ERROR, Expected status_code parameter but received: {kwargs}')

    def check_sut_not_containing_resources(kwargs: list) -> str:
        return f'Response body is empty'

    def check_notificationparams(kwargs: list) -> str:
        expected_parameters = ["format", "uri", "accept", "status", "timesSent"]
        result = [x for x in expected_parameters if x not in kwargs]

        if len(result) == 0:
            response = ("Response containing:\n"
                        f"    * payload['format'] is equal to '{kwargs['format']}'\n"
                        f"    * payload['endpoint']['uri'] is equal to '{kwargs['uri']}'\n"
                        f"    * payload['endpoint']['accept'] is equal to '{kwargs['accept']}'\n"
                        f"    * payload['status'] is equal to '{kwargs['status']}'\n"
                        f"    * payload['timesSent'] is equal to '{kwargs['timesSent']}\n"
                        f"    * payload['notification']['lastNotification'] is not Empty\n"
                        f"    * payload['notification']['lastSuccess'] is not Empty\n")
            return response
        else:
            raise Exception(f"ERROR, unexpected attribute '{result}', the attributes expected are "
                            f"'{expected_parameters}', but received: {kwargs}")

    def get_checks(self, **kwargs) -> str:
        checking = None

@@ -334,36 +386,6 @@ class Checks:

        return result

    """
    then {
             the SUT sends a valid Response containing 
                      Response Status Code set to 201 (Created) and
                      Response Body set to an array of created entities ids
    
            and created resources set to ${entities}
    }
then {
         the SUT sends a valid Response containing 
                  Response Status Code set to ${status_code}
                  Response Body containing 
                         ${appended_attrs_list}
         and contains ${entity} with ${appended_attrs_list}
}
    """

    """
    * Check Response Body Content
    
    (is not a final check) Check Response Body Containing Batch Operation Result
                                                                                             
    * Check NotificationParams
            
    Check Updated Resources Set To
        
    Check SUT Not Containing Resources
    and the SUT not containing resources with id in ${existing_entities_ids}
    """


if __name__ == "__main__":
    data = Checks()
@@ -433,8 +455,17 @@ if __name__ == "__main__":
                          next=''))
    print(data.get_checks(checks='Check Created Resource Set To'))
    print(data.get_checks(checks='Check Updated Resource Set To'))
    print(data.get_checks(checks='Check Updated Resources Set To',
                          number_entities=2))
    print(data.get_checks(checks='Check SUT Not Containing Resource',
                          status_code=404))
    print(data.get_checks(checks='Check SUT Not Containing Resources'))
    print(data.get_checks(checks='Check NotificationParams',
                          format="keyValues",
                          uri="http://my.endpoint.org/notify",
                          accept="application/json",
                          status="ok",
                          timesSent="1"))

    print()
    print(data.get_checks(checks=
+3 −0
Original line number Diff line number Diff line
@@ -228,6 +228,9 @@ class GenerateRobotData:
            content_type = ''
            body = ''

        # Generate Checks for Test Data
        list_checks = self.robot.get_checks(test_name=test.name, apiutils=self.apiutil)

        test_case = {
            'name': test.name,
            'permutation_tp_id': f'{self.base_TP_id}/{test.name.split(" ")[0]}',
+11 −11
Original line number Diff line number Diff line
from pprint import pprint
from generaterobotdata import GenerateRobotData


if __name__ == "__main__":
    data = GenerateRobotData(robot_file='../TP/NGSI-LD/CommonBehaviours/043.robot',
                             execdir='/home/fla/Documents/workspace/bdd/ngsi-ld-test-suite')
    data.parse_robot()
+72 −0
Original line number Diff line number Diff line
import re
import os
from checks import Checks


class ParseRobotFile:
    def __init__(self, filename: str, execdir: str):
        self.test_suite = os.path.basename(filename).split('.')[0]

        with open(filename, 'r') as file:
            # Read the contents of the file
            self.file_contents = file.read()
@@ -13,6 +17,7 @@ class ParseRobotFile:

        self.get_variables_data()
        self.get_apiutils_path()
        self.get_test_cases()

    def get_variables_data(self):
        string = self.get_substring(initial_string='*** Variables ***\n', final_string='*** ', include=False)
@@ -70,3 +75,70 @@ class ParseRobotFile:
            string = string[:index_end]

        return string

    def get_test_cases(self):
        index_start = self.file_contents.find('*** Test Cases ***')
        string = self.file_contents[index_start+len('*** Test Cases ***')+1:]
        print(string)

        pattern = f'{self.test_suite}_\d+\s.*'
        matches = re.findall(pattern=pattern, string=string)

        indexes = list()
        self.test_case_names = list()
        for match in matches:
            name = match.strip()
            self.test_case_names.append(name)
            indexes.append(string.find(name))

        self.test_cases = dict()
        for i in range(0, len(indexes)-1):
            self.test_cases[self.test_case_names[i]] = string[indexes[i]:indexes[i+1]]

        self.test_cases[self.test_case_names[-1]] = string[indexes[-1]:]
        print()

    def get_checks(self, test_name, apiutils):
        data = Checks()

        test_content = self.test_cases[test_name]

        # Get The lines starting by 'Check'
        checks = list()
        param = dict()
        lines_starting_with_check = re.findall(r'^\s*Check.*', test_content, re.MULTILINE)
        for line in lines_starting_with_check:
            check, param = self.get_data_check(content=test_content, checks=data, line=line.strip())
            result = data.get_checks(checks=check, **param)
            checks.append(check)

        return checks

    def get_data_check(self, content, checks, line):
        content = line.split("    ")
        aux = len(content)
        position_params = checks.args[content[0]]

        if aux == 1:
            # We are in multiline classification of the Check, need to extract the parameter for the next lines
            self.find_attributes_next_line(content=content, name=content[0])
        elif aux > 1:
            # We are in one line definiton
            param = dict()
            for i in range(0, len(position_params['position'])):
                param_key = position_params['params'][i]
                param_position = position_params['position'][i]
                param_value = self.variables[content[param_position]]
                param[param_key] = param_value

                return content[0], param
        else:
            raise Exception("ERROR, line should contain data")

    def find_attributes_next_line(self, content, name):
        index_start = content.find(name)

        aux = content[index_start+len(name)+1:]