-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matteo Botticci
committed
May 20, 2024
1 parent
6b9b492
commit d2dbe4f
Showing
4 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <gtest/gtest.h> | ||
#include "MockSubscriber.h" | ||
#include "ResultCode.h" | ||
|
||
using namespace DogBreeds::JackRussell; | ||
|
||
class AbstractTopicTest : public ::testing::Test { | ||
protected: | ||
std::shared_ptr<AbstractTopic> topic; | ||
void SetUp() override { | ||
topic = std::make_shared<AbstractTopic>("test_topic"); | ||
} | ||
}; | ||
|
||
TEST_F(AbstractTopicTest, AddSubscriber) { | ||
auto subscriber = std::make_shared<MockSubscriber<int>>("test_subscriber"); | ||
EXPECT_EQ(topic->addSubscriber(subscriber), ResultCode::OK); | ||
} | ||
|
||
TEST_F(AbstractTopicTest, RemoveSubscriber) { | ||
auto subscriber = std::make_shared<MockSubscriber<int>>("test_subscriber"); | ||
topic->addSubscriber(subscriber); | ||
EXPECT_EQ(topic->removeSubscriber(subscriber), ResultCode::OK); | ||
} | ||
|
||
TEST_F(AbstractTopicTest, GetName) { | ||
EXPECT_EQ(topic->getName(), "test_topic"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* MockSubscriber.h | ||
* | ||
* Created on: 20 mag 2024 | ||
* Author: BOTTICCIM | ||
*/ | ||
|
||
#ifndef TEST_MOCKSUBSCRIBER_H_ | ||
#define TEST_MOCKSUBSCRIBER_H_ | ||
|
||
#include "Subscriber.hpp" | ||
#include <gmock/gmock.h> | ||
|
||
namespace DogBreeds { | ||
namespace JackRussell { | ||
|
||
template <typename T> | ||
class MockSubscriber : public Subscriber<T> { | ||
public: | ||
MockSubscriber(const std::string &name) : Subscriber<T>(name) {} | ||
MOCK_METHOD(void, onMessage, (std::shared_ptr<T> message), (const, override)); | ||
}; | ||
|
||
} // namespace JackRussell | ||
} // namespace DogBreeds | ||
|
||
|
||
|
||
#endif /* TEST_MOCKSUBSCRIBER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// test_topic.cpp | ||
#include <gtest/gtest.h> | ||
#include "MockSubscriber.h" | ||
#include "Topic.hpp" | ||
#include "TopicManager.h" | ||
#include "ResultCode.h" | ||
|
||
using namespace DogBreeds::JackRussell; | ||
|
||
|
||
class TopicManagerTest : public ::testing::Test { | ||
protected: | ||
TopicManager &manager = TopicManager::getInstance(); | ||
void SetUp() override { | ||
manager = TopicManager::getInstance(); | ||
} | ||
}; | ||
|
||
TEST_F(TopicManagerTest, CreateTopic) { | ||
EXPECT_EQ(manager.createTopic<int>("test_topic"), ResultCode::OK); | ||
} | ||
|
||
TEST_F(TopicManagerTest, CreateExistingTopic) { | ||
manager.createTopic<int>("test_topic"); | ||
EXPECT_EQ(manager.createTopic<int>("test_topic"), ResultCode::TOPIC_ALREDY_EXIST); | ||
} | ||
|
||
TEST_F(TopicManagerTest, DeleteTopic) { | ||
manager.createTopic<int>("test_topic"); | ||
EXPECT_EQ(manager.deleteTopic("test_topic"), ResultCode::OK); | ||
} | ||
|
||
TEST_F(TopicManagerTest, SubscribeToTopic) { | ||
manager.createTopic<int>("test_topic"); | ||
auto subscriber = std::make_shared<MockSubscriber<int>>("test_subscriber"); | ||
EXPECT_EQ(manager.subscribeToTopic(subscriber, "test_topic"), ResultCode::OK); | ||
} | ||
|
||
TEST_F(TopicManagerTest, UnsubscribeFromTopic) { | ||
manager.createTopic<int>("test_topic"); | ||
auto subscriber = std::make_shared<MockSubscriber<int>>("test_subscriber"); | ||
manager.subscribeToTopic(subscriber, "test_topic"); | ||
EXPECT_EQ(manager.unsubscribeFromTopic(subscriber, "test_topic"), ResultCode::OK); | ||
} | ||
|
||
TEST_F(TopicManagerTest, PublishToTopic) { | ||
manager.createTopic<int>("test_topic"); | ||
auto subscriber = std::make_shared<MockSubscriber<int>>("test_subscriber"); | ||
manager.subscribeToTopic(subscriber, "test_topic"); | ||
|
||
auto message = std::make_shared<int>(42); | ||
EXPECT_CALL(*subscriber, onMessage(message)).Times(1); | ||
|
||
EXPECT_EQ(manager.publishToTopic("test_topic", message), ResultCode::OK); | ||
} | ||
|
||
|