replace self-defined http codes with standard definitions
[releng.git] / utils / test / testapi / opnfv_testapi / resources / testcase_handlers.py
1 ##############################################################################
2 # Copyright (c) 2015 Orange
3 # guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
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 httplib
10
11 from opnfv_testapi.resources import handlers
12 from opnfv_testapi.resources import testcase_models
13 from opnfv_testapi.tornado_swagger import swagger
14
15
16 class GenericTestcaseHandler(handlers.GenericApiHandler):
17     def __init__(self, application, request, **kwargs):
18         super(GenericTestcaseHandler, self).__init__(application,
19                                                      request,
20                                                      **kwargs)
21         self.table = self.db_testcases
22         self.table_cls = testcase_models.Testcase
23
24
25 class TestcaseCLHandler(GenericTestcaseHandler):
26     @swagger.operation(nickname="listAllTestCases")
27     def get(self, project_name):
28         """
29             @description: list all testcases of a project by project_name
30             @return 200: return all testcases of this project,
31                          empty list is no testcase exist in this project
32             @rtype: L{TestCases}
33         """
34         query = dict()
35         query['project_name'] = project_name
36         self._list(query)
37
38     @swagger.operation(nickname="createTestCase")
39     def post(self, project_name):
40         """
41             @description: create a testcase of a project by project_name
42             @param body: testcase to be created
43             @type body: L{TestcaseCreateRequest}
44             @in body: body
45             @rtype: L{CreateResponse}
46             @return 200: testcase is created in this project.
47             @raise 403: project not exist
48                         or testcase already exists in this project
49             @raise 400: body or name not provided
50         """
51         def p_query(data):
52             return {'name': data.project_name}
53
54         def tc_query(data):
55             return {
56                 'project_name': data.project_name,
57                 'name': data.name
58             }
59
60         def p_error(data):
61             message = 'Could not find project [{}]'.format(data.project_name)
62             return httplib.FORBIDDEN, message
63
64         def tc_error(data):
65             message = '{} already exists as a testcase in project {}'\
66                 .format(data.name, data.project_name)
67             return httplib.FORBIDDEN, message
68
69         miss_checks = ['name']
70         db_checks = [(self.db_projects, True, p_query, p_error),
71                      (self.db_testcases, False, tc_query, tc_error)]
72         self._create(miss_checks, db_checks, project_name=project_name)
73
74
75 class TestcaseGURHandler(GenericTestcaseHandler):
76     @swagger.operation(nickname='getTestCaseByName')
77     def get(self, project_name, case_name):
78         """
79             @description: get a single testcase
80                             by case_name and project_name
81             @rtype: L{Testcase}
82             @return 200: testcase exist
83             @raise 404: testcase not exist
84         """
85         query = dict()
86         query['project_name'] = project_name
87         query["name"] = case_name
88         self._get_one(query)
89
90     @swagger.operation(nickname="updateTestCaseByName")
91     def put(self, project_name, case_name):
92         """
93             @description: update a single testcase
94                             by project_name and case_name
95             @param body: testcase to be updated
96             @type body: L{TestcaseUpdateRequest}
97             @in body: body
98             @rtype: L{Project}
99             @return 200: update success
100             @raise 404: testcase or project not exist
101             @raise 403: new testcase name already exist in project
102                         or nothing to update
103         """
104         query = {'project_name': project_name, 'name': case_name}
105         db_keys = ['name', 'project_name']
106         self._update(query, db_keys)
107
108     @swagger.operation(nickname='deleteTestCaseByName')
109     def delete(self, project_name, case_name):
110         """
111             @description: delete a testcase by project_name and case_name
112             @return 200: delete success
113             @raise 404: testcase not exist
114         """
115         query = {'project_name': project_name, 'name': case_name}
116         self._delete(query)