summaryrefslogtreecommitdiffstats
path: root/utils/test/testapi/opnfv_testapi/resources/testcase_handlers.py
blob: 3debd69180f531a7f4a45db3c7d357a35256faf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
##############################################################################
# Copyright (c) 2015 Orange
# guyrodrigue.koffi@orange.com / koffirodrigue@gmail.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from opnfv_testapi.common import constants
from opnfv_testapi.resources import handlers
from opnfv_testapi.resources import testcase_models
from opnfv_testapi.tornado_swagger import swagger


class GenericTestcaseHandler(handlers.GenericApiHandler):
    def __init__(self, application, request, **kwargs):
        super(GenericTestcaseHandler, self).__init__(application,
                                                     request,
                                                     **kwargs)
        self.table = self.db_testcases
        self.table_cls = testcase_models.Testcase


class TestcaseCLHandler(GenericTestcaseHandler):
    @swagger.operation(nickname="listAllTestCases")
    def get(self, project_name):
        """
            @description: list all testcases of a project by project_name
            @return 200: return all testcases of this project,
                         empty list is no testcase exist in this project
            @rtype: L{TestCases}
        """
        query = dict()
        query['project_name'] = project_name
        self._list(query)

    @swagger.operation(nickname="createTestCase")
    def post(self, project_name):
        """
            @description: create a testcase of a project by project_name
            @param body: testcase to be created
            @type body: L{TestcaseCreateRequest}
            @in body: body
            @rtype: L{CreateResponse}
            @return 200: testcase is created in this project.
            @raise 403: project not exist
                        or testcase already exists in this project
            @raise 400: body or name not provided
        """
        def p_query(data):
            return {'name': data.project_name}

        def tc_query(data):
            return {
                'project_name': data.project_name,
                'name': data.name
            }

        def p_error(data):
            message = 'Could not find project [{}]'.format(data.project_name)
            return constants.HTTP_FORBIDDEN, message

        def tc_error(data):
            message = '{} already exists as a testcase in project {}'\
                .format(data.name, data.project_name)
            return constants.HTTP_FORBIDDEN, message

        miss_checks = ['name']
        db_checks = [(self.db_projects, True, p_query, p_error),
                     (self.db_testcases, False, tc_query, tc_error)]
        self._create(miss_checks, db_checks, project_name=project_name)


class TestcaseGURHandler(GenericTestcaseHandler):
    @swagger.operation(nickname='getTestCaseByName')
    def get(self, project_name, case_name):
        """
            @description: get a single testcase
                            by case_name and project_name
            @rtype: L{Testcase}
            @return 200: testcase exist
            @raise 404: testcase not exist
        """
        query = dict()
        query['project_name'] = project_name
        query["name"] = case_name
        self._get_one(query)

    @swagger.operation(nickname="updateTestCaseByName")
    def put(self, project_name, case_name):
        """
            @description: update a single testcase
                            by project_name and case_name
            @param body: testcase to be updated
            @type body: L{TestcaseUpdateRequest}
            @in body: body
            @rtype: L{Project}
            @return 200: update success
            @raise 404: testcase or project not exist
            @raise 403: new testcase name already exist in project
                        or nothing to update
        """
        query = {'project_name': project_name, 'name': case_name}
        db_keys = ['name', 'project_name']
        self._update(query, db_keys)

    @swagger.operation(nickname='deleteTestCaseByName')
    def delete(self, project_name, case_name):
        """
            @description: delete a testcase by project_name and case_name
            @return 200: delete success
            @raise 404: testcase not exist
        """
        query = {'project_name': project_name, 'name': case_name}
        self._delete(query)