Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/pull_and_compile_protos.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

REPO_URL = "https://github.com/eclipse-uprotocol/up-spec.git"
PROTO_REPO_DIR = os.path.abspath("../target")
TAG_NAME = "v1.6.0-alpha.2"
TAG_NAME = "v1.6.0-alpha.3"
PROTO_OUTPUT_DIR = os.path.abspath("../uprotocol/")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,43 +581,26 @@ async def test_unregister_notification_api_for_the_happy_path(self):

try:
await subscriber.register_for_notifications(self.topic, handler)
await subscriber.unregister_for_notifications(self.topic, handler)
await subscriber.unregister_for_notifications(self.topic)
except Exception as e:
self.fail(f"Exception occurred: {e}")

async def test_unregister_notification_api_topic_missing(self):
handler = MagicMock(spec=SubscriptionChangeHandler)
handler.handle_subscription_change.return_value = NotImplementedError(
"Unimplemented method 'handle_subscription_change'"
)
self.transport.get_source.return_value = self.source

subscriber = InMemoryUSubscriptionClient(self.transport, self.rpc_client, self.notifier)
self.assertIsNotNone(subscriber)
with self.assertRaises(ValueError) as error:
await subscriber.unregister_for_notifications(None, handler)
await subscriber.unregister_for_notifications(None)
self.assertEqual("Topic missing", str(error.exception))

async def test_unregister_notification_api_handler_missing(self):
self.transport.get_source.return_value = self.source

subscriber = InMemoryUSubscriptionClient(self.transport, self.rpc_client, self.notifier)
self.assertIsNotNone(subscriber)
with self.assertRaises(ValueError) as error:
await subscriber.unregister_for_notifications(self.topic, None)
self.assertEqual("Handler missing", str(error.exception))

async def test_unregister_notification_api_options_none(self):
handler = MagicMock(spec=SubscriptionChangeHandler)
handler.handle_subscription_change.return_value = NotImplementedError(
"Unimplemented method 'handle_subscription_change'"
)
self.transport.get_source.return_value = self.source

subscriber = InMemoryUSubscriptionClient(self.transport, self.rpc_client, self.notifier)
self.assertIsNotNone(subscriber)
with self.assertRaises(ValueError) as error:
await subscriber.unregister_for_notifications(self.topic, handler, None)
await subscriber.unregister_for_notifications(self.topic, None)
self.assertEqual("CallOptions missing", str(error.exception))

async def test_register_notification_api_options_none(self):
Expand Down
Empty file.
Empty file.
89 changes: 89 additions & 0 deletions tests/test_client/test_utwin/test_v2/test_simpleutwinclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""
SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation

See the NOTICE file(s) distributed with this work for additional
information regarding copyright ownership.

This program and the accompanying materials are made available under the
terms of the Apache License Version 2.0 which is available at

http://www.apache.org/licenses/LICENSE-2.0

