SerializableJsonEventTest.cpp 2.7 KB

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