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 opnfv_testapi.common import message
14 from opnfv_testapi.resources import project_models
15 from opnfv_testapi.resources import testcase_models
16 from opnfv_testapi.tests.unit import executor
17 from opnfv_testapi.tests.unit.resources import test_base as base
20 class TestCaseBase(base.TestBase):
22 super(TestCaseBase, self).setUp()
23 self.req_d = testcase_models.TestcaseCreateRequest('vping_1',
26 self.req_e = testcase_models.TestcaseCreateRequest('doctor_1',
29 self.update_d = testcase_models.TestcaseUpdateRequest('vping_1',
32 self.update_e = testcase_models.TestcaseUpdateRequest('doctor_1',
35 self.get_res = testcase_models.Testcase
36 self.list_res = testcase_models.Testcases
37 self.update_res = testcase_models.Testcase
38 self.basePath = '/api/v1/projects/%s/cases'
41 def assert_body(self, case, req=None):
44 self.assertEqual(case.name, req.name)
45 self.assertEqual(case.description, req.description)
46 self.assertEqual(case.url, req.url)
47 self.assertIsNotNone(case._id)
48 self.assertIsNotNone(case.creation_date)
50 def assert_update_body(self, old, new, req=None):
53 self.assertEqual(new.name, req.name)
54 self.assertEqual(new.description, req.description)
55 self.assertEqual(new.url, old.url)
56 self.assertIsNotNone(new._id)
57 self.assertIsNotNone(new.creation_date)
59 def create_project(self):
60 req_p = project_models.ProjectCreateRequest('functest',
62 self.create_help('/api/v1/projects', req_p)
63 self.project = req_p.name
66 return super(TestCaseBase, self).create_d(self.project)
69 return super(TestCaseBase, self).create_e(self.project)
71 def get(self, case=None):
72 return super(TestCaseBase, self).get(self.project, case)
74 def create(self, req=None, *args):
75 return super(TestCaseBase, self).create(req, self.project)
77 def update(self, new=None, case=None):
78 return super(TestCaseBase, self).update(new, self.project, case)
80 def delete(self, case):
81 return super(TestCaseBase, self).delete(self.project, case)
84 class TestCaseCreate(TestCaseBase):
85 @executor.create(httplib.BAD_REQUEST, message.no_body())
86 def test_noBody(self):
89 @executor.create(httplib.FORBIDDEN, message.not_found_base)
90 def test_noProject(self):
91 self.project = 'noProject'
94 @executor.create(httplib.BAD_REQUEST, message.missing('name'))
95 def test_emptyName(self):
96 req_empty = testcase_models.TestcaseCreateRequest('')
99 @executor.create(httplib.BAD_REQUEST, message.missing('name'))
100 def test_noneName(self):
101 req_none = testcase_models.TestcaseCreateRequest(None)
104 @executor.create(httplib.OK, '_assert_success')
105 def test_success(self):
108 def _assert_success(self, body):
109 self.assert_create_body(body, self.req_d, self.project)
111 @executor.create(httplib.FORBIDDEN, message.exist_base)
112 def test_alreadyExist(self):
117 class TestCaseGet(TestCaseBase):
119 super(TestCaseGet, self).setUp()
123 @executor.get(httplib.NOT_FOUND, message.not_found_base)
124 def test_notExist(self):
127 @executor.get(httplib.OK, 'assert_body')
128 def test_getOne(self):
129 return self.req_d.name
131 @executor.get(httplib.OK, '_list')
135 def _list(self, body):
136 for case in body.testcases:
137 if self.req_d.name == case.name:
138 self.assert_body(case)
140 self.assert_body(case, self.req_e)
143 class TestCaseUpdate(TestCaseBase):
145 super(TestCaseUpdate, self).setUp()
148 @executor.update(httplib.BAD_REQUEST, message.no_body())
149 def test_noBody(self):
150 return None, 'noBody'
152 @executor.update(httplib.NOT_FOUND, message.not_found_base)
153 def test_notFound(self):
154 return self.update_e, 'notFound'
156 @executor.update(httplib.FORBIDDEN, message.exist_base)
157 def test_newNameExist(self):
159 return self.update_e, self.req_d.name
161 @executor.update(httplib.FORBIDDEN, message.no_update())
162 def test_noUpdate(self):
163 return self.update_d, self.req_d.name
165 @executor.update(httplib.OK, '_update_success')
166 def test_success(self):
167 return self.update_e, self.req_d.name
169 @executor.update(httplib.OK, '_update_success')
170 def test_with_dollar(self):
171 update = copy.deepcopy(self.update_d)
172 update.description = {'2. change': 'dollar change'}
173 return update, self.req_d.name
175 def _update_success(self, request, body):
176 self.assert_update_body(self.req_d, body, request)
177 _, new_body = self.get(request.name)
178 self.assert_update_body(self.req_d, new_body, request)
181 class TestCaseDelete(TestCaseBase):
183 super(TestCaseDelete, self).setUp()
186 @executor.delete(httplib.NOT_FOUND, message.not_found_base)
187 def test_notFound(self):
190 @executor.delete(httplib.OK, '_delete_success')
191 def test_success(self):
192 return self.req_d.name
194 def _delete_success(self, body):
195 self.assertEqual(body, '')
196 code, body = self.get(self.req_d.name)
197 self.assertEqual(code, httplib.NOT_FOUND)
200 if __name__ == '__main__':