decouple the mutual-dependence of config.py and server.py
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / resources / test_base.py
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 ##############################################################################
9 import json
10 from os import path
11
12 import mock
13 from tornado import testing
14
15 from opnfv_testapi.resources import models
16 from opnfv_testapi.tests.unit import fake_pymongo
17
18
19 class TestBase(testing.AsyncHTTPTestCase):
20     headers = {'Content-Type': 'application/json; charset=UTF-8'}
21
22     def setUp(self):
23         self._patch_server()
24         self.basePath = ''
25         self.create_res = models.CreateResponse
26         self.get_res = None
27         self.list_res = None
28         self.update_res = None
29         self.req_d = None
30         self.req_e = None
31         self.addCleanup(self._clear)
32         super(TestBase, self).setUp()
33
34     def tearDown(self):
35         self.db_patcher.stop()
36         self.config_patcher.stop()
37
38     def _patch_server(self):
39         import argparse
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.cmd.server.get_db',
45                                      self._fake_pymongo)
46         self.config_patcher.start()
47         self.db_patcher.start()
48
49     def set_config_file(self):
50         self.config_file = 'normal.ini'
51
52     @staticmethod
53     def _fake_pymongo():
54         return fake_pymongo
55
56     def get_app(self):
57         from opnfv_testapi.cmd import server
58         return server.make_app()
59
60     def create_d(self, *args):
61         return self.create(self.req_d, *args)
62
63     def create_e(self, *args):
64         return self.create(self.req_e, *args)
65
66     def create(self, req=None, *args):
67         return self.create_help(self.basePath, req, *args)
68
69     def create_help(self, uri, req, *args):
70         if req and not isinstance(req, str) and hasattr(req, 'format'):
71             req = req.format()
72         res = self.fetch(self._update_uri(uri, *args),
73                          method='POST',
74                          body=json.dumps(req),
75                          headers=self.headers)
76
77         return self._get_return(res, self.create_res)
78
79     def get(self, *args):
80         res = self.fetch(self._get_uri(*args),
81                          method='GET',
82                          headers=self.headers)
83
84         def inner():
85             new_args, num = self._get_valid_args(*args)
86             return self.get_res \
87                 if num != self._need_arg_num(self.basePath) else self.list_res
88         return self._get_return(res, inner())
89
90     def query(self, query):
91         res = self.fetch(self._get_query_uri(query),
92                          method='GET',
93                          headers=self.headers)
94         return self._get_return(res, self.list_res)
95
96     def update(self, new=None, *args):
97         if new:
98             new = new.format()
99         res = self.fetch(self._get_uri(*args),
100                          method='PUT',
101                          body=json.dumps(new),
102                          headers=self.headers)
103         return self._get_return(res, self.update_res)
104
105     def delete(self, *args):
106         res = self.fetch(self._get_uri(*args),
107                          method='DELETE',
108                          headers=self.headers)
109         return res.code, res.body
110
111     @staticmethod
112     def _get_valid_args(*args):
113         new_args = tuple(['%s' % arg for arg in args if arg is not None])
114         return new_args, len(new_args)
115
116     def _need_arg_num(self, uri):
117         return uri.count('%s')
118
119     def _get_query_uri(self, query):
120         return self.basePath + '?' + query if query else self.basePath
121
122     def _get_uri(self, *args):
123         return self._update_uri(self.basePath, *args)
124
125     def _update_uri(self, uri, *args):
126         r_uri = uri
127         new_args, num = self._get_valid_args(*args)
128         if num != self._need_arg_num(uri):
129             r_uri += '/%s'
130
131         return r_uri % tuple(['%s' % arg for arg in new_args])
132
133     def _get_return(self, res, cls):
134         code = res.code
135         body = res.body
136         return code, self._get_return_body(code, body, cls)
137
138     @staticmethod
139     def _get_return_body(code, body, cls):
140         return cls.from_dict(json.loads(body)) if code < 300 and cls else body
141
142     def assert_href(self, body):
143         self.assertIn(self.basePath, body.href)
144
145     def assert_create_body(self, body, req=None, *args):
146         import inspect
147         if not req:
148             req = self.req_d
149         resource_name = ''
150         if inspect.isclass(req):
151             resource_name = req.name
152         elif isinstance(req, dict):
153             resource_name = req['name']
154         elif isinstance(req, str):
155             resource_name = json.loads(req)['name']
156         new_args = args + tuple([resource_name])
157         self.assertIn(self._get_uri(*new_args), body.href)
158
159     @staticmethod
160     def _clear():
161         fake_pymongo.pods.clear()
162         fake_pymongo.projects.clear()
163         fake_pymongo.testcases.clear()
164         fake_pymongo.results.clear()
165         fake_pymongo.scenarios.clear()