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.resources import project_models
14 from opnfv_testapi.resources import testcase_models
15 import test_base as base
18 class TestCaseBase(base.TestBase):
20 super(TestCaseBase, self).setUp()
21 self.req_d = testcase_models.TestcaseCreateRequest('vping_1',
24 self.req_e = testcase_models.TestcaseCreateRequest('doctor_1',
27 self.update_d = testcase_models.TestcaseUpdateRequest('vping_1',
30 self.update_e = testcase_models.TestcaseUpdateRequest('doctor_1',
33 self.get_res = testcase_models.Testcase
34 self.list_res = testcase_models.Testcases
35 self.update_res = testcase_models.Testcase
36 self.basePath = '/api/v1/projects/%s/cases'
39 def assert_body(self, case, req=None):
42 self.assertEqual(case.name, req.name)
43 self.assertEqual(case.description, req.description)
44 self.assertEqual(case.url, req.url)
45 self.assertIsNotNone(case._id)
46 self.assertIsNotNone(case.creation_date)
48 def assert_update_body(self, old, new, req=None):
51 self.assertEqual(new.name, req.name)
52 self.assertEqual(new.description, req.description)
53 self.assertEqual(new.url, old.url)
54 self.assertIsNotNone(new._id)
55 self.assertIsNotNone(new.creation_date)
57 def create_project(self):
58 req_p = project_models.ProjectCreateRequest('functest',
60 self.create_help('/api/v1/projects', req_p)
61 self.project = req_p.name
64 return super(TestCaseBase, self).create_d(self.project)
67 return super(TestCaseBase, self).create_e(self.project)
69 def get(self, case=None):
70 return super(TestCaseBase, self).get(self.project, case)
72 def update(self, new=None, case=None):
73 return super(TestCaseBase, self).update(new, self.project, case)
75 def delete(self, case):
76 return super(TestCaseBase, self).delete(self.project, case)
79 class TestCaseCreate(TestCaseBase):
80 def test_noBody(self):
81 (code, body) = self.create(None, 'vping')
82 self.assertEqual(code, httplib.BAD_REQUEST)
84 def test_noProject(self):
85 code, body = self.create(self.req_d, 'noProject')
86 self.assertEqual(code, httplib.FORBIDDEN)
87 self.assertIn('Could not find project', body)
89 def test_emptyName(self):
90 req_empty = testcase_models.TestcaseCreateRequest('')
91 (code, body) = self.create(req_empty, self.project)
92 self.assertEqual(code, httplib.BAD_REQUEST)
93 self.assertIn('name missing', body)
95 def test_noneName(self):
96 req_none = testcase_models.TestcaseCreateRequest(None)
97 (code, body) = self.create(req_none, self.project)
98 self.assertEqual(code, httplib.BAD_REQUEST)
99 self.assertIn('name missing', body)
101 def test_success(self):
102 code, body = self.create_d()
103 self.assertEqual(code, httplib.OK)
104 self.assert_create_body(body, None, self.project)
106 def test_alreadyExist(self):
108 code, body = self.create_d()
109 self.assertEqual(code, httplib.FORBIDDEN)
110 self.assertIn('already exists', body)
113 class TestCaseGet(TestCaseBase):
114 def test_notExist(self):
115 code, body = self.get('notExist')
116 self.assertEqual(code, httplib.NOT_FOUND)
118 def test_getOne(self):
120 code, body = self.get(self.req_d.name)
121 self.assertEqual(code, httplib.OK)
122 self.assert_body(body)
127 code, body = self.get()
128 for case in body.testcases:
129 if self.req_d.name == case.name:
130 self.assert_body(case)
132 self.assert_body(case, self.req_e)
135 class TestCaseUpdate(TestCaseBase):
136 def test_noBody(self):
137 code, _ = self.update(case='noBody')
138 self.assertEqual(code, httplib.BAD_REQUEST)
140 def test_notFound(self):
141 code, _ = self.update(self.update_e, 'notFound')
142 self.assertEqual(code, httplib.NOT_FOUND)
144 def test_newNameExist(self):
147 code, body = self.update(self.update_e, self.req_d.name)
148 self.assertEqual(code, httplib.FORBIDDEN)
149 self.assertIn("already exists", body)
151 def test_noUpdate(self):
153 code, body = self.update(self.update_d, self.req_d.name)
154 self.assertEqual(code, httplib.FORBIDDEN)
155 self.assertIn("Nothing to update", body)
157 def test_success(self):
159 code, body = self.get(self.req_d.name)
162 code, body = self.update(self.update_e, self.req_d.name)
163 self.assertEqual(code, httplib.OK)
164 self.assertEqual(_id, body._id)
165 self.assert_update_body(self.req_d, body, self.update_e)
167 _, new_body = self.get(self.req_e.name)
168 self.assertEqual(_id, new_body._id)
169 self.assert_update_body(self.req_d, new_body, self.update_e)
171 def test_with_dollar(self):
173 update = copy.deepcopy(self.update_d)
174 update.description = {'2. change': 'dollar change'}
175 code, body = self.update(update, self.req_d.name)
176 self.assertEqual(code, httplib.OK)
179 class TestCaseDelete(TestCaseBase):
180 def test_notFound(self):
181 code, body = self.delete('notFound')
182 self.assertEqual(code, httplib.NOT_FOUND)
184 def test_success(self):
186 code, body = self.delete(self.req_d.name)
187 self.assertEqual(code, httplib.OK)
188 self.assertEqual(body, '')
190 code, body = self.get(self.req_d.name)
191 self.assertEqual(code, httplib.NOT_FOUND)
194 if __name__ == '__main__':