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 ##############################################################################
11 from test_base import TestBase
12 from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest, \
13 Testcase, Testcases, TestcaseUpdateRequest
14 from opnfv_testapi.resources.project_models import ProjectCreateRequest
15 from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
16 HTTP_FORBIDDEN, HTTP_NOT_FOUND
19 class TestCaseBase(TestBase):
21 super(TestCaseBase, self).setUp()
22 self.req_d = TestcaseCreateRequest('vping_1',
25 self.req_e = TestcaseCreateRequest('doctor_1',
28 self.update_d = TestcaseUpdateRequest('vping_1',
31 self.update_e = TestcaseUpdateRequest('doctor_1',
34 self.get_res = Testcase
35 self.list_res = Testcases
36 self.update_res = 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 = ProjectCreateRequest('functest', 'vping-ssh test')
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, HTTP_BAD_REQUEST)
84 def test_noProject(self):
85 code, body = self.create(self.req_d, 'noProject')
86 self.assertEqual(code, HTTP_FORBIDDEN)
87 self.assertIn('Could not find project', body)
89 def test_emptyName(self):
90 req_empty = TestcaseCreateRequest('')
91 (code, body) = self.create(req_empty, self.project)
92 self.assertEqual(code, HTTP_BAD_REQUEST)
93 self.assertIn('name missing', body)
95 def test_noneName(self):
96 req_none = TestcaseCreateRequest(None)
97 (code, body) = self.create(req_none, self.project)
98 self.assertEqual(code, HTTP_BAD_REQUEST)
99 self.assertIn('name missing', body)
101 def test_success(self):
102 code, body = self.create_d()
103 self.assertEqual(code, HTTP_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, HTTP_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, HTTP_NOT_FOUND)
118 def test_getOne(self):
120 code, body = self.get(self.req_d.name)
121 self.assertEqual(code, HTTP_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, HTTP_BAD_REQUEST)
140 def test_notFound(self):
141 code, _ = self.update(self.update_e, 'notFound')
142 self.assertEqual(code, HTTP_NOT_FOUND)
144 def test_newNameExist(self):
147 code, body = self.update(self.update_e, self.req_d.name)
148 self.assertEqual(code, HTTP_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, HTTP_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, HTTP_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)
172 class TestCaseDelete(TestCaseBase):
173 def test_notFound(self):
174 code, body = self.delete('notFound')
175 self.assertEqual(code, HTTP_NOT_FOUND)
177 def test_success(self):
179 code, body = self.delete(self.req_d.name)
180 self.assertEqual(code, HTTP_OK)
181 self.assertEqual(body, '')
183 code, body = self.get(self.req_d.name)
184 self.assertEqual(code, HTTP_NOT_FOUND)
187 if __name__ == '__main__':