XmlParserTest.cpp 14 KB

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