1#!/usr/bin/env bash
2# Test: JSON empty/null values. See RFC7951
3# tests include:
4# - parsing in XML, output in JSON
5# - parsing in JSON, output in JSON
6# - parsing in JSON, output in XML?
7#
8# Magic line must be first in script (see README.md)
9s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi
10
11: ${clixon_util_json:=clixon_util_json}
12: ${clixon_util_xml:=clixon_util_xml}
13
14fyang=$dir/json.yang
15cat <<EOF > $fyang
16module json{
17   prefix j;
18   namespace "urn:example:clixon";
19   container leafs{
20     leaf a{
21       type int32;
22     }
23     leaf b{
24       type string;
25     }
26     leaf c{
27       type boolean;
28     }
29     leaf d{
30       type empty;
31     }
32   }
33   container leaf-lists{
34     leaf-list a{
35       type empty;
36     }
37     leaf-list b{
38       type string;
39     }
40   }
41   container containers{
42     presence true;
43     container a{
44     presence true;
45       leaf b {
46         type empty;
47       }
48     }
49   }
50   container anys{
51     anydata a;
52     anyxml b;
53   }
54}
55EOF
56
57new "test params: -y $fyang"
58
59# Leafs
60XML='<leafs xmlns="urn:example:clixon"><a>0</a><b></b><c>false</c><d></d></leafs>'
61JSON='{"json:leafs":{"a":0,"b":"","c":false,"d":[null]}}'
62
63new "leafs xml to json"
64expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
65
66new "leafs json to json"
67expecteofx "$clixon_util_xml -ovJjy $fyang" 0 "$JSON" "$JSON"
68
69new "leafs json to xml"
70expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
71
72# Leaf-lists single
73XML='<leaf-lists xmlns="urn:example:clixon"><a></a><b></b></leaf-lists>'
74JSON='{"json:leaf-lists":{"a":[null],"b":""}}'
75
76new "leaf-list single xml to json"
77expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
78
79new "leaf-list single json to json"
80expecteofx "$clixon_util_xml -ovjJy $fyang" 0 "$JSON" "$JSON"
81
82new "leaf-list single json to xml"
83expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
84
85# Leaf-lists multiple
86XML='<leaf-lists xmlns="urn:example:clixon"><a></a><a></a><b></b><b></b><b>null</b></leaf-lists>'
87JSON='{"json:leaf-lists":{"a":[[null],[null]],"b":["","","null"]}}'
88
89new "leaf-list multiple xml to json"
90expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
91
92new "leaf-list multiple json to json"
93expecteofx "$clixon_util_xml -ovjJy $fyang" 0 "$JSON" "$JSON"
94
95new "leaf-list multiple json to xml"
96expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
97
98# Empty container
99XML='<containers xmlns="urn:example:clixon"><a/></containers>'
100JSON='{"json:containers":{"a":{}}}'
101
102new "empty container xml to json"
103expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
104
105new "empty container json to json"
106expecteofx "$clixon_util_xml -ovjJy $fyang" 0 "$JSON" "$JSON"
107
108new "empty container json to xml"
109expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
110
111# Empty container whitespace
112XML='<containers xmlns="urn:example:clixon"><a> </a></containers>'
113JSON='{"json:containers":{"a":{}}}'
114
115new "empty container whitespace xml to json"
116expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
117
118# container empty leaf
119XML='<containers xmlns="urn:example:clixon"><a><b></b></a></containers>'
120JSON='{"json:containers":{"a":{"b":[null]}}}'
121
122new "container empty leaf xml to json"
123expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
124
125new "container empty leaf json to json"
126expecteofx "$clixon_util_xml -ovjJy $fyang" 0 "$JSON" "$JSON"
127
128new "container empty leaf json to xml"
129expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
130
131# anydata
132# An anydata instance is encoded in the same way as a container, as a name/object pair
133XML='<anys xmlns="urn:example:clixon"><a/></anys>'
134JSON='{"json:anys":{"a":{}}}'
135
136new "anydata xml to json"
137expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
138
139new "anydata json to json"
140expecteofx "$clixon_util_xml -ovjJy $fyang" 0 "$JSON" "$JSON"
141
142new "anydata json to xml"
143expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
144
145# anydata
146XML='<anys xmlns="urn:example:clixon"><a><c/><d/></a></anys>'
147JSON='{"json:anys":{"a":{"c":{},"d":{}}}}'
148
149new "anydata w empty node xml to json"
150expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
151
152new "anydata w empty node json to json"
153expecteofx "$clixon_util_xml -ovjJy $fyang" 0 "$JSON" "$JSON"
154
155new "anydata w empty node json to xml"
156expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
157
158# Anyxml
159XML='<anys xmlns="urn:example:clixon"><b/></anys>'
160JSON='{"json:anys":{"b":{}}}'
161
162new "anyxml xml to json"
163expecteofx "$clixon_util_xml -ovjy $fyang" 0 "$XML" "$JSON"
164
165new "anyxml json to json"
166expecteofx "$clixon_util_xml -ovjJy $fyang" 0 "$JSON" "$JSON"
167
168new "anyxml json to xml"
169expecteofx "$clixon_util_xml -ovJy $fyang" 0 "$JSON" "$XML"
170
171# JSON wrong in?
172# XXX: maybe this should give error?
173JSON='{"json:leafs":{"b":null}}'
174JSON2='{"json:leafs":{"b":""}}'
175new "leafs null in json to json"
176expecteofx "$clixon_util_xml -ovJjy $fyang" 0 "$JSON" "$JSON2"
177
178# XXX: maybe this should give error?
179JSON='{"json:leafs":{"b":[null]}}'
180JSON2='{"json:leafs":{"b":""}}'
181new "leafs [null] in json to json"
182expecteofx "$clixon_util_xml -ovJjy $fyang" 0 "$JSON" "$JSON2"
183
184# XXX: maybe this should give error?
185JSON='{"json:leafs":{"d":null}}'
186JSON2='{"json:leafs":{"d":[null]}}'
187new "leafs null in json to json"
188expecteofx "$clixon_util_xml -ovJjy $fyang" 0 "$JSON" "$JSON2"
189
190JSON='{"json:leafs":{"d":""}}'
191JSON2='{"json:leafs":{"d":[null]}}'
192new "leafs \"\" in json to json should give error"
193expecteofx "$clixon_util_xml -ovJjy $fyang" 255 "$JSON" "" 2> /dev/null
194
195rm -rf $dir
196
197# unset conditional parameters
198unset clixon_util_json
199unset clixon_util_xml
200