SerializableJsonEventTest.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-20
  6. * Changed: 2021-09-19
  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, constructor_parameter_not_set)
  24. {
  25. EXPECT_THROW({
  26. try
  27. {
  28. ls_std::SerializableJsonEvent serializable{nullptr};
  29. }
  30. catch (const ls_std::IllegalArgumentException &_exception)
  31. {
  32. throw;
  33. }
  34. }, ls_std::IllegalArgumentException);
  35. }
  36. TEST_F(SerializableJsonEventTest, marshal)
  37. {
  38. ls_std::Event event{"OPEN_DOOR_EVENT"};
  39. event.addParameter(ls_std::event_parameter{"key_available", "true"});
  40. event.addParameter(ls_std::event_parameter{"door_id", "16675"});
  41. ls_std::SerializableJsonEvent serializable{std::make_shared<ls_std::Event>(event)};
  42. ls_std::byte_field data = serializable.marshal();
  43. ASSERT_FALSE(data.empty());
  44. std::string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  45. ASSERT_STREQ(expectedString.c_str(), data.c_str());
  46. }
  47. TEST_F(SerializableJsonEventTest, unmarshal)
  48. {
  49. ls_std::Event event{"TMP_EVENT"};
  50. ls_std::SerializableJsonEvent serializable{std::make_shared<ls_std::Event>(event)};
  51. std::string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  52. serializable.unmarshal(jsonString);
  53. ASSERT_STREQ("OPEN_DOOR_EVENT", serializable.getValue()->getId().c_str());
  54. ls_std::event_parameter_list parameterList = serializable.getValue()->getParameterList();
  55. ASSERT_FALSE(parameterList.empty());
  56. ASSERT_EQ(2, parameterList.size());
  57. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  58. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  59. }
  60. TEST_F(SerializableJsonEventTest, setValue_parameter_not_set)
  61. {
  62. ls_std::Event event{"TMP_EVENT"};
  63. ls_std::SerializableJsonEvent serializable{std::make_shared<ls_std::Event>(event)};
  64. EXPECT_THROW({
  65. try
  66. {
  67. serializable.setValue(nullptr);
  68. }
  69. catch (const ls_std::IllegalArgumentException &_exception)
  70. {
  71. throw;
  72. }
  73. }, ls_std::IllegalArgumentException);
  74. }
  75. }