Skip to content

Commit

Permalink
Merge pull request #276 from eclipse-uprotocol/v1.0_up-v1.6.0
Browse files Browse the repository at this point in the history
Bringing 1.0 bugfixes back to main
  • Loading branch information
gregmedd committed Aug 13, 2024
2 parents 910bc9d + 506b110 commit 712bd81
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/communication/NotificationSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ NotificationSource::NotificationSource(
notify_builder_(
UMessageBuilder::notification(std::move(source), std::move(sink))
.withPriority(priority.value_or(v1::UPriority::UPRIORITY_CS1))) {
if (!transport_) {
throw transport::NullTransport("transport cannot be null");
}

if (payload_format.has_value()) {
notify_builder_.withPayloadFormat(payload_format.value());
}
Expand All @@ -35,11 +39,16 @@ NotificationSource::NotificationSource(
v1::UStatus NotificationSource::notify(
datamodel::builder::Payload&& payload) const {
auto message = notify_builder_.build(std::move(payload));

return transport_->send(std::move(message));
}

v1::UStatus NotificationSource::notify() const {
auto message = notify_builder_.build();
if (!transport_) {
throw transport::NullTransport("transport cannot be null");
}

return transport_->send(std::move(message));
}

Expand Down
8 changes: 8 additions & 0 deletions src/communication/Publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ Publisher::Publisher(std::shared_ptr<transport::UTransport> transport,
publish_builder_.withPayloadFormat(format).withPriority(
priority.value_or(v1::UPriority::UPRIORITY_CS1));

if (!transport_) {
throw transport::NullTransport("transport cannot be null");
}

if (ttl.has_value()) {
publish_builder_.withTtl(ttl.value());
}
}

v1::UStatus Publisher::publish(datamodel::builder::Payload&& payload) const {
auto message = publish_builder_.build(std::move(payload));
if (!transport_) {
throw transport::NullTransport("transport cannot be null");
}

return transport_->send(std::move(message));
}

Expand Down
16 changes: 15 additions & 1 deletion src/communication/RpcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ RpcServer::RpcServer(std::shared_ptr<transport::UTransport> transport,
std::optional<std::chrono::milliseconds> ttl)
: transport_(std::move(transport)),
ttl_(ttl),
expected_payload_format_(std::move(format)) {}
expected_payload_format_(std::move(format)) {
if (!transport_) {
throw transport::NullTransport("transport cannot be null");
}
}

RpcServer::ServerOrStatus RpcServer::create(
std::shared_ptr<transport::UTransport> transport,
Expand All @@ -29,6 +33,11 @@ RpcServer::ServerOrStatus RpcServer::create(
std::optional<std::chrono::milliseconds> ttl) {
// Validate the method name using a URI validator.
auto [valid, reason] = Validator::uri::isValidRpcMethod(method_name);

if (!transport) {
throw transport::NullTransport("transport cannot be null");
}

if (!valid) {
// If the method name is invalid, return an error status.
v1::UStatus status;
Expand Down Expand Up @@ -67,6 +76,11 @@ RpcServer::ServerOrStatus RpcServer::create(

v1::UStatus RpcServer::connect(const v1::UUri& method, RpcCallback&& callback) {
callback_ = std::move(callback);

if (!transport_) {
throw transport::NullTransport("transport cannot be null");
}

auto result = transport_->registerListener(
// listener=
[this](const v1::UMessage& request) {
Expand Down
3 changes: 3 additions & 0 deletions src/communication/Subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Subscriber::Subscriber(std::shared_ptr<transport::UTransport> transport,
ListenHandle&& subscription)
: transport_(transport), subscription_(std::move(subscription)) {
// Constructor body. Any additional setup can go here.
if (!transport_) {
throw transport::NullTransport("transport cannot be null");
}
}

} // namespace uprotocol::communication
10 changes: 10 additions & 0 deletions test/coverage/communication/NotificationSourceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,14 @@ TEST_F(TestNotificationSource, NotifyWithoutPayloadFailure) {
EXPECT_EQ(status.code(), retval.code());
}

// Test with Null transport
TEST_F(TestNotificationSource, NullTransport) {
auto transport = nullptr;

EXPECT_THROW(NotificationSource notificationSource(
transport, std::move(source_), std::move(sink_), format_,
priority_, ttl_),
uprotocol::transport::NullTransport);
}

} // namespace
8 changes: 8 additions & 0 deletions test/coverage/communication/PublisherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,12 @@ TEST_F(TestPublisher, PublishSuccessWithoutPriority) {
v1::UPriority::UPRIORITY_CS1);
}

// publisher with null transport
TEST_F(TestPublisher, PublisherWithNullTransport) {
auto transport = nullptr;
EXPECT_THROW(Publisher publisher(transport, std::move(topic_), format_,
priority_, ttl_),
uprotocol::transport::NullTransport);
}

} // namespace
14 changes: 14 additions & 0 deletions test/coverage/communication/RpcServerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ TEST_F(TestRpcServer, ConstructorValidParams) {
ASSERT_NE(serverPtr, nullptr);
}

// Null transport
TEST_F(TestRpcServer, CreateWithNullTransport) {
// Define a callback function to be used with the RpcServer
uprotocol::communication::RpcServer::RpcCallback callback =
RpcCallbackNoReturn;

auto transport = nullptr;
// Attempt to create an RpcServer instance with valid parameters
EXPECT_THROW(
auto serverOrStatus = uprotocol::communication::RpcServer::create(
transport, *method_uri_, std::move(callback)),
uprotocol::transport::NullTransport);
}

// Test to verify RpcServer construction with a specific payload format
TEST_F(TestRpcServer, ConstructorWithPayloadFormat) {
// Define a callback that returns a specific value, simulating a response
Expand Down

0 comments on commit 712bd81

Please sign in to comment.