blob: 665968e6b07f05310ad49bf051181f4a72872dbd [file] [log] [blame]
Eduardo Sousaa0117812019-02-05 15:57:09 +00001# Copyright 2018 Whitestack, LLC
2# Copyright 2018 Telefonica S.A.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# 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, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15#
16# For those usages not covered by the Apache License, Version 2.0 please
17# contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com
18##
19
Eduardo Sousa4d611d32018-05-09 19:20:37 +010020import http
21import pytest
22
23from osm_common.msgbase import MsgBase, MsgException
24
25
26def exception_message(message):
27 return "messaging exception " + message
28
tiernob20a9022018-05-22 12:07:05 +020029
Eduardo Sousa4d611d32018-05-09 19:20:37 +010030@pytest.fixture
31def msg_base():
32 return MsgBase()
33
tiernob20a9022018-05-22 12:07:05 +020034
Eduardo Sousa4d611d32018-05-09 19:20:37 +010035def test_constructor():
36 msgbase = MsgBase()
tiernob20a9022018-05-22 12:07:05 +020037 assert msgbase is not None
Eduardo Sousa4d611d32018-05-09 19:20:37 +010038 assert isinstance(msgbase, MsgBase)
39
tiernob20a9022018-05-22 12:07:05 +020040
Eduardo Sousa4d611d32018-05-09 19:20:37 +010041def test_connect(msg_base):
Eduardo Sousa5ffda642018-05-09 19:42:00 +010042 msg_base.connect(None)
Eduardo Sousa4d611d32018-05-09 19:20:37 +010043
tiernob20a9022018-05-22 12:07:05 +020044
Eduardo Sousa4d611d32018-05-09 19:20:37 +010045def test_disconnect(msg_base):
46 msg_base.disconnect()
47
tiernob20a9022018-05-22 12:07:05 +020048
Eduardo Sousa4d611d32018-05-09 19:20:37 +010049def test_write(msg_base):
50 with pytest.raises(MsgException) as excinfo:
51 msg_base.write("test", "test", "test")
garciadeblas2644b762021-03-24 09:21:01 +010052 assert str(excinfo.value).startswith(
53 exception_message("Method 'write' not implemented")
54 )
Eduardo Sousa4d611d32018-05-09 19:20:37 +010055 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
56
tiernob20a9022018-05-22 12:07:05 +020057
Eduardo Sousa4d611d32018-05-09 19:20:37 +010058def test_read(msg_base):
59 with pytest.raises(MsgException) as excinfo:
60 msg_base.read("test")
garciadeblas2644b762021-03-24 09:21:01 +010061 assert str(excinfo.value).startswith(
62 exception_message("Method 'read' not implemented")
63 )
Eduardo Sousa4d611d32018-05-09 19:20:37 +010064 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
65
tiernob20a9022018-05-22 12:07:05 +020066
Eduardo Sousa4d611d32018-05-09 19:20:37 +010067def test_aiowrite(msg_base, event_loop):
68 with pytest.raises(MsgException) as excinfo:
garciadeblas2644b762021-03-24 09:21:01 +010069 event_loop.run_until_complete(
70 msg_base.aiowrite("test", "test", "test", event_loop)
71 )
72 assert str(excinfo.value).startswith(
73 exception_message("Method 'aiowrite' not implemented")
74 )
Eduardo Sousa4d611d32018-05-09 19:20:37 +010075 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
76
tiernob20a9022018-05-22 12:07:05 +020077
Eduardo Sousa4d611d32018-05-09 19:20:37 +010078def test_aioread(msg_base, event_loop):
79 with pytest.raises(MsgException) as excinfo:
80 event_loop.run_until_complete(msg_base.aioread("test", event_loop))
garciadeblas2644b762021-03-24 09:21:01 +010081 assert str(excinfo.value).startswith(
82 exception_message("Method 'aioread' not implemented")
83 )
Eduardo Sousa4d611d32018-05-09 19:20:37 +010084 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR