XmlParserTest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-11-26
  6. * Changed: 2022-05-12
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.hpp>
  12. namespace
  13. {
  14. class XmlParserTest : public ::testing::Test
  15. {
  16. protected:
  17. XmlParserTest() = default;
  18. ~XmlParserTest() override = default;
  19. static ls::std::core::type::byte_field readXmlStateMachine()
  20. {
  21. std::string xmlPath = TestHelper::getResourcesFolderLocation() + "state_machine_test.xml";
  22. ls::std::io::File file{xmlPath};
  23. ls::std::core::type::byte_field data = ls::std::io::FileReader{file}.read();
  24. return data;
  25. }
  26. void SetUp() override
  27. {}
  28. void TearDown() override
  29. {}
  30. };
  31. TEST_F(XmlParserTest, constructor)
  32. {
  33. std::shared_ptr<ls::std::io::XmlDocument> document{};
  34. EXPECT_THROW({
  35. try
  36. {
  37. ls::std::io::XmlParser xmlParser{document};
  38. }
  39. catch (const ls::std::core::IllegalArgumentException &_exception)
  40. {
  41. throw;
  42. }
  43. }, ls::std::core::IllegalArgumentException);
  44. }
  45. TEST_F(XmlParserTest, read)
  46. {
  47. ls::std::io::XmlParser xmlParser{std::make_shared<ls::std::io::XmlDocument>()};
  48. std::list<std::shared_ptr<ls::std::io::XmlNode>> children, statesChildren, memoryChildren, connectionChildren{};
  49. std::list<std::shared_ptr<ls::std::io::XmlAttribute>> attributes{};
  50. ls::std::core::type::byte_field data = readXmlStateMachine();
  51. xmlParser.parse(data);
  52. // check declaration
  53. ASSERT_STREQ("UTF-8", xmlParser.getDocument()->getDeclaration()->getEncoding().c_str());
  54. ASSERT_STREQ("1.0", xmlParser.getDocument()->getDeclaration()->getVersion().c_str());
  55. ASSERT_STREQ("yes", xmlParser.getDocument()->getDeclaration()->getStandalone().c_str());
  56. // check root element
  57. std::shared_ptr<ls::std::io::XmlNode> root = xmlParser.getDocument()->getRootElement();
  58. ASSERT_STREQ("stateMachine", root->getName().c_str());
  59. ASSERT_STREQ("name", root->getAttributes().front()->getName().c_str());
  60. ASSERT_EQ(1, root->getAttributes().size());
  61. ASSERT_STREQ("test_machine", root->getAttributes().front()->getValue().c_str());
  62. // root children
  63. children = root->getChildren();
  64. ASSERT_EQ(3, children.size());
  65. ASSERT_STREQ("states", ls::std::core::STLUtils::getListElementAt(children, 0)->getName().c_str());
  66. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(children, 0)->getAttributes().empty());
  67. ASSERT_STREQ("currentState", ls::std::core::STLUtils::getListElementAt(children, 1)->getName().c_str());
  68. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(children, 1)->getAttributes().empty());
  69. ASSERT_STREQ("memory", ls::std::core::STLUtils::getListElementAt(children, 2)->getName().c_str());
  70. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(children, 2)->getAttributes().empty());
  71. // states
  72. statesChildren = ls::std::core::STLUtils::getListElementAt(children, 0)->getChildren();
  73. ASSERT_EQ(5, statesChildren.size());
  74. ASSERT_STREQ("state", ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getName().c_str());
  75. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getAttributes().size());
  76. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getAttributes().front()->getName().c_str());
  77. ASSERT_STREQ("A", ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getAttributes().front()->getValue().c_str());
  78. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getValue().empty());
  79. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getChildren().size());
  80. ASSERT_STREQ("connections", ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getChildren().front()->getName().c_str());
  81. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getChildren().front()->getValue().empty());
  82. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getChildren().front()->getAttributes().empty());
  83. connectionChildren = ls::std::core::STLUtils::getListElementAt(statesChildren, 0)->getChildren().front()->getChildren();
  84. ASSERT_EQ(1, connectionChildren.size());
  85. ASSERT_STREQ("connection", ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getName().c_str());
  86. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getValue().empty());
  87. attributes = ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getAttributes();
  88. ASSERT_EQ(3, attributes.size());
  89. ASSERT_STREQ("connectionId", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getName().c_str());
  90. ASSERT_STREQ("AB", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getValue().c_str());
  91. ASSERT_STREQ("condition", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getName().c_str());
  92. ASSERT_STREQ("false", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getValue().c_str());
  93. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getName().c_str());
  94. ASSERT_STREQ("B", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getValue().c_str());
  95. ASSERT_STREQ("state", ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getName().c_str());
  96. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getAttributes().size());
  97. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getAttributes().front()->getName().c_str());
  98. ASSERT_STREQ("B", ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getAttributes().front()->getValue().c_str());
  99. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getValue().empty());
  100. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getChildren().size());
  101. ASSERT_STREQ("connections", ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getChildren().front()->getName().c_str());
  102. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getChildren().front()->getValue().empty());
  103. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getChildren().front()->getAttributes().empty());
  104. connectionChildren = ls::std::core::STLUtils::getListElementAt(statesChildren, 1)->getChildren().front()->getChildren();
  105. ASSERT_EQ(2, connectionChildren.size());
  106. ASSERT_STREQ("connection", ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getName().c_str());
  107. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getValue().empty());
  108. attributes = ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getAttributes();
  109. ASSERT_EQ(3, attributes.size());
  110. ASSERT_STREQ("connectionId", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getName().c_str());
  111. ASSERT_STREQ("BC", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getValue().c_str());
  112. ASSERT_STREQ("condition", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getName().c_str());
  113. ASSERT_STREQ("false", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getValue().c_str());
  114. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getName().c_str());
  115. ASSERT_STREQ("C", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getValue().c_str());
  116. ASSERT_STREQ("connection", ls::std::core::STLUtils::getListElementAt(connectionChildren, 1)->getName().c_str());
  117. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(connectionChildren, 1)->getValue().empty());
  118. attributes = ls::std::core::STLUtils::getListElementAt(connectionChildren, 1)->getAttributes();
  119. ASSERT_EQ(3, attributes.size());
  120. ASSERT_STREQ("connectionId", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getName().c_str());
  121. ASSERT_STREQ("BD", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getValue().c_str());
  122. ASSERT_STREQ("condition", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getName().c_str());
  123. ASSERT_STREQ("false", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getValue().c_str());
  124. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getName().c_str());
  125. ASSERT_STREQ("D", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getValue().c_str());
  126. ASSERT_STREQ("state", ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getName().c_str());
  127. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getAttributes().size());
  128. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getAttributes().front()->getName().c_str());
  129. ASSERT_STREQ("C", ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getAttributes().front()->getValue().c_str());
  130. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getValue().empty());
  131. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getChildren().size());
  132. ASSERT_STREQ("connections", ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getChildren().front()->getName().c_str());
  133. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getChildren().front()->getValue().empty());
  134. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getChildren().front()->getAttributes().empty());
  135. connectionChildren = ls::std::core::STLUtils::getListElementAt(statesChildren, 2)->getChildren().front()->getChildren();
  136. ASSERT_EQ(1, connectionChildren.size());
  137. ASSERT_STREQ("connection", ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getName().c_str());
  138. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getValue().empty());
  139. attributes = ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getAttributes();
  140. ASSERT_EQ(3, attributes.size());
  141. ASSERT_STREQ("connectionId", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getName().c_str());
  142. ASSERT_STREQ("CE", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getValue().c_str());
  143. ASSERT_STREQ("condition", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getName().c_str());
  144. ASSERT_STREQ("false", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getValue().c_str());
  145. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getName().c_str());
  146. ASSERT_STREQ("E", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getValue().c_str());
  147. ASSERT_STREQ("state", ls::std::core::STLUtils::getListElementAt(statesChildren, 3)->getName().c_str());
  148. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 3)->getAttributes().size());
  149. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(statesChildren, 3)->getAttributes().front()->getName().c_str());
  150. ASSERT_STREQ("D", ls::std::core::STLUtils::getListElementAt(statesChildren, 3)->getAttributes().front()->getValue().c_str());
  151. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 3)->getChildren().front()->getAttributes().empty());
  152. connectionChildren = ls::std::core::STLUtils::getListElementAt(statesChildren, 3)->getChildren().front()->getChildren();
  153. ASSERT_EQ(1, connectionChildren.size());
  154. ASSERT_STREQ("connection", ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getName().c_str());
  155. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getValue().empty());
  156. attributes = ls::std::core::STLUtils::getListElementAt(connectionChildren, 0)->getAttributes();
  157. ASSERT_EQ(3, attributes.size());
  158. ASSERT_STREQ("connectionId", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getName().c_str());
  159. ASSERT_STREQ("DE", ls::std::core::STLUtils::getListElementAt(attributes, 0)->getValue().c_str());
  160. ASSERT_STREQ("condition", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getName().c_str());
  161. ASSERT_STREQ("false", ls::std::core::STLUtils::getListElementAt(attributes, 1)->getValue().c_str());
  162. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getName().c_str());
  163. ASSERT_STREQ("E", ls::std::core::STLUtils::getListElementAt(attributes, 2)->getValue().c_str());
  164. ASSERT_STREQ("state", ls::std::core::STLUtils::getListElementAt(statesChildren, 4)->getName().c_str());
  165. ASSERT_EQ(1, ls::std::core::STLUtils::getListElementAt(statesChildren, 4)->getAttributes().size());
  166. ASSERT_STREQ("id", ls::std::core::STLUtils::getListElementAt(statesChildren, 4)->getAttributes().front()->getName().c_str());
  167. ASSERT_STREQ("E", ls::std::core::STLUtils::getListElementAt(statesChildren, 4)->getAttributes().front()->getValue().c_str());
  168. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(statesChildren, 4)->getChildren().empty());
  169. // current state
  170. ASSERT_STREQ("currentState", ls::std::core::STLUtils::getListElementAt(children, 1)->getName().c_str());
  171. ASSERT_STREQ("A", ls::std::core::STLUtils::getListElementAt(children, 1)->getValue().c_str());
  172. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(children, 1)->getChildren().empty());
  173. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(children, 1)->getAttributes().empty());
  174. // memory
  175. memoryChildren = ls::std::core::STLUtils::getListElementAt(children, 2)->getChildren();
  176. ASSERT_EQ(3, memoryChildren.size());
  177. ASSERT_STREQ("location", ls::std::core::STLUtils::getListElementAt(memoryChildren, 0)->getName().c_str());
  178. ASSERT_STREQ("A", ls::std::core::STLUtils::getListElementAt(memoryChildren, 0)->getValue().c_str());
  179. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(memoryChildren, 0)->getChildren().empty());
  180. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(memoryChildren, 0)->getAttributes().empty());
  181. ASSERT_STREQ("location", ls::std::core::STLUtils::getListElementAt(memoryChildren, 1)->getName().c_str());
  182. ASSERT_STREQ("B", ls::std::core::STLUtils::getListElementAt(memoryChildren, 1)->getValue().c_str());
  183. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(memoryChildren, 1)->getChildren().empty());
  184. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(memoryChildren, 1)->getAttributes().empty());
  185. ASSERT_STREQ("location", ls::std::core::STLUtils::getListElementAt(memoryChildren, 2)->getName().c_str());
  186. ASSERT_STREQ("C", ls::std::core::STLUtils::getListElementAt(memoryChildren, 2)->getValue().c_str());
  187. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(memoryChildren, 2)->getChildren().empty());
  188. ASSERT_TRUE(ls::std::core::STLUtils::getListElementAt(memoryChildren, 2)->getAttributes().empty());
  189. }
  190. TEST_F(XmlParserTest, getDocument)
  191. {
  192. std::string xmlPath = TestHelper::getResourcesFolderLocation() + "state_machine_test.xml";
  193. ls::std::io::XmlParser xmlParser{std::make_shared<ls::std::io::XmlDocument>()};
  194. ASSERT_TRUE(xmlParser.getDocument() != nullptr);
  195. }
  196. TEST_F(XmlParserTest, setDocument)
  197. {
  198. std::string xmlPath = TestHelper::getResourcesFolderLocation() + "state_machine_test.xml";
  199. std::shared_ptr<ls::std::io::XmlDocument> document = std::make_shared<ls::std::io::XmlDocument>();
  200. ls::std::io::XmlParser xmlParser{document};
  201. ASSERT_TRUE(xmlParser.getDocument() == document);
  202. document = std::make_shared<ls::std::io::XmlDocument>();
  203. xmlParser.setDocument(document);
  204. ASSERT_TRUE(xmlParser.getDocument() == document);
  205. }
  206. TEST_F(XmlParserTest, setDocument_no_reference)
  207. {
  208. std::shared_ptr<ls::std::io::XmlDocument> document = std::make_shared<ls::std::io::XmlDocument>();
  209. ls::std::io::XmlParser xmlParser{document};
  210. EXPECT_THROW({
  211. try
  212. {
  213. xmlParser.setDocument(nullptr);
  214. }
  215. catch (const ls::std::core::IllegalArgumentException &_exception)
  216. {
  217. throw;
  218. }
  219. }, ls::std::core::IllegalArgumentException);
  220. }
  221. }