XmlParserTest.cpp 16 KB

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