def generate_test(url):
    print(('Generate test suite from ' + str(url)))
    try:
        swagger = requests.get(url).text
        data = json.loads(swagger)
        title = data['info']['title']
        host = data['host']
        basepath = data['basePath']
        scheme = data['schemes'][0]
    except:
        exit_with_errror('ERROR: cannot open or parse Swagger URL.')
    print(colored('Title:', 'yellow'), colored(title, 'green'))
    print(colored('Host:', 'yellow'), colored(host, 'green'))
    print(colored('Base Path:', 'yellow'), colored(basepath, 'green'))
    print(colored('Scheme:', 'yellow'), colored(scheme, 'green'))
    base_url = (((str(scheme) + '://') + str(host)) + str(basepath))
    gen_file = (((str(title).replace(' ', '_') + '-') + str(random.randint(1000, 9999))) + '.yaml')
    with io.open(gen_file, encoding='utf-8', mode='w') as f:
        f.write((('Base: ' + ('"%s"' % base_url)) + '\n'))
        f.write('Tests:\n')
        for api_path in data['paths']:
            print(colored('Path:', 'yellow'), colored(api_path, 'green'))
            for path_method in data['paths'][api_path]:
                method = str(path_method).upper()
                if (method == 'PARAMETERS'):
                    continue
                try:
                    description = data['paths'][api_path][path_method]['description']
                except:
                    description = ''
                try:
                    description += data['paths'][api_path][path_method]['summary']
                except:
                    dummy = 1
                try:
                    example = data['paths'][api_path][path_method]['parameters'][0]['schema']['example']
                except:
                    example = {}
                try:
                    response = list(data['paths'][api_path][path_method]['responses'].keys())[0]
                except:
                    response = '200'
                try:
                    params = data['paths'][api_path][path_method]['parameters']
                except:
                    params = ''
                if (response == 'default'):
                    response = '200'
                query_parameters = []
                params_str = ''
                params_num = 0
                for p in params:
                    if (str(p['in']).upper() == 'QUERY'):
                        query_parameters.append(str(p['name']))
                        if (params_num == 0):
                            params_str += (str(p['name']) + '=XXX')
                        else:
                            params_str += (('&' + str(p['name'])) + '=XXX')
                        params_num += 1
                print(('\t' + colored('Method:', 'magenta')), colored(method, 'cyan'))
                print(('\t' + colored('Description:', 'magenta')), colored(description, 'cyan'))
                print(('\t' + colored('Query Parameters:', 'magenta')), colored(query_parameters, 'cyan'))
                print(('\t' + colored('Expected Response:', 'magenta')), colored(response, 'cyan'))
                if (params_str == ''):
                    out_path = api_path
                else:
                    out_path = ((api_path + '?') + params_str)
                if (description == ''):
                    f.write((' # %s\n' % method))
                else:
                    f.write((' # %s - %s\n' % (method, description)))
                f.write((' - path: "%s"\n' % out_path))
                f.write(('   method: "%s"\n' % method))
                f.write('   headers: ""\n')
                if ((method == 'POST') or (method == 'PUT')):
                    f.write('   content-type: "application/json"\n')
                    f.write(('   body: %s\n' % str(example).replace("'", '"')))
                f.write(('   expect-response: "%s"\n' % response))
                if (method == 'GET'):
                    f.write('   expect-body: "json"\n')
                f.write('   \n')
    f.close()
    print(colored(('\n%s created' % gen_file), 'green'))
def generate_test(url):
    print(('Generate test suite from ' + str(url)))
    try:
        swagger = requests.get(url).text
        data = json.loads(swagger)
        title = data['info']['title']
        host = data['host']
        basepath = data['basePath']
        scheme = data['schemes'][0]
    except:
        exit_with_errror('ERROR: cannot open or parse Swagger URL.')
    print(colored('Title:', 'yellow'), colored(title, 'green'))
    print(colored('Host:', 'yellow'), colored(host, 'green'))
    print(colored('Base Path:', 'yellow'), colored(basepath, 'green'))
    print(colored('Scheme:', 'yellow'), colored(scheme, 'green'))
    base_url = (((str(scheme) + '://') + str(host)) + str(basepath))
    gen_file = (((str(title).replace(' ', '_') + '-') + str(random.randint(1000, 9999))) + '.yaml')
    with io.open(gen_file, encoding='utf-8', mode='w') as f:
        f.write((('Base: ' + ('"%s"' % base_url)) + '\n'))
        f.write('Tests:\n')
        for api_path in data['paths']:
            print(colored('Path:', 'yellow'), colored(api_path, 'green'))
            for path_method in data['paths'][api_path]:
                method = str(path_method).upper()
                if (method == 'PARAMETERS'):
                    continue
                try:
                    description = data['paths'][api_path][path_method]['description']
                except:
                    description = 
                try:
                    description += data['paths'][api_path][path_method]['summary']
                except:
                    dummy = 1
                try:
                    example = data['paths'][api_path][path_method]['parameters'][0]['schema']['example']
                except:
                    example = {}
                try:
                    response = list(data['paths'][api_path][path_method]['responses'].keys())[0]
                except:
                    response = '200'
                try:
                    params = data['paths'][api_path][path_method]['parameters']
                except:
                    params = 
                if (response == 'default'):
                    response = '200'
                query_parameters = []
                params_str = 
                params_num = 0
                for p in params:
                    if (str(p['in']).upper() == 'QUERY'):
                        query_parameters.append(str(p['name']))
                        if (params_num == 0):
                            params_str += (str(p['name']) + '=XXX')
                        else:
                            params_str += (('&' + str(p['name'])) + '=XXX')
                        params_num += 1
                print(('\t' + colored('Method:', 'magenta')), colored(method, 'cyan'))
                print(('\t' + colored('Description:', 'magenta')), colored(description, 'cyan'))
                print(('\t' + colored('Query Parameters:', 'magenta')), colored(query_parameters, 'cyan'))
                print(('\t' + colored('Expected Response:', 'magenta')), colored(response, 'cyan'))
                if (params_str == ):
                    out_path = api_path
                else:
                    out_path = ((api_path + '?') + params_str)
                if (description == ):
                    f.write((' # %s\n' % method))
                else:
                    f.write((' # %s - %s\n' % (method, description)))
                f.write((' - path: "%s"\n' % out_path))
                f.write(('   method: "%s"\n' % method))
                f.write('   headers: \n')
                if ((method == 'POST') or (method == 'PUT')):
                    f.write('   content-type: "application/json"\n')
                    f.write(('   body: %s\n' % str(example).replace("'", '"')))
                f.write(('   expect-response: "%s"\n' % response))
                if (method == 'GET'):
                    f.write('   expect-body: "json"\n')
                f.write('   \n')
    f.close()
    print(colored(('\n%s created' % gen_file), 'green'))