blob: 53163849ac31ddb38dcd9bb80bd9f97b8b0ddd52 [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átb4e5b182020-11-16 19:55:09 +010040 auto keys = ListInstance{
Václav Kubernát7707cae2020-01-16 12:04:53 +010041 {"number", int32_t{3}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +020042 expectedPath.m_nodes.emplace_back(module_{"mod"}, listElement_("list", keys));
Václav Kubernátf5f64f02019-03-19 17:15:47 +010043 }
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átb4e5b182020-11-16 19:55:09 +010048 auto keys = ListInstance{
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020049 {"department", identityRef_{"other", "engineering"}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +020050 expectedPath.m_nodes.emplace_back(module_{"mod"}, listElement_("company", keys));
Václav Kubernátb4e5b182020-11-16 19:55:09 +010051 keys = ListInstance{
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020052 {"id", int32_t{1337}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +020053 expectedPath.m_nodes.emplace_back(listElement_("inventory", keys));
Václav Kubernát1bcee3b2020-05-28 22:19:59 +020054 }
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']";
Václav Kubernátfaacd022020-07-08 16:44:38 +020059 expectedPath.m_nodes.emplace_back(module_{"mod"}, leafListElement_{"addresses", "0.0.0.0"s});
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020060 }
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";
Václav Kubernátfaacd022020-07-08 16:44:38 +020079 expected.m_nodes.emplace_back(module_{"mod"}, leafList_{"addresses"});
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020080
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";
Václav Kubernátfaacd022020-07-08 16:44:38 +020096 expected.m_source.m_nodes.emplace_back(module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s});
Václav Kubernátbf65dd72020-05-28 02:32:31 +020097 }
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";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200105 expected.m_source.m_nodes.emplace_back(nodeup_{});
106 expected.m_source.m_nodes.emplace_back(module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s});
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200107 }
108
109 SECTION("absolute")
110 {
111 input = "move /mod:addresses['1.2.3.4'] begin";
112 expected.m_source.m_scope = Scope::Absolute;
Václav Kubernátfaacd022020-07-08 16:44:38 +0200113 expected.m_source.m_nodes.emplace_back(module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s});
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200114 }
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";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200123 expected.m_source.m_nodes.emplace_back(module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s});
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200124 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'";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200130 expected.m_source.m_nodes.emplace_back(module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s});
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100131 expected.m_destination = yang::move::Relative{
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200132 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'";
Václav Kubernátfaacd022020-07-08 16:44:38 +0200140 expected.m_source.m_nodes.emplace_back(module_{"mod"}, leafListElement_{"addresses", "1.2.3.4"s});
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100141 expected.m_destination = yang::move::Relative{
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200142 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}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200152 expected.m_source.m_nodes.emplace_back(module_{"mod"}, listElement_("list", keys));
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100153 expected.m_destination = yang::move::Relative{
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200154 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}}};
Václav Kubernátfaacd022020-07-08 16:44:38 +0200164 expected.m_source.m_nodes.emplace_back(module_{"mod"}, listElement_("list", keys));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200165 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}