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.common import config
16 from opnfv_testapi.resources import models
17 from opnfv_testapi.tests.unit import fake_pymongo
19 config.Config.CONFIG = path.join(path.dirname(__file__),
20 '../../../../etc/config.ini')
23 class TestBase(testing.AsyncHTTPTestCase):
24 headers = {'Content-Type': 'application/json; charset=UTF-8'}
29 self.create_res = models.CreateResponse
32 self.update_res = None
35 self.addCleanup(self._clear)
36 super(TestBase, self).setUp()
39 self.db_patcher.stop()
41 def _patch_server(self):
42 from opnfv_testapi.cmd import server
45 path.join(path.dirname(__file__), path.pardir, 'common/normal.ini')
47 self.db_patcher = mock.patch('opnfv_testapi.cmd.server.get_db',
49 self.db_patcher.start()
56 from opnfv_testapi.cmd import server
57 return server.make_app()
59 def create_d(self, *args):
60 return self.create(self.req_d, *args)
62 def create_e(self, *args):
63 return self.create(self.req_e, *args)
65 def create(self, req=None, *args):
66 return self.create_help(self.basePath, req, *args)
68 def create_help(self, uri, req, *args):
69 if req and not isinstance(req, str) and hasattr(req, 'format'):
71 res = self.fetch(self._update_uri(uri, *args),
76 return self._get_return(res, self.create_res)
79 res = self.fetch(self._get_uri(*args),
84 new_args, num = self._get_valid_args(*args)
86 if num != self._need_arg_num(self.basePath) else self.list_res
87 return self._get_return(res, inner())
89 def query(self, query):
90 res = self.fetch(self._get_query_uri(query),
93 return self._get_return(res, self.list_res)
95 def update(self, new=None, *args):
98 res = self.fetch(self._get_uri(*args),
100 body=json.dumps(new),
101 headers=self.headers)
102 return self._get_return(res, self.update_res)
104 def delete(self, *args):
105 res = self.fetch(self._get_uri(*args),
107 headers=self.headers)
108 return res.code, res.body
111 def _get_valid_args(*args):
112 new_args = tuple(['%s' % arg for arg in args if arg is not None])
113 return new_args, len(new_args)
115 def _need_arg_num(self, uri):
116 return uri.count('%s')
118 def _get_query_uri(self, query):
119 return self.basePath + '?' + query if query else self.basePath
121 def _get_uri(self, *args):
122 return self._update_uri(self.basePath, *args)
124 def _update_uri(self, uri, *args):
126 new_args, num = self._get_valid_args(*args)
127 if num != self._need_arg_num(uri):
130 return r_uri % tuple(['%s' % arg for arg in new_args])
132 def _get_return(self, res, cls):
135 return code, self._get_return_body(code, body, cls)
138 def _get_return_body(code, body, cls):
139 return cls.from_dict(json.loads(body)) if code < 300 and cls else body
141 def assert_href(self, body):
142 self.assertIn(self.basePath, body.href)
144 def assert_create_body(self, body, req=None, *args):
149 if inspect.isclass(req):
150 resource_name = req.name
151 elif isinstance(req, dict):
152 resource_name = req['name']
153 elif isinstance(req, str):
154 resource_name = json.loads(req)['name']
155 new_args = args + tuple([resource_name])
156 self.assertIn(self._get_uri(*new_args), body.href)
160 fake_pymongo.pods.clear()
161 fake_pymongo.projects.clear()
162 fake_pymongo.testcases.clear()
163 fake_pymongo.results.clear()
164 fake_pymongo.scenarios.clear()