1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904 | // Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
#include <yang/tests/sysrepo_setup.h>
#include <yang/translator.h>
using namespace std;
using namespace isc;
using namespace isc::data;
using namespace isc::yang;
using namespace isc::yang::test;
using namespace libyang;
using namespace sysrepo;
struct TranslatorTest : ::testing::Test {
void SetUp() override {
SysrepoSetup::cleanSharedMemory();
cleanUp();
}
void TearDown() override {
cleanUp();
SysrepoSetup::cleanSharedMemory();
}
private:
void cleanUp() {
Session session(sysrepo::Connection{}.sessionStart());
session.switchDatastore(sysrepo::Datastore::Candidate);
session.deleteItem("/keatest-module:container");
session.deleteItem("/keatest-module:kernel-modules");
session.deleteItem("/keatest-module:list");
session.deleteItem("/keatest-module:main");
session.deleteItem("/keatest-module:presence-container");
session.applyChanges();
}
}; // TranslatorTest
namespace {
// Test constructor.
TEST_F(TranslatorTest, constructor) {
// Get a session.
Session sess(Connection{}.sessionStart());
sess.switchDatastore(sysrepo::Datastore::Candidate);
// Get a translator object.
unique_ptr<Translator> translator;
EXPECT_NO_THROW_LOG(translator.reset(new Translator(sess, "")));
}
// Test basic YANG to JSON value conversion using sysrepo test models.
TEST_F(TranslatorTest, getItem) {<--- syntax error
// Get a translator object to play with.
Session sess(Connection{}.sessionStart());
sess.switchDatastore(sysrepo::Datastore::Candidate);
unique_ptr<Translator> translator;
ASSERT_NO_THROW_LOG(translator.reset(new Translator(sess, "")));
string value;
ConstElementPtr element;
string xpath;
// String.
xpath = "/keatest-module:main/string";
value = "str";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ("str", element->stringValue());
element.reset();
// Bool.
xpath = "/keatest-module:main/boolean";
value = "true";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::boolean, element->getType());
EXPECT_TRUE(element->boolValue());
element.reset();
// Unsigned 8 bit integer.
xpath = "/keatest-module:main/ui8";
uint8_t u8(8);
value = to_string(u8);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(8, element->intValue());
element.reset();
// Unsigned 16 bit integer.
xpath = "/keatest-module:main/ui16";
uint16_t u16(16);
value = to_string(u16);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(16, element->intValue());
element.reset();
// Unsigned 32 bit integer.
xpath = "/keatest-module:main/ui32";
uint32_t u32(32);
value = to_string(u32);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(32, element->intValue());
element.reset();
// Unsigned 64 bit integer.
xpath = "/keatest-module:main/ui64";
uint64_t u64(64);
value = to_string(u64);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(64, element->intValue());
element.reset();
// Empty.
xpath = "/keatest-module:main/empty";
value = string();
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ(string(), element->stringValue());
element.reset();
// Signed 8 bit integer.
xpath = "/keatest-module:main/i8";
int8_t s8(8);
value = to_string(s8);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(8, element->intValue());
element.reset();
// Signed 16 bit integer.
xpath = "/keatest-module:main/i16";
int16_t s16(16);
value = to_string(s16);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(16, element->intValue());
element.reset();
// Signed 32 bit integer.
xpath = "/keatest-module:main/i32";
int32_t s32(32);
value = to_string(s32);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(32, element->intValue());
element.reset();
// Signed 64 bit integer.
xpath = "/keatest-module:main/i64";
int64_t s64(64);
value = to_string(s64);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::integer, element->getType());
EXPECT_EQ(64, element->intValue());
element.reset();
// Identity reference.
xpath = "/keatest-module:main/id_ref";
value = "keatest-module:id_1";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ("keatest-module:id_1", element->stringValue());
element.reset();
// Enumeration item.
xpath = "/keatest-module:main/enum";
value = "maybe";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ("maybe", element->stringValue());
element.reset();
// Bits.
xpath = "/keatest-module:main/options";
value = "strict recursive logging";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ("strict recursive logging", element->stringValue());
element.reset();
// Binary.
xpath = "/keatest-module:main/raw";
value = "Zm9vYmFy";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ("foobar", element->stringValue());
element.reset();
// Leaf-list: not yet exist.
xpath = "/keatest-module:main/numbers";
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
EXPECT_FALSE(element);
element.reset();
// No easy way to create it empty.
// Leaf-list: 1, 2 and 3.
u8 = 1;
value = to_string(u8);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
u8 = 2;
value = to_string(u8);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
u8 = 3;
value = to_string(u8);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::list, element->getType());
EXPECT_EQ(3, element->size());
EXPECT_EQ("[ 1, 2, 3 ]", element->str());
element.reset();
// Instance identifier.
xpath = "/keatest-module:main/instance_id";
value = "/keatest-module:main/numbers[.='1']";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ(value, element->stringValue());
element.reset();
// Union.
xpath = "/keatest-module:main/union";
value = "8";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ(value, element->stringValue());
element.reset();
value = "infinity";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ(value, element->stringValue());
element.reset();
// Leafref.
xpath = "/keatest-module:main/leafref-i8";
value = "9";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ(value, element->stringValue());
element.reset();
// Leafref.
xpath = "/keatest-module:main/leafref-raw";
value = "ff012345";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ(value, element->stringValue());
element.reset();
// Leafref.
xpath = "/keatest-module:main/leafref-string";
value = "string through leafref";
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::string, element->getType());
EXPECT_EQ(value, element->stringValue());
element.reset();
// Decimal 64.
xpath = "/keatest-module:main/dec64";
value = to_string(9.85);
EXPECT_NO_THROW_LOG(sess.setItem(xpath, value));
sess.applyChanges();
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
ASSERT_TRUE(element);
ASSERT_EQ(Element::real, element->getType());
EXPECT_EQ("9.85", element->str());
element.reset();
// Not existing.
xpath = "/keatest-module:main/no_such_node";
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
EXPECT_FALSE(element);
element.reset();
// Check error.
xpath = "null";
EXPECT_NO_THROW_LOG(element = translator->getItemFromAbsoluteXpath(xpath));
EXPECT_FALSE(element);
}
// Check the Translator::deleteItem function.
TEST_F(TranslatorTest, deleteItem) {
ElementPtr got;
string xpath;
// Get a translator object to play with.
Translator translator(Connection{}.sessionStart(), "keatest-module");
// Missing schema node
EXPECT_NO_THROW_LOG(translator.deleteItem("/keatest-module:main/no_such_node"));
// Existing schema node, but no data
xpath = "/keatest-module:main/string";
EXPECT_NO_THROW_LOG(translator.deleteItem(xpath));
EXPECT_NO_THROW_LOG(got = translator.getItemFromAbsoluteXpath(xpath));
EXPECT_FALSE(got);
got.reset();
// Existing schema node, existing data
translator.setItem(xpath, Element::create("value"), LeafBaseType::String);
EXPECT_NO_THROW_LOG(translator.deleteItem(xpath));
EXPECT_NO_THROW_LOG(got = translator.getItemFromAbsoluteXpath(xpath));
EXPECT_FALSE(got);
got.reset();
// Container schema node, no data
EXPECT_NO_THROW_LOG(translator.deleteItem("/keatest-module:main"));
EXPECT_NO_THROW_LOG(got = translator.getItemFromAbsoluteXpath(xpath));
EXPECT_FALSE(got);
got.reset();
// Container schema node, existing data
translator.setItem(xpath, Element::create("value"), LeafBaseType::String);
EXPECT_NO_THROW_LOG(translator.deleteItem("/keatest-module:main"));
EXPECT_NO_THROW_LOG(got = translator.getItemFromAbsoluteXpath(xpath));
EXPECT_FALSE(got);
got.reset();
}
// Test JSON to basic YANG value conversion using the static method.
TEST_F(TranslatorTest, valueTo) {
optional<string> value;
// Null.
ConstElementPtr element;
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::String));
EXPECT_EQ(nullopt, value);
EXPECT_FALSE(element);
// Container.
element = Element::createMap();
EXPECT_THROW_MSG(Translator::translateToYang(element, LeafBaseType::Unknown), NotImplemented,
"Translator::value(): map element");
// List.
element = Element::createList();
EXPECT_THROW_MSG(Translator::translateToYang(element, LeafBaseType::Unknown), NotImplemented,
"Translator::value(): list element");
// String.
string str("foo");
element = Element::create(str);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::String));
EXPECT_EQ(element->stringValue(), value);
// Bool.
element = Element::create(false);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Bool));
EXPECT_EQ(element->str(), value);
// Unsigned 8 bit integer.
element = Element::create(123);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Uint8));
EXPECT_EQ(element->str(), value);
element.reset();
// Unsigned 16 bit integer.
element = Element::create(12345);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Uint16));
EXPECT_EQ(element->str(), value);
element.reset();
// Unsigned 32 bit integer.
element = Element::create(123456789);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Uint32));
EXPECT_EQ(element->str(), value);
element.reset();
// Unsigned 64 bit integer.
element = Element::create(int64_t(1234567890123456));
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Uint64));
EXPECT_EQ("1234567890123456", value);
element.reset();
// Signed 8 bit integer.
element = Element::create(-123);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Int8));
EXPECT_EQ(element->str(), value);
element.reset();
// Signed 16 bit integer.
element = Element::create(-12345);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Int16));
EXPECT_EQ(element->str(), value);
element.reset();
// Signed 32 bit integer.
element = Element::create(-123456789);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Int32));
EXPECT_EQ(element->str(), value);
element.reset();
// Signed 64 bit integer.
element = Element::create(int64_t(-1234567890123456));
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Int64));
EXPECT_EQ("-1234567890123456", value);
element.reset();
// Identity reference.
element = Element::create(str);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::IdentityRef));
EXPECT_EQ(element->stringValue(), value);
// Enumeration item.
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Enum));
EXPECT_EQ(element->stringValue(), value);
// Binary.
element = Element::create("foobar");
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Binary));
EXPECT_EQ("Zm9vYmFy", value);
// Bits.
element = Element::create("foobar");
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Bits));
EXPECT_EQ(element->stringValue(), value);
// Decimal 64.
double d64(.1234);
element = Element::create(d64);
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Dec64));
EXPECT_EQ(element->str(), value);
// Empty.
element = Element::create(string());
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Empty));
EXPECT_EQ(element->stringValue(), value);
// Leafref.
element = Element::create("leafref");
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Leafref));
EXPECT_EQ(element->stringValue(), value);
// Union.
element = Element::create("union");
EXPECT_NO_THROW_LOG(value = Translator::translateToYang(element, LeafBaseType::Union));
EXPECT_EQ(element->stringValue(), value);
}
// Test JSON to basic YANG value conversion using sysrepo test models.
TEST_F(TranslatorTest, setItem) {
// Get a translator object to play with.
Session sess(Connection{}.sessionStart());
sess.switchDatastore(sysrepo::Datastore::Candidate);
unique_ptr<Translator> translator;
ASSERT_NO_THROW_LOG(translator.reset(new Translator(sess, "keatest-module")));
ElementPtr element;
string xpath;
optional<DataNode> data_node;
// List.
xpath = "/keatest-module:list[key='value']";
element = Element::create("value");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, ElementPtr(), LeafBaseType::Unknown));
EXPECT_NO_THROW_LOG(translator->setItem(xpath, ElementPtr(), LeafBaseType::Unknown));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath + "/key"));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath + "/key"));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::String, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// String.
xpath = "/keatest-module:main/string";
element = Element::create("str");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::String));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::String, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Bool.
xpath = "/keatest-module:main/boolean";
element = Element::create(true);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Bool));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Bool, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Unsigned 8 bit integer.
xpath = "/keatest-module:main/ui8";
element = Element::create(8);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Uint8));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Uint8, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Unsigned 16 bit integer.
xpath = "/keatest-module:main/ui16";
element = Element::create(16);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Uint16));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Uint16, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Unsigned 32 bit integer.
xpath = "/keatest-module:main/ui32";
element = Element::create(32);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Uint32));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Uint32, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Unsigned 64 bit integer.
xpath = "/keatest-module:main/ui64";
element = Element::create(64);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Uint64));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Uint64, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Empty.
xpath = "/keatest-module:main/empty";
element = Element::create(string());
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Empty));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Empty, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Signed 8 bit integer.
xpath = "/keatest-module:main/i8";
element = Element::create(8);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Int8));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Int8, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Signed 16 bit integer.
xpath = "/keatest-module:main/i16";
element = Element::create(16);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Int16));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Int16, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Signed 32 bit integer.
xpath = "/keatest-module:main/i32";
element = Element::create(32);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Int32));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Int32, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Signed 64 bit integer.
xpath = "/keatest-module:main/i64";
element = Element::create(64);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Int64));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Int64, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Identity reference.
xpath = "/keatest-module:main/id_ref";
element = Element::create("keatest-module:id_1");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::IdentityRef));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::IdentityRef, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Enumeration item.
xpath = "/keatest-module:main/enum";
element = Element::create("maybe");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Enum));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Enum, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Binary.
xpath = "/keatest-module:main/raw";
element = Element::create("foobar");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Binary));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Binary, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ("Zm9vYmFy", string(data_node->asTerm().valueStr()));
// Bits.
xpath = "/keatest-module:main/options";
element = Element::create("strict recursive logging");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Bits));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Bits, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Decimal 64.
xpath = "/keatest-module:main/dec64";
double d64(9.85);
element = Element::create(d64);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Dec64));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Dec64, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->str(), string(data_node->asTerm().valueStr()));
// Leaf-list.
xpath = "/keatest-module:main/numbers";
EXPECT_NO_THROW_LOG(translator->setItem(xpath, Element::fromJSON("1"), LeafBaseType::Uint8));
EXPECT_NO_THROW_LOG(translator->setItem(xpath, Element::fromJSON("2"), LeafBaseType::Uint8));
EXPECT_NO_THROW_LOG(translator->setItem(xpath, Element::fromJSON("3"), LeafBaseType::Uint8));
ElementPtr got(translator->getItemFromAbsoluteXpath(xpath));
ASSERT_EQ(LeafBaseType::Dec64, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ("[ 1, 2, 3 ]", got->str());
// Clean the leaf-list.
EXPECT_NO_THROW_LOG(translator->deleteItem(xpath));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
EXPECT_FALSE(data_node);
data_node.reset();
// Instance identifier.
xpath = "/keatest-module:main/instance_id";
element = Element::create("/keatest-module:main/numbers[.='1']");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::InstanceIdentifier));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::InstanceIdentifier, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Union.
xpath = "/keatest-module:main/union";
element = Element::create("8");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Union));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Union, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
element = Element::create("infinity");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Union));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Union, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Leafref.
xpath = "/keatest-module:main/leafref-i8";
element = Element::create("9");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Leafref));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Leafref, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Leafref.
xpath = "/keatest-module:main/leafref-raw";
element = Element::create("ff012345");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Leafref));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Leafref, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Leafref.
xpath = "/keatest-module:main/leafref-string";
element = Element::create("string through");
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Leafref));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
ASSERT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
ASSERT_TRUE(data_node);
ASSERT_EQ(LeafBaseType::Leafref, data_node->schema().asLeaf().valueType().base());
EXPECT_EQ(element->stringValue(), string(data_node->asTerm().valueStr()));
// Bad xpath.
xpath = "/keatest-module:main/no_such_node";
element = Element::create("str");
EXPECT_THROW_MSG(translator->setItem(xpath, element, LeafBaseType::String), NetconfError,
"setting item '\"str\"' at '" + xpath +
"': Session::setItem: Couldn't set "
"'/keatest-module:main/no_such_node' to 'str': SR_ERR_INVAL_ARG");
// Bad type.
xpath = "/keatest-module:main/string";
element = Element::create(true);
EXPECT_NO_THROW_LOG(translator->setItem(xpath, element, LeafBaseType::Bool));
element = translator->getItemFromAbsoluteXpath(xpath);
ASSERT_TRUE(element);
EXPECT_EQ(element->getType(), Element::string);
EXPECT_EQ(element->str(), "\"true\"");
// Delete (twice).
xpath = "/keatest-module:main/string";
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
EXPECT_TRUE(data_node);
EXPECT_NO_THROW_LOG(data_node = data_node->findPath(xpath));
EXPECT_TRUE(data_node);
ASSERT_TRUE(NodeType::Leaf == data_node->schema().nodeType());
EXPECT_EQ("true", data_node->asTerm().valueStr());
EXPECT_NO_THROW_LOG(translator->deleteItem(xpath));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
EXPECT_FALSE(data_node);
EXPECT_NO_THROW_LOG(translator->deleteItem(xpath));
EXPECT_NO_THROW_LOG(data_node = sess.getData(xpath));
EXPECT_FALSE(data_node);
}
// Test YANG container retrieval.
TEST_F(TranslatorTest, container) {
ElementPtr element;
// Get a translator object to play with.
Translator translator(Connection{}.sessionStart(), "keatest-module");
// Container with no data apparently throws.
EXPECT_THROW_MSG(element = translator.getItemFromAbsoluteXpath("/keatest-module:container"), NotImplemented,
"getting node of type 1 not supported, xpath is '/keatest-module:container'");
EXPECT_FALSE(element);
element.reset();
EXPECT_NO_THROW_LOG(
translator.setItem("/keatest-module:container",
ElementPtr(), LeafBaseType::Unknown));
// Container with data filled in throws when retrieving the container itself.
element = Element::create("Leaf value");
EXPECT_NO_THROW_LOG(
translator.setItem("/keatest-module:container/list[key1='key1'][key2='key2']/leaf",
element, LeafBaseType::String));
element.reset();
EXPECT_THROW_MSG(
element = translator.getItemFromAbsoluteXpath("/keatest-module:container"), NotImplemented,
"getting node of type 1 not supported, xpath is '/keatest-module:container'");
EXPECT_FALSE(element);
element.reset();
EXPECT_NO_THROW_LOG(
element = translator.getItemFromAbsoluteXpath("/keatest-module:container[key1='key1'][key2='key2']"));
EXPECT_FALSE(element);
}
// Test YANG list retrieval.
TEST_F(TranslatorTest, list) {
ElementPtr element;
// Get a translator to play with.
Translator translator(Connection{}.sessionStart(), "keatest-module");
// List with no data does not throw and returns a null ElementPtr by default.
EXPECT_NO_THROW_LOG(
element = translator.getItemFromAbsoluteXpath("/keatest-module:container/list"));
EXPECT_FALSE(element);
element.reset();
// List with data filled in throws when retrieving the list itself.
element = Element::create("Leaf value");
EXPECT_NO_THROW_LOG(
translator.setItem("/keatest-module:container/list[key1='key1'][key2='key2']/leaf", element,
LeafBaseType::String));
element.reset();
EXPECT_THROW_MSG(
element = translator.getItemFromAbsoluteXpath("/keatest-module:container/list"), NotImplemented,
"getting node of type 16 not supported, xpath is '/keatest-module:container/list'");
EXPECT_FALSE(element);
element.reset();
EXPECT_THROW_MSG(element = translator.getItemFromAbsoluteXpath(
"/keatest-module:container/list[key1='key1'][key2='key2']"), NotImplemented,
"getting node of type 16 not supported, xpath is "
"'/keatest-module:container/list[key1='key1'][key2='key2']'");
EXPECT_FALSE(element);
}
// Test that we can check if a schema node exists.
TEST_F(TranslatorTest, schemaNodeExists) {
// Get a translator to play with.
Translator translator(Connection{}.sessionStart(), "keatest-module");
EXPECT_FALSE(translator.schemaNodeExists("/keatest-module:main/no_such_node"));
EXPECT_TRUE(translator.schemaNodeExists("/keatest-module:main"));
EXPECT_TRUE(translator.schemaNodeExists("/keatest-module:main/string"));
EXPECT_TRUE(translator.schemaNodeExists("/keatest-module:container/list"));
}
} // anonymous namespace
|