1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation
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 ##############################################################################
13 from tornado import testing
15 from opnfv_testapi.resources import models
16 from opnfv_testapi.tests.unit import fake_pymongo
19 class TestBase(testing.AsyncHTTPTestCase):
20 headers = {'Content-Type': 'application/json; charset=UTF-8'}
25 self.create_res = models.CreateResponse
28 self.update_res = None
31 self.addCleanup(self._clear)
32 super(TestBase, self).setUp()
35 self.db_patcher.stop()
36 self.config_patcher.stop()
38 def _patch_server(self):
40 config = path.join(path.dirname(__file__), '../common/normal.ini')
41 self.config_patcher = mock.patch(
42 'argparse.ArgumentParser.parse_known_args',
43 return_value=(argparse.Namespace(config_file=config), None))
44 self.db_patcher = mock.patch('opnfv_testapi.db.api.DB',
46 self.config_patcher.start()
47 self.db_patcher.start()
49 def set_config_file(self):
50 self.config_file = 'normal.ini'
53 from opnfv_testapi.cmd import server
54 return server.make_app()
56 def create_d(self, *args):
57 return self.create(self.req_d, *args)
59 def create_e(self, *args):
60 return self.create(self.req_e, *args)
62 def create(self, req=None, *args):
63 return self.create_help(self.basePath, req, *args)
65 def create_help(self, uri, req, *args):
66 if req and not isinstance(req, str) and hasattr(req, 'format'):
68 res = self.fetch(self._update_uri(uri, *args),
73 return self._get_return(res, self.create_res)
76 res = self.fetch(self._get_uri(*args),
81 new_args, num = self._get_valid_args(*args)
83 if num != self._need_arg_num(self.basePath) else self.list_res
84 return self._get_return(res, inner())
86 def query(self, query):
87 res = self.fetch(self._get_query_uri(query),
90 return self._get_return(res, self.list_res)
92 def update(self, new=None, *args):
95 res = self.fetch(self._get_uri(*args),
99 return self._get_return(res, self.update_res)
101 def delete(self, *args):
102 res = self.fetch(self._get_uri(*args),
104 headers=self.headers)
105 return res.code, res.body
108 def _get_valid_args(*args):
109 new_args = tuple(['%s' % arg for arg in args if arg is not None])
110 return new_args, len(new_args)
112 def _need_arg_num(self, uri):
113 return uri.count('%s')
115 def _get_query_uri(self, query):
116 return self.basePath + '?' + query if query else self.basePath
118 def _get_uri(self, *args):
119 return self._update_uri(self.basePath, *args)
121 def _update_uri(self, uri, *args):
123 new_args, num = self._get_valid_args(*args)
124 if num != self._need_arg_num(uri):
127 return r_uri % tuple(['%s' % arg for arg in new_args])
129 def _get_return(self, res, cls):
132 return code, self._get_return_body(code, body, cls)
135 def _get_return_body(code, body, cls):
136 return cls.from_dict(json.loads(body)) if code < 300 and cls else body
138 def assert_href(self, body):
139 self.assertIn(self.basePath, body.href)
141 def assert_create_body(self, body, req=None, *args):
146 if inspect.isclass(req):
147 resource_name = req.name
148 elif isinstance(req, dict):
149 resource_name = req['name']
150 elif isinstance(req, str):
151 resource_name = json.loads(req)['name']
152 new_args = args + tuple([resource_name])
153 self.assertIn(self._get_uri(*new_args), body.href)
157 fake_pymongo.pods.clear()
158 fake_pymongo.projects.clear()
159 fake_pymongo.testcases.clear()
160 fake_pymongo.results.clear()
161 fake_pymongo.scenarios.clear()