EventTest.cpp 4.2 KB

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