test_message_conversion.py 14 KB

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