SerializableJsonStateTest.cpp 4.3 KB

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