Merge "dovetail: conditional step bugfix"
[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 httplib
11 import unittest
12
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
17 from opnfv_testapi.tests.unit import executor
18
19
20 class TestCaseBase(base.TestBase):
21     def setUp(self):
22         super(TestCaseBase, self).setUp()
23         self.req_d = testcase_models.TestcaseCreateRequest('vping_1',
24                                                            '/cases/vping_1',
25                                                            'vping-ssh test')
26         self.req_e = testcase_models.TestcaseCreateRequest('doctor_1',
27                                                            '/cases/doctor_1',
28                                                            'create doctor')
29         self.update_d = testcase_models.TestcaseUpdateRequest('vping_1',
30                                                               'vping-ssh test',
31                                                               'functest')
32         self.update_e = testcase_models.TestcaseUpdateRequest('doctor_1',
33                                                               'create doctor',
34                                                               'functest')
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'
39         self.create_project()
40
41     def assert_body(self, case, req=None):
42         if not req:
43             req = self.req_d
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)
49
50     def assert_update_body(self, old, new, req=None):
51         if not req:
52             req = self.req_d
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)
58
59     def create_project(self):
60         req_p = project_models.ProjectCreateRequest('functest',
61                                                     'vping-ssh test')
62         self.create_help('/api/v1/projects', req_p)
63         self.project = req_p.name
64
65     def create_d(self):
66         return super(TestCaseBase, self).create_d(self.project)
67
68     def create_e(self):
69         return super(TestCaseBase, self).create_e(self.project)
70
71     def get(self, case=None):
72         return super(TestCaseBase, self).get(self.project, case)
73
74     def create(self, req=None, *args):
75         return super(TestCaseBase, self).create(req, self.project)
76
77     def update(self, new=None, case=None):
78         return super(TestCaseBase, self).update(new, self.project, case)
79
80     def delete(self, case):
81         return super(TestCaseBase, self).delete(self.project, case)
82
83
84 class TestCaseCreate(TestCaseBase):
85     @executor.create(httplib.BAD_REQUEST, message.no_body())
86     def test_noBody(self):
87         return None
88
89     @executor.create(httplib.FORBIDDEN, message.not_found_base)
90     def test_noProject(self):
91         self.project = 'noProject'
92         return self.req_d
93
94     @executor.create(httplib.BAD_REQUEST, message.missing('name'))
95     def test_emptyName(self):
96         req_empty = testcase_models.TestcaseCreateRequest('')
97         return req_empty
98
99     @executor.create(httplib.BAD_REQUEST, message.missing('name'))
100     def test_noneName(self):
101         req_none = testcase_models.TestcaseCreateRequest(None)
102         return req_none
103
104     @executor.create(httplib.OK, '_assert_success')
105     def test_success(self):
106         return self.req_d
107
108     def _assert_success(self, body):
109         self.assert_create_body(body, self.req_d, self.project)
110
111     @executor.create(httplib.FORBIDDEN, message.exist_base)
112     def test_alreadyExist(self):
113         self.create_d()
114         return self.req_d
115
116
117 class TestCaseGet(TestCaseBase):
118     def setUp(self):
119         super(TestCaseGet, self).setUp()
120         self.create_d()
121         self.create_e()
122
123     @executor.get(httplib.NOT_FOUND, message.not_found_base)
124     def test_notExist(self):
125         return 'notExist'
126
127     @executor.get(httplib.OK, 'assert_body')
128     def test_getOne(self):
129         return self.req_d.name
130
131     @executor.get(httplib.OK, '_list')
132     def test_list(self):
133         return None
134
135     def _list(self, body):
136         for case in body.testcases:
137             if self.req_d.name == case.name:
138                 self.assert_body(case)
139             else:
140                 self.assert_body(case, self.req_e)
141
142
143 class TestCaseUpdate(TestCaseBase):
144     def setUp(self):
145         super(TestCaseUpdate, self).setUp()
146         self.create_d()
147
148     @executor.update(httplib.BAD_REQUEST, message.no_body())
149     def test_noBody(self):
150         return None, 'noBody'
151
152     @executor.update(httplib.NOT_FOUND, message.not_found_base)
153     def test_notFound(self):
154         return self.update_e, 'notFound'
155
156     @executor.update(httplib.FORBIDDEN, message.exist_base)
157     def test_newNameExist(self):
158         self.create_e()
159         return self.update_e, self.req_d.name
160
161     @executor.update(httplib.FORBIDDEN, message.no_update())
162     def test_noUpdate(self):
163         return self.update_d, self.req_d.name
164
165     @executor.update(httplib.OK, '_update_success')
166     def test_success(self):
167         return self.update_e, self.req_d.name
168
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
174
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)
179
180
181 class TestCaseDelete(TestCaseBase):
182     def setUp(self):
183         super(TestCaseDelete, self).setUp()
184         self.create_d()
185
186     @executor.delete(httplib.NOT_FOUND, message.not_found_base)
187     def test_notFound(self):
188         return 'notFound'
189
190     @executor.delete(httplib.OK, '_delete_success')
191     def test_success(self):
192         return self.req_d.name
193
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)
198
199
200 if __name__ == '__main__':
201     unittest.main()