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