test_schema.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from pyopenadr.utils import create_message, generate_id
  2. from lxml import etree
  3. import os
  4. from datetime import datetime, timedelta, timezone
  5. from termcolor import colored
  6. import jinja2
  7. DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"
  8. SCHEMA_LOCATION = os.path.join('schema', 'oadr_20b.xsd')
  9. schema_root = etree.parse(SCHEMA_LOCATION)
  10. schema = etree.XMLSchema(schema_root)
  11. parser = etree.XMLParser(schema=schema)
  12. def create_dummy_event(ven_id):
  13. """
  14. Creates a dummy event
  15. """
  16. now = datetime.now(timezone.utc)
  17. event_id = generate_id()
  18. active_period = {"dtstart": now + timedelta(minutes=1),
  19. "duration": timedelta(minutes=10)}
  20. event_descriptor = {"event_id": event_id,
  21. "modification_number": 1,
  22. "modification_date_time": now,
  23. "priority": 1,
  24. "market_context": "http://MarketContext1",
  25. "created_date_time": now,
  26. "event_status": "near",
  27. "test_event": "false",
  28. "vtn_comment": "This is an event"}
  29. event_signals = [{"intervals": [{"duration": timedelta(minutes=1), "uid": 1, "signal_payload": 8},
  30. {"duration": timedelta(minutes=1), "uid": 2, "signal_payload": 10},
  31. {"duration": timedelta(minutes=1), "uid": 3, "signal_payload": 12},
  32. {"duration": timedelta(minutes=1), "uid": 4, "signal_payload": 14},
  33. {"duration": timedelta(minutes=1), "uid": 5, "signal_payload": 16},
  34. {"duration": timedelta(minutes=1), "uid": 6, "signal_payload": 18},
  35. {"duration": timedelta(minutes=1), "uid": 7, "signal_payload": 20},
  36. {"duration": timedelta(minutes=1), "uid": 8, "signal_payload": 10},
  37. {"duration": timedelta(minutes=1), "uid": 9, "signal_payload": 20}],
  38. "signal_name": "LOAD_CONTROL",
  39. #"signal_name": "simple",
  40. #"signal_type": "level",
  41. "signal_type": "x-loadControlCapacity",
  42. "signal_id": generate_id(),
  43. "current_value": 0.0}]
  44. event_targets = [{"ven_id": 'VEN001'}, {"ven_id": 'VEN002'}]
  45. event = {'active_period': active_period,
  46. 'event_descriptor': event_descriptor,
  47. 'event_signals': event_signals,
  48. 'targets': event_targets,
  49. 'response_required': 'always'}
  50. return event
  51. # Test oadrPoll
  52. def test_message(type, **payload):
  53. try:
  54. message = create_message(type, **payload)
  55. etree.fromstring(message.encode('utf-8'), parser)
  56. print(colored(f"pass: {type} OK", "green"))
  57. except etree.XMLSyntaxError as err:
  58. print(colored(f"fail: {type} failed validation: {err}", "red"))
  59. print(message)
  60. except jinja2.exceptions.UndefinedError as err:
  61. print(colored(f"fail: {type} failed message construction: {err}", "yellow"))
  62. test_message('oadrCanceledOpt', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, opt_id=generate_id())
  63. test_message('oadrCanceledPartyRegistration', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, registration_id=generate_id(), ven_id='123ABC')
  64. test_message('oadrCanceledReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])
  65. test_message('oadrCanceledReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}], ven_id='123ABC')
  66. test_message('oadrCancelOpt', request_id=generate_id(), ven_id='123ABC', opt_id=generate_id())
  67. test_message('oadrCancelPartyRegistration', request_id=generate_id(), ven_id='123ABC', registration_id=generate_id())
  68. test_message('oadrCancelReport', request_id=generate_id(), ven_id='123ABC', report_request_id=generate_id(), report_to_follow=True)
  69. test_message('oadrCreatedEvent', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  70. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  71. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'},
  72. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(), 'event_id': generate_id(), 'modification_number': 1, 'opt_type': 'optIn'}],
  73. ven_id='123ABC')
  74. test_message('oadrCreatedReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}], ven_id='123ABC')
  75. test_message('oadrCreatedEvent', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  76. event_responses=[{'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  77. 'event_id': generate_id(),
  78. 'modification_number': 1,
  79. 'opt_type': 'optIn'},
  80. {'response_code': 200, 'response_description': 'OK', 'request_id': generate_id(),
  81. 'event_id': generate_id(),
  82. 'modification_number': 1,
  83. 'opt_type': 'optOut'}],
  84. ven_id='123ABC')
  85. test_message('oadrCreatedPartyRegistration', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  86. registration_id=generate_id(),
  87. ven_id='123ABC',
  88. profiles=[{'profile_name': '2.0b',
  89. 'transports': [{'transport_name': 'simpleHttp'}]}],
  90. vtn_id='VTN123')
  91. test_message('oadrCreatedReport', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, pending_reports=[{'request_id': generate_id()}, {'request_id': generate_id()}])
  92. test_message('oadrCreateOpt', opt_id=generate_id(),
  93. opt_type='optIn',
  94. opt_reason='participating',
  95. created_date_time=datetime.now(timezone.utc),
  96. request_id=generate_id(),
  97. event_id=generate_id(),
  98. modification_number=1,
  99. targets=[{'ven_id': '123ABC'}],
  100. ven_id='VEN123')
  101. test_message('oadrCreatePartyRegistration', request_id=generate_id(), ven_id='123ABC', profile_name='2.0b', transport_name='simpleHttp', transport_address='http://localhost', report_only=False, xml_signature=False, ven_name='test', http_pull_model=True)
  102. test_message('oadrCreateReport', request_id=generate_id(),
  103. ven_id='123ABC',
  104. report_requests=[{'report_request_id': 'd2b7bade5f',
  105. 'report_specifier': {'granularity': timedelta(seconds=900),
  106. 'report_back_duration': timedelta(seconds=900),
  107. 'report_interval': {'dtstart': datetime(2019, 11, 19, 11, 0, 18, 672768, tzinfo=timezone.utc),
  108. 'duration': timedelta(seconds=7200),
  109. 'tolerance': {'tolerate': {'startafter': timedelta(seconds=300)}}},
  110. 'report_specifier_id': '9c8bdc00e7',
  111. 'specifier_payload': {'r_id': 'd6e2e07485',
  112. 'reading_type': 'Direct Read'}}}])
  113. test_message('oadrDistributeEvent', request_id=generate_id(), response={'request_id': 123, 'response_code': 200, 'response_description': 'OK'}, events=[create_dummy_event(ven_id='123ABC')], vtn_id='VTN123')
  114. test_message('oadrPoll', ven_id='123ABC')
  115. test_message('oadrQueryRegistration', request_id=generate_id())
  116. test_message('oadrRegisteredReport', ven_id='VEN123', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()},
  117. report_requests=[{'report_request_id': generate_id(),
  118. 'report_specifier': {'report_specifier_id': generate_id(),
  119. 'granularity': timedelta(minutes=15),
  120. 'report_back_duration': timedelta(minutes=15),
  121. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  122. 'duration': timedelta(hours=2),
  123. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  124. 'notification': timedelta(minutes=30),
  125. 'ramp_up': timedelta(minutes=15),
  126. 'recovery': timedelta(minutes=5)},
  127. 'specifier_payload': {'r_id': generate_id(),
  128. 'reading_type': 'Direct Read'}}},
  129. {'report_request_id': generate_id(),
  130. 'report_specifier': {'report_specifier_id': generate_id(),
  131. 'granularity': timedelta(minutes=15),
  132. 'report_back_duration': timedelta(minutes=15),
  133. 'report_interval': {'dtstart': datetime.now(timezone.utc),
  134. 'duration': timedelta(hours=2),
  135. 'tolerance': {'tolerate': {'startafter': timedelta(minutes=5)}},
  136. 'notification': timedelta(minutes=30),
  137. 'ramp_up': timedelta(minutes=15),
  138. 'recovery': timedelta(minutes=5)},
  139. 'specifier_payload': {'r_id': generate_id(),
  140. 'reading_type': 'Direct Read'}}}])
  141. test_message('oadrRequestEvent', request_id=generate_id(), ven_id='123ABC')
  142. test_message('oadrRequestReregistration', ven_id='123ABC')
  143. test_message('oadrRegisterReport', request_id=generate_id(), reports=[{'report_id': generate_id(),
  144. 'report_descriptions': [{
  145. 'r_id': generate_id(),
  146. 'report_subject': {'ven_id': '123ABC'},
  147. 'report_data_source': {'ven_id': '123ABC'},
  148. 'report_type': 'reading',
  149. 'reading_type': 'Direct Read',
  150. 'market_context': 'http://localhost',
  151. 'sampling_rate': {'min_period': timedelta(minutes=1), 'max_period': timedelta(minutes=1), 'on_change': True}}],
  152. 'report_request_id': generate_id(),
  153. 'report_specifier_id': generate_id(),
  154. 'report_name': 'HISTORY_USAGE',
  155. 'created_date_time': datetime.now(timezone.utc)}],
  156. ven_id='123ABC',
  157. report_request_id=generate_id())
  158. test_message('oadrResponse', response={'response_code': 200, 'response_description': 'OK', 'request_id': generate_id()}, ven_id='123ABC')
  159. test_message('oadrResponse', response={'response_code': 200, 'response_description': 'OK', 'request_id': None}, ven_id='123ABC')
  160. # test_message('oadrUpdatedReport')
  161. # test_message('oadrUpdateReport')