blob: 000f3c733877bd3b55b9654b5d5cc13a29b31632 [file] [log] [blame]
Václav Kubernát73109382018-09-14 19:52:03 +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
Václav Kubernát26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Jan Kundrát6ee84792020-01-24 01:43:36 +010010#include <sysrepo-cpp/Session.hpp>
Václav Kubernáte7248b22020-06-26 15:38:59 +020011#include "proxy_datastore.hpp"
Václav Kubernátb4e5b182020-11-16 19:55:09 +010012#include "yang_schema.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020013
Václav Kubernátc31bd602019-03-07 11:44:48 +010014#ifdef sysrepo_BACKEND
Václav Kubernát73109382018-09-14 19:52:03 +020015#include "sysrepo_access.hpp"
Jan Kundrát7ec214d2020-06-19 17:05:07 +020016using OnInvalidSchemaPathCreate = DatastoreException;
Václav Kubernát654303f2020-07-31 13:16:54 +020017using OnInvalidSchemaPathDelete = DatastoreException;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020018using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception;
Václav Kubernát40776132021-02-03 08:47:33 +010019using OnInvalidRpcPath = std::runtime_error;
Václav Kubernát2edfe542021-02-03 08:08:29 +010020using OnInvalidRpcInput = sysrepo::sysrepo_exception;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020021using OnKeyNotFound = void;
Václav Kubernáta8789602020-07-20 15:18:19 +020022using OnExec = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010023#elif defined(netconf_BACKEND)
Jan Kundrát7ec214d2020-06-19 17:05:07 +020024using OnInvalidSchemaPathCreate = std::runtime_error;
25using OnInvalidSchemaPathDelete = std::runtime_error;
26using OnInvalidSchemaPathMove = std::runtime_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020027using OnInvalidRpcPath = std::runtime_error;
Václav Kubernát2edfe542021-02-03 08:08:29 +010028using OnInvalidRpcInput = std::runtime_error;
Jan Kundrát7ec214d2020-06-19 17:05:07 +020029using OnKeyNotFound = std::runtime_error;
Václav Kubernáta8789602020-07-20 15:18:19 +020030using OnExec = void;
Václav Kubernátc31bd602019-03-07 11:44:48 +010031#include "netconf_access.hpp"
Václav Kubernát74487df2020-06-04 01:29:28 +020032#elif defined(yang_BACKEND)
33#include <fstream>
34#include "yang_access.hpp"
35#include "yang_access_test_vars.hpp"
36using OnInvalidSchemaPathCreate = DatastoreException;
37using OnInvalidSchemaPathDelete = DatastoreException;
38using OnInvalidSchemaPathMove = DatastoreException;
39using OnInvalidRpcPath = DatastoreException;
Václav Kubernát2edfe542021-02-03 08:08:29 +010040using OnInvalidRpcInput = std::logic_error;
Václav Kubernát74487df2020-06-04 01:29:28 +020041using OnKeyNotFound = DatastoreException;
Václav Kubernáta8789602020-07-20 15:18:19 +020042using OnExec = std::logic_error;
Václav Kubernátc31bd602019-03-07 11:44:48 +010043#else
44#error "Unknown backend"
45#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +010046#include "pretty_printers.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020047#include "sysrepo_subscription.hpp"
Václav Kubernát8e121ff2019-10-15 15:47:45 +020048#include "utils.hpp"
Václav Kubernát73109382018-09-14 19:52:03 +020049
Jan Kundrát6ee84792020-01-24 01:43:36 +010050using namespace std::literals::string_literals;
51
Václav Kubernát69aabe92020-01-24 16:53:12 +010052class MockRecorder : public trompeloeil::mock_interface<Recorder> {
Václav Kubernát73109382018-09-14 19:52:03 +020053public:
Václav Kubernát69aabe92020-01-24 16:53:12 +010054 IMPLEMENT_MOCK3(write);
Václav Kubernát73109382018-09-14 19:52:03 +020055};
56
Jan Kundrátbb525b42020-02-04 11:56:59 +010057class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> {
58public:
59 IMPLEMENT_CONST_MOCK1(get_data);
60};
61
Jan Kundrát7ec214d2020-06-19 17:05:07 +020062namespace {
63template <class ...> constexpr std::false_type always_false [[maybe_unused]] {};
64template <class Exception, typename Callable> void catching(const Callable& what) {
65
66 if constexpr (std::is_same_v<Exception, void>) {
Jan Kundrát3867c9e2020-06-18 20:26:45 +020067 what();
Jan Kundrát7ec214d2020-06-19 17:05:07 +020068 } else if constexpr (std::is_same<Exception, std::runtime_error>()) {
69 // cannot use REQUIRE_THROWS_AS(..., Exception) directly because that one
70 // needs an extra `typename` deep in the bowels of doctest
71 REQUIRE_THROWS_AS(what(), std::runtime_error);
Václav Kubernát74487df2020-06-04 01:29:28 +020072 } else if constexpr (std::is_same<Exception, std::logic_error>()) {
73 REQUIRE_THROWS_AS(what(), std::logic_error);
Jan Kundrát7ec214d2020-06-19 17:05:07 +020074 } else if constexpr (std::is_same<Exception, DatastoreException>()) {
75 REQUIRE_THROWS_AS(what(), DatastoreException);
76 } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) {
77 REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception);
78 } else {
79 static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623
Jan Kundrát3867c9e2020-06-18 20:26:45 +020080 }
81}
Jan Kundrát7ec214d2020-06-19 17:05:07 +020082}
Jan Kundrát3867c9e2020-06-18 20:26:45 +020083
Václav Kubernát74487df2020-06-04 01:29:28 +020084#if defined(yang_BACKEND)
85class TestYangAccess : public YangAccess {
86public:
87 void commitChanges() override
88 {
89 YangAccess::commitChanges();
90 dumpToSysrepo();
91 }
92
93 void copyConfig(const Datastore source, const Datastore destination) override
94 {
95 YangAccess::copyConfig(source, destination);
96 dumpToSysrepo();
97 }
98
99private:
100 void dumpToSysrepo()
101 {
102 {
103 std::ofstream of(testConfigFile);
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200104 of << dump(DataFormat::Xml);
Václav Kubernát74487df2020-06-04 01:29:28 +0200105 }
Jan Kundrát86840ec2021-01-27 15:38:08 +0100106 auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running --module=example-schema";
Václav Kubernát74487df2020-06-04 01:29:28 +0200107 REQUIRE(std::system(command.c_str()) == 0);
108 }
109};
110#endif
111
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200112TEST_CASE("setting/getting values")
Václav Kubernát73109382018-09-14 19:52:03 +0200113{
Václav Kubernát654303f2020-07-31 13:16:54 +0200114 sr_log_stderr(SR_LL_DBG);
Václav Kubernát73109382018-09-14 19:52:03 +0200115 trompeloeil::sequence seq1;
116 MockRecorder mock;
Václav Kubernát654303f2020-07-31 13:16:54 +0200117 {
118 auto conn = std::make_shared<sysrepo::Connection>();
119 auto sess = std::make_shared<sysrepo::Session>(conn);
120 sess->copy_config(SR_DS_STARTUP, "example-schema", 1000, true);
121 }
Václav Kubernátab612e92019-11-26 19:51:31 +0100122 SysrepoSubscription subscription("example-schema", &mock);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100123
124#ifdef sysrepo_BACKEND
Václav Kubernát654303f2020-07-31 13:16:54 +0200125 SysrepoAccess datastore(Datastore::Running);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100126#elif defined(netconf_BACKEND)
Václav Kubernátd1beedc2020-09-07 12:09:05 +0200127 const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET");
128 NetconfAccess datastore(NETOPEER_SOCKET);
Václav Kubernát74487df2020-06-04 01:29:28 +0200129#elif defined(yang_BACKEND)
130 TestYangAccess datastore;
131 datastore.addSchemaDir(schemaDir);
132 datastore.addSchemaFile(exampleSchemaFile);
Václav Kubernátc31bd602019-03-07 11:44:48 +0100133#else
134#error "Unknown backend"
135#endif
Václav Kubernát73109382018-09-14 19:52:03 +0200136
Václav Kubernát69aabe92020-01-24 16:53:12 +0100137
Václav Kubernát134d78f2019-09-03 16:42:29 +0200138 SECTION("set leafInt8 to -128")
Václav Kubernát73109382018-09-14 19:52:03 +0200139 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100140 REQUIRE_CALL(mock, write("/example-schema:leafInt8", std::nullopt, "-128"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200141 datastore.setLeaf("/example-schema:leafInt8", int8_t{-128});
142 datastore.commitChanges();
143 }
144
145 SECTION("set leafInt16 to -32768")
146 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100147 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "-32768"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200148 datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768});
149 datastore.commitChanges();
150 }
151
152 SECTION("set leafInt32 to -2147483648")
153 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100154 REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200155 datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648});
156 datastore.commitChanges();
157 }
158
159 SECTION("set leafInt64 to -50000000000")
160 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100161 REQUIRE_CALL(mock, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200162 datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000});
163 datastore.commitChanges();
164 }
165
166 SECTION("set leafUInt8 to 255")
167 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100168 REQUIRE_CALL(mock, write("/example-schema:leafUInt8", std::nullopt, "255"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200169 datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255});
170 datastore.commitChanges();
171 }
172
173 SECTION("set leafUInt16 to 65535")
174 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100175 REQUIRE_CALL(mock, write("/example-schema:leafUInt16", std::nullopt, "65535"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200176 datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535});
177 datastore.commitChanges();
178 }
179
180 SECTION("set leafUInt32 to 4294967295")
181 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100182 REQUIRE_CALL(mock, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200183 datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295});
184 datastore.commitChanges();
185 }
186
187 SECTION("set leafUInt64 to 50000000000")
188 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100189 REQUIRE_CALL(mock, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s));
Václav Kubernát134d78f2019-09-03 16:42:29 +0200190 datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000});
Václav Kubernát73109382018-09-14 19:52:03 +0200191 datastore.commitChanges();
192 }
193
194 SECTION("set leafEnum to coze")
195 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100196 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "coze"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200197 datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"});
198 datastore.commitChanges();
199 }
200
201 SECTION("set leafDecimal to 123.544")
202 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100203 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.544"s));
Václav Kubernát73109382018-09-14 19:52:03 +0200204 datastore.setLeaf("/example-schema:leafDecimal", 123.544);
205 datastore.commitChanges();
206 }
207
Jan Kundrátd2872862020-06-18 21:02:00 +0200208 SECTION("set a string, then delete it")
209 {
210 REQUIRE_CALL(mock, write("/example-schema:leafString", std::nullopt, "blah"s));
211 datastore.setLeaf("/example-schema:leafString", "blah"s);
212 datastore.commitChanges();
213 DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}};
214 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
215
216 REQUIRE_CALL(mock, write("/example-schema:leafString", "blah"s, std::nullopt));
217 datastore.deleteItem("/example-schema:leafString");
218 datastore.commitChanges();
219 expected.clear();
220 REQUIRE(datastore.getItems("/example-schema:leafString") == expected);
221 }
222
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200223 SECTION("set a non-existing leaf")
224 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100225 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200226 datastore.setLeaf("/example-schema:non-existing", "what"s);
227 });
228 }
229
Václav Kubernát73109382018-09-14 19:52:03 +0200230 SECTION("create presence container")
231 {
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200232 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") == std::string::npos);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100233 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200234 datastore.createItem("/example-schema:pContainer");
Václav Kubernát73109382018-09-14 19:52:03 +0200235 datastore.commitChanges();
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200236 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") != std::string::npos);
Václav Kubernát73109382018-09-14 19:52:03 +0200237 }
238
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100239 SECTION("create/delete a list instance")
Václav Kubernát45f4a822019-05-29 21:10:50 +0200240 {
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100241 {
242 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s));
243 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200244 datastore.createItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100245 datastore.commitChanges();
246 }
247 {
248 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt));
249 REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200250 datastore.deleteItem("/example-schema:person[name='Nguyen']");
Václav Kubernátcc7a93f2020-02-04 11:48:15 +0100251 datastore.commitChanges();
252 }
Václav Kubernát45f4a822019-05-29 21:10:50 +0200253 }
254
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200255 SECTION("deleting non-existing list keys")
256 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100257 catching<OnKeyNotFound>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200258 datastore.deleteItem("/example-schema:person[name='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200259 datastore.commitChanges();
260 });
261 }
262
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200263 SECTION("accessing non-existing schema nodes as a list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200264 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100265 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200266 datastore.createItem("/example-schema:non-existing-list[xxx='blah']");
267 datastore.commitChanges();
268 });
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100269 catching<OnInvalidSchemaPathDelete>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200270 datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200271 datastore.commitChanges();
272 });
273 }
274
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200275 SECTION("leafref pointing to a key of a list")
276 {
277 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100278 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s));
279 REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s));
280 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s));
281 REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s));
282 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s));
283 REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200284 datastore.createItem("/example-schema:person[name='Dan']");
285 datastore.createItem("/example-schema:person[name='Elfi']");
286 datastore.createItem("/example-schema:person[name='Kolafa']");
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200287 datastore.commitChanges();
288 }
289
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200290 std::string value;
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200291 SECTION("Dan")
292 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200293 value = "Dan";
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200294 }
295
296 SECTION("Elfi")
297 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200298 value = "Elfi";
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200299 }
300
301 SECTION("Kolafa")
302 {
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200303 value = "Kolafa";
304 }
305
306 datastore.setLeaf("/example-schema:bossPerson", value);
307 {
308 REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, value));
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200309 datastore.commitChanges();
310 }
Václav Kubernát7b191ce2020-06-30 16:22:53 +0200311 REQUIRE(datastore.getItems("/example-schema:bossPerson") == DatastoreAccess::Tree{{"/example-schema:bossPerson", value}});
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200312 }
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200313 SECTION("bool values get correctly represented as bools")
314 {
315 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100316 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "true"s));
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200317 datastore.setLeaf("/example-schema:down", bool{true});
318 datastore.commitChanges();
319 }
320
Jan Kundrátb331b552020-01-23 15:25:29 +0100321 DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}};
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200322 REQUIRE(datastore.getItems("/example-schema:down") == expected);
323 }
Václav Kubernát3efb5ca2019-10-09 20:07:40 +0200324
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200325 SECTION("getting items from the whole module")
326 {
327 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100328 REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s));
329 REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "false"s));
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200330 datastore.setLeaf("/example-schema:up", bool{true});
331 datastore.setLeaf("/example-schema:down", bool{false});
332 datastore.commitChanges();
333 }
334
Václav Kubernátcf9224f2020-06-02 09:55:29 +0200335 DatastoreAccess::Tree expected{
Václav Kubernát654303f2020-07-31 13:16:54 +0200336 {"/example-schema:up", bool{true}},
337 {"/example-schema:down", bool{false}}
338 };
Václav Kubernát9456b5c2019-10-02 21:14:52 +0200339 REQUIRE(datastore.getItems("/example-schema:*") == expected);
340 }
341
Václav Kubernát152ce222019-12-19 12:23:32 +0100342 SECTION("getItems returns correct datatypes")
343 {
344 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100345 REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s));
Václav Kubernát152ce222019-12-19 12:23:32 +0100346 datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"});
347 datastore.commitChanges();
348 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100349 DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}};
Václav Kubernát152ce222019-12-19 12:23:32 +0100350
351 REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected);
352 }
353
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100354 SECTION("getItems on a list")
355 {
356 {
Václav Kubernát69aabe92020-01-24 16:53:12 +0100357 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s));
358 REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s));
359 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s));
360 REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s));
361 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s));
362 REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200363 datastore.createItem("/example-schema:person[name='Jan']");
364 datastore.createItem("/example-schema:person[name='Michal']");
365 datastore.createItem("/example-schema:person[name='Petr']");
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100366 datastore.commitChanges();
367 }
Jan Kundrátb331b552020-01-23 15:25:29 +0100368 DatastoreAccess::Tree expected{
Václav Kubernát144729d2020-01-08 15:20:35 +0100369 {"/example-schema:person[name='Jan']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100370 {"/example-schema:person[name='Jan']/name", std::string{"Jan"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100371 {"/example-schema:person[name='Michal']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100372 {"/example-schema:person[name='Michal']/name", std::string{"Michal"}},
Václav Kubernát144729d2020-01-08 15:20:35 +0100373 {"/example-schema:person[name='Petr']", special_{SpecialValue::List}},
Václav Kubernátd812cfb2020-01-07 17:30:20 +0100374 {"/example-schema:person[name='Petr']/name", std::string{"Petr"}}
375 };
376
377 REQUIRE(datastore.getItems("/example-schema:person") == expected);
378 }
379
Václav Kubernát69aabe92020-01-24 16:53:12 +0100380 SECTION("presence containers")
381 {
382 DatastoreAccess::Tree expected;
383 // Make sure it's not there before we create it
384 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
385
386 {
387 REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200388 datastore.createItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100389 datastore.commitChanges();
390 }
391 expected = {
392 {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}}
393 };
394 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
395
396 // Make sure it's not there after we delete it
397 {
398 REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200399 datastore.deleteItem("/example-schema:pContainer");
Václav Kubernát69aabe92020-01-24 16:53:12 +0100400 datastore.commitChanges();
401 }
402 expected = {};
403 REQUIRE(datastore.getItems("/example-schema:pContainer") == expected);
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100404 }
Václav Kubernát69aabe92020-01-24 16:53:12 +0100405
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200406 SECTION("creating a non-existing schema node as a container")
407 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100408 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200409 datastore.createItem("/example-schema:non-existing-presence-container");
410 datastore.commitChanges();
411 });
412 }
413
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200414 SECTION("deleting a non-existing schema node as a container or leaf")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200415 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100416 catching<OnInvalidSchemaPathDelete>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200417 datastore.deleteItem("/example-schema:non-existing-presence-container");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200418 datastore.commitChanges();
419 });
420 }
421
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100422 SECTION("nested presence container")
423 {
424 DatastoreAccess::Tree expected;
425 // Make sure it's not there before we create it
426 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
427 {
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100428 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200429 datastore.createItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100430 datastore.commitChanges();
431 }
432 expected = {
433 {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}}
434 };
435 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
436 {
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100437 REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200438 datastore.deleteItem("/example-schema:inventory/stuff");
Václav Kubernátda8e4b92020-02-04 11:56:30 +0100439 datastore.commitChanges();
440 }
441 expected = {};
442 REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected);
Václav Kubernát69aabe92020-01-24 16:53:12 +0100443 }
444
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100445 SECTION("floats")
446 {
447 datastore.setLeaf("/example-schema:leafDecimal", 123.4);
448 REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s));
449 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100450 DatastoreAccess::Tree expected{
Jan Kundrátbd3169c2020-02-03 19:31:34 +0100451 {"/example-schema:leafDecimal", 123.4},
452 };
453 REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected);
454 }
455
Jan Kundrát3ff50122020-05-07 00:37:50 +0200456 SECTION("unions")
457 {
458 datastore.setLeaf("/example-schema:unionIntString", int32_t{10});
459 REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s));
460 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100461 DatastoreAccess::Tree expected{
Jan Kundrát3ff50122020-05-07 00:37:50 +0200462 {"/example-schema:unionIntString", int32_t{10}},
463 };
464 REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected);
465 }
466
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100467 SECTION("identityref")
468 {
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200469 datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"});
470 REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s));
471 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100472 DatastoreAccess::Tree expected{
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200473 {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}},
474 };
475 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
476
477 datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"});
478 REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s));
479 datastore.commitChanges();
480 expected = {
481 {"/example-schema:beast", identityRef_{"example-schema", "Whale"}},
482 };
483 REQUIRE(datastore.getItems("/example-schema:beast") == expected);
484 }
485
Jan Kundrát68985442020-05-07 02:15:34 +0200486 SECTION("binary")
487 {
488 datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s});
489 REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s));
490 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100491 DatastoreAccess::Tree expected{
Jan Kundrát68985442020-05-07 02:15:34 +0200492 {"/example-schema:blob", binary_{"cHduegByIQ=="s}},
493 };
494 REQUIRE(datastore.getItems("/example-schema:blob") == expected);
495 }
496
Jan Kundrát379bb572020-05-07 03:23:13 +0200497 SECTION("empty")
498 {
499 datastore.setLeaf("/example-schema:dummy", empty_{});
500 REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s));
501 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100502 DatastoreAccess::Tree expected{
Jan Kundrát379bb572020-05-07 03:23:13 +0200503 {"/example-schema:dummy", empty_{}},
504 };
505 REQUIRE(datastore.getItems("/example-schema:dummy") == expected);
506 }
507
Václav Kubernát19097f32020-10-05 10:08:29 +0200508 SECTION("bits")
509 {
510 datastore.setLeaf("/example-schema:flags", bits_{{"sign", "carry"}});
511 REQUIRE_CALL(mock, write("/example-schema:flags", std::nullopt, "carry sign"s));
512 datastore.commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100513 DatastoreAccess::Tree expected{
Václav Kubernát19097f32020-10-05 10:08:29 +0200514 {"/example-schema:flags", bits_{{"carry", "sign"}}},
515 };
516 REQUIRE(datastore.getItems("/example-schema:flags") == expected);
517 }
518
Václav Kubernát74487df2020-06-04 01:29:28 +0200519#if not defined(yang_BACKEND)
Jan Kundrátbb525b42020-02-04 11:56:59 +0100520 SECTION("operational data")
521 {
522 MockDataSupplier mockOpsData;
Václav Kubernát654303f2020-07-31 13:16:54 +0200523 OperationalDataSubscription opsDataSub("example-schema", "/example-schema:temperature", mockOpsData);
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100524 OperationalDataSubscription opsDataSub2("example-schema", "/example-schema:users", mockOpsData);
Jan Kundrátbb525b42020-02-04 11:56:59 +0100525 DatastoreAccess::Tree expected;
526 std::string xpath;
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100527
Jan Kundrátbb525b42020-02-04 11:56:59 +0100528 SECTION("temperature")
529 {
530 expected = {{"/example-schema:temperature", int32_t{22}}};
531 xpath = "/example-schema:temperature";
532 }
533
Václav Kubernátf90a0b52020-11-06 05:53:03 +0100534 SECTION("key-less lists")
535 {
536 expected = {
537 {"/example-schema:users/userList[1]", special_{SpecialValue::List}},
538 {"/example-schema:users/userList[1]/name", std::string{"John"}},
539 {"/example-schema:users/userList[1]/otherfield", std::string{"LOL"}},
540 {"/example-schema:users/userList[2]", special_{SpecialValue::List}},
541 {"/example-schema:users/userList[2]/name", std::string{"Foo"}},
542 {"/example-schema:users/userList[2]/otherfield", std::string{"Bar"}},
543 };
544 xpath = "/example-schema:users";
545 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100546 REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected);
547 REQUIRE(datastore.getItems(xpath) == expected);
548 }
Václav Kubernát74487df2020-06-04 01:29:28 +0200549#endif
Jan Kundrátbb525b42020-02-04 11:56:59 +0100550
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200551 SECTION("leaf list")
552 {
553 DatastoreAccess::Tree expected;
554 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s));
555 REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200556 datastore.createItem("/example-schema:addresses[.='0.0.0.0']");
557 datastore.createItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200558 datastore.commitChanges();
559 expected = {
560 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
561 {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s},
562 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
563 };
564 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
565
566 REQUIRE_CALL(mock, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200567 datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200568 datastore.commitChanges();
569 expected = {
570 {"/example-schema:addresses", special_{SpecialValue::LeafList}},
571 {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s},
572 };
573 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
574
575 REQUIRE_CALL(mock, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200576 datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']");
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200577 datastore.commitChanges();
578 expected = {};
579 REQUIRE(datastore.getItems("/example-schema:addresses") == expected);
580 }
Jan Kundrátbb525b42020-02-04 11:56:59 +0100581
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200582 SECTION("deleting a non-existing leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200583 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100584 catching<OnKeyNotFound>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200585 datastore.deleteItem("/example-schema:addresses[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200586 datastore.commitChanges();
587 });
588 }
589
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200590 SECTION("accessing a non-existing schema node as a leaf-list")
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200591 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100592 catching<OnInvalidSchemaPathCreate>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200593 datastore.createItem("/example-schema:non-existing[.='non-existing']");
594 datastore.commitChanges();
595 });
596
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100597 catching<OnInvalidSchemaPathDelete>([&] {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200598 datastore.deleteItem("/example-schema:non-existing[.='non-existing']");
Jan Kundrát3867c9e2020-06-18 20:26:45 +0200599 datastore.commitChanges();
600 });
601 }
602
Václav Kubernát7160a132020-04-03 02:11:01 +0200603 SECTION("copying data from startup refreshes the data")
604 {
605 {
606 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
607 REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s));
608 datastore.setLeaf("/example-schema:leafInt16", int16_t{123});
609 datastore.commitChanges();
610 }
611 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}});
612 REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt));
613 datastore.copyConfig(Datastore::Startup, Datastore::Running);
614 REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{});
615 }
616
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200617 SECTION("moving leaflist instances")
618 {
619 DatastoreAccess::Tree expected;
620 {
Václav Kubernát654303f2020-07-31 13:16:54 +0200621 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "http"s));
622 // FIXME: Why no notifications for these??
623 // ... possibly because my subscription doesn't extract it properly?
624 // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s));
625 // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200626 REQUIRE_CALL(mock, write("/example-schema:protocols", "http"s, "ftp"s));
627 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "pop3"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200628 datastore.createItem("/example-schema:protocols[.='http']");
629 datastore.createItem("/example-schema:protocols[.='ftp']");
630 datastore.createItem("/example-schema:protocols[.='pop3']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200631 datastore.commitChanges();
632 expected = {
633 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
634 {"/example-schema:protocols[.='http']", "http"s},
635 {"/example-schema:protocols[.='ftp']", "ftp"s},
636 {"/example-schema:protocols[.='pop3']", "pop3"s},
637 };
638 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
639 }
640
641 std::string sourcePath;
642 SECTION("begin")
643 {
644 REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s));
645 sourcePath = "/example-schema:protocols[.='pop3']";
646 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
647 datastore.commitChanges();
648 expected = {
649 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
650 {"/example-schema:protocols[.='pop3']", "pop3"s},
651 {"/example-schema:protocols[.='http']", "http"s},
652 {"/example-schema:protocols[.='ftp']", "ftp"s},
653 };
654 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
655 }
656
657 SECTION("end")
658 {
659 sourcePath = "/example-schema:protocols[.='http']";
660 REQUIRE_CALL(mock, write("/example-schema:protocols", "pop3"s, "http"s));
661 datastore.moveItem(sourcePath, yang::move::Absolute::End);
662 datastore.commitChanges();
663 expected = {
664 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
665 {"/example-schema:protocols[.='ftp']", "ftp"s},
666 {"/example-schema:protocols[.='pop3']", "pop3"s},
667 {"/example-schema:protocols[.='http']", "http"s},
668 };
669 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
670 }
671
672 SECTION("after")
673 {
674 sourcePath = "/example-schema:protocols[.='http']";
675 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
676 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}});
677 datastore.commitChanges();
678 expected = {
679 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
680 {"/example-schema:protocols[.='ftp']", "ftp"s},
681 {"/example-schema:protocols[.='http']", "http"s},
682 {"/example-schema:protocols[.='pop3']", "pop3"s},
683 };
684 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
685 }
686
687 SECTION("before")
688 {
689 sourcePath = "/example-schema:protocols[.='http']";
690 REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s));
691 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}});
692 datastore.commitChanges();
693 expected = {
694 {"/example-schema:protocols", special_{SpecialValue::LeafList}},
695 {"/example-schema:protocols[.='ftp']", "ftp"s},
696 {"/example-schema:protocols[.='http']", "http"s},
697 {"/example-schema:protocols[.='pop3']", "pop3"s},
698 };
699 REQUIRE(datastore.getItems("/example-schema:protocols") == expected);
700 }
701 }
702
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200703 SECTION("moving non-existing schema nodes")
704 {
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100705 catching<OnInvalidSchemaPathMove>([&] {
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200706 datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin);
707 datastore.commitChanges();
708 });
709 }
710
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200711 SECTION("moving list instances")
712 {
713 DatastoreAccess::Tree expected;
714 {
Václav Kubernát654303f2020-07-31 13:16:54 +0200715 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", std::nullopt, ""s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200716 REQUIRE_CALL(mock, write("/example-schema:players[name='John']/name", std::nullopt, "John"s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200717 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", ""s, ""s));
718 REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s));
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200719 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", ""s, ""s));
Václav Kubernát654303f2020-07-31 13:16:54 +0200720 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s));
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200721 datastore.createItem("/example-schema:players[name='John']");
722 datastore.createItem("/example-schema:players[name='Eve']");
723 datastore.createItem("/example-schema:players[name='Adam']");
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200724 datastore.commitChanges();
725 expected = {
726 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
727 {"/example-schema:players[name='John']/name", "John"s},
728 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
729 {"/example-schema:players[name='Eve']/name", "Eve"s},
730 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
731 {"/example-schema:players[name='Adam']/name", "Adam"s},
732 };
733 REQUIRE(datastore.getItems("/example-schema:players") == expected);
734 }
735
736 std::string sourcePath;
737 SECTION("begin")
738 {
739 sourcePath = "/example-schema:players[name='Adam']";
740 REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s));
741 datastore.moveItem(sourcePath, yang::move::Absolute::Begin);
742 datastore.commitChanges();
743 expected = {
744 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
745 {"/example-schema:players[name='Adam']/name", "Adam"s},
746 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
747 {"/example-schema:players[name='John']/name", "John"s},
748 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
749 {"/example-schema:players[name='Eve']/name", "Eve"s},
750 };
751 REQUIRE(datastore.getItems("/example-schema:players") == expected);
752 }
753
754 SECTION("end")
755 {
756 sourcePath = "/example-schema:players[name='John']";
757 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
758 datastore.moveItem(sourcePath, yang::move::Absolute::End);
759 datastore.commitChanges();
760 expected = {
761 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
762 {"/example-schema:players[name='Eve']/name", "Eve"s},
763 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
764 {"/example-schema:players[name='Adam']/name", "Adam"s},
765 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
766 {"/example-schema:players[name='John']/name", "John"s},
767 };
768 REQUIRE(datastore.getItems("/example-schema:players") == expected);
769 }
770
771 SECTION("after")
772 {
773 sourcePath = "/example-schema:players[name='John']";
774 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
775 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}});
776 datastore.commitChanges();
777 expected = {
778 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
779 {"/example-schema:players[name='Eve']/name", "Eve"s},
780 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
781 {"/example-schema:players[name='John']/name", "John"s},
782 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
783 {"/example-schema:players[name='Adam']/name", "Adam"s},
784 };
785 REQUIRE(datastore.getItems("/example-schema:players") == expected);
786 }
787
788 SECTION("before")
789 {
790 sourcePath = "/example-schema:players[name='John']";
791 REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s));
792 datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}});
793 datastore.commitChanges();
794 expected = {
795 {"/example-schema:players[name='Eve']", special_{SpecialValue::List}},
796 {"/example-schema:players[name='Eve']/name", "Eve"s},
797 {"/example-schema:players[name='John']", special_{SpecialValue::List}},
798 {"/example-schema:players[name='John']/name", "John"s},
799 {"/example-schema:players[name='Adam']", special_{SpecialValue::List}},
800 {"/example-schema:players[name='Adam']/name", "Adam"s},
801 };
802 REQUIRE(datastore.getItems("/example-schema:players") == expected);
803 }
804 }
805
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200806 SECTION("getting /")
807 {
808 {
809 REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "64"s));
810 datastore.setLeaf("/example-schema:leafInt32", 64);
811 datastore.commitChanges();
812 }
813
814 DatastoreAccess::Tree expected{
Václav Kubernát654303f2020-07-31 13:16:54 +0200815 {"/example-schema:leafInt32", 64}
816 };
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200817 // This tests if we at least get the data WE added.
Václav Kubernát654303f2020-07-31 13:16:54 +0200818 REQUIRE(std::all_of(expected.begin(), expected.end(), [items = datastore.getItems("/")] (const auto& item) {
819 return std::find(items.begin(), items.end(), item) != items.end();
820 }));
Václav Kubernátfa96ac22020-06-18 17:03:52 +0200821 }
822
Václav Kubernát36986c52020-06-25 10:30:05 +0200823 SECTION("setting and removing without commit")
824 {
825 datastore.setLeaf("/example-schema:leafInt32", 64);
826 datastore.deleteItem("/example-schema:leafInt32");
827 }
828
Václav Kubernát70d7f7a2020-06-23 14:40:40 +0200829 SECTION("two key lists")
830 {
831 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']", std::nullopt, ""s));
832 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s));
833 REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s));
834 datastore.createItem("/example-schema:point[x='12'][y='10']");
835 datastore.commitChanges();
836 REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:point") != std::string::npos);
837 }
838
Václav Kubernát73109382018-09-14 19:52:03 +0200839 waitForCompletionAndBitMore(seq1);
840}
Jan Kundrát6ee84792020-01-24 01:43:36 +0100841
Václav Kubernát654303f2020-07-31 13:16:54 +0200842struct ActionCb {
843 int operator()(sysrepo::S_Session session,
844 const char* xpath,
845 [[maybe_unused]] const sysrepo::S_Vals input,
846 [[maybe_unused]] sr_event_t event,
847 [[maybe_unused]] uint32_t request_id,
848 sysrepo::S_Vals_Holder output)
Václav Kubernáta8789602020-07-20 15:18:19 +0200849 {
Václav Kubernát654303f2020-07-31 13:16:54 +0200850 if (session->get_context()->get_node(nullptr, xpath)->path(LYS_PATH_FIRST_PREFIX) == "/example-schema:ports/shutdown") {
Václav Kubernáta8789602020-07-20 15:18:19 +0200851 auto buf = output->allocate(1);
852 buf->val(0)->set(joinPaths(xpath, "success").c_str(), true);
853 return SR_ERR_OK;
854 }
855 throw std::runtime_error("unrecognized RPC");
856 }
Václav Kubernát654303f2020-07-31 13:16:54 +0200857};
Václav Kubernáta8789602020-07-20 15:18:19 +0200858
Václav Kubernát654303f2020-07-31 13:16:54 +0200859struct RpcCb {
860 int operator()([[maybe_unused]] sysrepo::S_Session session,
861 const char* xpath,
862 const sysrepo::S_Vals input,
863 [[maybe_unused]] sr_event_t event,
864 [[maybe_unused]] uint32_t request_id,
865 sysrepo::S_Vals_Holder output)
Jan Kundrát6ee84792020-01-24 01:43:36 +0100866 {
867 const auto nukes = "/example-schema:launch-nukes"s;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200868 if (xpath == "/example-schema:noop"s || xpath == "/example-schema:fire"s) {
Jan Kundrát6ee84792020-01-24 01:43:36 +0100869 return SR_ERR_OK;
Václav Kubernáte7248b22020-06-26 15:38:59 +0200870 }
871
872 if (xpath == nukes) {
Jan Kundrát6ee84792020-01-24 01:43:36 +0100873 uint64_t kilotons = 0;
874 bool hasCities = false;
875 for (size_t i = 0; i < input->val_cnt(); ++i) {
876 const auto& val = input->val(i);
Václav Kubernátf4b6a932020-07-09 10:34:19 +0200877 if (val->xpath() == nukes + "/payload") {
878 continue; // ignore, container
879 }
880 if (val->xpath() == nukes + "/description") {
881 continue; // unused
882 }
883
Jan Kundrát6ee84792020-01-24 01:43:36 +0100884 if (val->xpath() == nukes + "/payload/kilotons") {
885 kilotons = val->data()->get_uint64();
Jan Kundrát6ee84792020-01-24 01:43:36 +0100886 } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) {
887 hasCities = true;
888 } else {
889 throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath());
890 }
891 }
892 if (kilotons == 333'666) {
893 // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>.
894 return SR_ERR_OK;
895 }
896 auto buf = output->allocate(2);
897 size_t i = 0;
898 buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666});
899 buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons));
900 if (hasCities) {
901 buf = output->reallocate(output->val_cnt() + 2);
902 buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London");
903 buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin");
904 }
905 return SR_ERR_OK;
906 }
907 throw std::runtime_error("unrecognized RPC");
908 }
909};
910
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100911TEST_CASE("rpc/action")
912{
Jan Kundrát6ee84792020-01-24 01:43:36 +0100913 trompeloeil::sequence seq1;
Jan Kundrát6ee84792020-01-24 01:43:36 +0100914
915#ifdef sysrepo_BACKEND
Václav Kubernát654303f2020-07-31 13:16:54 +0200916 auto datastore = std::make_shared<SysrepoAccess>(Datastore::Running);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100917#elif defined(netconf_BACKEND)
Václav Kubernátd1beedc2020-09-07 12:09:05 +0200918 const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET");
919 auto datastore = std::make_shared<NetconfAccess>(NETOPEER_SOCKET);
Václav Kubernát74487df2020-06-04 01:29:28 +0200920#elif defined(yang_BACKEND)
Václav Kubernáte7248b22020-06-26 15:38:59 +0200921 auto datastore = std::make_shared<YangAccess>();
922 datastore->addSchemaDir(schemaDir);
923 datastore->addSchemaFile(exampleSchemaFile);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100924#else
925#error "Unknown backend"
926#endif
927
Václav Kubernát654303f2020-07-31 13:16:54 +0200928 auto srConn = std::make_shared<sysrepo::Connection>();
Václav Kubernáta8789602020-07-20 15:18:19 +0200929 auto srSession = std::make_shared<sysrepo::Session>(srConn);
930 auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession);
Václav Kubernát654303f2020-07-31 13:16:54 +0200931 auto rpcCb = std::make_shared<RpcCb>();
932 auto actionCb = std::make_shared<ActionCb>();
Václav Kubernáta8789602020-07-20 15:18:19 +0200933 sysrepo::Logs{}.set_stderr(SR_LL_INF);
Václav Kubernát654303f2020-07-31 13:16:54 +0200934 SysrepoSubscription subscription("example-schema", nullptr);
Václav Kubernáta8789602020-07-20 15:18:19 +0200935 // careful here, sysrepo insists on module_change CBs being registered before RPC CBs, otherwise there's a memleak
Václav Kubernát654303f2020-07-31 13:16:54 +0200936 srSubscription->rpc_subscribe("/example-schema:noop", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
937 srSubscription->rpc_subscribe("/example-schema:launch-nukes", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
938 srSubscription->rpc_subscribe("/example-schema:fire", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE);
939 srSubscription->rpc_subscribe("/example-schema:ports/shutdown", ActionCb{}, 0, SR_SUBSCR_CTX_REUSE);
Václav Kubernáte7248b22020-06-26 15:38:59 +0200940
Václav Kubernáta8789602020-07-20 15:18:19 +0200941 SECTION("rpc")
Jan Kundrát7ec214d2020-06-19 17:05:07 +0200942 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200943 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
944 return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
945 };
946 ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
Jan Kundrát6ee84792020-01-24 01:43:36 +0100947
Václav Kubernáta8789602020-07-20 15:18:19 +0200948 // ProxyDatastore cannot easily read DatastoreAccess::Tree, so we need to set the input via create/setLeaf/etc.
949 SECTION("valid")
950 {
951 std::string rpc;
952 DatastoreAccess::Tree input, output;
953
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100954 SECTION("noop")
955 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200956 rpc = "/example-schema:noop";
Václav Kubernátb3960f82020-12-01 03:21:48 +0100957 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +0200958 }
959
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100960 SECTION("small nuke")
961 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200962 rpc = "/example-schema:launch-nukes";
963 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +0100964 {joinPaths(rpc, "description"), "dummy"s},
965 {joinPaths(rpc, "payload/kilotons"), uint64_t{333'666}},
Václav Kubernáta8789602020-07-20 15:18:19 +0200966 };
Václav Kubernátb3960f82020-12-01 03:21:48 +0100967 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +0200968 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{333'666});
969 // no data are returned
970 }
971
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100972 SECTION("small nuke")
973 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200974 rpc = "/example-schema:launch-nukes";
975 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +0100976 {joinPaths(rpc, "description"), "dummy"s},
977 {joinPaths(rpc, "payload/kilotons"), uint64_t{4}},
Václav Kubernáta8789602020-07-20 15:18:19 +0200978 };
Václav Kubernátb3960f82020-12-01 03:21:48 +0100979 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +0200980 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{4});
981
982 output = {
983 {"blast-radius", uint32_t{33'666}},
984 {"actual-yield", uint64_t{5}},
985 };
986 }
987
Václav Kubernátb4e5b182020-11-16 19:55:09 +0100988 SECTION("with lists")
989 {
Václav Kubernáta8789602020-07-20 15:18:19 +0200990 rpc = "/example-schema:launch-nukes";
991 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +0100992 {joinPaths(rpc, "payload/kilotons"), uint64_t{6}},
993 {joinPaths(rpc, "cities/targets[city='Prague']/city"), "Prague"s},
Václav Kubernáta8789602020-07-20 15:18:19 +0200994 };
Václav Kubernátb3960f82020-12-01 03:21:48 +0100995 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +0200996 proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{6});
997 proxyDatastore.createItem("/example-schema:launch-nukes/example-schema:cities/example-schema:targets[city='Prague']");
998 output = {
999 {"blast-radius", uint32_t{33'666}},
1000 {"actual-yield", uint64_t{7}},
1001 {"damaged-places", special_{SpecialValue::PresenceContainer}},
1002 {"damaged-places/targets[city='London']", special_{SpecialValue::List}},
1003 {"damaged-places/targets[city='London']/city", "London"s},
1004 {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}},
1005 {"damaged-places/targets[city='Berlin']/city", "Berlin"s},
1006 };
1007 }
1008
Václav Kubernátb4e5b182020-11-16 19:55:09 +01001009 SECTION("with leafref")
1010 {
Václav Kubernáta8789602020-07-20 15:18:19 +02001011 datastore->createItem("/example-schema:person[name='Colton']");
1012 datastore->commitChanges();
1013
1014 rpc = "/example-schema:fire";
1015 input = {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001016 {joinPaths(rpc, "whom"), "Colton"s}
Václav Kubernáta8789602020-07-20 15:18:19 +02001017 };
Václav Kubernátb3960f82020-12-01 03:21:48 +01001018 proxyDatastore.initiate(rpc);
Václav Kubernáta8789602020-07-20 15:18:19 +02001019 proxyDatastore.setLeaf("/example-schema:fire/example-schema:whom", "Colton"s);
1020 }
1021
Václav Kubernátb3960f82020-12-01 03:21:48 +01001022 catching<OnExec>([&] { REQUIRE(datastore->execute(rpc, input) == output); });
Václav Kubernátb4e5b182020-11-16 19:55:09 +01001023 catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); });
Jan Kundrát7ec214d2020-06-19 17:05:07 +02001024 }
1025
Václav Kubernáta8789602020-07-20 15:18:19 +02001026 SECTION("non-existing RPC")
1027 {
Václav Kubernátb3960f82020-12-01 03:21:48 +01001028 catching<OnInvalidRpcPath>([&] { datastore->execute("/example-schema:non-existing", DatastoreAccess::Tree{}); });
Jan Kundrát7ec214d2020-06-19 17:05:07 +02001029 }
Václav Kubernát2edfe542021-02-03 08:08:29 +01001030
1031 SECTION("invalid RPC exec resets temporary datastore")
1032 {
1033 proxyDatastore.initiate("/example-schema:setIp");
1034 catching<OnInvalidRpcInput>([&] { auto output = proxyDatastore.execute(); });
1035 REQUIRE(proxyDatastore.inputDatastorePath() == std::nullopt);
1036 }
Jan Kundrát6ee84792020-01-24 01:43:36 +01001037 }
1038
Václav Kubernáta8789602020-07-20 15:18:19 +02001039 SECTION("action")
Jan Kundrát7ec214d2020-06-19 17:05:07 +02001040 {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001041 auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) {
1042 return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema()));
1043 };
1044 ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore);
Václav Kubernáta8789602020-07-20 15:18:19 +02001045 std::string path;
1046 DatastoreAccess::Tree input, output;
1047
1048 output = {
Václav Kubernáta8789602020-07-20 15:18:19 +02001049 {"success", true}
1050 };
1051 datastore->createItem("/example-schema:ports[name='A']");
1052 datastore->commitChanges();
Václav Kubernátb4e5b182020-11-16 19:55:09 +01001053 SECTION("shutdown")
1054 {
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001055 path = "/example-schema:ports[name='A']/example-schema:shutdown";
1056 input = {
1057 {"/example-schema:ports[name='A']/shutdown/force", true}
1058 };
1059 proxyDatastore.initiate(path);
1060 proxyDatastore.setLeaf("/example-schema:ports[name='A']/example-schema:shutdown/example-schema:force", true);
1061
Václav Kubernáta8789602020-07-20 15:18:19 +02001062 }
1063
Václav Kubernátb3960f82020-12-01 03:21:48 +01001064 catching<OnExec>([&] { REQUIRE(datastore->execute(path, input) == output); });
Václav Kubernát94bb7cf2021-02-03 09:59:39 +01001065 catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); });
Jan Kundrát6ee84792020-01-24 01:43:36 +01001066 }
1067
Jan Kundrát6ee84792020-01-24 01:43:36 +01001068 waitForCompletionAndBitMore(seq1);
1069}