XmlParserTest.cpp 16 KB

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