Debian Codegraph @

tests/v16/test_v16_charge_point.py

raw
1 import asyncio
2 import json
3 from unittest import mock
4
5 import pytest
6
7 from ocpp.exceptions import FormatViolationError, GenericError
8 from ocpp.messages import CallError
9 from ocpp.routing import after, create_route_map, on
10 from ocpp.v16 import ChargePoint, call, call_result
11 from ocpp.v16.enums import Action
12
13
14 @pytest.mark.asyncio
15 async def test_route_message_with_existing_route(
16 base_central_system, boot_notification_call
17 ):
18 """Test if the correct handler is called when routing a message.
19 Also test if payload of request is injected correctly in handler.
20
21 """
22
23 @on(Action.boot_notification)
24 def on_boot_notification(charge_point_model, charge_point_vendor, **kwargs): # noqa
25 assert charge_point_vendor == "Alfen BV"
26 assert charge_point_model == "ICU Eve Mini"
27 assert kwargs["firmware_version"] == "#1:3.4.0-2990#N:217H;1.0-223"
28
29 return call_result.BootNotification(
30 current_time="2018-05-29T17:37:05.495259",
31 interval=350,
32 status="Accepted",
33 )
34
35 @after(Action.boot_notification)
36 async def after_boot_notification(
37 charge_point_model, charge_point_vendor, **kwargs
38 ): # noqa
39 assert charge_point_vendor == "Alfen BV"
40 assert charge_point_model == "ICU Eve Mini"
41
42 setattr(base_central_system, "on_boot_notification", on_boot_notification)
43 setattr(base_central_system, "after_boot_notification", after_boot_notification)
44 base_central_system.route_map = create_route_map(base_central_system)
45
46 await base_central_system.route_message(boot_notification_call)
47 base_central_system._connection.send.assert_called_once_with(
48 json.dumps(
49 [
50 3,
51 "1",
52 {
53 "currentTime": "2018-05-29T17:37:05.495259",
54 "interval": 350,
55 "status": "Accepted",
56 },
57 ],
58 separators=(",", ":"),
59 )
60 )
61
62
63 @pytest.mark.asyncio
64 async def test_route_message_without_validation(base_central_system):
65 @on(Action.boot_notification, skip_schema_validation=True)
66 def on_boot_notification(**kwargs): # noqa
67 assert kwargs["firmware_version"] == "#1:3.4.0-2990#N:217H;1.0-223"
68
69 return call_result.BootNotification(
70 current_time="2018-05-29T17:37:05.495259",
71 interval=350,
72 # 'Yolo' is not a valid value for for field status.
73 status="Yolo",
74 )
75
76 setattr(base_central_system, "on_boot_notification", on_boot_notification)
77 base_central_system.route_map = create_route_map(base_central_system)
78
79 await base_central_system.route_message(
80 json.dumps(
81 [
82 2,
83 1,
84 "BootNotification",
85 {
86 # The payload is missing the required fields 'chargepointVendor'
87 # and 'chargePointModel'.
88 "firmwareVersion": "#1:3.4.0-2990#N:217H;1.0-223"
89 },
90 ],
91 separators=(",", ":"),
92 )
93 )
94
95 base_central_system._connection.send.call_args == mock.call(
96 json.dumps(
97 [
98 3,
99 "1",
100 {
101 "currentTime": "2018-05-29T17:37:05.495259",
102 "interval": 350,
103 "status": "Yolo",
104 },
105 ],
106 separators=(",", ":"),
107 )
108 )
109
110
111 @pytest.mark.asyncio
112 async def test_route_message_not_supported(base_central_system, not_supported_call):
113 """
114 Test that a CALLERROR is sent back, reporting that it is
115 not supported by OCPP version.
116
117 """
118
119 @on(Action.boot_notification)
120 def on_boot_notification(**kwargs): # noqa
121 assert kwargs["firmware_version"] == "#1:3.4.0-2990#N:217H;1.0-223"
122
123 return call_result.BootNotification(
124 current_time="2018-05-29T17:37:05.495259",
125 interval=350,
126 # 'Yolo' is not a valid value for for field status.
127 status="Yolo",
128 )
129
130 setattr(base_central_system, "on_boot_notification", on_boot_notification)
131 base_central_system.route_map = create_route_map(base_central_system)
132
133 await base_central_system.route_message(not_supported_call)
134 base_central_system._connection.send.assert_called_once_with(
135 json.dumps(
136 [
137 4,
138 1,
139 "NotSupported",
140 "Requested Action is not known by receiver",
141 {"cause": "InvalidAction not supported by OCPP1.6."},
142 ],
143 separators=(",", ":"),
144 )
145 )
146
147
148 @pytest.mark.asyncio
149 async def test_route_message_with_no_route(base_central_system, heartbeat_call):
150 """
151 Test that a CALLERROR is sent back, reporting that no handler is
152 registred for it.
153
154 """
155 # Empty the route map
156 base_central_system.route_map = {}
157
158 await base_central_system.route_message(heartbeat_call)
159 base_central_system._connection.send.assert_called_once_with(
160 json.dumps(
161 [
162 4,
163 1,
164 "NotImplemented",
165 "Request Action is recognized but not supported by the receiver",
166 {"cause": "No handler for Heartbeat registered."},
167 ],
168 separators=(",", ":"),
169 )
170 )
171
172
173 @pytest.mark.asyncio
174 async def test_send_call_with_timeout(connection):
175 cs = ChargePoint(id=1234, connection=connection, response_timeout=0.1)
176
177 payload = call.Reset(type="Hard")
178
179 with pytest.raises(asyncio.TimeoutError):
180 await cs.call(payload)
181
182 # Verify that lock is released if call() crahses. Not releasing the lock
183 # in case of an exception could lead to a deadlock. See
184 # https://github.com/mobilityhouse/ocpp/issues/46
185 assert cs._call_lock.locked() is False
186
187
188 @pytest.mark.asyncio
189 async def test_send_invalid_call(base_central_system):
190 payload = call.Reset(type="Medium")
191
192 with pytest.raises(FormatViolationError):
193 await base_central_system.call(payload)
194
195
196 @pytest.mark.asyncio
197 async def test_raise_call_error(base_central_system):
198 """
199 Test that getting a CallError will raise an Exception
200 if suppress argument is not True.
201
202 """
203 call_error = CallError(
204 unique_id="1337",
205 error_code="GenericError",
206 error_description="test_raise_call_error",
207 )
208 await base_central_system.route_message(call_error.to_json())
209
210 payload = call.ClearCache()
211 with pytest.raises(GenericError):
212 await base_central_system.call(payload, suppress=False)
213
214
215 @pytest.mark.asyncio
216 async def test_suppress_call_error(base_central_system):
217 """
218 Test that getting a CallError will suppress Exception
219 by default
220
221 """
222 call_error = CallError(
223 unique_id="1337",
224 error_code="GenericError",
225 error_description="test_raise_call_error",
226 )
227 await base_central_system.route_message(call_error.to_json())
228
229 payload = call.ClearCache()
230 await base_central_system.call(payload)
231
232
233 @pytest.mark.asyncio
234 async def test_call_with_unique_id_should_return_same_id(
235 mock_boot_request, mock_base_central_system
236 ):
237
238 expected_unique_id = "12345"
239 # Call the method being tested with a unique_id as a parameter
240 await mock_base_central_system.call(mock_boot_request, unique_id=expected_unique_id)
241 (
242 actual_unique_id,
243 _,
244 ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
245
246 # Check the actual unique id is equals to the one passed to the call method
247 assert actual_unique_id == expected_unique_id
248
249
250 @pytest.mark.asyncio
251 async def test_call_without_unique_id_should_return_a_random_value(
252 mock_boot_request, mock_base_central_system
253 ):
254
255 expected_unique_id = str(mock_base_central_system._unique_id_generator())
256
257 # Call the method being tested without passing a unique_id as a parameter
258 await mock_base_central_system.call(mock_boot_request)
259
260 (
261 actual_unique_id,
262 _,
263 ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
264 # Check the actual unique id is equals to the one internally generated
265 assert actual_unique_id == expected_unique_id
266
267
268 @pytest.mark.asyncio
269 async def test_call_skip_schema_validation(
270 mock_invalid_boot_request, mock_base_central_system
271 ):
272 """
273 Test that schema validation is skipped for an invalid boot notification request.
274
275 """
276
277 expected_unique_id = "12345"
278 # Call the method being tested with an invalid boot notification request
279 # and a unique_id as a parameter
280 await mock_base_central_system.call(
281 mock_invalid_boot_request,
282 unique_id=expected_unique_id,
283 skip_schema_validation=True,
284 )
285 (
286 actual_unique_id,
287 _,
288 ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
289
290 # Check the actual unique id is equals to the one passed to the call method
291 assert actual_unique_id == expected_unique_id