Merge "Do not run docker with -t option"
[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.common import message
12 from opnfv_testapi.resources import handlers
13 from opnfv_testapi.resources import testcase_models
14 from opnfv_testapi.tornado_swagger import swagger
15
16
17 class GenericTestcaseHandler(handlers.GenericApiHandler):
18     def __init__(self, application, request, **kwargs):
19         super(GenericTestcaseHandler, self).__init__(application,
20                                                      request,
21                                                      **kwargs)
22         self.table = self.db_testcases
23         self.table_cls = testcase_models.Testcase
24
25
26 class TestcaseCLHandler(GenericTestcaseHandler):
27     @swagger.operation(nickname="listAllTestCases")
28     def get(self, project_name):
29         """
30             @description: list all testcases of a project by project_name
31             @return 200: return all testcases of this project,
32                          empty list is no testcase exist in this project
33             @rtype: L{TestCases}
34         """
35         query = dict()
36         query['project_name'] = project_name
37         self._list(query)
38
39     @swagger.operation(nickname="createTestCase")
40     def post(self, project_name):
41         """
42             @description: create a testcase of a project by project_name
43             @param body: testcase to be created
44             @type body: L{TestcaseCreateRequest}
45             @in body: body
46             @rtype: L{CreateResponse}
47             @return 200: testcase is created in this project.
48             @raise 403: project not exist
49                         or testcase already exists in this project
50             @raise 400: body or name not provided
51         """
52         def p_query(data):
53             return {'name': data.project_name}
54
55         def tc_query(data):
56             return {
57                 'project_name': data.project_name,
58                 'name': data.name
59             }
60
61         def p_error(data):
62             return httplib.FORBIDDEN, message.not_found('project',
63                                                         data.project_name)
64
65         def tc_error(data):
66             return httplib.FORBIDDEN, message.exist('testcase', data.name)
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)