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