EventManagerTest.cpp 8.2 KB

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