def test_process_delete_request(self):
        # case 1: create parameter array using field information of parameters for
        # delete operation in swagger 2.0
        params = [self.field_info_mock_1, self.field_info_mock_4]
        spec = ApiSwaggerParaHandler()
        par_array_actual, new_url_actual = self.api_meta2spec.process_delete_request(
            self.url, params, self.type_dict, {}, {}, False, spec)
        par_array_expected = [{
            'required': True,
            'in': 'path',
            'name': 'mock_name_1',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 1 }',
            'type': 'string',
            'enum': ['enum-1', 'enum-2']
        }, {
            'required': True,
            'in': 'query',
            'name': 'mock_name_4',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 4 }',
            'type': 'string',
            'enum': ['enum-1', 'enum-2']
        }]
        self.assertEqual(par_array_expected, par_array_actual)
        self.assertEqual(self.new_url_expected, new_url_actual)

        # case 2: create parameter array using field information of parameters for
        # delete operation in openAPI 3.0
        spec = ApiOpenapiParaHandler()
        par_array_actual, new_url_actual = self.api_meta2spec.process_delete_request(
            self.url, params, self.type_dict, {}, {}, False, spec)
        par_array_expected = [{
            'required': True,
            'in': 'path',
            'name': 'mock_name_1',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 1 }',
            'enum': ['enum-1', 'enum-2'],
            'schema': {
                'type': 'string'
            }
        }, {
            'required': True,
            'in': 'query',
            'name': 'mock_name_4',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 4 }',
            'enum': ['enum-1', 'enum-2'],
            'schema': {
                'type': 'string'
            }
        }]
        self.assertEqual(par_array_expected, par_array_actual)
        self.assertEqual(self.new_url_expected, new_url_actual)
    def test_process_put_post_patch_request(self):
        # case 1: create parameter array using field information of parameters for
        # put, post, patch operations in swagger 2.0
        params = [
            self.field_info_mock_1, self.field_info_mock_2,
            self.field_info_mock_3, self.field_info_mock_4
        ]
        '''
        Below is the structure of the params list
        params = [
            FieldInfo(name = 'mock_name_1', documentation = 'mock documentation for field info 1',
            type = Type(category = 'USER_DEFINED', 
            user_defined_type = UserDefinedType( resource_type = 'com.vmware.vapi.structure', resource_id = 'com.vmware.package.mock')
                ), metadata = {
                    'PathVariable' : ElementMap( elements = {
                        'value': ElementValue(string_value = 'mock')
                    })
            ),
            FieldInfo(name = 'mock_name_2', documentation = 'mock documentation for field info 2',
            type = Type(category = 'USER_DEFINED', 
            user_defined_type = UserDefinedType( resource_type = 'com.vmware.vapi.structure', resource_id = 'com.vmware.package.mock')
                ), metadata = {
                    'Body' : ElementMap( elements = {
                        'value': ElementValue(string_value = 'mock')
                    })
            ),
            FieldInfo(name = 'mock_name_3', documentation = 'mock documentation for field info 3',
            type = Type(category = 'USER_DEFINED', 
            user_defined_type = UserDefinedType( resource_type = 'com.vmware.vapi.structure', resource_id = 'com.vmware.package.mock')
                ), metadata = {
                    'Query' : ElementMap( elements = {
                        'value': ElementValue(string_value = 'mock')
                    })
            ),
            FieldInfo(name = 'mock_name_4', documentation = 'mock documentation for field info 4',
            type = Type(category = 'USER_DEFINED', 
            user_defined_type = UserDefinedType( resource_type = 'com.vmware.vapi.structure', resource_id = 'com.vmware.package.mock')
                ), metadata = {
                    'metadata_key' : ElementMap( elements = {
                        'value': ElementValue(string_value = 'mock')
                    })
            )
        ]
        '''
        spec = ApiSwaggerParaHandler()
        par_array_actual, new_url_actual = self.api_meta2spec.process_put_post_patch_request(
            self.url, 'com.vmware.package.mock', 'mock_operation_name', params,
            None, self.type_dict, {}, {}, False, spec)
        par_array_expected = [{
            'required': True,
            'in': 'path',
            'name': 'mock_name_1',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 1 }',
            'type': 'string',
            'enum': ['enum-1', 'enum-2']
        }, {
            'in': 'body',
            'name': 'request_body',
            'schema': {
                '$ref': '#/definitions/ComVmwarePackageMockMockOperationName'
            }
        }, {
            'required': True,
            'in': 'query',
            'name': 'mock_name_3',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 3 }',
            'type': 'string',
            'enum': ['enum-1', 'enum-2']
        }, {
            'in': 'query',
            'name': 'mock_name_4',
            'description': 'mock description',
            'type': 'string',
            'enum': ['enum-1', 'enum-2'],
            'required': True
        }]
        self.assertEqual(par_array_expected, par_array_actual)
        self.assertEqual(self.new_url_expected, new_url_actual)

        # case 2: create parameter array using field information of parameters for
        # put, post, patch operations in openAPI 3.0
        spec = ApiOpenapiParaHandler()
        par_array_actual, new_url_actual = self.api_meta2spec.process_put_post_patch_request(
            self.url, 'com.vmware.package.mock', 'mock_operation_name', params,
            None, self.type_dict, {}, {}, False, spec)
        par_array_expected = [{
            'required': True,
            'in': 'path',
            'name': 'mock_name_1',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 1 }',
            'enum': ['enum-1', 'enum-2'],
            'schema': {
                'type': 'string'
            }
        }, {
            '$ref':
            '#/components/requestBodies/ComVmwarePackageMockMockOperationName'
        }, {
            'required': True,
            'in': 'query',
            'name': 'mock_name_3',
            'description':
            '{ 1. mock description }, { 2. mock documentation for field info 3 }',
            'enum': ['enum-1', 'enum-2'],
            'schema': {
                'type': 'string'
            }
        }, {
            'in': 'query',
            'name': 'mock_name_4',
            'description': 'mock description',
            'schema': {
                'description': 'mock description',
                'type': 'string',
                'enum': ['enum-1', 'enum-2'],
            }
        }]
        self.assertEqual(par_array_expected, par_array_actual)
        self.assertEqual(self.new_url_expected, new_url_actual)
