Apex: enable ha for os-odl_l3-fdio
[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 import test_base as base
17
18
19 class TestCaseBase(base.TestBase):
20     def setUp(self):
21         super(TestCaseBase, self).setUp()
22         self.req_d = testcase_models.TestcaseCreateRequest('vping_1',
23                                                            '/cases/vping_1',
24                                                            'vping-ssh test')
25         self.req_e = testcase_models.TestcaseCreateRequest('doctor_1',
26                                                            '/cases/doctor_1',
27                                                            'create doctor')
28         self.update_d = testcase_models.TestcaseUpdateRequest('vping_1',
29                                                               'vping-ssh test',
30                                                               'functest')
31         self.update_e = testcase_models.TestcaseUpdateRequest('doctor_1',
32                                                               'create doctor',
33                                                               'functest')
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'
38         self.create_project()
39
40     def assert_body(self, case, req=None):
41         if not req:
42             req = self.req_d
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)
48
49     def assert_update_body(self, old, new, req=None):
50         if not req:
51             req = self.req_d
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)
57
58     def create_project(self):
59         req_p = project_models.ProjectCreateRequest('functest',
60                                                     'vping-ssh test')
61         self.create_help('/api/v1/projects', req_p)
62         self.project = req_p.name
63
64     def create_d(self):
65         return super(TestCaseBase, self).create_d(self.project)
66
67     def create_e(self):
68         return super(TestCaseBase, self).create_e(self.project)
69
70     def get(self, case=None):
71         return super(TestCaseBase, self).get(self.project, case)
72
73     def update(self, new=None, case=None):
74         return super(TestCaseBase, self).update(new, self.project, case)
75
76     def delete(self, case):
77         return super(TestCaseBase, self).delete(self.project, case)
78
79
80 class TestCaseCreate(TestCaseBase):
81     def test_noBody(self):
82         (code, body) = self.create(None, 'vping')
83         self.assertEqual(code, httplib.BAD_REQUEST)
84
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)
89
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)
95
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)
101
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)
106
107     def test_alreadyExist(self):
108         self.create_d()
109         code, body = self.create_d()
110         self.assertEqual(code, httplib.FORBIDDEN)
111         self.assertIn(message.exist_base, body)
112
113
114 class TestCaseGet(TestCaseBase):
115     def test_notExist(self):
116         code, body = self.get('notExist')
117         self.assertEqual(code, httplib.NOT_FOUND)
118
119     def test_getOne(self):
120         self.create_d()
121         code, body = self.get(self.req_d.name)
122         self.assertEqual(code, httplib.OK)
123         self.assert_body(body)
124
125     def test_list(self):
126         self.create_d()
127         self.create_e()
128         code, body = self.get()
129         for case in body.testcases:
130             if self.req_d.name == case.name:
131                 self.assert_body(case)
132             else:
133                 self.assert_body(case, self.req_e)
134
135
136 class TestCaseUpdate(TestCaseBase):
137     def test_noBody(self):
138         code, _ = self.update(case='noBody')
139         self.assertEqual(code, httplib.BAD_REQUEST)
140
141     def test_notFound(self):
142         code, _ = self.update(self.update_e, 'notFound')
143         self.assertEqual(code, httplib.NOT_FOUND)
144
145     def test_newNameExist(self):
146         self.create_d()
147         self.create_e()
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)
151
152     def test_noUpdate(self):
153         self.create_d()
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)
157
158     def test_success(self):
159         self.create_d()
160         code, body = self.get(self.req_d.name)
161         _id = body._id
162
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)
167
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)
171
172     def test_with_dollar(self):
173         self.create_d()
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)
178
179
180 class TestCaseDelete(TestCaseBase):
181     def test_notFound(self):
182         code, body = self.delete('notFound')
183         self.assertEqual(code, httplib.NOT_FOUND)
184
185     def test_success(self):
186         self.create_d()
187         code, body = self.delete(self.req_d.name)
188         self.assertEqual(code, httplib.OK)
189         self.assertEqual(body, '')
190
191         code, body = self.get(self.req_d.name)
192         self.assertEqual(code, httplib.NOT_FOUND)
193
194
195 if __name__ == '__main__':
196     unittest.main()