SerializableJsonStateTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-15
  6. * Changed: 2022-05-12
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. namespace
  12. {
  13. class SerializableJsonStateTest : public ::testing::Test
  14. {
  15. protected:
  16. SerializableJsonStateTest() = default;
  17. ~SerializableJsonStateTest() override = default;
  18. void SetUp() override
  19. {}
  20. void TearDown() override
  21. {}
  22. };
  23. TEST_F(SerializableJsonStateTest, constructor_no_parameter_set)
  24. {
  25. EXPECT_THROW({
  26. try
  27. {
  28. ls::std::logic::SerializableJsonState serializable{nullptr};
  29. }
  30. catch (const ls::std::core::IllegalArgumentException &_exception)
  31. {
  32. throw;
  33. }
  34. }, ls::std::core::IllegalArgumentException);
  35. }
  36. TEST_F(SerializableJsonStateTest, marshal)
  37. {
  38. std::shared_ptr<ls::std::logic::State> x = std::make_shared<ls::std::logic::State>("A");
  39. x->addStateConnection(std::make_shared<ls::std::logic::StateConnection>("AB", "B"));
  40. x->addStateConnection(std::make_shared<ls::std::logic::StateConnection>("AC", "C"));
  41. ls::std::logic::SerializableJsonState serializable{x};
  42. ls::std::core::type::byte_field jsonString = serializable.marshal();
  43. ASSERT_TRUE(!jsonString.empty());
  44. std::string expectedJson = R"({"connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"},"AC":{"condition":false,"connectionId":"AC","stateId":"C"}},"id":"A"})";
  45. ASSERT_STREQ(expectedJson.c_str(), jsonString.c_str());
  46. }
  47. TEST_F(SerializableJsonStateTest, unmarshal)
  48. {
  49. std::shared_ptr<ls::std::logic::State> x = std::make_shared<ls::std::logic::State>("A");
  50. ls::std::logic::SerializableJsonState serializable{x};
  51. // before
  52. ASSERT_STREQ("A", x->getId().c_str());
  53. ASSERT_TRUE(x->getConnectedStates().empty());
  54. // after
  55. std::string jsonString = R"({"id":"A","connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"}}})";
  56. serializable.unmarshal(jsonString);
  57. ASSERT_STREQ("A", x->getId().c_str());
  58. ASSERT_EQ(1, x->getConnectedStates().size());
  59. ASSERT_STREQ("AB", x->getConnectedStates().at("AB")->getConnectionId().c_str());
  60. ASSERT_FALSE(x->getConnectedStates().at("AB")->isPassable());
  61. ASSERT_STREQ("B", x->getConnectedStates().at("AB")->getStateId().c_str());
  62. }
  63. TEST_F(SerializableJsonStateTest, getValue)
  64. {
  65. std::shared_ptr<ls::std::logic::State> x = std::make_shared<ls::std::logic::State>("A");
  66. ls::std::logic::SerializableJsonState serializable{x};
  67. ASSERT_TRUE(serializable.getValue() == x);
  68. }
  69. TEST_F(SerializableJsonStateTest, setValue)
  70. {
  71. std::shared_ptr<ls::std::logic::State> x = std::make_shared<ls::std::logic::State>("A");
  72. x->addStateConnection(std::make_shared<ls::std::logic::StateConnection>("AB", "B"));
  73. x->addStateConnection(std::make_shared<ls::std::logic::StateConnection>("AC", "C"));
  74. ls::std::logic::SerializableJsonState serializable{x};
  75. ls::std::core::type::byte_field jsonString = serializable.marshal();
  76. std::string expectedJson = R"({"connectedStates":{"AB":{"condition":false,"connectionId":"AB","stateId":"B"},"AC":{"condition":false,"connectionId":"AC","stateId":"C"}},"id":"A"})";
  77. ASSERT_STREQ(expectedJson.c_str(), jsonString.c_str());
  78. // setValue should now clear json
  79. std::shared_ptr<ls::std::logic::State> y = std::make_shared<ls::std::logic::State>("B");
  80. serializable.setValue(y);
  81. jsonString = serializable.marshal();
  82. ASSERT_STREQ(R"({"id":"B"})", jsonString.c_str());
  83. }
  84. TEST_F(SerializableJsonStateTest, setValue_parameter_not_set)
  85. {
  86. std::shared_ptr<ls::std::logic::State> state = std::make_shared<ls::std::logic::State>("A");
  87. ls::std::logic::SerializableJsonState serializable{state};
  88. EXPECT_THROW({
  89. try
  90. {
  91. serializable.setValue(nullptr);
  92. }
  93. catch (const ls::std::core::IllegalArgumentException &_exception)
  94. {
  95. throw;
  96. }
  97. }, ls::std::core::IllegalArgumentException);
  98. }
  99. }