blob: 76b3c1575d31a35a1b1f0aa7990261ee125ff408 [file] [log] [blame]
Václav Kubernát0d4db442018-07-18 17:18:43 +02001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
3 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
4 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#include "trompeloeil_catch.h"
10#include "yang_schema.hpp"
11
12const char* schema = R"(
13module example-schema {
Václav Kubernát6a713d62018-10-03 18:47:34 +020014 yang-version 1.1;
Václav Kubernát0d4db442018-07-18 17:18:43 +020015 namespace "http://example.com/example-sports";
16 prefix coze;
17
18 container a {
19 container a2 {
20 container a3 {
21 presence true;
22 }
23 }
24
25 leaf leafa {
26 type string;
27 }
28 }
29
30 container b {
31 container b2 {
32 presence true;
33 container b3 {
34 }
35 }
36 }
37
38 leaf leafString {
39 type string;
40 }
41
42 leaf leafDecimal {
43 type decimal64 {
44 fraction-digits 5;
45 }
46 }
47
48 leaf leafBool {
49 type boolean;
50 }
51
52 leaf leafInt {
53 type int32;
54 }
55
56 leaf leafUint {
57 type uint32;
58 }
59
60 leaf leafEnum {
61 type enumeration {
62 enum lol;
63 enum data;
64 enum coze;
65 }
66 }
67
Václav Kubernát6a713d62018-10-03 18:47:34 +020068 typedef enumTypedef {
69 type enumeration {
70 enum lol;
71 enum data;
72 enum coze;
73 }
74 }
75
76 typedef enumTypedefRestricted {
77 type enumTypedef {
78 enum lol;
79 enum data;
80 }
81 }
82
83 leaf leafEnumTypedef {
84 type enumTypedef;
85 }
86
87 leaf leafEnumTypedefRestricted {
88 type enumTypedef {
89 enum data;
90 enum coze;
91 }
92 }
93
94 leaf leafEnumTypedefRestricted2 {
95 type enumTypedefRestricted;
96 }
97
Václav Kubernát0d4db442018-07-18 17:18:43 +020098 list _list {
99 key number;
100
101 leaf number {
102 type int32;
103 }
104
105 container contInList {
106 presence true;
107 }
108 }
109
110 list twoKeyList {
111 key "name number";
112
113 leaf number {
114 type int32;
115 }
116
117 leaf name {
118 type string;
119 }
120 }
121})";
122
123TEST_CASE("yangschema")
124{
125 YangSchema ys;
126 ys.addSchemaString(schema);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200127 schemaPath_ path;
Václav Kubernát0d4db442018-07-18 17:18:43 +0200128 ModuleNodePair node;
129
130 SECTION("positive")
131 {
132 SECTION("isContainer")
133 {
134 SECTION("example-schema:a")
135 {
136 node.first = "example-schema";
137 node.second = "a";
138 }
139
140 SECTION("example-schema:a/a2")
141 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200142 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200143 node.second = "a2";
144 }
145
146 REQUIRE(ys.isContainer(path, node));
147 }
148 SECTION("isLeaf")
149 {
150 SECTION("example-schema:leafString")
151 {
152 node.first = "example-schema";
153 node.second = "leafString";
154 }
155
156 SECTION("example-schema:a/leafa")
157 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200158 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200159 node.first = "example-schema";
160 node.second = "leafa";
161 }
162
163 REQUIRE(ys.isLeaf(path, node));
164 }
165 SECTION("isModule")
166 {
167 REQUIRE(ys.isModule(path, "example-schema"));
168 }
169 SECTION("isList")
170 {
171 SECTION("example-schema:_list")
172 {
173 node.first = "example-schema";
174 node.second = "_list";
175 }
176
177 SECTION("example-schema:twoKeyList")
178 {
179 node.first = "example-schema";
180 node.second = "twoKeyList";
181 }
182
183 REQUIRE(ys.isList(path, node));
184 }
185 SECTION("isPresenceContainer")
186 {
187 SECTION("example-schema:a/a2/a3")
188 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200189 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
190 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a2")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200191 node.second = "a3";
192 }
193
194 REQUIRE(ys.isPresenceContainer(path, node));
195 }
196 SECTION("leafEnumHasValue")
197 {
Václav Kubernát0d4db442018-07-18 17:18:43 +0200198 std::string value;
Václav Kubernát6a713d62018-10-03 18:47:34 +0200199 SECTION("leafEnum")
200 {
201 node.first = "example-schema";
202 node.second = "leafEnum";
Václav Kubernát0d4db442018-07-18 17:18:43 +0200203
Václav Kubernát6a713d62018-10-03 18:47:34 +0200204 SECTION("lol")
205 value = "lol";
Václav Kubernát0d4db442018-07-18 17:18:43 +0200206
Václav Kubernát6a713d62018-10-03 18:47:34 +0200207 SECTION("data")
208 value = "data";
Václav Kubernát0d4db442018-07-18 17:18:43 +0200209
Václav Kubernát6a713d62018-10-03 18:47:34 +0200210 SECTION("coze")
211 value = "coze";
212 }
213
214 SECTION("leafEnumTypedef")
215 {
216 node.first = "example-schema";
217 node.second = "leafEnumTypedef";
218
219 SECTION("lol")
220 value = "lol";
221
222 SECTION("data")
223 value = "data";
224
225 SECTION("coze")
226 value = "coze";
227 }
228
229 SECTION("leafEnumTypedefRestricted")
230 {
231 node.first = "example-schema";
232 node.second = "leafEnumTypedefRestricted";
233
234 SECTION("data")
235 value = "data";
236
237 SECTION("coze")
238 value = "coze";
239 }
240
241 SECTION("leafEnumTypedefRestricted2")
242 {
243 node.first = "example-schema";
244 node.second = "leafEnumTypedefRestricted2";
245
246 SECTION("lol")
247 value = "lol";
248
249 SECTION("data")
250 value = "data";
251 }
Václav Kubernát0d4db442018-07-18 17:18:43 +0200252
253 REQUIRE(ys.leafEnumHasValue(path, node, value));
254 }
255 SECTION("listHasKey")
256 {
257 std::string key;
258
259 SECTION("_list")
260 {
261 node.first = "example-schema";
262 node.second = "_list";
263 SECTION("number")
264 key = "number";
265 }
266
267 SECTION("twoKeyList")
268 {
269 node.first = "example-schema";
270 node.second = "twoKeyList";
271 SECTION("number")
272 key = "number";
273 SECTION("name")
274 key = "name";
275 }
276
277 REQUIRE(ys.listHasKey(path, node, key));
278 }
279 SECTION("listKeys")
280 {
281 std::set<std::string> set;
282
283 SECTION("_list")
284 {
285 set = {"number"};
286 node.first = "example-schema";
287 node.second = "_list";
288 }
289
290 SECTION("twoKeyList")
291 {
292 set = {"number", "name"};
293 node.first = "example-schema";
294 node.second = "twoKeyList";
295 }
296
297 REQUIRE(ys.listKeys(path, node) == set);
298 }
299 SECTION("leafType")
300 {
301 yang::LeafDataTypes type;
302
303 SECTION("leafString")
304 {
305 node.first = "example-schema";
306 node.second = "leafString";
307 type = yang::LeafDataTypes::String;
308 }
309
310 SECTION("leafDecimal")
311 {
312 node.first = "example-schema";
313 node.second = "leafDecimal";
314 type = yang::LeafDataTypes::Decimal;
315 }
316
317 SECTION("leafBool")
318 {
319 node.first = "example-schema";
320 node.second = "leafBool";
321 type = yang::LeafDataTypes::Bool;
322 }
323
324 SECTION("leafInt")
325 {
326 node.first = "example-schema";
327 node.second = "leafInt";
328 type = yang::LeafDataTypes::Int;
329 }
330
331 SECTION("leafUint")
332 {
333 node.first = "example-schema";
334 node.second = "leafUint";
335 type = yang::LeafDataTypes::Uint;
336 }
337
338 SECTION("leafEnum")
339 {
340 node.first = "example-schema";
341 node.second = "leafEnum";
342 type = yang::LeafDataTypes::Enum;
343 }
344
345 REQUIRE(ys.leafType(path, node) == type);
346 }
347 SECTION("childNodes")
348 {
349 std::set<std::string> set;
350
351 SECTION("<root>")
352 {
353 set = {"example-schema:a", "example-schema:b", "example-schema:leafString",
354 "example-schema:leafDecimal", "example-schema:leafBool", "example-schema:leafInt",
Václav Kubernát6a713d62018-10-03 18:47:34 +0200355 "example-schema:leafUint", "example-schema:leafEnum", "example-schema:leafEnumTypedef",
356 "example-schema:leafEnumTypedefRestricted", "example-schema:leafEnumTypedefRestricted2",
357 "example-schema:_list", "example-schema:twoKeyList"};
Václav Kubernát0d4db442018-07-18 17:18:43 +0200358 }
359
360 SECTION("a")
361 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200362 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200363 set = {"example-schema:a2", "example-schema:leafa"};
364 }
365
Václav Kubernáte7d4aea2018-09-11 18:15:48 +0200366 REQUIRE(ys.childNodes(path, Recursion::NonRecursive) == set);
Václav Kubernát0d4db442018-07-18 17:18:43 +0200367 }
368 }
369
370 SECTION("negative")
371 {
372 SECTION("nonexistent nodes")
373 {
374 SECTION("example-schema:coze")
375 {
376 node.first = "example-schema";
377 node.second = "coze";
378 }
379
380 SECTION("example-schema:a/nevim")
381 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200382 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200383 node.second = "nevim";
384 }
385
386 SECTION("modul:a/nevim")
387 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200388 path.m_nodes.push_back(schemaNode_(module_{"modul"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200389 node.second = "nevim";
390 }
391
392 REQUIRE(!ys.isPresenceContainer(path, node));
393 REQUIRE(!ys.isList(path, node));
394 REQUIRE(!ys.isLeaf(path, node));
395 REQUIRE(!ys.isContainer(path, node));
396 }
397
398 SECTION("\"is\" methods return false for existing nodes for different nodetypes")
399 {
400 SECTION("example-schema:a")
401 {
402 node.first = "example-schema";
403 node.second = "a";
404 }
405
406 SECTION("example-schema:a/a2")
407 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200408 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200409 node.second = "a2";
410 }
411
412 REQUIRE(!ys.isPresenceContainer(path, node));
413 REQUIRE(!ys.isList(path, node));
414 REQUIRE(!ys.isLeaf(path, node));
415 }
416
417 SECTION("nodetype-specific methods called with different nodetypes")
418 {
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200419 path.m_nodes.push_back(schemaNode_(module_{"example-schema"}, container_("a")));
Václav Kubernát0d4db442018-07-18 17:18:43 +0200420 node.second = "a2";
421
422 REQUIRE(!ys.leafEnumHasValue(path, node, "haha"));
423 REQUIRE(!ys.listHasKey(path, node, "chacha"));
424 }
425
426 SECTION("nonexistent module")
427 {
428 REQUIRE(!ys.isModule(path, "notAModule"));
429 }
430 }
431}