| 1 |
import json |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from ocpp.routing import after, create_route_map, on |
| 6 |
from ocpp.v201 import call_result |
| 7 |
from ocpp.v201.enums import Action |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.asyncio |
| 11 |
async def test_route_message_with_existing_route( |
| 12 |
base_central_system, boot_notification_call |
| 13 |
): |
| 14 |
"""Test if the correct handler is called when routing a message. |
| 15 |
Also test if payload of request is injected correctly in handler. |
| 16 |
|
| 17 |
""" |
| 18 |
|
| 19 |
@on(Action.boot_notification) |
| 20 |
def on_boot_notification(reason, charging_station, **kwargs): |
| 21 |
assert reason == "PowerUp" |
| 22 |
assert charging_station == { |
| 23 |
"vendor_name": "ICU Eve Mini", |
| 24 |
"firmware_version": "#1:3.4.0-2990#N:217H;1.0-223", |
| 25 |
"model": "ICU Eve Mini", |
| 26 |
} |
| 27 |
|
| 28 |
return call_result.BootNotification( |
| 29 |
current_time="2018-05-29T17:37:05.495259", |
| 30 |
interval=350, |
| 31 |
status="Accepted", |
| 32 |
) |
| 33 |
|
| 34 |
@after(Action.boot_notification) |
| 35 |
def after_boot_notification(reason, charging_station, **kwargs): |
| 36 |
assert reason == "PowerUp" |
| 37 |
assert charging_station == { |
| 38 |
"vendor_name": "ICU Eve Mini", |
| 39 |
"firmware_version": "#1:3.4.0-2990#N:217H;1.0-223", |
| 40 |
"model": "ICU Eve Mini", |
| 41 |
} |
| 42 |
|
| 43 |
setattr(base_central_system, "on_boot_notification", on_boot_notification) |
| 44 |
setattr(base_central_system, "after_boot_notification", after_boot_notification) |
| 45 |
base_central_system.route_map = create_route_map(base_central_system) |
| 46 |
|
| 47 |
await base_central_system.route_message(boot_notification_call) |
| 48 |
base_central_system._connection.send.assert_called_once_with( |
| 49 |
json.dumps( |
| 50 |
[ |
| 51 |
3, |
| 52 |
"1", |
| 53 |
{ |
| 54 |
"currentTime": "2018-05-29T17:37:05.495259", |
| 55 |
"interval": 350, |
| 56 |
"status": "Accepted", |
| 57 |
}, |
| 58 |
], |
| 59 |
separators=(",", ":"), |
| 60 |
) |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.asyncio |
| 65 |
async def test_route_message_with_no_route(base_central_system, heartbeat_call): |
| 66 |
""" |
| 67 |
Test that a CALLERROR is sent back, reporting that no handler is |
| 68 |
registred for it. |
| 69 |
|
| 70 |
""" |
| 71 |
# Empty the route map |
| 72 |
base_central_system.route_map = {} |
| 73 |
|
| 74 |
await base_central_system.route_message(heartbeat_call) |
| 75 |
base_central_system._connection.send.assert_called_once_with( |
| 76 |
json.dumps( |
| 77 |
[ |
| 78 |
4, |
| 79 |
1, |
| 80 |
"NotImplemented", |
| 81 |
"Request Action is recognized but not supported by the receiver", |
| 82 |
{"cause": "No handler for Heartbeat registered."}, |
| 83 |
], |
| 84 |
separators=(",", ":"), |
| 85 |
) |
| 86 |
) |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.asyncio |
| 90 |
async def test_call_with_unique_id_should_return_same_id( |
| 91 |
mock_boot_request, mock_base_central_system |
| 92 |
): |
| 93 |
|
| 94 |
expected_unique_id = "12345" |
| 95 |
# Call the method being tested with a unique_id as a parameter |
| 96 |
await mock_base_central_system.call(mock_boot_request, unique_id=expected_unique_id) |
| 97 |
( |
| 98 |
actual_unique_id, |
| 99 |
_, |
| 100 |
) = mock_base_central_system._get_specific_response.call_args_list[0][0] |
| 101 |
|
| 102 |
# Check the actual unique id is equals to the one passed to the call method |
| 103 |
assert actual_unique_id == expected_unique_id |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.asyncio |
| 107 |
async def test_call_without_unique_id_should_return_a_random_value( |
| 108 |
mock_boot_request, mock_base_central_system |
| 109 |
): |
| 110 |
|
| 111 |
expected_unique_id = str(mock_base_central_system._unique_id_generator()) |
| 112 |
|
| 113 |
# Call the method being tested without passing a unique_id as a parameter |
| 114 |
await mock_base_central_system.call(mock_boot_request) |
| 115 |
|
| 116 |
( |
| 117 |
actual_unique_id, |
| 118 |
_, |
| 119 |
) = mock_base_central_system._get_specific_response.call_args_list[0][0] |
| 120 |
# Check the actual unique id is equals to the one internally generated |
| 121 |
assert actual_unique_id == expected_unique_id |