SPDX-License-Identifier: Apache-2.0
"""

import unittest
from unittest.mock import AsyncMock

from uprotocol.client.utwin.v2.simpleutwinclient import SimpleUTwinClient
from uprotocol.communication.rpcclient import RpcClient
from uprotocol.communication.upayload import UPayload
from uprotocol.communication.ustatuserror import UStatusError
from uprotocol.core.utwin.v2.utwin_pb2 import GetLastMessagesResponse
from uprotocol.v1.ucode_pb2 import UCode
from uprotocol.v1.uri_pb2 import UUri, UUriBatch


class SimpleUTwinClientTest(unittest.IsolatedAsyncioTestCase):
def setUp(self):
# Mocking RpcClient
self.rpc_client = AsyncMock(spec=RpcClient)

# Creating a sample UUri for tests
self.topic = UUri(authority_name="test", ue_id=3, ue_version_major=1, resource_id=0x8000)

async def test_get_last_messages(self):
"""
Test calling get_last_messages() with valid topics.
"""
# Creating a UUriBatch with one topic
topics = UUriBatch(uris=[self.topic])

# Mocking RpcClient's invoke_method to return a successful response
self.rpc_client.invoke_method.return_value = UPayload.pack(GetLastMessagesResponse())

client = SimpleUTwinClient(self.rpc_client)
response = await client.get_last_messages(topics)

self.assertIsNotNone(response)
self.assertIsInstance(response, GetLastMessagesResponse)

async def test_get_last_messages_empty_topics(self):
"""
Test calling get_last_messages() with empty topics.
"""
# Creating an empty UUriBatch
topics = UUriBatch()

client = SimpleUTwinClient(self.rpc_client)

with self.assertRaises(UStatusError) as context:
await client.get_last_messages(topics)

# Asserting the exception type and message
self.assertEqual(context.exception.status.code, UCode.INVALID_ARGUMENT)
self.assertEqual(context.exception.status.message, "topics must not be empty")

async def test_get_last_messages_exception(self):
"""
Test calling get_last_messages() when the RpcClient completes exceptionally.
"""
# Creating a UUriBatch with one topic
topics = UUriBatch(uris=[self.topic])

# Mocking RpcClient's invoke_method to raise an exception
exception = UStatusError.from_code_message(UCode.NOT_FOUND, "Not found")
self.rpc_client.invoke_method.return_value = exception

client = SimpleUTwinClient(self.rpc_client)

with self.assertRaises(UStatusError) as context:
await client.get_last_messages(topics)

# Asserting the exception type and message
self.assertEqual(context.exception.status.code, UCode.NOT_FOUND)
self.assertEqual(context.exception.status.message, "Not found")


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion tests/test_communication/test_simplepublisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class TestSimplePublisher(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.transport = MagicMock(spec=UTransport)
self.topic = UUri(authority_name="neelam", ue_id=3, ue_version_major=1, resource_id=2)
self.topic = UUri(authority_name="neelam", ue_id=3, ue_version_major=1, resource_id=0x8000)

async def test_send_publish(self):
self.transport.send.return_value = UStatus(code=UCode.OK)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_communication/test_upayload.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_is_empty_when_passing_null(self):
self.assertTrue(UPayload.is_empty(None))

def test_unpacking_a_upayload_calling_unpack_with_null(self):
self.assertFalse(isinstance(UPayload.unpack_from_umessage(None, UUri), message.Message))
self.assertFalse(isinstance(UPayload.unpack(None, UUri), message.Message))
self.assertFalse(isinstance(UPayload.unpack(UPayload.pack(None), UUri), message.Message))

Expand Down Expand Up @@ -113,6 +114,14 @@ def test_hash_code(self):
payload = UPayload.pack_to_any(uri)
self.assertEqual(payload.__hash__(), payload.__hash__())

def test_unpack_passing_a_valid_umessage(self):
uri = UUri(authority_name="Neelam")
payload = UPayload.pack_to_any(uri)
umsg = UMessage(payload=payload.data)
unpacked = UPayload.unpack_from_umessage(umsg, UUri)
self.assertTrue(isinstance(unpacked, message.Message))
self.assertEqual(uri, unpacked)


if __name__ == '__main__':
unittest.main()
97 changes: 83 additions & 14 deletions tests/test_transport/test_builder/test_umessagebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from uprotocol.v1.ucode_pb2 import UCode
from uprotocol.v1.umessage_pb2 import UMessage
from uprotocol.v1.uri_pb2 import UUri
from uprotocol.v1.uuid_pb2 import UUID


def build_source():
Expand All @@ -38,6 +39,14 @@ def build_sink():
return UUri(ue_id=2, ue_version_major=1, resource_id=0)


def build_topic():
return UUri(ue_id=2, ue_version_major=1, resource_id=0x8000)


def build_method():
return UUri(ue_id=2, ue_version_major=1, resource_id=1)


def get_uuid():
return Factories.UPROTOCOL.create()

Expand All @@ -47,7 +56,7 @@ def test_publish(self):
"""
Test Publish
"""
publish: UMessage = UMessageBuilder.publish(build_source()).build()
publish: UMessage = UMessageBuilder.publish(build_topic()).build()
self.assertIsNotNone(publish)
self.assertEqual(UMessageType.UMESSAGE_TYPE_PUBLISH, publish.attributes.type)
self.assertEqual(UPriority.UPRIORITY_CS1, publish.attributes.priority)
Expand All @@ -57,7 +66,7 @@ def test_notification(self):
Test Notification
"""
sink = build_sink()
notification: UMessage = UMessageBuilder.notification(build_source(), sink).build()
notification: UMessage = UMessageBuilder.notification(build_topic(), sink).build()
self.assertIsNotNone(notification)
self.assertEqual(
UMessageType.UMESSAGE_TYPE_NOTIFICATION,
Expand All @@ -70,7 +79,7 @@ def test_request(self):
"""
Test Request
"""
sink = build_sink()
sink = build_method()
ttl = 1000
request: UMessage = UMessageBuilder.request(build_source(), sink, ttl).build()
self.assertIsNotNone(request)
Expand All @@ -83,7 +92,7 @@ def test_request_with_priority(self):
"""
Test Request
"""
sink = build_sink()
sink = build_method()
ttl = 1000
request: UMessage = (
UMessageBuilder.request(build_source(), sink, ttl).with_priority(UPriority.UPRIORITY_CS5).build()
Expand All @@ -100,7 +109,7 @@ def test_response(self):
"""
sink = build_sink()
req_id = get_uuid()
response: UMessage = UMessageBuilder.response(build_source(), sink, req_id).build()
response: UMessage = UMessageBuilder.response(build_method(), sink, req_id).build()
self.assertIsNotNone(response)
self.assertEqual(UMessageType.UMESSAGE_TYPE_RESPONSE, response.attributes.type)
self.assertEqual(UPriority.UPRIORITY_CS4, response.attributes.priority)
Expand All @@ -111,7 +120,7 @@ def test_response_with_existing_request(self):
"""
Test Response with existing request
"""
request: UMessage = UMessageBuilder.request(build_source(), build_sink(), 1000).build()
request: UMessage = UMessageBuilder.request(build_source(), build_method(), 1000).build()
response: UMessage = UMessageBuilder.response_for_request(request.attributes).build()
self.assertIsNotNone(response)
self.assertEqual(UMessageType.UMESSAGE_TYPE_RESPONSE, response.attributes.type)
Expand All @@ -126,7 +135,7 @@ def test_build(self):
Test Build
"""
builder: UMessageBuilder = (
UMessageBuilder.publish(build_source())
UMessageBuilder.publish(build_topic())
.with_token("test_token")
.with_permission_level(2)
.with_commstatus(UCode.CANCELLED)
Expand All @@ -147,7 +156,7 @@ def test_build_with_upayload(self):
"""
Test building UMessage with UPayload payload
"""
message: UMessage = UMessageBuilder.publish(build_source()).build_from_upayload(
message: UMessage = UMessageBuilder.publish(build_topic()).build_from_upayload(
UPayload(format=UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF, data=build_sink().SerializeToString())
)
self.assertIsNotNone(message)
Expand All @@ -162,7 +171,7 @@ def test_build_with_any_payload(self):
"""
Test building UMessage with Any payload
"""
message: UMessage = UMessageBuilder.publish(build_source()).build_from_upayload(
message: UMessage = UMessageBuilder.publish(build_topic()).build_from_upayload(
UPayload(format=UPayloadFormat.UPAYLOAD_FORMAT_PROTOBUF_WRAPPED_IN_ANY, data=Any().SerializeToString())
)
self.assertIsNotNone(message)
Expand All @@ -179,7 +188,7 @@ def test_build_response_with_wrong_priority(self):
"""
sink = build_sink()
req_id = get_uuid()
response = UMessageBuilder.response(build_source(), sink, req_id).with_priority(UPriority.UPRIORITY_CS3).build()
response = UMessageBuilder.response(build_method(), sink, req_id).with_priority(UPriority.UPRIORITY_CS3).build()
self.assertIsNotNone(response)
self.assertEqual(UMessageType.UMESSAGE_TYPE_RESPONSE, response.attributes.type)
self.assertEqual(UPriority.UPRIORITY_CS4, response.attributes.priority)
Expand All @@ -190,7 +199,7 @@ def test_build_request_with_wrong_priority(self):
"""
Test building request with wrong priority
"""
sink = build_sink()
sink = build_method()
ttl = 1000
request = UMessageBuilder.request(build_source(), sink, ttl).with_priority(UPriority.UPRIORITY_CS0).build()
self.assertIsNotNone(request)
Expand All @@ -204,7 +213,7 @@ def test_build_notification_with_wrong_priority(self):
Test building notification with wrong priority
"""
sink = build_sink()
notification = UMessageBuilder.notification(build_source(), sink).with_priority(UPriority.UPRIORITY_CS0).build()
notification = UMessageBuilder.notification(build_topic(), sink).with_priority(UPriority.UPRIORITY_CS0).build()
self.assertIsNotNone(notification)
self.assertEqual(
UMessageType.UMESSAGE_TYPE_NOTIFICATION,
Expand All @@ -217,7 +226,7 @@ def test_build_publish_with_wrong_priority(self):
"""
Test building publish with wrong priority
"""
publish = UMessageBuilder.publish(build_source()).with_priority(UPriority.UPRIORITY_CS0).build()
publish = UMessageBuilder.publish(build_topic()).with_priority(UPriority.UPRIORITY_CS0).build()
self.assertIsNotNone(publish)
self.assertEqual(UMessageType.UMESSAGE_TYPE_PUBLISH, publish.attributes.type)
self.assertEqual(UPriority.UPRIORITY_CS1, publish.attributes.priority)
Expand All @@ -226,7 +235,7 @@ def test_build_publish_with_priority(self):
"""
Test building publish with priority
"""
publish = UMessageBuilder.publish(build_source()).with_priority(UPriority.UPRIORITY_CS4).build()
publish = UMessageBuilder.publish(build_topic()).with_priority(UPriority.UPRIORITY_CS4).build()
self.assertIsNotNone(publish)
self.assertEqual(UMessageType.UMESSAGE_TYPE_PUBLISH, publish.attributes.type)
self.assertEqual(UPriority.UPRIORITY_CS4, publish.attributes.priority)
Expand Down Expand Up @@ -293,3 +302,63 @@ def test_response_req_id_is_none(self):
"""
with self.assertRaises(ValueError):
UMessageBuilder.response(build_source(), build_sink(), None)

def test_publish_with_invalid_source(self):
with self.assertRaises(ValueError):
UMessageBuilder.publish(build_source()).build()

def test_notification_with_invalid_source(self):
with self.assertRaises(ValueError):
UMessageBuilder.notification(build_source(), build_sink()).build()

def test_notification_with_invalid_sink(self):
with self.assertRaises(ValueError):
UMessageBuilder.notification(build_topic(), build_topic()).build()

def test_request_with_invalid_source(self):
with self.assertRaises(ValueError):
UMessageBuilder.request(build_method(), build_method(), 1000).build()

def test_request_with_invalid_sink(self):
with self.assertRaises(ValueError):
UMessageBuilder.request(build_source(), build_source(), 1000).build()

def test_request_with_invalid_source_and_sink(self):
with self.assertRaises(ValueError):
UMessageBuilder.request(build_method(), build_source(), 1000).build()

def test_request_with_null_ttl(self):
with self.assertRaises(ValueError):
UMessageBuilder.request(build_source(), build_method(), None).build()

def test_request_with_negative_ttl(self):
with self.assertRaises(ValueError):
UMessageBuilder.request(build_source(), build_method(), -1).build()

def test_response_with_invalid_source(self):
with self.assertRaises(ValueError):
UMessageBuilder.response(build_sink(), build_sink(), Factories.UPROTOCOL.create()).build()

def test_response_with_invalid_sink(self):
with self.assertRaises(ValueError):
UMessageBuilder.response(build_method(), build_method(), Factories.UPROTOCOL.create()).build()

def test_response_with_invalid_source_and_sink(self):
with self.assertRaises(ValueError):
UMessageBuilder.response(build_source(), build_source(), Factories.UPROTOCOL.create()).build()

def test_response_with_null_req_id(self):
with self.assertRaises(ValueError):
UMessageBuilder.response(build_method(), build_sink(), None).build()

def test_response_with_invalid_req_id(self):
with self.assertRaises(ValueError):
UMessageBuilder.response(build_method(), build_sink(), UUID()).build()

def test_notification_with_invalid_source_and_sink(self):
with self.assertRaises(ValueError):
UMessageBuilder.notification(build_sink(), build_source()).build()

def test_response_builder_with_invalid_request_type(self):
with self.assertRaises(ValueError):
UMessageBuilder.response_for_request(UAttributes()).build()
Loading
Loading