2929from concurrent .futures import Future
3030from google .protobuf .any_pb2 import Any
3131from google .protobuf .wrappers_pb2 import Int32Value
32+ from uprotocol .rpc .calloptions import CallOptions
33+
3234from uprotocol .cloudevent .cloudevents_pb2 import CloudEvent
3335from uprotocol .proto .uattributes_pb2 import UPriority
3436from uprotocol .proto .upayload_pb2 import UPayload , UPayloadFormat
35- from uprotocol .proto .uri_pb2 import UUri , UEntity
37+ from uprotocol .proto .uri_pb2 import UUri , UEntity , UAuthority
3638from uprotocol .proto .ustatus_pb2 import UStatus , UCode
3739from uprotocol .rpc .rpcclient import RpcClient
3840from uprotocol .rpc .rpcmapper import RpcMapper
3941from uprotocol .rpc .rpcresult import RpcResult
4042from uprotocol .transport .builder .uattributesbuilder import UAttributesBuilder
4143from uprotocol .uri .serializer .longuriserializer import LongUriSerializer
44+ from uprotocol .uri .factory .uresource_builder import UResourceBuilder
45+ from uprotocol .proto .umessage_pb2 import UMessage
46+
47+
48+ def build_source ():
49+ return UUri (authority = UAuthority (name = "vcu.someVin.veh.ultifi.gm.com" ),
50+ entity = UEntity (name = "petapp.ultifi.gm.com" , version_major = 1 ),
51+ resource = UResourceBuilder .for_rpc_request (None ))
4252
4353
4454def build_cloud_event ():
@@ -55,77 +65,77 @@ def build_topic():
5565 return LongUriSerializer ().deserialize ("//vcu.vin/hartley/1/rpc.Raise" )
5666
5767
58- def build_uattributes ():
59- return UAttributesBuilder . request ( UPriority . UPRIORITY_CS4 , UUri ( entity = UEntity ( name = "hartley" )), 1000 ). build ()
68+ def build_calloptions ():
69+ return CallOptions ()
6070
6171
6272class ReturnsNumber3 (RpcClient ):
63- def invoke_method (self , topic , payload , attributes ):
73+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
6474 future = Future ()
6575 any_obj = Any ()
6676 any_obj .Pack (Int32Value (value = 3 ))
6777 data = UPayload (format = UPayloadFormat .UPAYLOAD_FORMAT_PROTOBUF , value = any_obj .SerializeToString ())
68- future .set_result (data )
78+ future .set_result (UMessage ( payload = data ) )
6979 return future
7080
7181
7282class HappyPath (RpcClient ):
73- def invoke_method (self , topic , payload , attributes ):
83+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
7484 future = Future ()
7585 data = build_upayload ()
76- future .set_result (data )
86+ future .set_result (UMessage ( payload = data ) )
7787 return future
7888
7989
8090class WithUStatusCodeInsteadOfHappyPath (RpcClient ):
81- def invoke_method (self , topic , payload , attributes ):
91+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
8292 future = Future ()
8393 status = UStatus (code = UCode .INVALID_ARGUMENT , message = "boom" )
8494 any_value = Any ()
8595 any_value .Pack (status )
8696 data = UPayload (format = UPayloadFormat .UPAYLOAD_FORMAT_PROTOBUF , value = any_value .SerializeToString ())
87- future .set_result (data )
97+ future .set_result (UMessage ( payload = data ) )
8898 return future
8999
90100
91101class WithUStatusCodeHappyPath (RpcClient ):
92- def invoke_method (self , topic , payload , attributes ):
102+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
93103 future = Future ()
94104 status = UStatus (code = UCode .OK , message = "all good" )
95105 any_value = Any ()
96106 any_value .Pack (status )
97107 data = UPayload (format = UPayloadFormat .UPAYLOAD_FORMAT_PROTOBUF , value = any_value .SerializeToString ())
98- future .set_result (data )
108+ future .set_result (UMessage ( payload = data ) )
99109 return future
100110
101111
102112class ThatBarfsCrapyPayload (RpcClient ):
103- def invoke_method (self , topic , payload , attributes ):
113+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
104114 future = Future ()
105115 response = UPayload (format = UPayloadFormat .UPAYLOAD_FORMAT_RAW , value = bytes ([0 ]))
106- future .set_result (response )
116+ future .set_result (UMessage ( payload = response ) )
107117 return future
108118
109119
110120class ThatCompletesWithAnException (RpcClient ):
111- def invoke_method (self , topic , payload , attributes ):
121+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
112122 future = Future ()
113123 future .set_exception (RuntimeError ("Boom" ))
114124 return future
115125
116126
117127class ThatReturnsTheWrongProto (RpcClient ):
118- def invoke_method (self , topic , payload , attributes ):
128+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
119129 future = Future ()
120130 any_value = Any ()
121131 any_value .Pack (Int32Value (value = 42 ))
122132 data = UPayload (format = UPayloadFormat .UPAYLOAD_FORMAT_PROTOBUF , value = any_value .SerializeToString ())
123- future .set_result (data )
133+ future .set_result (UMessage ( payload = data ) )
124134 return future
125135
126136
127137class WithNullInPayload (RpcClient ):
128- def invoke_method (self , topic , payload , attributes ):
138+ def invoke_method (self , topic : UUri , payload : UPayload , options : CallOptions ):
129139 future = Future ()
130140 future .set_result (None )
131141 return future
@@ -170,14 +180,14 @@ class TestRpc(unittest.TestCase):
170180
171181 def test_compose_happy_path (self ):
172182 rpc_response = RpcMapper .map_response_to_result (
173- ReturnsNumber3 ().invoke_method (build_topic (), build_upayload (), build_uattributes ()), Int32Value )
183+ ReturnsNumber3 ().invoke_method (build_topic (), build_upayload (), build_calloptions ()), Int32Value )
174184 mapped = rpc_response .map (lambda x : x .value + 5 )
175185 self .assertTrue (rpc_response .isSuccess ())
176186 self .assertEqual (8 , mapped .successValue ())
177187
178188 def test_compose_that_returns_status (self ):
179189 rpc_response = RpcMapper .map_response_to_result (
180- WithUStatusCodeInsteadOfHappyPath ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
190+ WithUStatusCodeInsteadOfHappyPath ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
181191 Int32Value )
182192 mapped = rpc_response .map (lambda x : x .value + 5 )
183193 self .assertTrue (rpc_response .isFailure ())
@@ -186,7 +196,7 @@ def test_compose_that_returns_status(self):
186196
187197 def test_compose_with_failure (self ):
188198 rpc_response = RpcMapper .map_response_to_result (
189- ThatCompletesWithAnException ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
199+ ThatCompletesWithAnException ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
190200 Int32Value )
191201 mapped = rpc_response .map (lambda x : x .value + 5 )
192202 self .assertTrue (rpc_response .isFailure ())
@@ -195,47 +205,48 @@ def test_compose_with_failure(self):
195205
196206 def test_success_invoke_method_happy_flow_using_mapResponseToRpcResponse (self ):
197207 rpc_response = RpcMapper .map_response_to_result (
198- HappyPath ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
208+ HappyPath ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
199209 CloudEvent )
200210 self .assertTrue (rpc_response .isSuccess ())
201211 self .assertEqual (build_cloud_event (), rpc_response .successValue ())
202212
203213 def test_fail_invoke_method_when_invoke_method_returns_a_status_using_mapResponseToRpcResponse (self ):
204214 rpc_response = RpcMapper .map_response_to_result (
205- WithUStatusCodeInsteadOfHappyPath ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
215+ WithUStatusCodeInsteadOfHappyPath ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
206216 CloudEvent )
207217 self .assertTrue (rpc_response .isFailure ())
208218 self .assertEqual (UCode .INVALID_ARGUMENT , rpc_response .failureValue ().code )
209219 self .assertEqual ("boom" , rpc_response .failureValue ().message )
210220
211221 def test_fail_invoke_method_when_invoke_method_threw_an_exception_using_mapResponseToRpcResponse (self ):
212222 rpc_response = RpcMapper .map_response_to_result (
213- ThatCompletesWithAnException ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
223+ ThatCompletesWithAnException ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
214224 CloudEvent )
215225 self .assertTrue (rpc_response .isFailure ())
216226 self .assertEqual (UCode .UNKNOWN , rpc_response .failureValue ().code )
217227 self .assertEqual ("Boom" , rpc_response .failureValue ().message )
218228
219229 def test_fail_invoke_method_when_invoke_method_returns_a_bad_proto_using_mapResponseToRpcResponse (self ):
220230 rpc_response = RpcMapper .map_response_to_result (
221- ThatReturnsTheWrongProto ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
231+ ThatReturnsTheWrongProto ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
222232 CloudEvent )
223233 self .assertTrue (rpc_response .isFailure ())
224234 self .assertEqual (UCode .UNKNOWN , rpc_response .failureValue ().code )
225- self .assertEqual ("Unknown payload type [type.googleapis.com/google.protobuf.Int32Value]. Expected [io.cloudevents.v1.CloudEvent]" , rpc_response .failureValue ().message )
235+ self .assertEqual (
236+ "Unknown payload type [type.googleapis.com/google.protobuf.Int32Value]. Expected ["
237+ "io.cloudevents.v1.CloudEvent]" ,
238+ rpc_response .failureValue ().message )
226239
227240 def test_success_invoke_method_happy_flow_using_mapResponse (self ):
228241 rpc_response = RpcMapper .map_response (
229- HappyPath ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
242+ HappyPath ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
230243 CloudEvent )
231244 self .assertEqual (build_cloud_event (), rpc_response .result ())
232245
233246 def test_fail_invoke_method_when_invoke_method_returns_a_status_using_mapResponse (self ):
234247 rpc_response = RpcMapper .map_response (
235- WithUStatusCodeInsteadOfHappyPath ().invoke_method (build_topic (), build_upayload (), build_uattributes ()),
248+ WithUStatusCodeInsteadOfHappyPath ().invoke_method (build_topic (), build_upayload (), build_calloptions ()),
236249 CloudEvent )
237- exception = RuntimeError ("Unknown payload type [type.googleapis.com/uprotocol.v1.UStatus]. Expected [CloudEvent]" )
238- self .assertEqual (str (exception ),str (rpc_response .exception ()))
239-
240-
241-
250+ exception = RuntimeError (
251+ "Unknown payload type [type.googleapis.com/uprotocol.v1.UStatus]. Expected [CloudEvent]" )
252+ self .assertEqual (str (exception ), str (rpc_response .exception ()))
0 commit comments