SerializableJsonEventTest.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-20
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. namespace
  12. {
  13. class SerializableJsonEventTest : public ::testing::Test
  14. {
  15. protected:
  16. SerializableJsonEventTest() = default;
  17. ~SerializableJsonEventTest() override = default;
  18. void SetUp() override
  19. {}
  20. void TearDown() override
  21. {}
  22. };
  23. TEST_F(SerializableJsonEventTest, marshal)
  24. {
  25. ls_std::Event event{"OPEN_DOOR_EVENT"};
  26. event.addParameter(ls_std::event_parameter{"key_available", "true"});
  27. event.addParameter(ls_std::event_parameter{"door_id", "16675"});
  28. ls_std::SerializableJsonEvent serializable{std::make_shared<ls_std::Event>(event)};
  29. ls_std::byte_field data = serializable.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(SerializableJsonEventTest, unmarshal)
  35. {
  36. ls_std::Event event{"TMP_EVENT"};
  37. ls_std::SerializableJsonEvent serializable{std::make_shared<ls_std::Event>(event)};
  38. std::string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  39. serializable.unmarshal(jsonString);
  40. ASSERT_STREQ("OPEN_DOOR_EVENT", serializable.getValue()->getId().c_str());
  41. ls_std::event_parameter_list parameterList = serializable.getValue()->getParameterList();
  42. ASSERT_FALSE(parameterList.empty());
  43. ASSERT_EQ(2, parameterList.size());
  44. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  45. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  46. }
  47. }