EventManagerTest.cpp 8.5 KB

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