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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
"""Test OnapService module."""
# Copyright 2022 Orange, Deutsche Telekom AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest import mock
from unittest.mock import ANY
import pytest
from requests import Response, Session
from requests import ConnectionError, RequestException
from onapsdk.exceptions import (
RequestError, APIError, ResourceNotFound, InvalidResponse, ConnectionFailed
)
from onapsdk.onap_service import OnapService
from onapsdk.sdc.vendor import Vendor
def http_codes():
return [
400, # Bad Request
401, # Unauthorized
403, # Forbidden
405, # Method Not Allowed
408, # Request Timeout
415, # Unsupported Media Type
429, # Too Many Requests
500, # Internal Server Error
501, # Not Implemented
502, # Bad Gateway
503, # Service Unavailable
504 # Gateway Timeout
]
class TestException(Exception):
"""Test exception."""
def test_init():
"""Test initialization."""
svc = OnapService()
def test_class_variables():
"""Test class variables."""
assert OnapService.server == None
assert OnapService.headers == {
"Content-Type": "application/json",
"Accept": "application/json",
}
assert OnapService.proxy == None
def test_set_proxy():
"""Test set_proxy()."""
assert OnapService.proxy == None
Vendor.set_proxy({'the', 'proxy'})
assert OnapService.proxy == {'the', 'proxy'}
Vendor.set_proxy(None)
assert OnapService.proxy == None
# ------------------
@mock.patch.object(Session, 'request')
def test_send_message_OK(mock_request):
"""Returns response if OK."""
svc = OnapService()
mocked_response = Response()
mocked_response.status_code = 200
mock_request.return_value = mocked_response
expect_headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
response = svc.send_message("GET", 'test get', 'http://my.url/')
mock_request.assert_called_once_with('GET', 'http://my.url/', timeout=60,
headers=expect_headers, verify=False,
proxies=None)
assert response == mocked_response
@mock.patch.object(Session, 'request')
def test_send_message_custom_header_OK(mock_request):
"""Returns response if returns OK with a custom header."""
svc = OnapService()
mocked_response = Response()
mocked_response.status_code = 200
mock_request.return_value = mocked_response
expect_headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Custom": "Header"
}
response = svc.send_message("GET", 'test get', 'http://my.url/',
headers=expect_headers)
mock_request.assert_called_once_with('GET', 'http://my.url/', timeout=60,
headers=expect_headers, verify=False,
proxies=None)
assert response == mocked_response
@mock.patch.object(OnapService, '_set_basic_auth_if_needed')
@mock.patch.object(Session, 'request')
def test_send_message_with_basic_auth(mock_request, mock_set_basic_auth_if_needed):
"""Should give response of request if OK."""
svc = OnapService()
mocked_response = Response()
mocked_response.status_code = 200
basic_auth = {'username': 'user1', "password": "password1"}
mock_request.return_value = mocked_response
expect_headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Once": "Upon a time"
}
response = svc.send_message("GET", 'test get', 'http://my.url/',
headers=expect_headers, basic_auth=basic_auth)
mock_set_basic_auth_if_needed.assert_called_once_with(basic_auth, ANY)
mock_request.assert_called_once_with('GET', 'http://my.url/', timeout=60,
headers=expect_headers, verify=False,
proxies=None)
assert response == mocked_response
@mock.patch.object(Session, 'request')
def test_send_message_resource_not_found(mock_request):
"""Should raise ResourceNotFound if status code 404."""
svc = OnapService()
mocked_response = Response()
mocked_response.status_code = 404
mock_request.return_value = mocked_response
with pytest.raises(ResourceNotFound) as exc:
svc.send_message("GET", 'test get', 'http://my.url/')
assert exc.type is ResourceNotFound
mock_request.assert_called_once()
@mock.patch.object(Session, 'request')
@pytest.mark.parametrize("code", http_codes())
def test_send_message_api_error(mock_request, code):
"""Raise APIError if status code is between 400 and 599, and not 404."""
svc = OnapService()
mocked_response = Response()
mocked_response.status_code = code
mock_request.return_value = mocked_response
with pytest.raises(APIError) as exc:
svc.send_message("GET", 'test get', 'http://my.url/')
assert exc.type is APIError
mock_request.assert_called_once()
@mock.patch.object(Session, 'request')
def test_send_message_connection_failed(mock_request):
"""Should raise ResourceNotFound if status code 404."""
svc = OnapService()
mock_request.side_effect = ConnectionError
with pytest.raises(ConnectionFailed) as exc:
svc.send_message("GET", 'test get', 'http://my.url/')
assert exc.type is ConnectionFailed
mock_request.assert_called_once()
@mock.patch.object(Session, 'request')
def test_send_message_request_error(mock_request):
"""Should raise RequestError for an amiguous request exception."""
svc = OnapService()
mock_request.side_effect = RequestException
with pytest.raises(RequestError) as exc:
svc.send_message("GET", 'test get', 'http://my.url/')
assert exc.type is RequestError
mock_request.assert_called_once()
@mock.patch.object(Session, 'request')
def test_send_message_custom_error(mock_request):
"""Should raise RequestError for an amiguous request exception."""
svc = OnapService()
mock_request.side_effect = RequestException
with pytest.raises(TestException) as exc:
svc.send_message("GET", 'test get', 'http://my.url/',
exception=TestException)
assert exc.type is TestException
mock_request.assert_called_once()
@mock.patch.object(OnapService, 'send_message')
def test_send_message_json_OK(mock_send):
"""JSON is received and successfully decoded."""
svc = OnapService()
mocked_response = Response()
mocked_response._content = b'{"yolo": "yala"}'
mocked_response.encoding = "UTF-8"
mocked_response.status_code = 200
mock_send.return_value = mocked_response
response = svc.send_message_json("GET", 'test get', 'http://my.url/')
mock_send.assert_called_once_with("GET", 'test get', 'http://my.url/')
assert response['yolo'] == 'yala'
@mock.patch.object(OnapService, 'send_message')
def test_send_message_json_invalid_response(mock_send):
"""Raises InvalidResponse if response is not JSON."""
svc = OnapService()
mocked_response = Response()
mocked_response._content = b'{yolo}'
mocked_response.encoding = "UTF-8"
mocked_response.status_code = 200
mock_send.return_value = mocked_response
with pytest.raises(InvalidResponse) as exc:
svc.send_message_json("GET", 'test get', 'http://my.url/')
assert exc.type is InvalidResponse
mock_send.assert_called_once()
@mock.patch.object(OnapService, 'send_message')
def test_send_message_json_connection_failed(mock_send):
"""ConnectionFailed from send_message is handled."""
svc = OnapService()
mock_send.side_effect = ConnectionFailed
with pytest.raises(ConnectionFailed) as exc:
svc.send_message_json("GET", 'test get', 'http://my.url/')
assert exc.type is ConnectionFailed
mock_send.assert_called_once()
@mock.patch.object(OnapService, 'send_message')
def test_send_message_json_api_error(mock_send):
"""APIError (error codes) from send_message is handled."""
svc = OnapService()
mock_send.side_effect = APIError
with pytest.raises(APIError) as exc:
svc.send_message_json("GET", 'test get', 'http://my.url/')
assert exc.type is APIError
mock_send.assert_called_once()
@mock.patch.object(OnapService, 'send_message')
def test_send_message_json_resource_not_found(mock_send):
"""ResourceNotFound exception from send_message is handled."""
svc = OnapService()
mock_send.side_effect = ResourceNotFound
with pytest.raises(ResourceNotFound) as exc:
svc.send_message_json("GET", 'test get', 'http://my.url/')
assert exc.type is ResourceNotFound
mock_send.assert_called_once()
@mock.patch.object(OnapService, 'send_message')
def test_send_message_json_request_error(mock_send):
"""RequestError exception from send_message is handled."""
svc = OnapService()
mock_send.side_effect = RequestError
with pytest.raises(RequestError) as exc:
svc.send_message_json("GET", 'test get', 'http://my.url/')
assert exc.type is RequestError
mock_send.assert_called_once()
@mock.patch.object(OnapService, 'send_message')
def test_send_message_json_custom_error(mock_send):
"""RequestError exception from send_message is handled."""
svc = OnapService()
mock_send.side_effect = RequestError
with pytest.raises(TestException) as exc:
svc.send_message_json("GET", 'test get', 'http://my.url/',
exception=TestException)
assert exc.type is TestException
mock_send.assert_called_once()
@mock.patch("onapsdk.onap_service.requests.Session")
def test_set_header(mock_session):
OnapService.send_message(method="GET", action='test get', url='http://my.url/', timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-key" not in headers
mock_session.reset_mock()
OnapService.set_header({"test-header-key": "test-header-value"})
OnapService.send_message("GET", 'test get', 'http://my.url/', timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-key" in headers
assert headers["test-header-key"] == "test-header-value"
mock_session.reset_mock()
OnapService.send_message("GET", 'test get', 'http://my.url/', headers={}, timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-key" in headers
assert headers["test-header-key"] == "test-header-value"
mock_session.reset_mock()
OnapService.send_message("GET", 'test get', 'http://my.url/', headers={"test-header-key": "test-header-another-value"}, timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-key" in headers
assert headers["test-header-key"] == "test-header-value"
mock_session.reset_mock()
OnapService.set_header(None)
OnapService.send_message("GET", 'test get', 'http://my.url/', timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-key" not in headers
def test_header_method():
return {"test-header-callable-key": "test-header-callable-value"}
mock_session.reset_mock()
OnapService.set_header(test_header_method)
OnapService.send_message("GET", 'test get', 'http://my.url/', headers={}, timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-callable-key" in headers
assert headers["test-header-callable-key"] == "test-header-callable-value"
mock_session.reset_mock()
OnapService.send_message("GET", 'test get', 'http://my.url/', headers={"test-header-key": "test-header-value"}, timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-callable-key" in headers
assert headers["test-header-callable-key"] == "test-header-callable-value"
assert "test-header-key" in headers
assert headers["test-header-key"] == "test-header-value"
mock_session.reset_mock()
OnapService.set_header({"test-header-dict-key": "test-header-dict-value"})
OnapService.send_message("GET", 'test get', 'http://my.url/', headers={}, timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-callable-key" in headers
assert headers["test-header-callable-key"] == "test-header-callable-value"
assert "test-header-dict-key" in headers
assert headers["test-header-dict-key"] == "test-header-dict-value"
mock_session.reset_mock()
OnapService.send_message("GET", 'test get', 'http://my.url/', headers={"test-header-common-key": "test-header-common-value"}, timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-callable-key" in headers
assert headers["test-header-callable-key"] == "test-header-callable-value"
assert "test-header-dict-key" in headers
assert headers["test-header-dict-key"] == "test-header-dict-value"
assert "test-header-common-key" in headers
assert headers["test-header-common-key"] == "test-header-common-value"
mock_session.reset_mock()
cert = mock.MagicMock(name="test-cert")
OnapService.send_message("GET", 'test get', 'http://my.url/', headers={"test-header-common-key": "test-header-common-value"}, cert=cert, timeout=0)
_, _, kwargs = mock_session.return_value.request.mock_calls[0]
headers = kwargs["headers"]
assert "test-header-callable-key" in headers
assert headers["test-header-callable-key"] == "test-header-callable-value"
assert "test-header-dict-key" in headers
assert headers["test-header-dict-key"] == "test-header-dict-value"
assert "test-header-common-key" in headers
assert headers["test-header-common-key"] == "test-header-common-value"
|