Merge "Remove compass4nfv weekly danube job"
[releng.git] / utils / test / testapi / opnfv_testapi / handlers / 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
10 from opnfv_testapi.handlers import base_handlers
11 from opnfv_testapi.models import testcase_models
12 from opnfv_testapi.tornado_swagger import swagger
13
14
15 class GenericTestcaseHandler(base_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         self._list(query={'project_name': project_name})
34
35     @swagger.operation(nickname="createTestCase")
36     def post(self, project_name):
37         """
38             @description: create a testcase of a project by project_name
39             @param body: testcase to be created
40             @type body: L{TestcaseCreateRequest}
41             @in body: body
42             @rtype: L{CreateResponse}
43             @return 200: testcase is created in this project.
44             @raise 403: project not exist
45                         or testcase already exists in this project
46             @raise 400: body or name not provided
47         """
48         def project_query():
49             return {'name': project_name}
50
51         def testcase_query():
52             return {'project_name': project_name,
53                     'name': self.json_args.get('name')}
54         miss_fields = ['name']
55         carriers = [(self.db_projects, project_query)]
56         self._create(miss_fields=miss_fields,
57                      carriers=carriers,
58                      query=testcase_query,
59                      project_name=project_name)
60
61
62 class TestcaseGURHandler(GenericTestcaseHandler):
63     @swagger.operation(nickname='getTestCaseByName')
64     def get(self, project_name, case_name):
65         """
66             @description: get a single testcase
67                             by case_name and project_name
68             @rtype: L{Testcase}
69             @return 200: testcase exist
70             @raise 404: testcase not exist
71         """
72         query = dict()
73         query['project_name'] = project_name
74         query["name"] = case_name
75         self._get_one(query=query)
76
77     @swagger.operation(nickname="updateTestCaseByName")
78     def put(self, project_name, case_name):
79         """
80             @description: update a single testcase
81                             by project_name and case_name
82             @param body: testcase to be updated
83             @type body: L{TestcaseUpdateRequest}
84             @in body: body
85             @rtype: L{Project}
86             @return 200: update success
87             @raise 404: testcase or project not exist
88             @raise 403: new testcase name already exist in project
89                         or nothing to update
90         """
91         query = {'project_name': project_name, 'name': case_name}
92         db_keys = ['name', 'project_name']
93         self._update(query=query, db_keys=db_keys)
94
95     @swagger.operation(nickname='deleteTestCaseByName')
96     def delete(self, project_name, case_name):
97         """
98             @description: delete a testcase by project_name and case_name
99             @return 200: delete success
100             @raise 404: testcase not exist
101         """
102         query = {'project_name': project_name, 'name': case_name}
103         self._delete(query=query)