SerializableJsonEventTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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-22
  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. using namespace ::testing;
  17. namespace
  18. {
  19. class SerializableJsonEventTest : public Test
  20. {
  21. protected:
  22. SerializableJsonEventTest() = default;
  23. ~SerializableJsonEventTest() override = default;
  24. void SetUp() override
  25. {}
  26. void TearDown() override
  27. {}
  28. };
  29. TEST_F(SerializableJsonEventTest, constructor_parameter_not_set)
  30. {
  31. EXPECT_THROW(
  32. {
  33. try
  34. {
  35. SerializableJsonEvent serializable{nullptr};
  36. }
  37. catch (const IllegalArgumentException &_exception)
  38. {
  39. throw;
  40. }
  41. },
  42. IllegalArgumentException);
  43. }
  44. TEST_F(SerializableJsonEventTest, marshal)
  45. {
  46. Event event{"OPEN_DOOR_EVENT"};
  47. event.addParameter(event_parameter{"key_available", "true"});
  48. event.addParameter(event_parameter{"door_id", "16675"});
  49. SerializableJsonEvent serializable{make_shared<Event>(event)};
  50. byte_field data = serializable.marshal();
  51. ASSERT_FALSE(data.empty());
  52. string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  53. ASSERT_STREQ(expectedString.c_str(), data.c_str());
  54. }
  55. TEST_F(SerializableJsonEventTest, unmarshal)
  56. {
  57. Event event{"TMP_EVENT"};
  58. SerializableJsonEvent serializable{make_shared<Event>(event)};
  59. string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  60. serializable.unmarshal(jsonString);
  61. ASSERT_STREQ("OPEN_DOOR_EVENT", serializable.getValue()->getId().c_str());
  62. event_parameter_list parameterList = serializable.getValue()->getParameterList();
  63. ASSERT_FALSE(parameterList.empty());
  64. ASSERT_EQ(2, parameterList.size());
  65. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  66. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  67. }
  68. TEST_F(SerializableJsonEventTest, setValue_parameter_not_set)
  69. {
  70. Event event{"TMP_EVENT"};
  71. SerializableJsonEvent serializable{make_shared<Event>(event)};
  72. EXPECT_THROW(
  73. {
  74. try
  75. {
  76. serializable.setValue(nullptr);
  77. }
  78. catch (const IllegalArgumentException &_exception)
  79. {
  80. throw;
  81. }
  82. },
  83. IllegalArgumentException);
  84. }
  85. }