395bfb39cd6658b5a3e9c8f9b0d122c62864a6d2
[ric-plt/lib/rmr.git] / src / bindings / rmr-python / rmr / rmr_mocks / rmr_mocks.py
1 # ==================================================================================
2 #       Copyright (c) 2019 Nokia
3 #       Copyright (c) 2018-2019 AT&T Intellectual Property.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 # ==================================================================================
17
18 """
19 Provides mocks that are useful for end applications unit testing
20 """
21
22 import json
23 import uuid
24
25
26 def rcv_mock_generator(msg_payload, msg_type, msg_state, jsonb, timeout=0):
27     """
28     generates a mock function that can be used to monkeypatch rmr_torcv_msg or rmr_rcv_msg
29     """
30
31     def f(_mrc, sbuf, _timeout=timeout):  # last param is needed for calls to rmr_torcv_msg, but not in rmr_rcv_msg
32         sbuf.contents.mtype = msg_type
33         payload = json.dumps(msg_payload).encode("utf-8") if jsonb else msg_payload
34         sbuf.contents.payload = payload
35         sbuf.contents.len = len(payload)
36         sbuf.contents.state = msg_state
37         return sbuf
38
39     return f
40
41
42 def send_mock_generator(msg_state):
43     """
44     generates a mock function that can be used to monkeypatch rmr_send_msg
45     usage example:
46         monkeypatch.setattr('rmr.rmr.rmr_send_msg', rmr_mocks.send_mock_generator(0))
47     """
48
49     def f(_unused, sbuf):
50         sbuf.contents.state = msg_state
51         return sbuf
52
53     return f
54
55
56 class _Sbuf_Contents:
57     """fake version of how pointers work (ctype pointer access is done by accessing a magical attrivute called "contents"""
58
59     def __init__(self):
60         self.state = 0
61         self.mtype = 0
62         self.len = 0
63         self.payload = ""
64         self.xaction = uuid.uuid1().hex.encode("utf-8")
65         self.sub_id = 0
66
67     def __str__(self):
68         return str(
69             {
70                 "state": self.state,
71                 "mtype": self.mtype,
72                 "len": self.len,
73                 "payload": self.payload,
74                 "xaction": self.xaction,
75                 "sub_id": self.sub_id,
76             }
77         )
78
79
80 class Rmr_mbuf_t:
81     """fake version of rmr.rmr_mbuf_t"""
82
83     def __init__(self):
84         self.contents = _Sbuf_Contents()
85
86
87 def patch_rmr(monkeypatch):
88     """
89     Patch rmr; requires a monkeypatch (pytest) object to be passed in
90     """
91
92     def fake_alloc(_unused, _alsounused):
93         return Rmr_mbuf_t()
94
95     def fake_set_payload_and_length(payload, sbuf):
96         sbuf.contents.payload = payload
97         sbuf.contents.len = len(payload)
98
99     def fake_generate_and_set_transaction_id(sbuf):
100         sbuf.contents.xaction = uuid.uuid1().hex.encode("utf-8")
101
102     def fake_get_payload(sbuf):
103         return sbuf.contents.payload
104
105     def fake_get_meid(_sbuf):
106         return None  # this is not a part of rmr_mbuf_t
107
108     def fake_get_src(_sbuf):
109         return "localtest:80"  # this is not a part of rmr_mbuf_t
110
111     def fake_rmr_payload_size(_sbuf):
112         return 4096
113
114     monkeypatch.setattr("rmr.rmr.rmr_alloc_msg", fake_alloc)
115     monkeypatch.setattr("rmr.rmr.set_payload_and_length", fake_set_payload_and_length)
116     monkeypatch.setattr("rmr.rmr.generate_and_set_transaction_id", fake_generate_and_set_transaction_id)
117     monkeypatch.setattr("rmr.rmr.get_payload", fake_get_payload)
118     monkeypatch.setattr("rmr.rmr.get_src", fake_get_src)
119     monkeypatch.setattr("rmr.rmr.get_meid", fake_get_meid)
120     monkeypatch.setattr("rmr.rmr.rmr_payload_size", fake_rmr_payload_size)