EventTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-26
  6. * Changed: 2021-04-23
  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. };
  23. // implementation
  24. TEST_F(EventTest, marshal)
  25. {
  26. ls_std::Event event{"OPEN_DOOR_EVENT"};
  27. event.addParameter(ls_std::event_parameter{"key_available", "true"});
  28. event.addParameter(ls_std::event_parameter{"door_id", "16675"});
  29. std::shared_ptr<ls_std::SerializableJSONEvent> serializable = std::make_shared<ls_std::SerializableJSONEvent>(std::make_shared<ls_std::Event>(event));
  30. event.setSerializable(serializable);
  31. ls_std::byte_field data = event.marshal();
  32. ASSERT_FALSE(data.empty());
  33. std::string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  34. ASSERT_STREQ(expectedString.c_str(), data.c_str());
  35. }
  36. TEST_F(EventTest, unmarshal)
  37. {
  38. std::shared_ptr<ls_std::Event> event = std::make_shared<ls_std::Event>("TMP_EVENT");
  39. std::shared_ptr<ls_std::SerializableJSONEvent> serializable = std::make_shared<ls_std::SerializableJSONEvent>(event);
  40. event->setSerializable(serializable);
  41. std::string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  42. event->unmarshal(jsonString);
  43. ASSERT_STREQ("OPEN_DOOR_EVENT", event->getId().c_str());
  44. ls_std::event_parameter_list parameterList = event->getParameterList();
  45. ASSERT_FALSE(parameterList.empty());
  46. ASSERT_EQ(2, parameterList.size());
  47. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  48. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  49. }
  50. // additional functionality
  51. TEST_F(EventTest, getClassName)
  52. {
  53. ls_std::Event event{"OPEN_DOOR_EVENT"};
  54. ASSERT_STREQ("Event", event.getClassName().c_str());
  55. }
  56. TEST_F(EventTest, addParameter)
  57. {
  58. ls_std::Event event{"OPEN_DOOR_EVENT"};
  59. ASSERT_TRUE(event.getParameterList().empty());
  60. event.addParameter(ls_std::event_parameter("key", "yes"));
  61. event.addParameter(ls_std::event_parameter("facing_door", "yes"));
  62. ls_std::event_parameter_list parameterList = event.getParameterList();
  63. ASSERT_FALSE(parameterList.empty());
  64. ASSERT_EQ(2, parameterList.size());
  65. ASSERT_STREQ("yes", parameterList.at("key").c_str());
  66. ASSERT_STREQ("yes", parameterList.at("facing_door").c_str());
  67. }
  68. TEST_F(EventTest, clearParameterList)
  69. {
  70. ls_std::Event event{"OPEN_DOOR_EVENT"};
  71. event.addParameter(ls_std::event_parameter("key", "yes"));
  72. event.addParameter(ls_std::event_parameter("facing_door", "yes"));
  73. ASSERT_EQ(2, event.getParameterList().size());
  74. event.clearParameterList();
  75. ASSERT_TRUE(event.getParameterList().empty());
  76. ASSERT_EQ(0, event.getParameterList().size());
  77. }
  78. TEST_F(EventTest, getId)
  79. {
  80. ls_std::Event event{"OPEN_DOOR_EVENT"};
  81. ASSERT_STREQ("OPEN_DOOR_EVENT", event.getId().c_str());
  82. }
  83. TEST_F(EventTest, getParameterList)
  84. {
  85. ls_std::Event event{"OPEN_DOOR_EVENT"};
  86. ASSERT_TRUE(event.getParameterList().empty());
  87. }
  88. TEST_F(EventTest, removeParameter)
  89. {
  90. ls_std::Event event{"OPEN_DOOR_EVENT"};
  91. ASSERT_TRUE(event.getParameterList().empty());
  92. event.addParameter(ls_std::event_parameter("key", "yes"));
  93. event.addParameter(ls_std::event_parameter("facing_door", "yes"));
  94. ASSERT_FALSE(event.getParameterList().empty());
  95. ASSERT_EQ(2, event.getParameterList().size());
  96. event.removeParameter("key");
  97. ASSERT_EQ(1, event.getParameterList().size());
  98. event.removeParameter("facing_door");
  99. ASSERT_EQ(0, event.getParameterList().size());
  100. ASSERT_TRUE(event.getParameterList().empty());
  101. }
  102. TEST_F(EventTest, setId)
  103. {
  104. ls_std::Event event{"OPEN_DOOR_EVENT"};
  105. ASSERT_STREQ("OPEN_DOOR_EVENT", event.getId().c_str());
  106. event.setId("ANOTHER_EVENT");
  107. ASSERT_STREQ("ANOTHER_EVENT", event.getId().c_str());
  108. }
  109. }