2424#
2525# -------------------------------------------------------------------------
2626
27+ from datetime import datetime , timezone , timedelta
28+
2729from uprotocol .proto .uri_pb2 import UUri , UEntity , UResource
2830from uprotocol .uri .serializer .longuriserializer import LongUriSerializer
2931from uprotocol .cloudevent .cloudevents_pb2 import CloudEvent
3032from uprotocol .proto .uattributes_pb2 import UMessageType , UPriority
33+ from uprotocol .proto .upayload_pb2 import UPayloadFormat
3134from uprotocol .cloudevent .factory .cloudeventfactory import CloudEventFactory
3235from uprotocol .proto .ustatus_pb2 import UCode
3336from uprotocol .uuid .factory .uuidfactory import Factories
@@ -90,6 +93,31 @@ def build_cloud_event_for_test():
9093 return cloud_event
9194
9295
96+ def build_cloud_event_for_test_with_id (id ):
97+ source = build_uri_for_test ()
98+ proto_payload = build_proto_payload_for_test ()
99+ # additional attributes
100+ u_cloud_event_attributes = (
101+ UCloudEventAttributesBuilder ()
102+ .with_hash ("somehash" )
103+ .with_priority (UPriority .UPRIORITY_CS1 )
104+ .with_ttl (3 )
105+ .with_token ("someOAuthToken" )
106+ .build ()
107+ )
108+
109+ # build the cloud event
110+ cloud_event = CloudEventFactory .build_base_cloud_event (
111+ id ,
112+ source ,
113+ proto_payload .SerializeToString (),
114+ proto_payload .type_url ,
115+ u_cloud_event_attributes ,
116+ UCloudEvent .get_event_type (UMessageType .UMESSAGE_TYPE_PUBLISH ),
117+ )
118+ return cloud_event
119+
120+
93121class TestUCloudEvent (unittest .TestCase ):
94122 DATA_CONTENT_TYPE = "application/x-protobuf"
95123
@@ -102,6 +130,7 @@ def test_extract_sink_from_cloudevent_when_sink_exists(self):
102130 sink = "//bo.cloud/petapp/1/rpc.response"
103131 cloud_event = build_cloud_event_for_test ()
104132 cloud_event .__setitem__ ("sink" , sink )
133+ cloud_event .__setitem__ ("plevel" , 4 )
105134 self .assertEqual (sink , UCloudEvent .get_sink (cloud_event ))
106135
107136 def test_extract_sink_from_cloudevent_when_sink_does_not_exist (self ):
@@ -193,6 +222,82 @@ def test_extract_platform_error_from_cloudevent_when_platform_error_exists_in_wr
193222 UCode .OK , UCloudEvent .get_communication_status (cloud_event )
194223 )
195224
225+ def test_extract_platform_error_from_cloudevent_when_platform_error_exists (
226+ self ,
227+ ):
228+ cloud_event = build_cloud_event_for_test ()
229+ cloud_event .__setitem__ ("commstatus" , UCode .INVALID_ARGUMENT )
230+ self .assertEqual (
231+ UCode .INVALID_ARGUMENT ,
232+ UCloudEvent .get_communication_status (cloud_event ),
233+ )
234+
235+ def test_extract_platform_error_from_cloudevent_when_platform_error_does_not_exist (
236+ self ,
237+ ):
238+ cloud_event = build_cloud_event_for_test ()
239+ self .assertEqual (
240+ UCode .OK , UCloudEvent .get_communication_status (cloud_event )
241+ )
242+
243+ def test_adding_platform_error_to_existing_cloudevent (
244+ self ,
245+ ):
246+ cloud_event = build_cloud_event_for_test ()
247+ self .assertEqual (
248+ UCode .OK , UCloudEvent .get_communication_status (cloud_event )
249+ )
250+
251+ cloud_event_1 = UCloudEvent .add_communication_status (
252+ cloud_event , UCode .DEADLINE_EXCEEDED
253+ )
254+
255+ self .assertEqual (
256+ UCode .OK , UCloudEvent .get_communication_status (cloud_event )
257+ )
258+
259+ self .assertEqual (
260+ UCode .DEADLINE_EXCEEDED ,
261+ UCloudEvent .get_communication_status (cloud_event_1 ),
262+ )
263+
264+ def test_adding_empty_platform_error_to_existing_cloudevent (
265+ self ,
266+ ):
267+ cloud_event = build_cloud_event_for_test ()
268+ self .assertEqual (
269+ UCode .OK , UCloudEvent .get_communication_status (cloud_event )
270+ )
271+
272+ cloud_event_1 = UCloudEvent .add_communication_status (cloud_event , None )
273+
274+ self .assertEqual (
275+ UCode .OK , UCloudEvent .get_communication_status (cloud_event )
276+ )
277+
278+ self .assertEqual (cloud_event , cloud_event_1 )
279+
280+ def test_extract_creation_timestamp_from_cloudevent_UUID_Id_when_not_a_UUIDV8_id (
281+ self ,
282+ ):
283+ cloud_event = build_cloud_event_for_test ()
284+ self .assertEqual (None , UCloudEvent .get_creation_timestamp (cloud_event ))
285+
286+ def test_extract_creation_timestamp_from_cloudevent_UUIDV8_Id_when_UUIDV8_id_is_valid (
287+ self ,
288+ ):
289+ uuid = Factories .UPROTOCOL .create ()
290+ str_uuid = LongUuidSerializer .instance ().serialize (uuid )
291+ cloud_event = build_cloud_event_for_test_with_id (str_uuid )
292+ maybe_creation_timestamp = UCloudEvent .get_creation_timestamp (
293+ cloud_event
294+ )
295+ self .assertIsNotNone (maybe_creation_timestamp )
296+ creation_timestamp = maybe_creation_timestamp / 1000
297+
298+ now_timestamp = datetime .now (timezone .utc ).timestamp ()
299+ self .assertAlmostEqual (creation_timestamp , now_timestamp , delta = 1 )
300+
196301 def test_cloudevent_is_not_expired_cd_when_no_ttl_configured (self ):
197302 cloud_event = build_cloud_event_for_test ()
198303 cloud_event .__delitem__ ("ttl" )
@@ -214,6 +319,14 @@ def test_cloudevent_is_not_expired_cd_when_ttl_is_minus_one(self):
214319 UCloudEvent .is_expired_by_cloud_event_creation_date (cloud_event )
215320 )
216321
322+ def test_cloudevent_is_expired_cd_when_ttl_is_one (self ):
323+ cloud_event = build_cloud_event_for_test ()
324+ cloud_event .__setitem__ ("ttl" , 1 )
325+ time .sleep (0.002 )
326+ self .assertTrue (
327+ UCloudEvent .is_expired_by_cloud_event_creation_date (cloud_event )
328+ )
329+
217330 def test_cloudevent_is_expired_when_ttl_1_mili (self ):
218331 uuid = Factories .UPROTOCOL .create ()
219332 str_uuid = LongUuidSerializer .instance ().serialize (uuid )
@@ -294,6 +407,8 @@ def test_from_message_with_valid_message(self):
294407 UCloudEvent .get_type (cloud_event1 ),
295408 )
296409
410+
411+
297412 def test_to_from_message_from_request_cloudevent (self ):
298413 # additional attributes
299414 u_cloud_event_attributes = (
@@ -444,6 +559,8 @@ def test_to_from_message_from_UCP_cloudevent(self):
444559 u_cloud_event_attributes ,
445560 )
446561 cloud_event .__setitem__ ("priority" , "CS4" )
562+ cloud_event .__setitem__ ("commstatus" , 16 )
563+ cloud_event .__setitem__ ("permission_level" , 4 )
447564
448565 result = UCloudEvent .toMessage (cloud_event )
449566 self .assertIsNotNone (result )
@@ -458,3 +575,39 @@ def test_from_message_with_null_message(self):
458575 with self .assertRaises (ValueError ) as context :
459576 UCloudEvent .fromMessage (None )
460577 self .assertTrue ("message cannot be null." in context .exception )
578+
579+ def test_cloud_event_to_string (self ):
580+ u_cloud_event_attributes = (
581+ UCloudEventAttributesBuilder ()
582+ .with_ttl (3 )
583+ .with_token ("someOAuthToken" )
584+ .build ()
585+ )
586+
587+ cloud_event = CloudEventFactory .request (
588+ build_uri_for_test (),
589+ "//bo.cloud/petapp/1/rpc.response" ,
590+ CloudEventFactory .generate_cloud_event_id (),
591+ build_proto_payload_for_test (),
592+ u_cloud_event_attributes ,
593+ )
594+ cloud_event_string = UCloudEvent .to_string (cloud_event )
595+ self .assertTrue (
596+ "source='/body.access//door.front_left#Door', sink='//bo.cloud/petapp/1/rpc.response', type='req.v1'}"
597+ in cloud_event_string
598+ )
599+
600+ def test_cloud_event_to_string_none (self ):
601+ cloud_event_string = UCloudEvent .to_string (None )
602+ self .assertEqual (
603+ cloud_event_string , "null"
604+ )
605+
606+ def test_get_upayload_format_from_content_type (self ):
607+ new_format = UCloudEvent ().get_upayload_format_from_content_type ("application/json" )
608+ self .assertEqual (new_format , UPayloadFormat .UPAYLOAD_FORMAT_JSON )
609+
610+ def test_to_message_none_entry (self ):
611+ with self .assertRaises (ValueError ) as context :
612+ UCloudEvent ().toMessage (None )
613+ self .assertTrue ("Cloud Event can't be None" in context .exception )
0 commit comments