Skip to content

Commit

Permalink
Merge pull request eclipse-uprotocol#244 from eclipse-uprotocol/v1.0_…
Browse files Browse the repository at this point in the history
…up-v1.6.0

Merging extra tests back from the v1.0 release branch
  • Loading branch information
gregmedd authored Jul 29, 2024
2 parents df0891f + 074301e commit f02105f
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 12 deletions.
98 changes: 93 additions & 5 deletions test/extra/NotificationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@
//
// SPDX-License-Identifier: Apache-2.0

#include <google/protobuf/util/message_differencer.h>
#include <gtest/gtest.h>
#include <up-cpp/communication/NotificationSink.h>
#include <up-cpp/communication/NotificationSource.h>

#include "UTransportMock.h"
#include "up-cpp/datamodel/serializer/UUri.h"

namespace {
using namespace uprotocol::communication;
using namespace uprotocol::datamodel::serializer::uri;
using MsgDiff = google::protobuf::util::MessageDifferencer;

class TestFixture : public testing::Test {
class NotificationTest : public testing::Test {
protected:
// Run once per TEST_F.
// Used to set up clean environments per test.
Expand All @@ -22,16 +31,95 @@ class TestFixture : public testing::Test {

// Run once per execution of the test application.
// Used for setup of all tests. Has access to this instance.
TestFixture() = default;
~TestFixture() = default;
NotificationTest() = default;
~NotificationTest() = default;

// Run once per execution of the test application.
// Used only for global setup outside of tests.
static void SetUpTestSuite() {}
static void TearDownTestSuite() {}

[[nodiscard]] uprotocol::v1::UUri buildValidTestTopic() const;
[[nodiscard]] uprotocol::v1::UUri buildValidDefaultSourceURI() const;
};

// TODO replace
TEST_F(TestFixture, SomeTestName) {}
[[nodiscard]] uprotocol::v1::UUri NotificationTest::buildValidDefaultSourceURI()
const {
uprotocol::v1::UUri testDefaultSourceURI_;
testDefaultSourceURI_.set_authority_name("10.0.0.1");
testDefaultSourceURI_.set_ue_id(0x18000);
testDefaultSourceURI_.set_ue_version_major(0x1);
testDefaultSourceURI_.set_resource_id(0x0);
return testDefaultSourceURI_;
}

[[nodiscard]] uprotocol::v1::UUri NotificationTest::buildValidTestTopic()
const {
uprotocol::v1::UUri testTopic_;
testTopic_.set_authority_name("10.0.0.2");
testTopic_.set_ue_id(0x00011101);
testTopic_.set_ue_version_major(0xF8);
testTopic_.set_resource_id(0x8101);
return testTopic_;
}

TEST_F(NotificationTest, NotificationSuccess) {
// Initialize
uprotocol::v1::UPayloadFormat format =
uprotocol::v1::UPayloadFormat::UPAYLOAD_FORMAT_TEXT;
std::optional<uprotocol::v1::UPriority> priority =
uprotocol::v1::UPriority::UPRIORITY_CS1;

std::optional<std::chrono::milliseconds> ttl =
std::chrono::milliseconds(1000);
uprotocol::v1::UUri testDefaultSourceURI = buildValidDefaultSourceURI();
uprotocol::v1::UUri testTopic = buildValidTestTopic();

// Notify Sink
auto transportMockNotificationSink =
std::make_shared<uprotocol::test::UTransportMock>(testDefaultSourceURI);

uprotocol::v1::UMessage capture_msg;
auto callback = [&capture_msg](const auto& message) {
capture_msg = message;
};

auto result = NotificationSink::create(transportMockNotificationSink,
std::move(callback), testTopic);

// Notify Source
std::string testPayloadStr = "test_payload";
auto transportMockNotificationSource_ =
std::make_shared<uprotocol::test::UTransportMock>(testDefaultSourceURI);

auto movableTopic = testTopic;

NotificationSource notificationSource(
transportMockNotificationSource_, std::move(movableTopic),
std::move(testDefaultSourceURI), format, priority, ttl);

uprotocol::datamodel::builder::Payload testPayload(testPayloadStr, format);
auto status = notificationSource.notify(std::move(testPayload));

EXPECT_EQ(
AsString::serialize(
transportMockNotificationSource_->message_.attributes().source()),
AsString::serialize(transportMockNotificationSink->source_filter_));

EXPECT_EQ(
AsString::serialize(
transportMockNotificationSource_->message_.attributes().sink()),
AsString::serialize(
transportMockNotificationSink->sink_filter_.value()));

// Manually bridge the two transports
transportMockNotificationSink->mockMessage(
transportMockNotificationSource_->message_);

// Test
EXPECT_TRUE(MsgDiff::Equals(transportMockNotificationSource_->message_,
capture_msg));
EXPECT_EQ(testPayloadStr, capture_msg.payload());
}

} // namespace
86 changes: 79 additions & 7 deletions test/extra/PublisherSubscriberTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,101 @@
//
// SPDX-License-Identifier: Apache-2.0

#include <google/protobuf/util/message_differencer.h>
#include <gtest/gtest.h>

#include "UTransportMock.h"
#include "up-cpp/communication/Publisher.h"
#include "up-cpp/communication/Subscriber.h"
#include "up-cpp/datamodel/serializer/UUri.h"

using namespace uprotocol::communication;
using namespace uprotocol::v1;
using namespace uprotocol::datamodel::serializer::uri;
using MsgDiff = google::protobuf::util::MessageDifferencer;

namespace {
using namespace uprotocol::datamodel::builder;

class TestFixture : public testing::Test {
class TestPublisherSubscriber : public testing::Test {
protected:
// Run once per TEST_F.
// Used to set up clean environments per test.
void SetUp() override {}
void SetUp() override {
source_.set_authority_name("10.0.0.1");
source_.set_ue_id(0x00011101);
source_.set_ue_version_major(0xF1);
source_.set_resource_id(0x0);

topic_.set_authority_name("10.0.0.1");
topic_.set_ue_id(0x10010001);
topic_.set_ue_version_major(0xF8);
topic_.set_resource_id(0x8101);
transportMock_ =
std::make_shared<uprotocol::test::UTransportMock>(source_);

format_ = UPayloadFormat::UPAYLOAD_FORMAT_TEXT;
priority_ = UPriority::UPRIORITY_CS2;
ttl_ = std::chrono::milliseconds(1000);
}

void TearDown() override {}

// Run once per execution of the test application.
// Used for setup of all tests. Has access to this instance.
TestFixture() = default;
~TestFixture() = default;
TestPublisherSubscriber() = default;
~TestPublisherSubscriber() = default;

// Run once per execution of the test application.
// Used only for global setup outside of tests.
static void SetUpTestSuite() {}
static void TearDownTestSuite() {}

std::shared_ptr<uprotocol::test::UTransportMock> transportMock_;
UUri source_;
UUri topic_;
UPayloadFormat format_;
std::optional<UPriority> priority_;
std::optional<std::chrono::milliseconds> ttl_;
};

// TODO replace
TEST_F(TestFixture, SomeTestName) {}
TEST_F(TestPublisherSubscriber, PubSubSuccess) {
// sub
auto transportSub =
std::make_shared<uprotocol::test::UTransportMock>(source_);

uprotocol::v1::UMessage captured_message;
auto callback = [&captured_message](auto message) {
captured_message = message;
};

auto result =
Subscriber::subscribe(transportSub, topic_, std::move(callback));

// pub
std::string testPayloadStr = "test_payload";
auto movableTopic = topic_;
Publisher publisher(transportMock_, std::move(movableTopic), format_,
priority_, ttl_);

uprotocol::v1::UStatus retval;
retval.set_code(uprotocol::v1::UCode::OK);
transportMock_->send_status_ = retval;

Payload testPayload(testPayloadStr, format_);
auto status = publisher.publish(std::move(testPayload));

// Test
EXPECT_EQ(
AsString::serialize(transportMock_->message_.attributes().source()),
AsString::serialize(transportSub->source_filter_));

// Manually bridge the two transports
transportSub->mockMessage(transportMock_->message_);

// Test
EXPECT_TRUE(MsgDiff::Equals(transportMock_->message_, captured_message));
EXPECT_EQ(testPayloadStr, captured_message.payload());
}

} // namespace
} // namespace

0 comments on commit f02105f

Please sign in to comment.