SerializableJsonEventTest.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-20
  6. * Changed: 2022-11-09
  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. try
  32. {
  33. SerializableJsonEvent serializable{nullptr};
  34. }
  35. catch (const IllegalArgumentException &_exception)
  36. {
  37. throw;
  38. }
  39. }, IllegalArgumentException);
  40. }
  41. TEST_F(SerializableJsonEventTest, marshal)
  42. {
  43. Event event{"OPEN_DOOR_EVENT"};
  44. event.addParameter(event_parameter{"key_available", "true"});
  45. event.addParameter(event_parameter{"door_id", "16675"});
  46. SerializableJsonEvent serializable{make_shared<Event>(event)};
  47. byte_field data = serializable.marshal();
  48. ASSERT_FALSE(data.empty());
  49. string expectedString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  50. ASSERT_STREQ(expectedString.c_str(), data.c_str());
  51. }
  52. TEST_F(SerializableJsonEventTest, unmarshal)
  53. {
  54. Event event{"TMP_EVENT"};
  55. SerializableJsonEvent serializable{make_shared<Event>(event)};
  56. string jsonString = R"({"id":"OPEN_DOOR_EVENT","parameterList":{"door_id":["door_id","16675"],"key_available":["key_available","true"]}})";
  57. serializable.unmarshal(jsonString);
  58. ASSERT_STREQ("OPEN_DOOR_EVENT", serializable.getValue()->getId().c_str());
  59. event_parameter_list parameterList = serializable.getValue()->getParameterList();
  60. ASSERT_FALSE(parameterList.empty());
  61. ASSERT_EQ(2, parameterList.size());
  62. ASSERT_STREQ("16675", parameterList.at("door_id").c_str());
  63. ASSERT_STREQ("true", parameterList.at("key_available").c_str());
  64. }
  65. TEST_F(SerializableJsonEventTest, setValue_parameter_not_set)
  66. {
  67. Event event{"TMP_EVENT"};
  68. SerializableJsonEvent serializable{make_shared<Event>(event)};
  69. EXPECT_THROW({
  70. try
  71. {
  72. serializable.setValue(nullptr);
  73. }
  74. catch (const IllegalArgumentException &_exception)
  75. {
  76. throw;
  77. }
  78. }, IllegalArgumentException);
  79. }
  80. }