EventManagerTest.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2023-02-06
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls-std-event-test.hpp>
  11. #include <ls-std/ls-std-event.hpp>
  12. using namespace ls::std::core;
  13. using namespace ls::std::core::type;
  14. using namespace ls::std::event;
  15. using namespace ::std;
  16. using namespace test::event;
  17. namespace
  18. {
  19. class EventManagerTest : public ::testing::Test
  20. {
  21. protected:
  22. EventManagerTest() = default;
  23. ~EventManagerTest() override = default;
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. };
  29. TEST_F(EventManagerTest, getClassName)
  30. {
  31. EventManager eventManager{};
  32. ASSERT_STREQ("EventManager", eventManager.getClassName().c_str());
  33. }
  34. TEST_F(EventManagerTest, subscribe_empty_id)
  35. {
  36. EXPECT_THROW(
  37. {
  38. try
  39. {
  40. EventManager eventManager{};
  41. eventManager.subscribe("", make_shared<DailyNewsAgency>());
  42. }
  43. catch (const IllegalArgumentException &_exception)
  44. {
  45. throw;
  46. }
  47. },
  48. IllegalArgumentException);
  49. }
  50. TEST_F(EventManagerTest, subscribe_no_listener)
  51. {
  52. EXPECT_THROW(
  53. {
  54. try
  55. {
  56. EventManager eventManager{};
  57. eventManager.subscribe("TMP_ID", nullptr);
  58. }
  59. catch (const IllegalArgumentException &_exception)
  60. {
  61. throw;
  62. }
  63. },
  64. IllegalArgumentException);
  65. }
  66. TEST_F(EventManagerTest, subscribe_no_event_handler_available)
  67. {
  68. EXPECT_THROW(
  69. {
  70. try
  71. {
  72. EventManager eventManager{};
  73. eventManager.subscribe("TMP_DIR", make_shared<DailyNewsAgency>());
  74. }
  75. catch (const EventNotSubscribedException &_exception)
  76. {
  77. throw;
  78. }
  79. },
  80. EventNotSubscribedException);
  81. }
  82. TEST_F(EventManagerTest, unsubscribe_empty_id)
  83. {
  84. EXPECT_THROW(
  85. {
  86. try
  87. {
  88. EventManager eventManager{};
  89. eventManager.unsubscribe("", make_shared<DailyNewsAgency>());
  90. }
  91. catch (const IllegalArgumentException &_exception)
  92. {
  93. throw;
  94. }
  95. },
  96. IllegalArgumentException);
  97. }
  98. TEST_F(EventManagerTest, unsubscribe_no_listener)
  99. {
  100. EXPECT_THROW(
  101. {
  102. try
  103. {
  104. EventManager eventManager{};
  105. eventManager.unsubscribe("TMP_ID", nullptr);
  106. }
  107. catch (const IllegalArgumentException &_exception)
  108. {
  109. throw;
  110. }
  111. },
  112. IllegalArgumentException);
  113. }
  114. TEST_F(EventManagerTest, addEventHandler)
  115. {
  116. EventManager eventManager{};
  117. ASSERT_TRUE(eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID")));
  118. }
  119. TEST_F(EventManagerTest, addEventHandler_event_handler_already_exists)
  120. {
  121. EventManager eventManager{};
  122. ASSERT_TRUE(eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID")));
  123. ASSERT_FALSE(eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID")));
  124. }
  125. TEST_F(EventManagerTest, addEventHandler_no_reference)
  126. {
  127. EXPECT_THROW(
  128. {
  129. try
  130. {
  131. EventManager eventManager{};
  132. eventManager.addEventHandler(nullptr);
  133. }
  134. catch (const IllegalArgumentException &_exception)
  135. {
  136. throw;
  137. }
  138. },
  139. IllegalArgumentException);
  140. }
  141. TEST_F(EventManagerTest, fire_event_handler_not_available)
  142. {
  143. EXPECT_THROW(
  144. {
  145. try
  146. {
  147. EventManager eventManager{};
  148. eventManager.fire(Event{"TMP_ID"});
  149. }
  150. catch (const EventNotHandledException &_exception)
  151. {
  152. throw;
  153. }
  154. },
  155. EventNotHandledException);
  156. }
  157. TEST_F(EventManagerTest, hasEventHandler)
  158. {
  159. EventManager eventManager{};
  160. eventManager.addEventHandler(make_shared<EventHandler>("TMP_ID"));
  161. ASSERT_TRUE(eventManager.hasEventHandler("TMP_ID"));
  162. }
  163. TEST_F(EventManagerTest, hasEventHandler_no_event_handler_available)
  164. {
  165. EventManager eventManager{};
  166. ASSERT_FALSE(eventManager.hasEventHandler("TMP_ID"));
  167. }
  168. TEST_F(EventManagerTest, hasEventHandler_empty_id)
  169. {
  170. EXPECT_THROW(
  171. {
  172. try
  173. {
  174. EventManager eventManager{};
  175. bool hasHandler = eventManager.hasEventHandler("");
  176. }
  177. catch (const IllegalArgumentException &_exception)
  178. {
  179. throw;
  180. }
  181. },
  182. IllegalArgumentException);
  183. }
  184. TEST_F(EventManagerTest, removeEventHandler)
  185. {
  186. EventManager eventManager{};
  187. shared_ptr<EventHandler> eventHandler = make_shared<EventHandler>("TMP_ID");
  188. eventManager.addEventHandler(eventHandler);
  189. ASSERT_TRUE(eventManager.removeEventHandler(eventHandler));
  190. }
  191. TEST_F(EventManagerTest, removeEventHandler_no_event_handler_available)
  192. {
  193. EventManager eventManager{};
  194. ASSERT_FALSE(eventManager.removeEventHandler(make_shared<EventHandler>("TMP_ID")));
  195. }
  196. TEST_F(EventManagerTest, removeEventHandler_no_reference)
  197. {
  198. EXPECT_THROW(
  199. {
  200. try
  201. {
  202. EventManager eventManager{};
  203. eventManager.removeEventHandler(nullptr);
  204. }
  205. catch (const IllegalArgumentException &_exception)
  206. {
  207. throw;
  208. }
  209. },
  210. IllegalArgumentException);
  211. }
  212. TEST_F(EventManagerTest, production_example)
  213. {
  214. string news, expectedNews{};
  215. event_id seriousNewsEventId = SeriousNewsEvent{""}.getId();
  216. event_id gossipNewsEventId = GossipNewsEvent{""}.getId();
  217. // create event handler
  218. shared_ptr<EventHandler> seriousNewsEventHandler = make_shared<EventHandler>(seriousNewsEventId); // event id
  219. shared_ptr<EventHandler> gossipNewsEventHandler = make_shared<EventHandler>(gossipNewsEventId); // event id
  220. // create and fill event manager with handler
  221. shared_ptr<EventManager> eventManager = make_shared<EventManager>();
  222. eventManager->addEventHandler(seriousNewsEventHandler);
  223. eventManager->addEventHandler(gossipNewsEventHandler);
  224. // create news agency (listener)
  225. shared_ptr<DailyNewsAgency> dailyNews = make_shared<DailyNewsAgency>();
  226. shared_ptr<GossipNewsAgency> gossipNews = make_shared<GossipNewsAgency>();
  227. // fire SeriousNewsEvent event with no effect
  228. eventManager->fire(SeriousNewsEvent(news)); // event call
  229. ASSERT_TRUE(dailyNews->getNews().empty());
  230. ASSERT_TRUE(gossipNews->getNews().empty());
  231. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  232. eventManager->subscribe(seriousNewsEventId, dailyNews);
  233. eventManager->subscribe(seriousNewsEventId, gossipNews);
  234. news = "COVID-19 is still going on!";
  235. eventManager->fire(SeriousNewsEvent(news)); // event call
  236. expectedNews = "DailyNewsAgency: " + news;
  237. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  238. expectedNews = "GossipNewsAgency: " + news;
  239. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  240. dailyNews->clear();
  241. gossipNews->clear();
  242. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  243. eventManager->unsubscribe(seriousNewsEventId, gossipNews);
  244. eventManager->fire(SeriousNewsEvent(news)); // event call
  245. expectedNews = "DailyNewsAgency: " + news;
  246. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  247. ASSERT_TRUE(gossipNews->getNews().empty());
  248. dailyNews->clear();
  249. gossipNews->clear();
  250. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  251. eventManager->subscribe(gossipNewsEventId, gossipNews);
  252. eventManager->subscribe(seriousNewsEventId, gossipNews);
  253. news = "COVID-19 is still going on!";
  254. eventManager->fire(SeriousNewsEvent(news)); // event call
  255. expectedNews = "GossipNewsAgency: " + news;
  256. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  257. news = "ape likes banana!";
  258. eventManager->fire(GossipNewsEvent(news)); // event call
  259. expectedNews = "GossipNewsAgency: " + news;
  260. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  261. }
  262. }