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 test_base as base
19 class TestCaseBase(base.TestBase):
21 super(TestCaseBase, self).setUp()
22 self.req_d = testcase_models.TestcaseCreateRequest('vping_1',
25 self.req_e = testcase_models.TestcaseCreateRequest('doctor_1',
28 self.update_d = testcase_models.TestcaseUpdateRequest('vping_1',
31 self.update_e = testcase_models.TestcaseUpdateRequest('doctor_1',
34 self.get_res = testcase_models.Testcase
35 self.list_res = testcase_models.Testcases
36 self.update_res = testcase_models.Testcase
37 self.basePath = '/api/v1/projects/%s/cases'
40 def assert_body(self, case, req=None):
43 self.assertEqual(case.name, req.name)
44 self.assertEqual(case.description, req.description)
45 self.assertEqual(case.url, req.url)
46 self.assertIsNotNone(case._id)
47 self.assertIsNotNone(case.creation_date)
49 def assert_update_body(self, old, new, req=None):
52 self.assertEqual(new.name, req.name)
53 self.assertEqual(new.description, req.description)
54 self.assertEqual(new.url, old.url)
55 self.assertIsNotNone(new._id)
56 self.assertIsNotNone(new.creation_date)
58 def create_project(self):
59 req_p = project_models.ProjectCreateRequest('functest',
61 self.create_help('/api/v1/projects', req_p)
62 self.project = req_p.name
65 return super(TestCaseBase, self).create_d(self.project)
68 return super(TestCaseBase, self).create_e(self.project)
70 def get(self, case=None):
71 return super(TestCaseBase, self).get(self.project, case)
73 def update(self, new=None, case=None):
74 return super(TestCaseBase, self).update(new, self.project, case)
76 def delete(self, case):
77 return super(TestCaseBase, self).delete(self.project, case)
80 class TestCaseCreate(TestCaseBase):
81 def test_noBody(self):
82 (code, body) = self.create(None, 'vping')
83 self.assertEqual(code, httplib.BAD_REQUEST)
85 def test_noProject(self):
86 code, body = self.create(self.req_d, 'noProject')
87 self.assertEqual(code, httplib.FORBIDDEN)
88 self.assertIn(message.not_found_base, body)
90 def test_emptyName(self):
91 req_empty = testcase_models.TestcaseCreateRequest('')
92 (code, body) = self.create(req_empty, self.project)
93 self.assertEqual(code, httplib.BAD_REQUEST)
94 self.assertIn(message.missing('name'), body)
96 def test_noneName(self):
97 req_none = testcase_models.TestcaseCreateRequest(None)
98 (code, body) = self.create(req_none, self.project)
99 self.assertEqual(code, httplib.BAD_REQUEST)
100 self.assertIn(message.missing('name'), body)
102 def test_success(self):
103 code, body = self.create_d()
104 self.assertEqual(code, httplib.OK)
105 self.assert_create_body(body, None, self.project)
107 def test_alreadyExist(self):
109 code, body = self.create_d()
110 self.assertEqual(code, httplib.FORBIDDEN)
111 self.assertIn(message.exist_base, body)
114 class TestCaseGet(TestCaseBase):
115 def test_notExist(self):
116 code, body = self.get('notExist')
117 self.assertEqual(code, httplib.NOT_FOUND)
119 def test_getOne(self):
121 code, body = self.get(self.req_d.name)
122 self.assertEqual(code, httplib.OK)
123 self.assert_body(body)
128 code, body = self.get()
129 for case in body.testcases:
130 if self.req_d.name == case.name:
131 self.assert_body(case)
133 self.assert_body(case, self.req_e)
136 class TestCaseUpdate(TestCaseBase):
137 def test_noBody(self):
138 code, _ = self.update(case='noBody')
139 self.assertEqual(code, httplib.BAD_REQUEST)
141 def test_notFound(self):
142 code, _ = self.update(self.update_e, 'notFound')
143 self.assertEqual(code, httplib.NOT_FOUND)
145 def test_newNameExist(self):
148 code, body = self.update(self.update_e, self.req_d.name)
149 self.assertEqual(code, httplib.FORBIDDEN)
150 self.assertIn(message.exist_base, body)
152 def test_noUpdate(self):
154 code, body = self.update(self.update_d, self.req_d.name)
155 self.assertEqual(code, httplib.FORBIDDEN)
156 self.assertIn(message.no_update(), body)
158 def test_success(self):
160 code, body = self.get(self.req_d.name)
163 code, body = self.update(self.update_e, self.req_d.name)
164 self.assertEqual(code, httplib.OK)
165 self.assertEqual(_id, body._id)
166 self.assert_update_body(self.req_d, body, self.update_e)
168 _, new_body = self.get(self.req_e.name)
169 self.assertEqual(_id, new_body._id)
170 self.assert_update_body(self.req_d, new_body, self.update_e)
172 def test_with_dollar(self):
174 update = copy.deepcopy(self.update_d)
175 update.description = {'2. change': 'dollar change'}
176 code, body = self.update(update, self.req_d.name)
177 self.assertEqual(code, httplib.OK)
180 class TestCaseDelete(TestCaseBase):
181 def test_notFound(self):
182 code, body = self.delete('notFound')
183 self.assertEqual(code, httplib.NOT_FOUND)
185 def test_success(self):
187 code, body = self.delete(self.req_d.name)
188 self.assertEqual(code, httplib.OK)
189 self.assertEqual(body, '')
191 code, body = self.get(self.req_d.name)
192 self.assertEqual(code, httplib.NOT_FOUND)
195 if __name__ == '__main__':