Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * @file test_tree_data.cpp |
| 3 | * @author: Hrvoje Varga <hrvoje.varga@sartura.hr> |
| 4 | * @brief unit tests for functions from tree_data.h header |
| 5 | * |
| 6 | * Copyright (C) 2018 Deutsche Telekom AG. |
| 7 | * |
| 8 | * Author: Hrvoje Varga <hrvoje.varga@sartura.hr> |
| 9 | * |
| 10 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 11 | * You may not use this file except in compliance with the License. |
| 12 | * You may obtain a copy of the License at |
| 13 | * |
| 14 | * https://opensource.org/licenses/BSD-3-Clause |
| 15 | */ |
| 16 | |
Mislav Novakovic | 43d4ea3 | 2018-03-19 14:35:34 +0100 | [diff] [blame] | 17 | #include "Libyang.hpp" |
| 18 | #include "Tree_Data.hpp" |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 19 | |
| 20 | #include "../tests/config.h" |
| 21 | #include "microtest.h" |
| 22 | |
| 23 | const char *lys_module_a = \ |
| 24 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 25 | <module name=\"a\" \ |
| 26 | xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" \ |
| 27 | xmlns:md=\"urn:ietf:params:xml:ns:yang:ietf-yang-metadata\"\ |
| 28 | xmlns:a=\"urn:a\"> \ |
| 29 | <namespace uri=\"urn:a\"/> \ |
| 30 | <prefix value=\"a_mod\"/> \ |
| 31 | <include module=\"asub\"/> \ |
| 32 | <include module=\"atop\"/> \ |
| 33 | <import module=\"ietf-yang-metadata\"> \ |
| 34 | <prefix value=\"md\"/> \ |
| 35 | </import> \ |
| 36 | <feature name=\"foo\"/> \ |
| 37 | <grouping name=\"gg\"> \ |
| 38 | <leaf name=\"bar-gggg\"> \ |
| 39 | <type name=\"string\"/> \ |
| 40 | </leaf> \ |
| 41 | </grouping> \ |
| 42 | <md:annotation name=\"test\"> \ |
| 43 | <type name=\"string\"/> \ |
| 44 | </md:annotation> \ |
| 45 | <container name=\"x\"> \ |
| 46 | <leaf name=\"bar-leaf\"> \ |
| 47 | <type name=\"string\"/> \ |
| 48 | </leaf> \ |
| 49 | <uses name=\"gg\"> \ |
| 50 | </uses> \ |
| 51 | <leaf name=\"baz\"> \ |
| 52 | <type name=\"string\"/> \ |
| 53 | </leaf> \ |
| 54 | <leaf name=\"bubba\"> \ |
| 55 | <type name=\"string\"/> \ |
| 56 | </leaf> \ |
| 57 | <leaf name=\"number32\"> \ |
| 58 | <type name=\"int32\"/> \ |
| 59 | </leaf> \ |
| 60 | <leaf name=\"number64\"> \ |
| 61 | <type name=\"int64\"/> \ |
| 62 | </leaf> \ |
| 63 | <leaf name=\"def-leaf\"> \ |
| 64 | <type name=\"string\"/> \ |
| 65 | <default value=\"def\"/> \ |
| 66 | </leaf> \ |
| 67 | </container> \ |
| 68 | <leaf name=\"y\"><type name=\"string\"/></leaf> \ |
| 69 | <anyxml name=\"any\"/> \ |
| 70 | <augment target-node=\"/x\"> \ |
| 71 | <container name=\"bar-y\"/> \ |
| 72 | </augment> \ |
| 73 | <rpc name=\"bar-rpc\"> \ |
| 74 | </rpc> \ |
| 75 | <rpc name=\"foo-rpc\"> \ |
| 76 | </rpc> \ |
| 77 | <rpc name=\"rpc1\"> \ |
| 78 | <input> \ |
| 79 | <leaf name=\"input-leaf1\"> \ |
| 80 | <type name=\"string\"/> \ |
| 81 | </leaf> \ |
| 82 | <container name=\"x\"> \ |
| 83 | <leaf name=\"input-leaf2\"> \ |
| 84 | <type name=\"string\"/> \ |
| 85 | </leaf> \ |
| 86 | </container> \ |
| 87 | </input> \ |
| 88 | <output> \ |
| 89 | <leaf name=\"output-leaf1\"> \ |
| 90 | <type name=\"string\"/> \ |
| 91 | </leaf> \ |
| 92 | <leaf name=\"output-leaf2\"> \ |
| 93 | <type name=\"string\"/> \ |
| 94 | </leaf> \ |
| 95 | <container name=\"rpc-container\"> \ |
| 96 | <leaf name=\"output-leaf3\"> \ |
| 97 | <type name=\"string\"/> \ |
| 98 | </leaf> \ |
| 99 | </container> \ |
| 100 | </output> \ |
| 101 | </rpc> \ |
| 102 | <list name=\"l\"> \ |
| 103 | <key value=\"key1 key2\"/> \ |
| 104 | <leaf name=\"key1\"> \ |
| 105 | <type name=\"uint8\"/> \ |
| 106 | </leaf> \ |
| 107 | <leaf name=\"key2\"> \ |
| 108 | <type name=\"uint8\"/> \ |
| 109 | </leaf> \ |
| 110 | <leaf name=\"value\"> \ |
| 111 | <type name=\"string\"/> \ |
| 112 | </leaf> \ |
| 113 | </list> \ |
| 114 | </module> \ |
| 115 | "; |
| 116 | |
| 117 | const char *result_xml = "<x xmlns=\"urn:a\"><bubba>test</bubba></x>"; |
| 118 | const char *result_xml_format ="\ |
| 119 | <x xmlns=\"urn:a\">\n\ |
| 120 | <bubba>test</bubba>\n\ |
| 121 | </x>\n\ |
| 122 | "; |
| 123 | |
| 124 | const char *result_json = "\ |
| 125 | {\n\ |
| 126 | \"a:x\": {\n\ |
| 127 | \"bubba\": \"test\"\n\ |
| 128 | }\n\ |
| 129 | }\n\ |
| 130 | "; |
| 131 | |
Mislav Novakovic | 25a4063 | 2018-03-19 14:39:03 +0100 | [diff] [blame^] | 132 | TEST(test_ly_ctx_parse_data_mem) |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 133 | { |
| 134 | const char *a_data_xml = "\ |
| 135 | <x xmlns=\"urn:a\">\n\ |
| 136 | <bubba>test</bubba>\n\ |
| 137 | </x>\n"; |
| 138 | |
| 139 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 140 | const char *yin_file = TESTS_DIR "/api/files/a.yin"; |
| 141 | |
| 142 | try { |
| 143 | auto ctx = S_Context(new Context(yang_folder)); |
| 144 | ASSERT_NOTNULL(ctx); |
Mislav Novakovic | 25a4063 | 2018-03-19 14:39:03 +0100 | [diff] [blame^] | 145 | ctx->parse_module_path(yin_file, LYS_IN_YIN); |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 146 | |
Mislav Novakovic | 25a4063 | 2018-03-19 14:39:03 +0100 | [diff] [blame^] | 147 | auto root = ctx->parse_data_mem(a_data_xml, LYD_XML, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT); |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 148 | ASSERT_NOTNULL(root); |
| 149 | ASSERT_STREQ("x", root->schema()->name()); |
| 150 | } catch( const std::exception& e ) { |
| 151 | ASSERT_FALSE(e.what()); |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | |
Mislav Novakovic | 25a4063 | 2018-03-19 14:39:03 +0100 | [diff] [blame^] | 156 | TEST(test_ly_ctx_parse_data_fd) |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 157 | { |
| 158 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 159 | const char *yin_file = TESTS_DIR "/api/files/a.yin"; |
| 160 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 161 | |
| 162 | try { |
| 163 | auto ctx = S_Context(new Context(yang_folder)); |
| 164 | ASSERT_NOTNULL(ctx); |
Mislav Novakovic | 25a4063 | 2018-03-19 14:39:03 +0100 | [diff] [blame^] | 165 | ctx->parse_module_path(yin_file, LYS_IN_YIN); |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 166 | |
| 167 | FILE *f = fopen(config_file, "r"); |
| 168 | auto fd = f->_fileno; |
Mislav Novakovic | 25a4063 | 2018-03-19 14:39:03 +0100 | [diff] [blame^] | 169 | auto root = ctx->parse_data_fd(fd, LYD_XML, LYD_OPT_NOSIBLINGS | LYD_OPT_STRICT); |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 170 | ASSERT_NOTNULL(root); |
| 171 | ASSERT_STREQ("x", root->schema()->name()); |
| 172 | fclose(f); |
| 173 | } catch( const std::exception& e ) { |
| 174 | ASSERT_FALSE(e.what()); |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | TEST(test_ly_ctx_parse_data_path) |
| 180 | { |
| 181 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 182 | const char *yin_file = TESTS_DIR "/api/files/a.yin"; |
| 183 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 184 | const char *module_name = "a"; |
| 185 | const char *schema_name = "x"; |
| 186 | |
| 187 | try { |
| 188 | auto ctx = S_Context(new Context(yang_folder)); |
| 189 | ASSERT_NOTNULL(ctx); |
Mislav Novakovic | 25a4063 | 2018-03-19 14:39:03 +0100 | [diff] [blame^] | 190 | auto module = ctx->parse_module_path(yin_file, LYS_IN_YIN); |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 191 | ASSERT_NOTNULL(module); |
| 192 | ASSERT_STREQ(module_name, module->name()); |
| 193 | |
| 194 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 195 | ASSERT_NOTNULL(root); |
| 196 | ASSERT_STREQ(schema_name, root->schema()->name()); |
| 197 | } catch( const std::exception& e ) { |
| 198 | ASSERT_FALSE(e.what()); |
| 199 | return; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | TEST(test_ly_ctx_parse_data_path_invalid) |
| 204 | { |
| 205 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 206 | |
| 207 | try { |
| 208 | auto ctx = S_Context(new Context(yang_folder)); |
| 209 | ASSERT_NOTNULL(ctx); |
| 210 | |
| 211 | auto root = ctx->parse_data_path("INVALID_PATH", LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 212 | throw std::logic_error("exception not thrown"); |
| 213 | } catch( const std::logic_error& e ) { |
| 214 | ASSERT_FALSE(e.what()); |
| 215 | return; |
| 216 | } catch( const std::runtime_error& e ) { |
| 217 | return; |
| 218 | } catch( const std::exception& e ) { |
| 219 | ASSERT_FALSE(e.what()); |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | TEST(test_ly_data_node) |
| 225 | { |
| 226 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 227 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 228 | |
| 229 | try { |
| 230 | auto ctx = S_Context(new Context(yang_folder)); |
| 231 | ASSERT_NOTNULL(ctx); |
| 232 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 233 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 234 | ASSERT_NOTNULL(root); |
| 235 | |
| 236 | auto new_node = S_Data_Node(new Data_Node(root, root->child()->schema()->module(), "bar-y")); |
| 237 | ASSERT_NOTNULL(new_node); |
| 238 | new_node = S_Data_Node(new Data_Node(root, root->schema()->module(), "number32", "100")); |
| 239 | ASSERT_NOTNULL(new_node); |
| 240 | auto dup_node = new_node->dup(0); |
| 241 | ASSERT_NOTNULL(dup_node); |
| 242 | } catch( const std::exception& e ) { |
| 243 | ASSERT_FALSE(e.what()); |
| 244 | return; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | TEST(test_ly_data_node_new_path) |
| 249 | { |
| 250 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 251 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 252 | |
| 253 | try { |
| 254 | auto ctx = S_Context(new Context(yang_folder)); |
| 255 | ASSERT_NOTNULL(ctx); |
| 256 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 257 | ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 258 | auto mod = ctx->get_module("a", nullptr, 1); |
| 259 | ASSERT_NOTNULL(mod); |
| 260 | |
| 261 | auto root = S_Data_Node(new Data_Node(ctx, "/a:x/bar-gggg", "a", LYD_ANYDATA_CONSTSTRING, 0)); |
| 262 | ASSERT_NOTNULL(root); |
| 263 | ASSERT_STREQ("x", root->schema()->name()); |
| 264 | ASSERT_STREQ("bar-gggg", root->child()->schema()->name()); |
| 265 | |
| 266 | auto node = root->new_path(ctx, "def-leaf", "def", LYD_ANYDATA_CONSTSTRING, LYD_PATH_OPT_DFLT); |
| 267 | ASSERT_NOTNULL(node); |
| 268 | ASSERT_STREQ("def-leaf", node->schema()->name()); |
| 269 | ASSERT_EQ(1, node->dflt()); |
| 270 | |
| 271 | node = root->new_path(ctx, "def-leaf", "def", LYD_ANYDATA_CONSTSTRING, 0); |
| 272 | ASSERT_NOTNULL(node); |
| 273 | ASSERT_STREQ("def-leaf", node->schema()->name()); |
| 274 | ASSERT_EQ(0, node->dflt()); |
| 275 | |
| 276 | node = root->new_path(ctx, "bubba", "b", LYD_ANYDATA_CONSTSTRING, 0); |
| 277 | ASSERT_NOTNULL(node); |
| 278 | ASSERT_STREQ("bubba", node->schema()->name()); |
| 279 | |
| 280 | node = root->new_path(ctx, "/a:x/number32", "3", LYD_ANYDATA_CONSTSTRING, 0); |
| 281 | ASSERT_NOTNULL(node); |
| 282 | ASSERT_STREQ("number32", node->schema()->name()); |
| 283 | |
| 284 | node = root->new_path(ctx, "/a:l[key1='1'][key2='2']/value", nullptr, LYD_ANYDATA_CONSTSTRING, 0); |
| 285 | ASSERT_NOTNULL(node); |
| 286 | ASSERT_STREQ("l", node->schema()->name()); |
| 287 | ASSERT_STREQ("key1", node->child()->schema()->name()); |
| 288 | ASSERT_STREQ("key2", node->child()->next()->schema()->name()); |
| 289 | ASSERT_STREQ("value", node->child()->next()->next()->schema()->name()); |
| 290 | } catch (const std::exception& e) { |
| 291 | ASSERT_FALSE(e.what()); |
| 292 | return; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | TEST(test_ly_data_node_insert) |
| 297 | { |
| 298 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 299 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 300 | |
| 301 | try { |
| 302 | auto ctx = S_Context(new Context(yang_folder)); |
| 303 | ASSERT_NOTNULL(ctx); |
| 304 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 305 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 306 | ASSERT_NOTNULL(root); |
| 307 | auto new_node = S_Data_Node(new Data_Node(root, root->schema()->module(), "number32", "200")); |
| 308 | ASSERT_NOTNULL(new_node); |
| 309 | auto rc = root->insert(new_node); |
| 310 | ASSERT_EQ(0, rc); |
| 311 | ASSERT_STREQ("number32", root->child()->prev()->schema()->name()); |
| 312 | } catch (const std::exception& e) { |
| 313 | ASSERT_FALSE(e.what()); |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | TEST(test_ly_data_node_insert_sibling) |
| 319 | { |
| 320 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 321 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 322 | |
| 323 | try { |
| 324 | auto ctx = S_Context(new Context(yang_folder)); |
| 325 | ASSERT_NOTNULL(ctx); |
| 326 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 327 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 328 | ASSERT_NOTNULL(root); |
| 329 | |
| 330 | auto last = root->prev(); |
| 331 | auto new_node = S_Data_Node(new Data_Node(nullptr, root->schema()->module(), "y", "test")); |
| 332 | ASSERT_NOTNULL(new_node); |
| 333 | auto rc = root->insert_sibling(new_node); |
| 334 | ASSERT_EQ(0, rc); |
| 335 | ASSERT_STRNEQ(last->schema()->name(), root->prev()->schema()->name()); |
| 336 | ASSERT_STREQ("y", root->prev()->schema()->name()); |
| 337 | } catch (const std::exception& e) { |
| 338 | ASSERT_FALSE(e.what()); |
| 339 | return; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | TEST(test_ly_data_node_insert_before) |
| 344 | { |
| 345 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 346 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 347 | |
| 348 | try { |
| 349 | auto ctx = S_Context(new Context(yang_folder)); |
| 350 | ASSERT_NOTNULL(ctx); |
| 351 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 352 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 353 | ASSERT_NOTNULL(root); |
| 354 | |
| 355 | auto last = root->prev(); |
| 356 | auto new_node = S_Data_Node(new Data_Node(nullptr, root->schema()->module(), "y", "test")); |
| 357 | ASSERT_NOTNULL(new_node); |
| 358 | auto rc = root->insert_before(new_node); |
| 359 | ASSERT_EQ(0, rc); |
| 360 | ASSERT_STRNEQ(last->schema()->name(), root->prev()->schema()->name()); |
| 361 | ASSERT_STREQ("y", root->prev()->schema()->name()); |
| 362 | } catch (const std::exception& e) { |
| 363 | ASSERT_FALSE(e.what()); |
| 364 | return; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | TEST(test_ly_data_node_insert_after) |
| 369 | { |
| 370 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 371 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 372 | |
| 373 | try { |
| 374 | auto ctx = S_Context(new Context(yang_folder)); |
| 375 | ASSERT_NOTNULL(ctx); |
| 376 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 377 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 378 | ASSERT_NOTNULL(root); |
| 379 | |
| 380 | auto last = root->next(); |
| 381 | auto new_node = S_Data_Node(new Data_Node(nullptr, root->schema()->module(), "y", "test")); |
| 382 | ASSERT_NOTNULL(new_node); |
| 383 | auto rc = root->insert_after(new_node); |
| 384 | ASSERT_EQ(0, rc); |
| 385 | ASSERT_STRNEQ(last->schema()->name(), root->next()->schema()->name()); |
| 386 | ASSERT_STREQ("y", root->next()->schema()->name()); |
| 387 | } catch (const std::exception& e) { |
| 388 | ASSERT_FALSE(e.what()); |
| 389 | return; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | TEST(test_ly_data_node_schema_sort) |
| 394 | { |
| 395 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 396 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 397 | |
| 398 | try { |
| 399 | auto ctx = S_Context(new Context(yang_folder)); |
| 400 | ASSERT_NOTNULL(ctx); |
| 401 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 402 | ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 403 | auto mod = ctx->get_module("a", nullptr, 1); |
| 404 | ASSERT_NOTNULL(mod); |
| 405 | |
| 406 | auto root = S_Data_Node(new Data_Node(nullptr, mod, "l")); // FIXME: memory leak |
| 407 | ASSERT_NOTNULL(root); |
| 408 | auto node = S_Data_Node(new Data_Node(root, mod, "key1", "1")); |
| 409 | ASSERT_NOTNULL(node); |
| 410 | node = S_Data_Node(new Data_Node(root, mod, "key2", "2")); |
| 411 | ASSERT_NOTNULL(node); |
| 412 | |
| 413 | node = S_Data_Node(new Data_Node(nullptr, mod, "x")); |
| 414 | ASSERT_NOTNULL(node); |
| 415 | auto rc = root->insert_after(node); |
| 416 | ASSERT_EQ(0, rc); |
| 417 | |
| 418 | auto node2 = S_Data_Node(new Data_Node(node, mod, "bubba", "a")); |
| 419 | ASSERT_NOTNULL(node2); |
| 420 | node2 = S_Data_Node(new Data_Node(node, mod, "bar-gggg", "b")); |
| 421 | ASSERT_NOTNULL(node2); |
| 422 | node2 = S_Data_Node(new Data_Node(node, mod, "number64", "64")); |
| 423 | ASSERT_NOTNULL(node2); |
| 424 | node2 = S_Data_Node(new Data_Node(node, mod, "number32", "32")); |
| 425 | ASSERT_NOTNULL(node2); |
| 426 | |
| 427 | rc = root->schema_sort(1); |
| 428 | ASSERT_EQ(0, rc); |
| 429 | |
| 430 | root = node; |
| 431 | ASSERT_STREQ("x", root->schema()->name()); |
| 432 | ASSERT_STREQ("l", root->next()->schema()->name()); |
| 433 | |
| 434 | ASSERT_STREQ("bar-gggg", root->child()->schema()->name()); |
| 435 | ASSERT_STREQ("bubba", root->child()->next()->schema()->name()); |
| 436 | ASSERT_STREQ("number32", root->child()->next()->next()->schema()->name()); |
| 437 | ASSERT_STREQ("number64", root->child()->next()->next()->next()->schema()->name()); |
| 438 | } catch (const std::exception& e) { |
| 439 | ASSERT_FALSE(e.what()); |
| 440 | return; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | TEST(test_ly_data_node_find_path) |
| 445 | { |
| 446 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 447 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 448 | |
| 449 | try { |
| 450 | auto ctx = S_Context(new Context(yang_folder)); |
| 451 | ASSERT_NOTNULL(ctx); |
| 452 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 453 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 454 | ASSERT_NOTNULL(root); |
| 455 | |
| 456 | auto node = root->child(); |
| 457 | ASSERT_NOTNULL(node); |
| 458 | auto set = node->find_path("/a:x/bubba"); |
| 459 | ASSERT_NOTNULL(set); |
| 460 | ASSERT_EQ(1, set->number()); |
| 461 | } catch (const std::exception& e) { |
| 462 | ASSERT_FALSE(e.what()); |
| 463 | return; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | TEST(test_ly_data_node_find_instance) |
| 468 | { |
| 469 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 470 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 471 | |
| 472 | try { |
| 473 | auto ctx = S_Context(new Context(yang_folder)); |
| 474 | ASSERT_NOTNULL(ctx); |
| 475 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 476 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 477 | ASSERT_NOTNULL(root); |
| 478 | |
| 479 | auto node = root->child(); |
| 480 | ASSERT_NOTNULL(node); |
| 481 | auto set = node->find_instance(node->schema()); |
| 482 | ASSERT_NOTNULL(set); |
| 483 | ASSERT_EQ(1, set->number()); |
| 484 | } catch (const std::exception& e) { |
| 485 | ASSERT_FALSE(e.what()); |
| 486 | return; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | TEST(test_ly_data_node_validate) |
| 491 | { |
| 492 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 493 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 494 | |
| 495 | try { |
| 496 | auto ctx = S_Context(new Context(yang_folder)); |
| 497 | ASSERT_NOTNULL(ctx); |
| 498 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 499 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 500 | ASSERT_NOTNULL(root); |
| 501 | |
| 502 | auto rc = root->validate(LYD_OPT_CONFIG, ctx); |
| 503 | ASSERT_EQ(0, rc); |
| 504 | auto new_node = S_Data_Node(new Data_Node(root, root->schema()->module(), "number32", "1")); |
| 505 | ASSERT_NOTNULL(new_node); |
| 506 | rc = root->insert(new_node); |
| 507 | ASSERT_EQ(0, rc); |
| 508 | rc = root->validate(LYD_OPT_CONFIG, ctx); |
| 509 | ASSERT_EQ(0, rc); |
| 510 | } catch (const std::exception& e) { |
| 511 | ASSERT_FALSE(e.what()); |
| 512 | return; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | TEST(test_ly_data_node_unlink) |
| 517 | { |
| 518 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 519 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 520 | |
| 521 | try { |
| 522 | auto ctx = S_Context(new Context(yang_folder)); |
| 523 | ASSERT_NOTNULL(ctx); |
| 524 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 525 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 526 | ASSERT_NOTNULL(root); |
| 527 | |
| 528 | auto node = root->child(); |
| 529 | auto new_node = S_Data_Node(new Data_Node(root, root->schema()->module(), "number32", "1")); |
| 530 | ASSERT_NOTNULL(new_node); |
| 531 | auto rc = root->insert(new_node); |
| 532 | ASSERT_EQ(0, rc); |
| 533 | |
| 534 | auto schema = node->prev()->schema(); |
| 535 | if (LYS_LEAF == schema->nodetype() || LYS_LEAFLIST == schema->nodetype()) { |
Mislav Novakovic | 4b0d5a2 | 2018-03-14 16:02:52 +0100 | [diff] [blame] | 536 | auto casted = S_Data_Node_Leaf_List(new Data_Node_Leaf_List(node->prev())); |
| 537 | ASSERT_STREQ("1", casted->value_str()); |
Hrvoje Varga | f3caa52 | 2018-03-14 14:29:00 +0100 | [diff] [blame] | 538 | } else { |
| 539 | throw std::logic_error(""); |
| 540 | } |
| 541 | rc = node->prev()->unlink(); |
| 542 | ASSERT_EQ(0, rc); |
| 543 | schema = node->prev()->schema(); |
| 544 | if (LYS_LEAF == schema->nodetype() || LYS_LEAFLIST == schema->nodetype()) { |
| 545 | throw std::logic_error(""); |
| 546 | } |
| 547 | } catch( const std::logic_error& e ) { |
| 548 | ASSERT_FALSE(e.what()); |
| 549 | return; |
| 550 | } catch( const std::runtime_error& e ) { |
| 551 | return; |
| 552 | } catch (const std::exception& e) { |
| 553 | ASSERT_FALSE(e.what()); |
| 554 | return; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | TEST(test_ly_data_node_print_mem_xml) |
| 559 | { |
| 560 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 561 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 562 | |
| 563 | try { |
| 564 | auto ctx = S_Context(new Context(yang_folder)); |
| 565 | ASSERT_NOTNULL(ctx); |
| 566 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 567 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 568 | ASSERT_NOTNULL(root); |
| 569 | |
| 570 | auto result = root->print_mem(LYD_XML, 0); |
| 571 | ASSERT_STREQ(result_xml, result); |
| 572 | } catch (const std::exception& e) { |
| 573 | ASSERT_FALSE(e.what()); |
| 574 | return; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | TEST(test_ly_data_node_print_mem_xml_format) |
| 579 | { |
| 580 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 581 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 582 | |
| 583 | try { |
| 584 | auto ctx = S_Context(new Context(yang_folder)); |
| 585 | ASSERT_NOTNULL(ctx); |
| 586 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 587 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 588 | ASSERT_NOTNULL(root); |
| 589 | |
| 590 | auto result = root->print_mem(LYD_XML, LYP_FORMAT); |
| 591 | ASSERT_STREQ(result_xml_format, result); |
| 592 | } catch (const std::exception& e) { |
| 593 | ASSERT_FALSE(e.what()); |
| 594 | return; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | TEST(test_ly_data_node_print_mem_json) |
| 599 | { |
| 600 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 601 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 602 | |
| 603 | try { |
| 604 | auto ctx = S_Context(new Context(yang_folder)); |
| 605 | ASSERT_NOTNULL(ctx); |
| 606 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 607 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 608 | ASSERT_NOTNULL(root); |
| 609 | |
| 610 | auto result = root->print_mem(LYD_JSON, LYP_FORMAT); |
| 611 | ASSERT_STREQ(result_json, result); |
| 612 | } catch (const std::exception& e) { |
| 613 | ASSERT_FALSE(e.what()); |
| 614 | return; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | TEST(test_ly_data_node_path) |
| 619 | { |
| 620 | const char *yang_folder = TESTS_DIR "/api/files"; |
| 621 | const char *config_file = TESTS_DIR "/api/files/a.xml"; |
| 622 | |
| 623 | try { |
| 624 | auto ctx = S_Context(new Context(yang_folder)); |
| 625 | ASSERT_NOTNULL(ctx); |
| 626 | ctx->parse_module_mem(lys_module_a, LYS_IN_YIN); |
| 627 | auto root = ctx->parse_data_path(config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT); |
| 628 | ASSERT_NOTNULL(root); |
| 629 | |
| 630 | std::string path = root->path(); |
| 631 | ASSERT_NOTNULL(path.c_str()); |
| 632 | ASSERT_STREQ("/a:x", path.c_str()); |
| 633 | path = root->child()->path(); |
| 634 | ASSERT_NOTNULL(path.c_str()); |
| 635 | ASSERT_STREQ("/a:x/bubba", path.c_str()); |
| 636 | } catch (const std::exception& e) { |
| 637 | ASSERT_FALSE(e.what()); |
| 638 | return; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | TEST_MAIN(); |