test_client_registration.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # SPDX-License-Identifier: Apache-2.0
  2. # Copyright 2020 Contributors to OpenLEADR
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from pyopenadr import OpenADRClient, OpenADRServer, enums
  13. from pyopenadr.utils import generate_id
  14. from pyopenadr.messaging import create_message, parse_message
  15. from datetime import datetime, timezone, timedelta
  16. import asyncio
  17. import sqlite3
  18. import pytest
  19. from aiohttp import web
  20. SERVER_PORT = 8001
  21. VEN_NAME = 'myven'
  22. VEN_ID = '1234abcd'
  23. VTN_ID = "TestVTN"
  24. async def _on_create_party_registration(payload):
  25. registration_id = generate_id()
  26. payload = {'response': {'response_code': 200,
  27. 'response_description': 'OK',
  28. 'request_id': payload['request_id']},
  29. 'ven_id': VEN_ID,
  30. 'registration_id': registration_id,
  31. 'profiles': [{'profile_name': '2.0b',
  32. 'transports': {'transport_name': 'simpleHttp'}}],
  33. 'requested_oadr_poll_freq': timedelta(seconds=10)}
  34. return 'oadrCreatedPartyRegistration', payload
  35. @pytest.fixture
  36. async def start_server():
  37. server = OpenADRServer(vtn_id=VTN_ID)
  38. server.add_handler('on_create_party_registration', _on_create_party_registration)
  39. runner = web.AppRunner(server.app)
  40. await runner.setup()
  41. site = web.TCPSite(runner, 'localhost', SERVER_PORT)
  42. await site.start()
  43. yield
  44. await runner.cleanup()
  45. @pytest.mark.asyncio
  46. async def test_query_party_registration(start_server):
  47. client = OpenADRClient(ven_name=VEN_NAME,
  48. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  49. response_type, response_payload = await client.query_registration()
  50. assert response_type == 'oadrCreatedPartyRegistration'
  51. assert response_payload['vtn_id'] == VTN_ID
  52. @pytest.mark.asyncio
  53. async def test_create_party_registration(start_server):
  54. client = OpenADRClient(ven_name=VEN_NAME,
  55. vtn_url=f"http://localhost:{SERVER_PORT}/OpenADR2/Simple/2.0b")
  56. response_type, response_payload = await client.create_party_registration()
  57. assert response_type == 'oadrCreatedPartyRegistration'
  58. assert response_payload['ven_id'] == VEN_ID