EventTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-26
  6. * Changed: 2021-05-24
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. namespace
  12. {
  13. class EventTest : public ::testing::Test
  14. {
  15. protected:
  16. EventTest() = default;
  17. ~EventTest() override = default;
  18. void SetUp() override
  19. {}
  20. void TearDown() override
  21. {}
  22. static ls_std::Event createSerializableTestEvent()
  23. {
  24. ls_std::Event event{"OPEN_DOOR_EVENT"};
  25. event.addParameter(ls_std::event_parameter{"key_available", "true"});
  26. event.addParameter(ls_std::event_parameter{"door_id", "16675"});
  27. std::shared_ptr<ls_std::SerializableJsonEvent> serializable = std::make_shared<ls_std::SerializableJsonEvent>(std::make_shared<ls_std::Event>(event));
  28. event.setSerializable(serializable);
  29. return event;
  30. }
  31. };
  32. // implementation
  33. TEST_F(EventTest, marshal)
  34. {
  35. ls_std::Event event = createSerializableTestEvent();
  36. ls_std::byte_field data = event.marshal();
  37. ASSERT_FALSE(data.empty());
  38. std::string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  39. ASSERT_STREQ(expectedString.c_str(), data.c_str());
  40. }
  41. TEST_F(EventTest, unmarshal)
  42. {
  43. std::shared_ptr<ls_std::Event> event = std::make_shared<ls_std::Event>("TMP_EVENT");
  44. std::shared_ptr<ls_std::SerializableJsonEvent> serializable = std::make_shared<ls_std::SerializableJsonEvent>(event);
  45. event->setSerializable(serializable);
  46. std::string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  47. event->unmarshal(jsonString);
  48. ASSERT_STREQ("OPEN_DOOR_EVENT", event->getId().c_str());
  49. ls_std::event_parameter_list parameterList = event->getParameterList();
  50. ASSERT_FALSE(parameterList.empty());
  51. ASSERT_EQ(2, parameterList.size());
  52. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  53. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  54. }
  55. // additional functionality
  56. TEST_F(EventTest, getClassName)
  57. {
  58. ls_std::Event event{"TMP_ID"};
  59. ASSERT_STREQ("Event", event.getClassName().c_str());
  60. }
  61. TEST_F(EventTest, constructor_empty_parameter)
  62. {
  63. EXPECT_THROW({
  64. try
  65. {
  66. ls_std::Event event{""};
  67. }
  68. catch (const ls_std::IllegalArgumentException &_exception)
  69. {
  70. throw;
  71. }
  72. }, ls_std::IllegalArgumentException);
  73. }
  74. TEST_F(EventTest, addParameter)
  75. {
  76. ls_std::Event event{"TMP_ID"};
  77. ASSERT_TRUE(event.getParameterList().empty());
  78. ASSERT_TRUE(event.addParameter(ls_std::event_parameter("key", "yes")));
  79. ASSERT_TRUE(event.addParameter(ls_std::event_parameter("facing_door", "yes")));
  80. }
  81. TEST_F(EventTest, addParameter_element_already_exists)
  82. {
  83. ls_std::Event event{"TMP_ID"};
  84. ASSERT_TRUE(event.getParameterList().empty());
  85. ASSERT_TRUE(event.addParameter(ls_std::event_parameter("key", "yes")));
  86. ASSERT_FALSE(event.addParameter(ls_std::event_parameter("key", "yes")));
  87. }
  88. TEST_F(EventTest, clearParameterList)
  89. {
  90. ls_std::Event event{"OPEN_DOOR_EVENT"};
  91. event.addParameter(ls_std::event_parameter("key", "yes"));
  92. event.addParameter(ls_std::event_parameter("facing_door", "yes"));
  93. ASSERT_EQ(2, event.getParameterList().size());
  94. event.clearParameterList();
  95. ASSERT_TRUE(event.getParameterList().empty());
  96. ASSERT_EQ(0, event.getParameterList().size());
  97. }
  98. TEST_F(EventTest, getId)
  99. {
  100. ls_std::Event event{"OPEN_DOOR_EVENT"};
  101. ASSERT_STREQ("OPEN_DOOR_EVENT", event.getId().c_str());
  102. }
  103. TEST_F(EventTest, getParameterList)
  104. {
  105. ls_std::Event event{"OPEN_DOOR_EVENT"};
  106. ASSERT_TRUE(event.getParameterList().empty());
  107. }
  108. TEST_F(EventTest, removeParameter)
  109. {
  110. ls_std::Event event{"OPEN_DOOR_EVENT"};
  111. ASSERT_TRUE(event.getParameterList().empty());
  112. event.addParameter(ls_std::event_parameter("key", "yes"));
  113. event.addParameter(ls_std::event_parameter("facing_door", "yes"));
  114. ASSERT_TRUE(event.removeParameter("key"));
  115. ASSERT_TRUE(event.removeParameter("facing_door"));
  116. }
  117. TEST_F(EventTest, removeParameter_elenent_does_not_exist)
  118. {
  119. ls_std::Event event{"OPEN_DOOR_EVENT"};
  120. ASSERT_FALSE(event.removeParameter("key"));
  121. ASSERT_FALSE(event.removeParameter("facing_door"));
  122. }
  123. TEST_F(EventTest, setId)
  124. {
  125. ls_std::Event event{"OPEN_DOOR_EVENT"};
  126. ASSERT_STREQ("OPEN_DOOR_EVENT", event.getId().c_str());
  127. event.setId("ANOTHER_EVENT");
  128. ASSERT_STREQ("ANOTHER_EVENT", event.getId().c_str());
  129. }
  130. TEST_F(EventTest, setId_empty_parameter)
  131. {
  132. EXPECT_THROW({
  133. try
  134. {
  135. ls_std::Event event{"TMP_ID"};
  136. event.setId("");
  137. }
  138. catch (const ls_std::IllegalArgumentException &_exception)
  139. {
  140. throw;
  141. }
  142. }, ls_std::IllegalArgumentException);
  143. }
  144. TEST_F(EventTest, setSerializable_no_reference)
  145. {
  146. EXPECT_THROW({
  147. try
  148. {
  149. ls_std::Event event{"TMP_ID"};
  150. event.setSerializable(nullptr);
  151. }
  152. catch (const ls_std::IllegalArgumentException &_exception)
  153. {
  154. throw;
  155. }
  156. }, ls_std::IllegalArgumentException);
  157. }
  158. }