c0494db5d32330992ee55027194803f47d809763
[releng.git] / utils / test / testapi / opnfv_testapi / tests / unit / test_testcase.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 copy
10 import unittest
11
12 from opnfv_testapi.common import constants
13 from opnfv_testapi.resources import project_models
14 from opnfv_testapi.resources import testcase_models
15 import test_base as base
16
17
18 class TestCaseBase(base.TestBase):
19     def setUp(self):
20         super(TestCaseBase, self).setUp()
21         self.req_d = testcase_models.TestcaseCreateRequest('vping_1',
22                                                            '/cases/vping_1',
23                                                            'vping-ssh test')
24         self.req_e = testcase_models.TestcaseCreateRequest('doctor_1',
25                                                            '/cases/doctor_1',
26                                                            'create doctor')
27         self.update_d = testcase_models.TestcaseUpdateRequest('vping_1',
28                                                               'vping-ssh test',
29                                                               'functest')
30         self.update_e = testcase_models.TestcaseUpdateRequest('doctor_1',
31                                                               'create doctor',
32                                                               'functest')
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'
37         self.create_project()
38
39     def assert_body(self, case, req=None):
40         if not req:
41             req = self.req_d
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)
47
48     def assert_update_body(self, old, new, req=None):
49         if not req:
50             req = self.req_d
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)
56
57     def create_project(self):
58         req_p = project_models.ProjectCreateRequest('functest',
59                                                     'vping-ssh test')
60         self.create_help('/api/v1/projects', req_p)
61         self.project = req_p.name
62
63     def create_d(self):
64         return super(TestCaseBase, self).create_d(self.project)
65
66     def create_e(self):
67         return super(TestCaseBase, self).create_e(self.project)
68
69     def get(self, case=None):
70         return super(TestCaseBase, self).get(self.project, case)
71
72     def update(self, new=None, case=None):
73         return super(TestCaseBase, self).update(new, self.project, case)
74
75     def delete(self, case):
76         return super(TestCaseBase, self).delete(self.project, case)
77
78
79 class TestCaseCreate(TestCaseBase):
80     def test_noBody(self):
81         (code, body) = self.create(None, 'vping')
82         self.assertEqual(code, constants.HTTP_BAD_REQUEST)
83
84     def test_noProject(self):
85         code, body = self.create(self.req_d, 'noProject')
86         self.assertEqual(code, constants.HTTP_FORBIDDEN)
87         self.assertIn('Could not find project', body)
88
89     def test_emptyName(self):
90         req_empty = testcase_models.TestcaseCreateRequest('')
91         (code, body) = self.create(req_empty, self.project)
92         self.assertEqual(code, constants.HTTP_BAD_REQUEST)
93         self.assertIn('name missing', body)
94
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, constants.HTTP_BAD_REQUEST)
99         self.assertIn('name missing', body)
100
101     def test_success(self):
102         code, body = self.create_d()
103         self.assertEqual(code, constants.HTTP_OK)
104         self.assert_create_body(body, None, self.project)
105
106     def test_alreadyExist(self):
107         self.create_d()
108         code, body = self.create_d()
109         self.assertEqual(code, constants.HTTP_FORBIDDEN)
110         self.assertIn('already exists', body)
111
112
113 class TestCaseGet(TestCaseBase):
114     def test_notExist(self):
115         code, body = self.get('notExist')
116         self.assertEqual(code, constants.HTTP_NOT_FOUND)
117
118     def test_getOne(self):
119         self.create_d()
120         code, body = self.get(self.req_d.name)
121         self.assertEqual(code, constants.HTTP_OK)
122         self.assert_body(body)
123
124     def test_list(self):
125         self.create_d()
126         self.create_e()
127         code, body = self.get()
128         for case in body.testcases:
129             if self.req_d.name == case.name:
130                 self.assert_body(case)
131             else:
132                 self.assert_body(case, self.req_e)
133
134
135 class TestCaseUpdate(TestCaseBase):
136     def test_noBody(self):
137         code, _ = self.update(case='noBody')
138         self.assertEqual(code, constants.HTTP_BAD_REQUEST)
139
140     def test_notFound(self):
141         code, _ = self.update(self.update_e, 'notFound')
142         self.assertEqual(code, constants.HTTP_NOT_FOUND)
143
144     def test_newNameExist(self):
145         self.create_d()
146         self.create_e()
147         code, body = self.update(self.update_e, self.req_d.name)
148         self.assertEqual(code, constants.HTTP_FORBIDDEN)
149         self.assertIn("already exists", body)
150
151     def test_noUpdate(self):
152         self.create_d()
153         code, body = self.update(self.update_d, self.req_d.name)
154         self.assertEqual(code, constants.HTTP_FORBIDDEN)
155         self.assertIn("Nothing to update", body)
156
157     def test_success(self):
158         self.create_d()
159         code, body = self.get(self.req_d.name)
160         _id = body._id
161
162         code, body = self.update(self.update_e, self.req_d.name)
163         self.assertEqual(code, constants.HTTP_OK)
164         self.assertEqual(_id, body._id)
165         self.assert_update_body(self.req_d, body, self.update_e)
166
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)
170
171     def test_with_dollar(self):
172         self.create_d()
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, constants.HTTP_OK)
177
178
179 class TestCaseDelete(TestCaseBase):
180     def test_notFound(self):
181         code, body = self.delete('notFound')
182         self.assertEqual(code, constants.HTTP_NOT_FOUND)
183
184     def test_success(self):
185         self.create_d()
186         code, body = self.delete(self.req_d.name)
187         self.assertEqual(code, constants.HTTP_OK)
188         self.assertEqual(body, '')
189
190         code, body = self.get(self.req_d.name)
191         self.assertEqual(code, constants.HTTP_NOT_FOUND)
192
193
194 if __name__ == '__main__':
195     unittest.main()