EventManagerTest.cpp 8.8 KB

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