blob: c646e13b563c3414ff2c33b4261bd9723841d9ff [file] [log] [blame]
Václav Kubernátf5f64f02019-03-19 17:15:47 +01001/*
2 * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubernat@cesnet.cz>
5 *
6*/
7
Václav Kubernát26b56082020-02-03 18:28:56 +01008#include "trompeloeil_doctest.hpp"
Václav Kubernátf5f64f02019-03-19 17:15:47 +01009#include "parser.hpp"
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020010#include "pretty_printers.hpp"
Václav Kubernátf5f64f02019-03-19 17:15:47 +010011#include "static_schema.hpp"
12
13TEST_CASE("list manipulation")
14{
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020015 using namespace std::string_literals;
Václav Kubernátf5f64f02019-03-19 17:15:47 +010016 auto schema = std::make_shared<StaticSchema>();
17 schema->addModule("mod");
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020018 schema->addModule("other");
Václav Kubernátefcac932020-01-10 15:26:32 +010019 schema->addList("/", "mod:list", {"number"});
Václav Kubernát3a99f002020-03-31 02:27:41 +020020 schema->addLeaf("/mod:list", "mod:number", yang::Int32{});
21 schema->addLeaf("/mod:list", "mod:leafInList", yang::String{});
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020022 schema->addLeafList("/", "mod:addresses", yang::String{});
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020023 schema->addIdentity(std::nullopt, identityRef_{"other", "deptypes"});
24 schema->addIdentity(identityRef_{"other", "deptypes"}, identityRef_{"other", "engineering"});
25 schema->addList("/", "mod:company", {"department"});
26 schema->addLeaf("/mod:company", "mod:department", schema->validIdentities("other", "deptypes"));
27 schema->addList("/mod:company", "mod:inventory", {"id"});
28 schema->addLeaf("/mod:company/mod:inventory", "mod:id", yang::Int32{});
Václav Kubernátbf65dd72020-05-28 02:32:31 +020029 schema->addContainer("/", "mod:cont");
Václav Kubernátf5f64f02019-03-19 17:15:47 +010030 Parser parser(schema);
31 std::string input;
32 std::ostringstream errorStream;
33
34 SECTION("creating/deleting list instances")
35 {
36 dataPath_ expectedPath;
37 SECTION("mod:list[number=3]")
38 {
39 input = "mod:list[number=3]";
Václav Kubernátc15fe822020-06-04 11:28:39 +020040 auto keys = ListInstance {
Václav Kubernát7707cae2020-01-16 12:04:53 +010041 {"number", int32_t{3}}};
Václav Kubernátf5f64f02019-03-19 17:15:47 +010042 expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
43 }
44
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020045 SECTION("mod:company[department=other:engineering]/inventory[id=1337]")
46 {
47 input = "mod:company[department=other:engineering]/inventory[id=1337]";
Václav Kubernátc15fe822020-06-04 11:28:39 +020048 auto keys = ListInstance {
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020049 {"department", identityRef_{"other", "engineering"}}};
50 expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("company", keys)});
Václav Kubernátc15fe822020-06-04 11:28:39 +020051 keys = ListInstance {
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020052 {"id", int32_t{1337}}};
53 expectedPath.m_nodes.push_back(dataNode_{listElement_("inventory", keys)});
54 }
55
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020056 SECTION("create mod:addresses['0.0.0.0']")
57 {
58 input = "mod:addresses['0.0.0.0']";
59 expectedPath.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "0.0.0.0"s}});
60 }
61
62
Václav Kubernátf5f64f02019-03-19 17:15:47 +010063 command_ parsedCreate = parser.parseCommand("create " + input, errorStream);
64 command_ parsedDelete = parser.parseCommand("delete " + input, errorStream);
65 create_ expectedCreate;
66 expectedCreate.m_path = expectedPath;
67 delete_ expectedDelete;
68 expectedDelete.m_path = expectedPath;
69 REQUIRE(parsedCreate.type() == typeid(create_));
70 REQUIRE(parsedDelete.type() == typeid(delete_));
71 REQUIRE(boost::get<create_>(parsedCreate) == expectedCreate);
72 REQUIRE(boost::get<delete_>(parsedDelete) == expectedDelete);
73 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020074
75 SECTION("retrieving all leaflist instances")
76 {
77 dataPath_ expected;
78 input = "get mod:addresses";
79 expected.m_nodes.push_back(dataNode_{module_{"mod"}, leafList_{"addresses"}});
80
81 get_ expectedGet;
82 expectedGet.m_path = expected;
83 command_ commandGet = parser.parseCommand(input, errorStream);
84 REQUIRE(commandGet.type() == typeid(get_));
85 REQUIRE(boost::get<get_>(commandGet) == expectedGet);
86 }
Václav Kubernátbf65dd72020-05-28 02:32:31 +020087
88 SECTION("moving (leaf)list instances")
89 {
90 move_ expected;
91 SECTION("begin")
92 {
93 SECTION("cwd: /")
94 {
95 input = "move mod:addresses['1.2.3.4'] begin";
96 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}});
97 }
98
99 SECTION("cwd: /mod:cont")
100 {
101 parser.changeNode(dataPath_{Scope::Absolute, {dataNode_{module_{"mod"}, container_{"cont"}}}});
102 SECTION("relative")
103 {
104 input = "move ../mod:addresses['1.2.3.4'] begin";
105 expected.m_source.m_nodes.push_back(dataNode_{nodeup_{}});
106 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}});
107 }
108
109 SECTION("absolute")
110 {
111 input = "move /mod:addresses['1.2.3.4'] begin";
112 expected.m_source.m_scope = Scope::Absolute;
113 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}});
114 }
115 }
116
117 expected.m_destination = yang::move::Absolute::Begin;
118 }
119
120 SECTION("end")
121 {
122 input = "move mod:addresses['1.2.3.4'] end";
123 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}});
124 expected.m_destination = yang::move::Absolute::End;
125 }
126
127 SECTION("after")
128 {
129 input = "move mod:addresses['1.2.3.4'] after '0.0.0.0'";
130 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}});
131 expected.m_destination = yang::move::Relative {
132 yang::move::Relative::Position::After,
133 {{".", "0.0.0.0"s}}
134 };
135 }
136
137 SECTION("before")
138 {
139 input = "move mod:addresses['1.2.3.4'] before '0.0.0.0'";
140 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s}});
141 expected.m_destination = yang::move::Relative {
142 yang::move::Relative::Position::Before,
143 {{".", "0.0.0.0"s}}
144 };
145 }
146
147 SECTION("list instance with destination")
148 {
149 input = "move mod:list[number=12] before [number=15]";
150 auto keys = std::map<std::string, leaf_data_>{
151 {"number", int32_t{12}}};
152 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
153 expected.m_destination = yang::move::Relative {
154 yang::move::Relative::Position::Before,
155 ListInstance{{"number", int32_t{15}}}
156 };
157 }
158
159 SECTION("list instance without destination")
160 {
161 input = "move mod:list[number=3] begin";
162 auto keys = std::map<std::string, leaf_data_>{
163 {"number", int32_t{3}}};
164 expected.m_source.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)});
165 expected.m_destination = yang::move::Absolute::Begin;
166 }
167
168 command_ commandMove = parser.parseCommand(input, errorStream);
169 REQUIRE(commandMove.type() == typeid(move_));
170 REQUIRE(boost::get<move_>(commandMove) == expected);
171 }
Václav Kubernátf5f64f02019-03-19 17:15:47 +0100172}