1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corp
3 # feng.xiaowei@zte.com.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from concurrent.futures import ThreadPoolExecutor
16 O_get_secure_cookie = (
17 'opnfv_testapi.handlers.base_handlers.GenericApiHandler.get_secure_cookie')
20 def thread_execute(method, *args, **kwargs):
21 with ThreadPoolExecutor(max_workers=2) as executor:
22 result = executor.submit(method, *args, **kwargs)
26 def mock_invalid_lfid():
27 def _mock_invalid_lfid(xstep):
28 def wrap(self, *args, **kwargs):
29 with mock.patch(O_get_secure_cookie) as m_cookie:
30 m_cookie.return_value = 'InvalidUser'
31 return xstep(self, *args, **kwargs)
33 return _mock_invalid_lfid
36 def mock_valid_lfid():
37 def _mock_valid_lfid(xstep):
38 def wrap(self, *args, **kwargs):
39 with mock.patch(O_get_secure_cookie) as m_cookie:
40 m_cookie.return_value = 'ValidUser'
41 return xstep(self, *args, **kwargs)
43 return _mock_valid_lfid
46 def upload(excepted_status, excepted_response):
47 def _upload(create_request):
48 @functools.wraps(create_request)
50 request = create_request(self)
51 status, body = self.upload(request)
52 if excepted_status == httplib.OK:
53 getattr(self, excepted_response)(body)
55 self.assertIn(excepted_response, body)
60 def create(excepted_status, excepted_response):
61 def _create(create_request):
62 @functools.wraps(create_request)
64 request = create_request(self)
65 status, body = self.create(request)
66 if excepted_status == httplib.OK:
67 getattr(self, excepted_response)(body)
69 self.assertIn(excepted_response, body)
74 def get(excepted_status, excepted_response):
75 def _get(get_request):
76 @functools.wraps(get_request)
78 request = get_request(self)
79 status, body = self.get(request)
80 if excepted_status == httplib.OK:
81 getattr(self, excepted_response)(body)
83 self.assertIn(excepted_response, body)
88 def update(excepted_status, excepted_response):
89 def _update(update_request):
90 @functools.wraps(update_request)
92 request, resource = update_request(self)
93 status, body = self.update(request, resource)
94 if excepted_status == httplib.OK:
95 getattr(self, excepted_response)(request, body)
97 self.assertIn(excepted_response, body)
102 def delete(excepted_status, excepted_response):
103 def _delete(delete_request):
104 @functools.wraps(delete_request)
106 request = delete_request(self)
107 if isinstance(request, tuple):
108 status, body = self.delete(request[0], *(request[1]))
110 status, body = self.delete(request)
111 if excepted_status == httplib.OK:
112 getattr(self, excepted_response)(body)
114 self.assertIn(excepted_response, body)
119 def query(excepted_status, excepted_response, number=0):
120 def _query(get_request):
121 @functools.wraps(get_request)
123 request = get_request(self)
124 status, body = self.query(request)
125 if excepted_status == httplib.OK:
126 getattr(self, excepted_response)(body, number)
128 self.assertIn(excepted_response, body)