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)
Example #2
0
class TestApiSwaggerParaHandler(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'
    field_info_mock = mock.Mock()
    field_info_type = mock.Mock()
    field_info_type.category = 'USER_DEFINED'
    field_info_type.user_defined_type = user_defined_type_mock
    field_info_mock.type = field_info_type
    field_info_mock.documentation = 'fieldInfoMockDescription'
    field_info_mock.name = 'fieldInfoMockName'
    field_info_mock.metadata = {}
    structure_info_mock = mock.Mock()
    structure_info_mock.fields = [field_info_mock]
    structure_dict = {
        'com.vmware.package.mock': structure_info_mock
    }
    enum_dict = {}
    api_swagger_parahandler = ApiSwaggerParaHandler()

    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' : { 
                'description' : 'typeObjectMockDescription',
                'type': 'MockType'
            },
            'ComVmwarePackageMock': {
                'description': 'typeObjectMockDescription',
                'type': 'MockType'
            }
        }
        
        parameter_obj_actual = self.api_swagger_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': True,
            'in': 'path',
            'name': 'fieldInfoMockName',
            'description': '{ 1. typeObjectMockDescription }, { 2. fieldInfoMockDescription }',
            'type': 'MockType'
        }                        
        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 = {
            'com.vmware.package.mock': {},
            'ComVmwarePackageMock': {}
        }
        body_param_list = [self.field_info_mock]
        parameter_obj_actual = self.api_swagger_parahandler.wrap_body_params('com.vmware.package.mock', 'mockOperationName', 
                                                                           body_param_list, type_dict, self.structure_dict, 
                                                                           self.enum_dict, False)         
        parameter_obj_expected = {
            'in': 'body',
            'name': 'request_body',
            'schema': {'$ref': '#/definitions/ComVmwarePackageMockMockOperationName'}
        }
        self.assertEqual(parameter_obj_expected, parameter_obj_actual)

        # validate parameter object by creating json wrappers around body object
        element_value_mock = mock.Mock()
        element_value_mock.string_value = 'elementMockName'
        metadata_mock = mock.Mock()
        metadata_mock.elements = {"name": element_value_mock}

        self.field_info_mock.metadata = {"BodyField": metadata_mock}
        parameter_obj_actual = self.api_swagger_parahandler.wrap_body_params('com.vmware.package.mock', 'mockOperationName',
                                                                           body_param_list, type_dict, self.structure_dict,
                                                                           self.enum_dict, False)
        parameter_obj_expected = {
            'in': 'body',
            'name': 'request_body',
            'required': True,
            'schema': {'$ref': '#/definitions/ComVmwarePackageMockMockOperationName'}
        }
        self.assertEqual(parameter_obj_expected, parameter_obj_actual)
    
    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' : {  #type_ref
                'properties': { 
                    'property-name': {   #property_value
                        'type': 'array',
                        'items': {
                            '$ref': '#/definitions/com.vmware.package.mock.items'
                        },
                        'description' : 'mock property description'
                    }
                }
            },
            'com.vmware.package.mock.items': {
                'type': 'string',
                'description': 'some mock description'
            },
            'ComVmwarePackageMock': {  # type_ref
                'properties': {
                    'property-name': {  # property_value
                        'type': 'array',
                        'items': {
                            '$ref': '#/definitions/com.vmware.package.mock.items'
                        },
                        'description': 'mock property description'
                    }
                }
            },
            'ComVmwarePackageMockItems': {
                'type': 'string',
                'description': 'some mock description'
            }
        }
        structure_dict = {}
        par_array_actual = self.api_swagger_parahandler.flatten_query_param_spec(query_info_mock, type_dict, 
                                                        structure_dict, self.enum_dict, False)
        par_array_expected = [
            {
                'in':'query',
                'name': 'property-name',
                'type': 'array',
                'collectionFormat': 'multi',
                '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' : None,
            'ComVmwarePackageMock': None
        }
        par_array_actual = self.api_swagger_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' : {  #type_ref
                'properties': { 
                    'property-name': {   #property_value
                        '$ref': '#/definitions/com.vmware.package.mock.property'
                    },
                    'property-name-mock': {}
                },
                'required': ['property-name-mock']
            },
            'com.vmware.package.mock.property': { #prop_obj
                'type': 'object',
                'enum': ['enum-value-1, enum-value-2'],
                'description': 'mock property description'

            },
            'ComVmwarePackageMock': {  # type_ref
                'properties': {
                    'property-name': {  # property_value
                        '$ref': '#/definitions/com.vmware.package.mock.property'
                    },
                    'property-name-mock': {}
                },
                'required': ['property-name-mock']
            },
            'ComVmwarePackageMockProperty': {  # prop_obj
                'type': 'object',
                'enum': ['enum-value-1, enum-value-2'],
                'description': 'mock property description'

            }
        }
        par_array_actual = self.api_swagger_parahandler.flatten_query_param_spec(query_info_mock, type_dict, 
                                                        structure_dict, self.enum_dict, False)
        par_array_expected = [
            {
                'in':'query',
                'name': 'property-name',
                'type': 'string',
                'enum': ['enum-value-1, enum-value-2'], 
                'description': 'mock property description',
                'required': False
            },
            {
                'in': 'query',
                'name': 'property-name-mock',
                'required': True
            }
        ]
        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' : {  #type_ref
                'description' : 'mock description',
                'type': 'string',
                'enum': ['enum-1', 'enum-2']
            },
            'ComVmwarePackageMock': {  # type_ref
                'description': 'mock description',
                'type': 'string',
                'enum': ['enum-1', 'enum-2']
            }
        }
        par_array_actual = self.api_swagger_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',
                'type': 'string',
                'enum': ['enum-1', 'enum-2'], 
                'required': True
            }
        ]
        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_swagger_parahandler.flatten_query_param_spec(query_info_mock, type_dict, 
                                                        structure_dict, self.enum_dict, False)
        par_array_expected = [
            {
                'type': 'string',
                'in':'query',
                'name': 'QueryParameterMockName',
                'description': 'QueryMockDescription',
                'required': True
            }
        ]
        self.assertEqual(par_array_expected, par_array_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)