SerializableJsonStateMachineTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-17
  6. * Changed: 2022-05-15
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestDataFactory.hpp>
  12. #include "classes/io/xml/TestHelper.hpp"
  13. namespace
  14. {
  15. class SerializableJsonStateMachineTest : public ::testing::Test
  16. {
  17. protected:
  18. SerializableJsonStateMachineTest() = default;
  19. ~SerializableJsonStateMachineTest() override = default;
  20. void SetUp() override
  21. {}
  22. void TearDown() override
  23. {}
  24. };
  25. TEST_F(SerializableJsonStateMachineTest, constructor_no_parameter_set)
  26. {
  27. EXPECT_THROW({
  28. try
  29. {
  30. ls::std::logic::SerializableJsonStateMachine serializable{nullptr};
  31. }
  32. catch (const ls::std::core::IllegalArgumentException &_exception)
  33. {
  34. throw;
  35. }
  36. }, ls::std::core::IllegalArgumentException);
  37. }
  38. // implementation
  39. TEST_F(SerializableJsonStateMachineTest, marshal)
  40. {
  41. ls::std::logic::StateMachine stateMachine = ls_std_test::TestDataFactory::createStateMachine();
  42. stateMachine.setStartState("A");
  43. stateMachine.setMemory({"A", "B", "C"});
  44. ls::std::logic::SerializableJsonStateMachine serializable{std::make_shared<ls::std::logic::StateMachine>(stateMachine)};
  45. std::string jsonString = serializable.marshal();
  46. ASSERT_TRUE(!jsonString.empty());
  47. ls::std::io::File file{ls_std_io_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json"};
  48. ls::std::io::FileReader reader{file};
  49. ls::std::boxing::String data{reader.read()};
  50. ASSERT_TRUE(data.contains(jsonString));
  51. }
  52. TEST_F(SerializableJsonStateMachineTest, unmarshal)
  53. {
  54. ls::std::io::File file{ls_std_io_test::TestHelper::getResourcesFolderLocation() + "/state_machine_test.json"};
  55. ls::std::io::FileReader reader{file};
  56. std::shared_ptr<ls::std::logic::StateMachine> machine = std::make_shared<ls::std::logic::StateMachine>("bla");
  57. ls::std::logic::SerializableJsonStateMachine serializable{machine};
  58. serializable.unmarshal(reader.read());
  59. ASSERT_STREQ("test_machine", machine->getName().c_str());
  60. // check memory
  61. ASSERT_FALSE(machine->getMemory().empty());
  62. ASSERT_EQ(3, machine->getMemory().size());
  63. ASSERT_STREQ("A", machine->getMemory().at(0).c_str());
  64. ASSERT_STREQ("B", machine->getMemory().at(1).c_str());
  65. ASSERT_STREQ("C", machine->getMemory().at(2).c_str());
  66. // check current state
  67. ASSERT_TRUE(machine->getCurrentState() != nullptr);
  68. ASSERT_STREQ("A", machine->getCurrentState()->getId().c_str());
  69. // check states
  70. ASSERT_TRUE(!machine->getStates().empty());
  71. ASSERT_EQ(5, machine->getStates().size());
  72. std::shared_ptr<ls::std::logic::State> state = machine->getStates().at("A");
  73. ASSERT_STREQ("A", state->getId().c_str());
  74. ASSERT_EQ(1, state->getConnectedStates().size());
  75. ASSERT_STREQ("AB", state->getConnectedStates().at("AB")->getConnectionId().c_str());
  76. state = machine->getStates().at("B");
  77. ASSERT_STREQ("B", state->getId().c_str());
  78. ASSERT_EQ(2, state->getConnectedStates().size());
  79. ASSERT_STREQ("BC", state->getConnectedStates().at("BC")->getConnectionId().c_str());
  80. ASSERT_STREQ("BD", state->getConnectedStates().at("BD")->getConnectionId().c_str());
  81. state = machine->getStates().at("C");
  82. ASSERT_STREQ("C", state->getId().c_str());
  83. ASSERT_EQ(2, state->getConnectedStates().size());
  84. ASSERT_STREQ("CB", state->getConnectedStates().at("CB")->getConnectionId().c_str());
  85. ASSERT_STREQ("CE", state->getConnectedStates().at("CE")->getConnectionId().c_str());
  86. state = machine->getStates().at("D");
  87. ASSERT_STREQ("D", state->getId().c_str());
  88. ASSERT_EQ(1, state->getConnectedStates().size());
  89. ASSERT_STREQ("DE", state->getConnectedStates().at("DE")->getConnectionId().c_str());
  90. state = machine->getStates().at("E");
  91. ASSERT_STREQ("E", state->getId().c_str());
  92. ASSERT_TRUE(state->getConnectedStates().empty());
  93. }
  94. TEST_F(SerializableJsonStateMachineTest, getValue)
  95. {
  96. std::shared_ptr<ls::std::logic::StateMachine> x = std::make_shared<ls::std::logic::StateMachine>("bla");
  97. ls::std::logic::SerializableJsonStateMachine serializable{x};
  98. ASSERT_TRUE(serializable.getValue() == x);
  99. }
  100. TEST_F(SerializableJsonStateMachineTest, setValue)
  101. {
  102. std::shared_ptr<ls::std::logic::StateMachine> x = std::make_shared<ls::std::logic::StateMachine>("bla");
  103. ls::std::logic::SerializableJsonStateMachine serializable{x};
  104. ASSERT_TRUE(serializable.getValue() == x);
  105. x = std::make_shared<ls::std::logic::StateMachine>("bla2");
  106. serializable.setValue(x);
  107. ASSERT_TRUE(serializable.getValue() == x);
  108. }
  109. TEST_F(SerializableJsonStateMachineTest, setValue_no_parameter_set)
  110. {
  111. std::shared_ptr<ls::std::logic::StateMachine> stateMachine = std::make_shared<ls::std::logic::StateMachine>("bla");
  112. ls::std::logic::SerializableJsonStateMachine serializable{stateMachine};
  113. EXPECT_THROW({
  114. try
  115. {
  116. serializable.setValue(nullptr);
  117. }
  118. catch (const ls::std::core::IllegalArgumentException &_exception)
  119. {
  120. throw;
  121. }
  122. }, ls::std::core::IllegalArgumentException);
  123. }
  124. }