File size: 4,784 Bytes
6c50d1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json

from flex.core import validate as flex_validate


class ViolationType:
    SCHEMA_VIOLATION = "SCHEMA_VIOLATION"
    STATUS_CODE = "STATUS_CODE"
    SCHEMA_MISSING = "SCHEMA_MISSING"


class Violation:
    def __init__(self, method, api_response, violate_type):
        self.method = method
        self.method_name = method.method_name
        self.method_path = method.method_path
        self.api_response = api_response
        self.violate_type = violate_type
        self.schema_violations = None

    def __str__(self):
        str_result = f'Request ID:{self.api_response.request_id}, Method: {self.method_name}, Method Path: {self.method_path},' \
                     f' Status Code:{self.api_response.status_code}, Violation:{self.violate_type} ,'
        if self.violate_type == ViolationType.SCHEMA_VIOLATION:
            str_result += f'Violation Messages: {self.schema_violations} ,'
        str_result += f'Request Content: {self.api_response.content}'
        return str_result

    def signature(self):
        return f'{self.api_response.api_name}-{self.violate_type}-{self.api_response.status_code}'

    def json(self):
        result = {
            "requestID": self.api_response.request_id,
            "method": self.method_name,
            "methodPath": self.method_path,
            "statusCode": self.api_response.status_code,
            "violation": self.violate_type,
            "requestContent": self.api_response.content
        }
        if self.violate_type == ViolationType.SCHEMA_VIOLATION:
            result["violationMessage"] = self.schema_violations
        return result


class Reponse:
    def __init__(self, body):
        self.request_id = body["resquestId"]
        self.method = body["method"]
        self.api_name = body["apiName"]
        self.url = body["url"]
        self.status_code = body["statusCode"]
        self.content = None
        if body.__contains__("content"):
            try:
                self.content = json.loads(body["content"])
            except:
                self.content = body["content"]
        self.response = body


def wrap_response(response):
    return Reponse(response)


def resolve_response(data):
    responses = data
    result = []
    for resp in responses:
        result.append(wrap_response(resp))
    return result


def find_method_by_response(apis, response):
    api_name = response.api_name
    for api in apis:
        for method in api.methods:
            if method.method_name == api_name:
                return method
    print(api_name, "not found ")
    assert False


def validate_nominal(method, response):
    results = []
    # FIXME: bypass status 0 in huawei
    if response.status_code == 0:
        return results
    if response.status_code >= 500:
        results.append(Violation(method, response, ViolationType.STATUS_CODE))
    if not method.response_parameter.__contains__(str(response.status_code)):
        results.append(Violation(method, response, ViolationType.SCHEMA_MISSING))
    if method.response_parameter.__contains__(str(response.status_code)):
        schema = method.response_parameter[str(response.status_code)].schema
        try:
            flex_validate(schema, response.content)
        except Exception as ex:
            if schema != None:
                violation = Violation(method, response, ViolationType.SCHEMA_VIOLATION)
                violation.schema_violations = str(ex)
                results.append(violation)
    return results


def validate_non_nominal(method, response):
    results = []
    # FIXME: bypass status 0 in huawei
    if response.status_code == 0:
        return results
    if response.status_code >= 500 and not method.response_parameter.__contains__(str(response.status_code)):
        results.append(Violation(method, response, ViolationType.STATUS_CODE))
    if not method.response_parameter.__contains__(str(response.status_code)):
        results.append(Violation(method, response, ViolationType.SCHEMA_MISSING))
    if method.response_parameter.__contains__(str(response.status_code)):
        schema = method.response_parameter[str(response.status_code)].schema
        try:
            flex_validate(schema, response.content)
        except Exception as ex:
            violation = Violation(method.method_name, response, ViolationType.SCHEMA_VIOLATION)
            violation.schema_violations = str(ex)


def validate(resp_data, apis, nominal=True):
    responses = resolve_response(resp_data)
    result = []
    for response in responses:
        method = find_method_by_response(apis, response)
        if nominal:
            result.extend(validate_nominal(method, response))
        else:
            result.extend(validate_non_nominal(method, response))
    return result