SerializableJsonEventTest.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-20
  6. * Changed: 2023-03-25
  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. 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. }