SerializableJsonEventTest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-20
  6. * Changed: 2022-05-14
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std_core.hpp>
  11. #include <ls_std/ls_std_event.hpp>
  12. namespace
  13. {
  14. class SerializableJsonEventTest : public ::testing::Test
  15. {
  16. protected:
  17. SerializableJsonEventTest() = default;
  18. ~SerializableJsonEventTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. TEST_F(SerializableJsonEventTest, constructor_parameter_not_set)
  25. {
  26. EXPECT_THROW({
  27. try
  28. {
  29. ls::std::event::SerializableJsonEvent serializable{nullptr};
  30. }
  31. catch (const ls::std::core::IllegalArgumentException &_exception)
  32. {
  33. throw;
  34. }
  35. }, ls::std::core::IllegalArgumentException);
  36. }
  37. TEST_F(SerializableJsonEventTest, marshal)
  38. {
  39. ls::std::event::Event event{"OPEN_DOOR_EVENT"};
  40. event.addParameter(ls::std::core::type::event_parameter{"key_available", "true"});
  41. event.addParameter(ls::std::core::type::event_parameter{"door_id", "16675"});
  42. ls::std::event::SerializableJsonEvent serializable{std::make_shared<ls::std::event::Event>(event)};
  43. ls::std::core::type::byte_field data = serializable.marshal();
  44. ASSERT_FALSE(data.empty());
  45. std::string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  46. ASSERT_STREQ(expectedString.c_str(), data.c_str());
  47. }
  48. TEST_F(SerializableJsonEventTest, unmarshal)
  49. {
  50. ls::std::event::Event event{"TMP_EVENT"};
  51. ls::std::event::SerializableJsonEvent serializable{std::make_shared<ls::std::event::Event>(event)};
  52. std::string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  53. serializable.unmarshal(jsonString);
  54. ASSERT_STREQ("OPEN_DOOR_EVENT", serializable.getValue()->getId().c_str());
  55. ls::std::core::type::event_parameter_list parameterList = serializable.getValue()->getParameterList();
  56. ASSERT_FALSE(parameterList.empty());
  57. ASSERT_EQ(2, parameterList.size());
  58. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  59. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  60. }
  61. TEST_F(SerializableJsonEventTest, setValue_parameter_not_set)
  62. {
  63. ls::std::event::Event event{"TMP_EVENT"};
  64. ls::std::event::SerializableJsonEvent serializable{std::make_shared<ls::std::event::Event>(event)};
  65. EXPECT_THROW({
  66. try
  67. {
  68. serializable.setValue(nullptr);
  69. }
  70. catch (const ls::std::core::IllegalArgumentException &_exception)
  71. {
  72. throw;
  73. }
  74. }, ls::std::core::IllegalArgumentException);
  75. }
  76. }