SerializableJsonEventTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-20
  6. * Changed: 2023-05-19
  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. public:
  26. SerializableJsonEventTest() = default;
  27. ~SerializableJsonEventTest() override = default;
  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. SerializableJsonEvent serializable{make_shared<Event>("TMP_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. SerializableJsonEvent serializable{make_shared<Event>("TMP_EVENT")};
  70. EXPECT_THROW(
  71. {
  72. try
  73. {
  74. serializable.setValue(nullptr);
  75. }
  76. catch (const IllegalArgumentException &_exception)
  77. {
  78. throw;
  79. }
  80. },
  81. IllegalArgumentException);
  82. }
  83. }