2e089ec61beceb9434f744241c8ceb68d99b0c93
[optf/osdf.git] / test / operation / test_responses.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2017-2018 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18 import unittest
19 import json
20 import yaml
21
22 from osdf.adapters.local_data import local_policies
23
24
25 class TestLocalPolicies(unittest.TestCase):
26
27     def __init__(self, *args, **kwargs):
28         super(self.__class__, self).__init__(*args, **kwargs)
29         self.folder = './test/policy-local-files'
30         self.invalid_policies = ['INVALID-one.json', 'INVALID-two.json']
31         self.valid_policies = [
32             'Affinity_vCPE_1.json',
33             'Capacity_vG_1.json',
34             'Distance_vG_1.json',
35             'Placement_Optimization_1.json',
36             'hpa_policy_vGMuxInfra_1.json',
37             'vnfPolicy_vG.json',
38             'Capacity_vGMuxInfra.json',
39             'Distance_vGMuxInfra_1.json',
40             'Min_Guarantee_vGMuxInfra_1.json',
41             'QueryPolicy_vCPE.json',
42             'hpa_policy_vG_1.json',
43             'vnfPolicy_vGMuxInfra.json'
44             ]
45        
46     def test_get_local_policies_no_policies(self):
47         with self.assertRaises(FileNotFoundError):
48              res = local_policies.get_local_policies(self.folder, self.invalid_policies)
49              assert res is None
50
51     def test_get_local_valid_policies(self):
52         res = local_policies.get_local_policies(self.folder, self.valid_policies)
53         assert len(res) == len(self.valid_policies)
54
55     def test_get_subset_valid_policies(self):
56         wanted = [ x[:-5] for x in self.valid_policies[:1] ]
57         res = local_policies.get_local_policies(self.folder, self.valid_policies, wanted)
58         assert len(res) == len(wanted)
59
60
61 if __name__ == "__main__":
62     unittest.main()
63
64 from flask import Response
65
66 from osdf import ACCEPTED_MESSAGE_TEMPLATE
67
68
69 def osdf_response_for_request_accept(req_id="", text="", response_code=202, as_http=True):
70     """Helper method to create a response object for request acceptance, so that the object can be sent to a client
71     :param req_id: request ID provided by the caller
72     :param text: extra text description about accepting the request (e.g. "Request accepted")
73     :param response_code: the HTTP status code to send -- default is 202 (accepted)
74     :param as_http: whether to send response as HTTP response object or as a string
75     :return: if as_http is True, return a HTTP Response object. Otherwise, return json-encoded-message
76     """
77     response_message = ACCEPTED_MESSAGE_TEMPLATE.render(description=text, request_id=req_id)
78     if not as_http:
79         return response_message
80
81     response = Response(response_message, content_type='application/json; charset=utf-8')
82     response.headers.add('content-length', len(response_message))
83     response.status_code = response_code
84     return response