示例#3
0
class TestApiOpenapiParaHandler(unittest.TestCase):

    user_defined_type_mock = mock.Mock()
    user_defined_type_mock.resource_type = 'com.vmware.vapi.structure'
    user_defined_type_mock.resource_id = 'com.vmware.package.mock-1'

    generic_instantiation_element_type_mock = mock.Mock()
    generic_instantiation_element_type_mock.category = 'USER_DEFINED'
    generic_instantiation_element_type_mock.user_defined_type = user_defined_type_mock
    generic_instantiation_mock = mock.Mock()
    generic_instantiation_mock.generic_type = 'OPTIONAL'
    generic_instantiation_mock.element_type = generic_instantiation_element_type_mock

    field_info_mock = mock.Mock()
    field_info_type = mock.Mock()
    field_info_type.category = 'GENERIC'
    field_info_type.generic_instantiation = generic_instantiation_mock
    field_info_mock.type = field_info_type
    field_info_mock.documentation = 'Mock Description for Field Info Object'
    field_info_mock.name = 'fieldInfoMockName'
    structure_info_mock = mock.Mock()
    structure_info_mock.fields = [field_info_mock]
    structure_dict = {'com.vmware.package.mock-1': structure_info_mock}
    enum_dict = {}
    api_openapi_parahandler = ApiOpenapiParaHandler()

    def test_convert_field_info_to_swagger_parameter(self):
        # generic construction of parameter object (dictionary) using field info of path and query parameters
        type_dict = {
            'com.vmware.package.mock-1': {
                'description': 'Mock Description for Type Object',
                'type': 'Mock-Type'
            },
            'ComVmwarePackageMock-1': {
                'description': 'Mock Description for Type Object',
                'type': 'Mock-Type'
            }
        }
        parameter_obj_actual = self.api_openapi_parahandler.convert_field_info_to_swagger_parameter(
            'path', self.field_info_mock, type_dict, self.structure_dict,
            self.enum_dict, False)
        parameter_obj_expected = {
            'required': False,
            'in': 'path',
            'name': 'fieldInfoMockName',
            'description':
            '{ 1. Mock Description for Type Object }, { 2. Mock Description for Field Info Object }',
            'schema': {
                'type': 'Mock-Type'
            }
        }
        self.assertEqual(parameter_obj_expected, parameter_obj_actual)

    def test_wrap_body_params(self):
        # validate parameter object by creating json wrappers around body object
        type_dict = {'ComVmwarePackageMock-1': {}}
        body_param_list = [self.field_info_mock]
        parameter_obj_actual = self.api_openapi_parahandler.wrap_body_params(
            'com.vmware.package.mock-1', 'mockOperationName', body_param_list,
            type_dict, self.structure_dict, self.enum_dict, False)
        parameter_obj_expected = {
            '$ref':
            '#/components/requestBodies/ComVmwarePackageMock-1MockOperationName'
        }
        self.assertEqual(parameter_obj_expected, parameter_obj_actual)
        type_dict_expected = {
            'ComVmwarePackageMock-1': {},
            'requestBodies': {
                'ComVmwarePackageMock-1MockOperationName': {
                    'content': {
                        'application/json': {
                            'schema': {
                                '$ref':
                                '#/components/schemas/ComVmwarePackageMock-1MockOperationName'
                            }
                        }
                    }
                }
            },
            'ComVmwarePackageMock-1MockOperationName': {
                'required': False,
                '$ref': '#/components/schemas/ComVmwarePackageMock-1',
                'description': 'Mock Description for Field Info Object'
            }
        }
        self.assertEqual(type_dict, type_dict_expected)

    def test_flatten_query_param_spec(self):
        # case 1: parameter object takes reference from type_dict key
        # case 1.1: type dict reference value contains properties
        # case 1.1.1: property value inside type_dict is defined in-place
        query_info_mock = mock.Mock()
        query_info_type = mock.Mock()
        query_info_type.category = 'USER_DEFINED'
        query_info_type.user_defined_type = self.user_defined_type_mock
        query_info_mock.type = query_info_type
        query_info_mock.documentation = 'QueryMockDescription'
        query_info_mock.name = 'QueryParameterMockName'
        type_dict = {
            'com.vmware.package.mock-1': {  #type_ref
                'properties': {
                    'property-name': {  #property_value
                        'type': 'array',
                        'items': {
                            '$ref':
                            '#/components/schemas/com.vmware.package.mock.items'
                        },
                        'description': 'mock property description'
                    }
                }
            },
            'com.vmware.package.mock.items': {
                'type': 'string',
                'description': 'some mock description'
            },
            'ComVmwarePackageMock-1': {  # type_ref
                'properties': {
                    'property-name': {  # property_value
                        'type': 'array',
                        'items': {
                            '$ref':
                            '#/components/schemas/com.vmware.package.mock.items'
                        },
                        'description': 'mock property description'
                    }
                }
            },
            'ComVmwarePackageItems': {
                'type': 'string',
                'description': 'some mock description'
            }
        }
        structure_dict = {}
        par_array_actual = self.api_openapi_parahandler.flatten_query_param_spec(
            query_info_mock, type_dict, structure_dict, self.enum_dict, False)
        par_array_expected = [{
            'in': 'query',
            'name': 'property-name',
            'schema': {
                'type': 'array',
                'items': {
                    'type': 'string'
                }
            },
            'description': 'mock property description'
        }]
        self.assertEqual(par_array_expected, par_array_actual)

        # type reference dictionary is empty
        type_dict = {'com.vmware.package.mock-1': None}
        par_array_actual = self.api_openapi_parahandler.flatten_query_param_spec(
            query_info_mock, type_dict, structure_dict, self.enum_dict, False)
        par_array_expected = None
        self.assertEqual(par_array_expected, par_array_actual)

        # case 1.1.2: property value is referenced from type_dict
        type_dict = {
            'com.vmware.package.mock-1': {  #type_ref
                'properties': {
                    'property-name': {  #property_value
                        '$ref':
                        '#/components/schemas/com.vmware.package.mock.property'
                    },
                    'property-name-mock': {}
                },
                'required': ['property-name']
            },
            'com.vmware.package.mock.property': {  #prop_obj
                'type': 'object',
                'enum': ['enum-value-1, enum-value-2'],
                'description': 'mock property description'
            },
            'ComVmwarePackageMock-1': {  #type_ref
                'properties': {
                    'property-name': {  #property_value
                        '$ref':
                        '#/components/schemas/com.vmware.package.mock.property'
                    },
                    'property-name-mock': {}
                },
                'required': ['property-name']
            },
            'ComVmwarePackageMockProperty': {  #prop_obj
                'type': 'object',
                'enum': ['enum-value-1, enum-value-2'],
                'description': 'mock property description'
            }
        }
        par_array_actual = self.api_openapi_parahandler.flatten_query_param_spec(
            query_info_mock, type_dict, structure_dict, self.enum_dict, False)
        par_array_expected = [{
            'in': 'query',
            'name': 'property-name',
            'schema': {
                'type': 'object',
                'enum': ['enum-value-1, enum-value-2'],
                'description': 'mock property description'
            },
            'required': True
        }, {
            'in': 'query',
            'name': 'property-name-mock',
            'schema': {},
            'required': False
        }]
        self.assertEqual(par_array_expected, par_array_actual)

        # case 1.2: type dict reference value does not contain properties
        type_dict = {
            'com.vmware.package.mock-1': {  #type_ref
                'description': 'mock description',
                'type': 'string',
                'enum': ['enum-1', 'enum-2']
            },
            'ComVmwarePackageMock-1': {  # type_ref
                'description': 'mock description',
                'type': 'string',
                'enum': ['enum-1', 'enum-2']
            }
        }
        par_array_actual = self.api_openapi_parahandler.flatten_query_param_spec(
            query_info_mock, type_dict, structure_dict, self.enum_dict, False)
        par_array_expected = [{
            'in': 'query',
            'name': 'QueryParameterMockName',
            'description': 'mock description',
            'schema': {
                'description': 'mock description',
                'type': 'string',
                'enum': ['enum-1', 'enum-2']
            }
        }]
        self.assertEqual(par_array_expected, par_array_actual)

        # case 2: parameter object does not take reference from type dict key
        query_info_mock = mock.Mock()
        query_info_type = mock.Mock()
        query_info_type.category = 'BUILTIN'
        query_info_type.builtin_type = 'string'
        query_info_mock.type = query_info_type
        query_info_mock.documentation = 'QueryMockDescription'
        query_info_mock.name = 'QueryParameterMockName'
        type_dict = {}
        par_array_actual = self.api_openapi_parahandler.flatten_query_param_spec(
            query_info_mock, type_dict, structure_dict, self.enum_dict, False)
        par_array_expected = [{
            'in': 'query',
            'name': 'QueryParameterMockName',
            'description': 'QueryMockDescription',
            'required': True,
            'schema': {
                'type': 'string'
            }
        }]
        self.assertEqual(par_array_expected, par_array_actual)