Skip to content

Commit 41858cc

Browse files
committed
tests: Add JSON action parsing tests
Signed-off-by: Christian Hopps <[email protected]>
1 parent f06e22f commit 41858cc

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

tests/data.rs

+93-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use yang3::context::{Context, ContextFlags};
33
use yang3::data::{
44
Data, DataDiff, DataDiffFlags, DataFormat, DataImplicitFlags,
55
DataOperation, DataParserFlags, DataPrinterFlags, DataTree,
6-
DataValidationFlags,
6+
DataTreeOwningRef, DataValidationFlags,
77
};
88

99
static SEARCH_DIR: &str = "./assets/yang/";
@@ -162,6 +162,23 @@ static JSON_RPC1: &str = r###"
162162
"routing-protocol-instance-name":"main"
163163
}
164164
}"###;
165+
static JSON_ACTION1: &str = r###"
166+
{
167+
"ietf-routing:routing": {
168+
"ribs": {
169+
"rib": [
170+
{
171+
"name": "default",
172+
"active-route": {
173+
"route": {
174+
"source-protocol": "direct"
175+
}
176+
}
177+
}
178+
]
179+
}
180+
}
181+
}"###;
165182

166183
macro_rules! assert_data_eq {
167184
($dnode1:expr, $dnode2:expr) => {
@@ -240,6 +257,16 @@ fn parse_json_rpc<'a>(ctx: &'a Context, string: &str) -> DataTree<'a> {
240257
.expect("Failed to parse YANG RPC")
241258
}
242259

260+
fn parse_json_rpc_reply<'a>(ctx: &'a Context, string: &str) -> DataTree<'a> {
261+
DataTree::parse_op_string(
262+
&ctx,
263+
string,
264+
DataFormat::JSON,
265+
DataOperation::ReplyYang,
266+
)
267+
.expect("Failed to parse YANG RPC")
268+
}
269+
243270
#[test]
244271
fn data_find_xpath() {
245272
let ctx = create_context();
@@ -571,6 +598,71 @@ fn data_iterator_traverse_rpc() {
571598
);
572599
}
573600

601+
#[test]
602+
fn data_iterator_traverse_action() {
603+
let ctx = create_context();
604+
let mut tree1 = DataTreeOwningRef::new_path(
605+
&ctx,
606+
"/ietf-routing:routing/ribs/rib[name=\"default\"]/active-route",
607+
None,
608+
false,
609+
)
610+
.expect("Failed to create OP node");
611+
612+
tree1
613+
.parse_restconf_reply_op(
614+
r###"
615+
{
616+
"ietf-routing:output": {
617+
"route": {
618+
"source-protocol": "direct"
619+
}
620+
}
621+
}"###,
622+
DataFormat::JSON,
623+
)
624+
.expect("Failed to parse YANG ACTION REPLY");
625+
626+
assert_eq!(
627+
tree1
628+
.tree()
629+
.traverse()
630+
.map(|dnode| dnode.path())
631+
.collect::<Vec<String>>(),
632+
vec![
633+
"/ietf-routing:routing",
634+
"/ietf-routing:routing/ribs",
635+
"/ietf-routing:routing/ribs/rib[name='default']",
636+
"/ietf-routing:routing/ribs/rib[name='default']/name",
637+
"/ietf-routing:routing/ribs/rib[name='default']/active-route",
638+
"/ietf-routing:routing/ribs/rib[name='default']/active-route/route",
639+
"/ietf-routing:routing/ribs/rib[name='default']/active-route/route/source-protocol",
640+
]
641+
);
642+
643+
//
644+
// Check generic interface
645+
//
646+
647+
let tree1 = parse_json_rpc_reply(&ctx, JSON_ACTION1);
648+
assert_eq!(
649+
tree1
650+
.tree()
651+
.traverse()
652+
.map(|dnode| dnode.path())
653+
.collect::<Vec<String>>(),
654+
vec![
655+
"/ietf-routing:routing",
656+
"/ietf-routing:routing/ribs",
657+
"/ietf-routing:routing/ribs/rib[name='default']",
658+
"/ietf-routing:routing/ribs/rib[name='default']/name",
659+
"/ietf-routing:routing/ribs/rib[name='default']/active-route",
660+
"/ietf-routing:routing/ribs/rib[name='default']/active-route/route",
661+
"/ietf-routing:routing/ribs/rib[name='default']/active-route/route/source-protocol",
662+
]
663+
);
664+
}
665+
574666
#[test]
575667
fn data_iterator_ancestors() {
576668
let ctx = create_context();

0 commit comments

Comments
 (0)