@@ -65,6 +65,7 @@ const char *lys_module_a = \
6565 <type name=\" string\" /> \
6666 <default value=\" def\" /> \
6767 </leaf> \
68+ <anydata name=\" any-data\" /> \
6869 </container> \
6970 <leaf name=\" y\" ><type name=\" string\" /></leaf> \
7071 <anyxml name=\" any\" /> \
@@ -231,10 +232,6 @@ TEST(test_ly_data_node)
231232
232233 auto new_node = std::make_shared<libyang::Data_Node>(root, root->child ()->schema ()->module (), " bar-y" );
233234 ASSERT_NOTNULL (new_node);
234- new_node = std::make_shared<libyang::Data_Node>(root, root->schema ()->module (), " number32" , " 100" );
235- ASSERT_NOTNULL (new_node);
236- auto dup_node = new_node->dup (0 );
237- ASSERT_NOTNULL (dup_node);
238235 } catch ( const std::exception& e ) {
239236 mt::printFailed (e.what (), stdout);
240237 throw ;
@@ -480,11 +477,11 @@ TEST(test_ly_data_node_find_instance)
480477 ASSERT_EQ (1 , set->number ());
481478 } catch (const std::exception& e) {
482479 mt::printFailed (e.what (), stdout);
483- return ;
480+ throw ;
484481 }
485482}
486483
487- TEST (test_ly_data_node_validate )
484+ TEST (test_ly_data_node_validate_ctx )
488485{
489486 const char *yang_folder = TESTS_DIR " /api/files" ;
490487 const char *config_file = TESTS_DIR " /api/files/a.xml" ;
@@ -620,7 +617,150 @@ TEST(test_ly_data_node_path)
620617 ASSERT_STREQ (" /a:x/bubba" , path.c_str ());
621618 } catch (const std::exception& e) {
622619 mt::printFailed (e.what (), stdout);
623- return ;
620+ throw ;
621+ }
622+ }
623+
624+ TEST (test_ly_data_node_leaf)
625+ {
626+ const char *yang_folder = TESTS_DIR " /api/files" ;
627+ const char *config_file = TESTS_DIR " /api/files/a.xml" ;
628+
629+ try {
630+ auto ctx = std::make_shared<libyang::Context>(yang_folder);
631+ ASSERT_NOTNULL (ctx);
632+ ctx->parse_module_mem (lys_module_a, LYS_IN_YIN);
633+ auto root = ctx->parse_data_path (config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
634+ ASSERT_NOTNULL (root);
635+
636+ auto new_node = std::make_shared<libyang::Data_Node>(root, root->schema ()->module (), " number32" , " 100" );
637+ ASSERT_NOTNULL (new_node);
638+ } catch ( const std::exception& e ) {
639+ mt::printFailed (e.what (), stdout);
640+ throw ;
641+ }
642+ }
643+
644+ TEST (test_ly_data_node_anydata)
645+ {
646+ const char *yang_folder = TESTS_DIR " /api/files" ;
647+ const char *config_file = TESTS_DIR " /api/files/a.xml" ;
648+
649+ try {
650+ auto ctx = std::make_shared<libyang::Context>(yang_folder);
651+ ASSERT_NOTNULL (ctx);
652+ ctx->parse_module_mem (lys_module_a, LYS_IN_YIN);
653+ auto root = ctx->parse_data_path (config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
654+ ASSERT_NOTNULL (root);
655+ auto mod = ctx->get_module (" a" , nullptr , 1 );
656+
657+ auto new_node = std::make_shared<libyang::Data_Node>(root, mod, " any-data" , " 100" , LYD_ANYDATA_CONSTSTRING);
658+ ASSERT_NOTNULL (new_node);
659+ } catch ( const std::exception& e ) {
660+ mt::printFailed (e.what (), stdout);
661+ throw ;
662+ }
663+ }
664+
665+ TEST (test_ly_data_node_dup)
666+ {
667+ const char *yang_folder = TESTS_DIR " /api/files" ;
668+ const char *config_file = TESTS_DIR " /api/files/a.xml" ;
669+
670+ try {
671+ auto ctx = std::make_shared<libyang::Context>(yang_folder);
672+ ASSERT_NOTNULL (ctx);
673+ ctx->parse_module_mem (lys_module_a, LYS_IN_YIN);
674+ auto root = ctx->parse_data_path (config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
675+ ASSERT_NOTNULL (root);
676+
677+ auto new_node = std::make_shared<libyang::Data_Node>(root, root->child ()->schema ()->module (), " bar-y" );
678+ ASSERT_NOTNULL (new_node);
679+ auto dup_node = new_node->dup (0 );
680+ ASSERT_NOTNULL (dup_node);
681+ } catch ( const std::exception& e ) {
682+ mt::printFailed (e.what (), stdout);
683+ throw ;
684+ }
685+ }
686+
687+ TEST (test_ly_data_node_dup_to_ctx)
688+ {
689+ const char *sch = " module x {"
690+ " namespace urn:x;"
691+ " prefix x;"
692+ " leaf x { type string; }}" ;
693+ const char *data = " <x xmlns=\" urn:x\" >hello</x>" ;
694+
695+ try {
696+ auto ctx1 = std::make_shared<libyang::Context>(nullptr );
697+ ASSERT_NOTNULL (ctx1);
698+ ctx1->parse_module_mem (sch, LYS_IN_YANG);
699+ auto data1 = ctx1->parse_data_mem (data, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
700+ ASSERT_NOTNULL (data1);
701+
702+ auto ctx2 = std::make_shared<libyang::Context>(nullptr );
703+ ASSERT_NOTNULL (ctx2);
704+ // we expect NULL due to missing schema in the second ctx
705+ auto dup_node = data1->dup_to_ctx (1 , ctx2);
706+ ASSERT_NULL (dup_node);
707+
708+ ctx2->parse_module_mem (sch, LYS_IN_YANG);
709+ // now we expect success due to schema being added to the second ctx
710+ dup_node = data1->dup_to_ctx (0 , ctx2);
711+ ASSERT_NOTNULL (dup_node);
712+ } catch ( const std::exception& e ) {
713+ mt::printFailed (e.what (), stdout);
714+ throw ;
715+ }
716+ }
717+
718+ TEST (test_ly_data_node_validate_node)
719+ {
720+ const char *yang_folder = TESTS_DIR " /api/files" ;
721+ const char *config_file = TESTS_DIR " /api/files/a.xml" ;
722+
723+ try {
724+ auto ctx = std::make_shared<libyang::Context>(yang_folder);
725+ ASSERT_NOTNULL (ctx);
726+ ctx->parse_module_mem (lys_module_a, LYS_IN_YIN);
727+ auto root = ctx->parse_data_path (config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
728+ ASSERT_NOTNULL (root);
729+
730+ auto rc = root->validate (LYD_OPT_CONFIG, ctx);
731+ ASSERT_EQ (0 , rc);
732+ auto new_node = std::make_shared<libyang::Data_Node>(root, root->schema ()->module (), " number32" , " 1" );
733+ ASSERT_NOTNULL (new_node);
734+ rc = root->validate (LYD_OPT_CONFIG, new_node);
735+ ASSERT_EQ (0 , rc);
736+ } catch (const std::exception& e) {
737+ mt::printFailed (e.what (), stdout);
738+ throw ;
739+ }
740+ }
741+
742+ TEST (test_ly_data_node_validate_value)
743+ {
744+ const char *yang_folder = TESTS_DIR " /api/files" ;
745+ const char *config_file = TESTS_DIR " /api/files/a.xml" ;
746+
747+ try {
748+ auto ctx = std::make_shared<libyang::Context>(yang_folder);
749+ ASSERT_NOTNULL (ctx);
750+ ctx->parse_module_mem (lys_module_a, LYS_IN_YIN);
751+ auto root = ctx->parse_data_path (config_file, LYD_XML, LYD_OPT_CONFIG | LYD_OPT_STRICT);
752+ ASSERT_NOTNULL (root);
753+
754+ auto rc = root->validate (LYD_OPT_CONFIG, ctx);
755+ ASSERT_EQ (0 , rc);
756+ auto new_node = std::make_shared<libyang::Data_Node>(root, root->schema ()->module (), " number32" , " 1" );
757+ ASSERT_NOTNULL (new_node);
758+ ASSERT_EQ (new_node->validate_value (" 1" ), 0 );
759+ ASSERT_EQ (new_node->validate_value (" 100" ), 0 );
760+ ASSERT_EQ (new_node->validate_value (" 110000000" ), 0 );
761+ } catch (const std::exception &e) {
762+ mt::printFailed (e.what (), stdout);
763+ throw ;
624764 }
625765}
626766
0 commit comments