a145c00dacd10a467dc289bfb788eb51eddec7dd
[releng.git] / utils / test / result_collection_api / 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 unittest
10
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
17
18
19 class TestCaseBase(TestBase):
20     def setUp(self):
21         super(TestCaseBase, self).setUp()
22         self.req_d = TestcaseCreateRequest('vping_1',
23                                            '/cases/vping_1',
24                                            'vping-ssh test')
25         self.req_e = TestcaseCreateRequest('doctor_1',
26                                            '/cases/doctor_1',
27                                            'create doctor')
28         self.update_d = TestcaseUpdateRequest('vping_1',
29                                               'vping-ssh test',
30                                               'functest')
31         self.update_e = TestcaseUpdateRequest('doctor_1',
32                                               'create doctor',
33                                               'functest')
34         self.get_res = Testcase
35         self.list_res = Testcases
36         self.update_res = 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 = ProjectCreateRequest('functest', '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, HTTP_BAD_REQUEST)
83
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)
88
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)
94
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)
100
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)
105
106     def test_alreadyExist(self):
107         self.create_d()
108         code, body = self.create_d()
109         self.assertEqual(code, 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, 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, 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, HTTP_BAD_REQUEST)
139
140     def test_notFound(self):
141         code, _ = self.update(self.update_e, 'notFound')
142         self.assertEqual(code, 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, 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, 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, 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
172 class TestCaseDelete(TestCaseBase):
173     def test_notFound(self):
174         code, body = self.delete('notFound')
175         self.assertEqual(code, HTTP_NOT_FOUND)
176
177     def test_success(self):
178         self.create_d()
179         code, body = self.delete(self.req_d.name)
180         self.assertEqual(code, HTTP_OK)
181         self.assertEqual(body, '')
182
183         code, body = self.get(self.req_d.name)
184         self.assertEqual(code, HTTP_NOT_FOUND)
185
186
187 if __name__ == '__main__':
188     unittest.main()