SerializableJsonEventTest.cpp 2.9 KB

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