update link to upper-constraints.txt
[optf/osdf.git] / test / logging / test_osdf_logging.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 from unittest import mock
20
21 from osdf.logging import osdf_logging as L1
22 from osdf.logging.osdf_logging import OOFOSDFLogMessageFormatter as formatter
23 from osdf.logging.osdf_logging import OOFOSDFLogMessageHelper as MH
24
25
26 class TestOSDFLogging(unittest.TestCase):
27
28     def __init__(self, *args, **kwargs):
29         super(self.__class__, self).__init__(*args, **kwargs)
30         self.MH = MH()
31         self.err = Exception("Some Exception")
32         self.req_id = "TEST-Req-ID"
33         self.url = mock.MagicMock()
34         self.request = mock.MagicMock()
35         self.client = mock.MagicMock()
36         self.service_name = mock.MagicMock()
37         self.callback_url = mock.MagicMock()
38         self.service_name = mock.MagicMock()
39         self.body = mock.MagicMock()
40         self.desc = mock.MagicMock()
41         self.response = mock.MagicMock()
42         self.remote_addr = mock.MagicMock()
43         self.json_body = mock.MagicMock()
44         self.F = formatter
45
46     def test_format_exception(self):
47         L1.format_exception(Exception("Some error"))
48
49     def test_accepted_valid_request(self):
50         res = formatter.accepted_valid_request(self.req_id, self.request)
51         assert res.startswith("Accepted valid request")
52
53     def test_invalid_request(self):
54         res = formatter.invalid_request(self.req_id, self.err)
55         assert res.startswith("Invalid request")
56
57     def test_invalid_response(self):
58         res = formatter.invalid_response(self.req_id, self.err)
59         assert res.startswith("Invalid response")
60
61     def test_malformed_request(self):
62         res = formatter.malformed_request(self.request, self.err)
63         assert res.startswith("Malformed request")
64
65     def test_malformed_response(self):
66         res = formatter.malformed_response(self.response, self.client, self.err)
67         assert res.startswith("Malformed response")
68
69     def test_need_policies(self):
70         res = formatter.need_policies(self.req_id)
71         assert res.startswith("Policies required but")
72
73     def test_policy_service_error(self):
74         res = formatter.policy_service_error(self.url, self.req_id, self.err)
75         assert res.startswith("Unable to call policy")
76
77     def test_requesting_url(self):
78         res = formatter.requesting_url(self.url, self.req_id)
79         assert res.startswith("Making a call to URL")
80
81     def test_requesting(self):
82         res = formatter.requesting(self.service_name, self.req_id)
83         assert res.startswith("Making a call to service")
84
85     def test_error_requesting(self):
86         res = formatter.error_requesting(self.service_name, self.req_id, self.err)
87         assert res.startswith("Error while requesting service")
88
89     def test_error_calling_back(self):
90         res = formatter.error_calling_back(self.service_name, self.callback_url, self.err)
91         assert res.startswith("Error while posting result to callback URL")
92
93     def test_calling_back(self):
94         res = formatter.calling_back(self.req_id, self.callback_url)
95         assert res.startswith("Posting result to callback URL")
96
97     def test_calling_back_with_body(self):
98         res = formatter.calling_back_with_body(self.req_id, self.callback_url, self.body)
99         assert res.startswith("Posting result to callback URL")
100
101     def test_received_request(self):
102         res = formatter.received_request(self.url, self.remote_addr, self.json_body)
103         assert res.startswith("Received a call")
104
105     def test_new_worker_thread(self):
106         res = formatter.new_worker_thread(self.req_id)
107         assert res.startswith("Initiating new worker thread")
108
109     def test_inside_worker_thread(self):
110         res = formatter.inside_worker_thread(self.req_id)
111         assert res.startswith("Inside worker thread for request ID")
112
113     def test_inside_new_thread(self):
114         res = formatter.inside_new_thread(self.req_id)
115         assert res.startswith("Spinning up a new thread for request ID")
116
117     def test_processing(self):
118         res = formatter.processing(self.req_id, self.desc)
119         assert res.startswith("Processing request ID")
120
121     def test_processed(self):
122         res = formatter.processed(self.req_id, self.desc)
123         assert res.startswith("Processed request ID")
124
125     def test_error_while_processing(self):
126         res = formatter.error_while_processing(self.req_id, self.desc, self.err)
127         assert res.startswith("Error while processing request ID")
128
129     def test_creating_local_env(self):
130         res = formatter.creating_local_env(self.req_id)
131         assert res.startswith("Creating local environment request ID")
132
133     def test_error_local_env(self):
134         res = formatter.error_local_env(self.req_id, self.desc, self.err)
135         assert res.startswith("Error while creating local environment for request ID")
136
137     def test_error_response_posting(self):
138         res = formatter.error_response_posting(self.req_id, self.desc, self.err)
139         assert res.startswith("Error while posting a response for a request ID")
140
141     def test_received_http_response(self):
142         res = formatter.received_http_response(self.response)
143         assert res.startswith("Received response [code: ")
144
145     def test_sending_response(self):
146         res = formatter.sending_response(self.req_id, self.desc)
147         assert res.startswith("Response is sent for request ID")
148
149     def test_listening_response(self):
150         res = formatter.listening_response(self.req_id, self.desc)
151         assert res.startswith("Response is sent for request ID")
152
153     def test_items_received(self):
154         res = formatter.items_received(10, "messages")
155         assert res == "Received 10 messages"
156
157     def test_items_sent(self):
158         res = formatter.items_sent(10, "messages")
159         assert res == "Published 10 messages"
160
161     def test_warn_audit_error(self):
162         """Log the message to error_log.warn and audit_log.warn"""
163         L1.warn_audit_error("Some warning message")
164
165
166 if __name__ == "__main__":
167     unittest.main()