EventManagerTest.cpp 8.5 KB

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