EventManagerTest.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-27
  6. * Changed: 2023-05-16
  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. auto 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{};
  221. string expectedNews{};
  222. event_id seriousNewsEventId = SeriousNewsEvent{""}.getId();
  223. event_id gossipNewsEventId = GossipNewsEvent{""}.getId();
  224. // create event handler
  225. auto seriousNewsEventHandler = make_shared<EventHandler>(seriousNewsEventId); // event id
  226. auto gossipNewsEventHandler = make_shared<EventHandler>(gossipNewsEventId); // event id
  227. // create and fill event manager with handler
  228. auto eventManager = make_shared<EventManager>();
  229. eventManager->addEventHandler(seriousNewsEventHandler);
  230. eventManager->addEventHandler(gossipNewsEventHandler);
  231. // create news agency (listener)
  232. auto dailyNews = make_shared<DailyNewsAgency>();
  233. auto gossipNews = make_shared<GossipNewsAgency>();
  234. // fire SeriousNewsEvent event with no effect
  235. eventManager->fire(SeriousNewsEvent(news)); // event call
  236. ASSERT_TRUE(dailyNews->getNews().empty());
  237. ASSERT_TRUE(gossipNews->getNews().empty());
  238. // now subscribe to SeriousNewsEvent and fire SeriousNewsEvent event
  239. eventManager->subscribe(seriousNewsEventId, dailyNews);
  240. eventManager->subscribe(seriousNewsEventId, gossipNews);
  241. news = "COVID-19 is still going on!";
  242. eventManager->fire(SeriousNewsEvent(news)); // event call
  243. expectedNews = "DailyNewsAgency: " + news;
  244. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  245. expectedNews = "GossipNewsAgency: " + news;
  246. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  247. dailyNews->clear();
  248. gossipNews->clear();
  249. // unsubscribe SeriousNewsEvent from GossipNewsAgency
  250. eventManager->unsubscribe(seriousNewsEventId, gossipNews);
  251. eventManager->fire(SeriousNewsEvent(news)); // event call
  252. expectedNews = "DailyNewsAgency: " + news;
  253. ASSERT_STREQ(expectedNews.c_str(), dailyNews->getNews().c_str());
  254. ASSERT_TRUE(gossipNews->getNews().empty());
  255. dailyNews->clear();
  256. gossipNews->clear();
  257. // now let GossipNewsAgency subscribe to SeriousNewsEvent + GossipNewsEvent and fire both of them
  258. eventManager->subscribe(gossipNewsEventId, gossipNews);
  259. eventManager->subscribe(seriousNewsEventId, gossipNews);
  260. news = "COVID-19 is still going on!";
  261. eventManager->fire(SeriousNewsEvent(news)); // event call
  262. expectedNews = "GossipNewsAgency: " + news;
  263. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  264. news = "ape likes banana!";
  265. eventManager->fire(GossipNewsEvent(news)); // event call
  266. expectedNews = "GossipNewsAgency: " + news;
  267. ASSERT_STREQ(expectedNews.c_str(), gossipNews->getNews().c_str());
  268. }
  269. }