#!/usr/bin/env bash # Test: JSON (leaf-)list and YANG. See RFC7951 sec 5.3 / 5.4 # Magic line must be first in script (see README.md) s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi : ${clixon_util_json:=clixon_util_json} : ${clixon_util_xml:=clixon_util_xml} fyang=$dir/json.yang cat < $fyang module json{ prefix ex; namespace "urn:example:clixon"; container c{ leaf-list l1{ type int32; } list l2{ key name; leaf name{ type int32; } leaf value{ type string; } } leaf extra{ type string; } } leaf-list l1{ description "top-level"; type int32; } list l2{ description "top-level"; key name; leaf name{ type int32; } leaf value{ type string; } } } EOF # JSON list input/output tests auth test with arguments: # 1. test name # 2. JSON # 3. XML function testrun() { test=$1 json=$2 xml=$3 new "$test json in/out" expecteofx "$clixon_util_json -jy $fyang -D $DBG" 0 "$json" "$json" new "$test json in / xml out" expecteofx "$clixon_util_json -y $fyang -D $DBG" 0 "$json" "$xml" new "$test xml in / json out" expecteofx "$clixon_util_xml -ojvy $fyang -D $DBG" 0 "$xml" "$json" } new "test params: -y $fyang" testrun "top one leaf-list" '{"json:l1":[1]}' '1' # XXX There is a problem with how clixon shows json and xml on top-level # Typically, there is a loop over xml children to cope with the case if there is more # than one top-level which is invalid in XML. # See for example clixon_util_xml.c, clixon_util_json.c, cli_auto_show, cli_show_config #testrun "top two leaf-list" '{"json:l1":[1,2]}' '12' testrun "one leaf-list" '{"json:c":{"l1":[1]}}' '1' testrun "two leaf-list" '{"json:c":{"l1":[1,2]}}' '12' testrun "three leaf-list" '{"json:c":{"l1":[1,2,3]}}' '123' testrun "multiple leaf-list" '{"json:c":{"l1":[1,2],"extra":"abc"}}' '12abc' testrun "top one list" '{"json:l2":[{"name":1,"value":"x"}]}' '1x' # XXX #testrun "top two list" '{"json:l2":[{"name":1,"value":"x"},{"name":2,"value":"x"}]}' '1x2x' testrun "one list" '{"json:c":{"l2":[{"name":1,"value":"x"}]}}' '1x' testrun "two list" '{"json:c":{"l2":[{"name":1,"value":"x"},{"name":2,"value":"y"}]}}' '1x2y' testrun "three list" '{"json:c":{"l2":[{"name":1,"value":"x"},{"name":2,"value":"y"},{"name":3,"value":"z"}]}}' '1x2y3z' rm -rf $dir # unset conditional parameters unset clixon_util_json unset clixon_util_xml new "endtest" endtest