c162ac0dbc24c2337bc9c26b07e495e2a57f189b
[optf/osdf.git] / test / mainapp / test_osdfapp.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 osdfapp
19 import unittest
20
21 from osdf.operation.exceptions import BusinessException
22 from requests import Request, RequestException
23 from schematics.exceptions import DataError
24 from unittest import mock, TestCase
25 from unittest.mock import patch
26
27
28 class TestOSDFApp(TestCase):
29
30     def setUp(self):
31         self.patcher_g = patch('osdfapp.g', return_value={'request_id':'DUMMY-REQ'})
32         self.Mock_g = self.patcher_g.start()
33         # self.patcher2 = patch('package.module.Class2')
34         # self.MockClass2 = self.patcher2.start()
35
36     def tearDown(self):
37         patch.stopall()
38
39     def dummy_request_exception(self):
40         e = RequestException("Web Request Exception Description")
41         e.response = mock.MagicMock()
42         e.request = Request(method="GET", url="SOME-URL")
43         e.response.status_code = 400
44         e.response.content = "Some request exception occurred"
45         # request().raise_for_status.side_effect = e
46         return e
47  
48     def test_handle_business_exception(self):
49         e = BusinessException("Business Exception Description")
50         resp = osdfapp.handle_business_exception(e)
51         assert resp.status_code == 400
52
53     def test_handle_request_exception(self):
54         e = self.dummy_request_exception()
55         resp = osdfapp.handle_request_exception(e)
56         assert resp.status_code == 400
57
58     def test_handle_data_error(self):
59         e = DataError({"A1": "A1 Data Error"})
60         resp = osdfapp.handle_data_error(e)
61         assert resp.status_code == 400
62
63     def test_internal_failure(self):
64         e = Exception("An Internal Error")
65         resp = osdfapp.internal_failure(e)
66         assert resp.status_code == 500
67
68     def test_getOptions_default(self):
69         opts = osdfapp.getOptions(["PROG"])  # ensure nothing breaks
70
71
72 if __name__ == "__main__":
73     unittest.main()
74