SerializableJSONEventTest.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-20
  6. * Changed: 2020-12-20
  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. void TearDown() override {}
  20. };
  21. TEST_F(SerializableJSONEventTest, marshal)
  22. {
  23. ls_std::Event event {"OPEN_DOOR_EVENT"};
  24. event.addParameter(ls_std::event_parameter{"key_available", "true"});
  25. event.addParameter(ls_std::event_parameter{"door_id", "16675"});
  26. ls_std::SerializableJSONEvent serializable {std::make_shared<ls_std::Event>(event)};
  27. ls_std::byte_field data = serializable.marshal();
  28. ASSERT_FALSE(data.empty());
  29. std::string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  30. ASSERT_STREQ(expectedString.c_str(), data.c_str());
  31. }
  32. TEST_F(SerializableJSONEventTest, unmarshal)
  33. {
  34. ls_std::Event event {"TMP_EVENT"};
  35. ls_std::SerializableJSONEvent serializable {std::make_shared<ls_std::Event>(event)};
  36. std::string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  37. serializable.unmarshal(jsonString);
  38. ASSERT_STREQ("OPEN_DOOR_EVENT", serializable.getValue()->getId().c_str());
  39. ls_std::event_parameter_list parameterList = serializable.getValue()->getParameterList();
  40. ASSERT_FALSE(parameterList.empty());
  41. ASSERT_EQ(2, parameterList.size());
  42. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  43. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  44. }
  45. }