Skip to content

Commit 1d1da8a

Browse files
[Matthew D'Alonzo] initial commit (#15)
* [Matthew D'Alonzo] initial commit * [Matthew D'Alonzo] Removed protos from repository, added proto compiler script * Re-Added cloudevents_pb2.py * Add missing dependencies and fix uri serializer test * Resolved PR comments: removed javadoc references, changed send api to receive UMessage as parameter, removed 8 from comment, made Source a required field * Made on_receive receive uMessages, removed payload reference in rpcmapper * Fixed rpcmapper tests * Bug fix --------- Co-authored-by: gzm6xl_gme <[email protected]> Co-authored-by: Neelam Kushwah <[email protected]>
1 parent af9a04a commit 1d1da8a

27 files changed

+615
-281
lines changed

README.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ python -m pip install .
5454

5555
*This will install the up-python, making its classes and modules available for import in your python code.*
5656

57-
=== Using The Sdk
57+
=== Using The Library
5858

59-
The SDK is broken up into different packages that are described in <<sdk-packages>> below. Each package contains a README.adoc file that describes the purpose of the package and how to use it. Packages are organized into the following directories:
59+
The Library is broken up into different packages that are described in <<sdk-packages>> below. Each package contains a README.adoc file that describes the purpose of the package and how to use it. Packages are organized into the following directories:
6060

6161
.Package Folders
6262
[#pkg-folders,width=100%,cols="20%,80%",options="header"]

tests/test_rpc/test_rpc.py

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,26 @@
2929
from concurrent.futures import Future
3030
from google.protobuf.any_pb2 import Any
3131
from google.protobuf.wrappers_pb2 import Int32Value
32+
from uprotocol.rpc.calloptions import CallOptions
33+
3234
from uprotocol.cloudevent.cloudevents_pb2 import CloudEvent
3335
from uprotocol.proto.uattributes_pb2 import UPriority
3436
from 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
3638
from uprotocol.proto.ustatus_pb2 import UStatus, UCode
3739
from uprotocol.rpc.rpcclient import RpcClient
3840
from uprotocol.rpc.rpcmapper import RpcMapper
3941
from uprotocol.rpc.rpcresult import RpcResult
4042
from uprotocol.transport.builder.uattributesbuilder import UAttributesBuilder
4143
from 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

4454
def 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

6272
class 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

7282
class 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

8090
class 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

91101
class 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

102112
class 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

110120
class 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

117127
class 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

127137
class 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()))

tests/test_transport/test_builder/test_uattributesbuilder.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@
2929
from uprotocol.transport.builder.uattributesbuilder import UAttributesBuilder
3030
from uprotocol.proto.uattributes_pb2 import UPriority, UMessageType
3131
from uprotocol.proto.uri_pb2 import UUri, UAuthority, UEntity
32-
from uprotocol.uri.builder.uresource_builder import UResourceBuilder
32+
from uprotocol.uri.factory.uresource_builder import UResourceBuilder
3333
from uprotocol.uuid.factory.uuidfactory import Factories
3434

35+
def build_source():
36+
return UUri(authority=UAuthority(name="vcu.someVin.veh.ultifi.gm.com"),
37+
entity=UEntity(name="petapp.ultifi.gm.com", version_major=1),
38+
resource=UResourceBuilder.for_rpc_request(None))
39+
3540

3641
def build_sink():
3742
return UUri(authority=UAuthority(name="vcu.someVin.veh.ultifi.gm.com"),
@@ -46,16 +51,18 @@ def get_uuid():
4651
class TestUAttributesBuilder(unittest.TestCase):
4752

4853
def test_publish(self):
49-
builder = UAttributesBuilder.publish(UPriority.UPRIORITY_CS1)
54+
source = build_source()
55+
builder = UAttributesBuilder.publish(source, UPriority.UPRIORITY_CS1)
5056
self.assertIsNotNone(builder)
5157
attributes = builder.build()
5258
self.assertIsNotNone(attributes)
5359
self.assertEqual(UMessageType.UMESSAGE_TYPE_PUBLISH, attributes.type)
5460
self.assertEqual(UPriority.UPRIORITY_CS1, attributes.priority)
5561

5662
def test_notification(self):
63+
source = build_source()
5764
sink = build_sink()
58-
builder = UAttributesBuilder.notification(UPriority.UPRIORITY_CS1, sink)
65+
builder = UAttributesBuilder.notification(source, sink, UPriority.UPRIORITY_CS1)
5966
self.assertIsNotNone(builder)
6067
attributes = builder.build()
6168
self.assertIsNotNone(attributes)
@@ -64,9 +71,10 @@ def test_notification(self):
6471
self.assertEqual(sink, attributes.sink)
6572

6673
def test_request(self):
74+
source = build_source()
6775
sink = build_sink()
6876
ttl = 1000
69-
builder = UAttributesBuilder.request(UPriority.UPRIORITY_CS4, sink, ttl)
77+
builder = UAttributesBuilder.request(source, sink, UPriority.UPRIORITY_CS4, ttl)
7078
self.assertIsNotNone(builder)
7179
attributes = builder.build()
7280
self.assertIsNotNone(attributes)
@@ -76,9 +84,10 @@ def test_request(self):
7684
self.assertEqual(ttl, attributes.ttl)
7785

7886
def test_response(self):
87+
source = build_source()
7988
sink = build_sink()
8089
req_id = get_uuid()
81-
builder = UAttributesBuilder.response(UPriority.UPRIORITY_CS6, sink, req_id)
90+
builder = UAttributesBuilder.response(source, sink, UPriority.UPRIORITY_CS6, req_id)
8291
self.assertIsNotNone(builder)
8392
attributes = builder.build()
8493
self.assertIsNotNone(attributes)
@@ -89,14 +98,15 @@ def test_response(self):
8998

9099
def test_build(self):
91100
req_id = get_uuid()
92-
builder = UAttributesBuilder.publish(UPriority.UPRIORITY_CS1).withTtl(1000).withToken("test_token").withSink(
101+
builder = UAttributesBuilder.publish(build_source(), UPriority.UPRIORITY_CS1).withTtl(1000).withToken("test_token").withSink(
93102
build_sink()).withPermissionLevel(2).withCommStatus(1).withReqId(req_id)
94103
attributes = builder.build()
95104
self.assertIsNotNone(attributes)
96105
self.assertEqual(UMessageType.UMESSAGE_TYPE_PUBLISH, attributes.type)
97106
self.assertEqual(UPriority.UPRIORITY_CS1, attributes.priority)
98107
self.assertEqual(1000, attributes.ttl)
99108
self.assertEqual("test_token", attributes.token)
109+
self.assertEqual(build_source(), attributes.source)
100110
self.assertEqual(build_sink(), attributes.sink)
101111
self.assertEqual(2, attributes.permission_level)
102112
self.assertEqual(1, attributes.commstatus)

0 commit comments

Comments
 (0)