Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 1 | /* |
| 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át | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 10 | #include <sysrepo-cpp/Session.hpp> |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 11 | #include "proxy_datastore.hpp" |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 12 | #include "yang_schema.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 13 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 14 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 15 | #include "sysrepo_access.hpp" |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 16 | using OnInvalidSchemaPathCreate = DatastoreException; |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 17 | using OnInvalidSchemaPathDelete = DatastoreException; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 18 | using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception; |
Václav Kubernát | 4077613 | 2021-02-03 08:47:33 +0100 | [diff] [blame] | 19 | using OnInvalidRpcPath = std::runtime_error; |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 20 | using OnInvalidRpcInput = sysrepo::sysrepo_exception; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 21 | using OnKeyNotFound = void; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 22 | using OnExec = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 23 | #elif defined(netconf_BACKEND) |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 24 | using OnInvalidSchemaPathCreate = std::runtime_error; |
| 25 | using OnInvalidSchemaPathDelete = std::runtime_error; |
| 26 | using OnInvalidSchemaPathMove = std::runtime_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 27 | using OnInvalidRpcPath = std::runtime_error; |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 28 | using OnInvalidRpcInput = std::runtime_error; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 29 | using OnKeyNotFound = std::runtime_error; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 30 | using OnExec = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 31 | #include "netconf_access.hpp" |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 32 | #elif defined(yang_BACKEND) |
| 33 | #include <fstream> |
| 34 | #include "yang_access.hpp" |
| 35 | #include "yang_access_test_vars.hpp" |
| 36 | using OnInvalidSchemaPathCreate = DatastoreException; |
| 37 | using OnInvalidSchemaPathDelete = DatastoreException; |
| 38 | using OnInvalidSchemaPathMove = DatastoreException; |
| 39 | using OnInvalidRpcPath = DatastoreException; |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 40 | using OnInvalidRpcInput = std::logic_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 41 | using OnKeyNotFound = DatastoreException; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 42 | using OnExec = std::logic_error; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 43 | #else |
| 44 | #error "Unknown backend" |
| 45 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 46 | #include "pretty_printers.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 47 | #include "sysrepo_subscription.hpp" |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 48 | #include "utils.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 49 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 50 | using namespace std::literals::string_literals; |
| 51 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 52 | class MockRecorder : public trompeloeil::mock_interface<Recorder> { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 53 | public: |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 54 | IMPLEMENT_MOCK3(write); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 57 | class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> { |
| 58 | public: |
| 59 | IMPLEMENT_CONST_MOCK1(get_data); |
| 60 | }; |
| 61 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 62 | namespace { |
| 63 | template <class ...> constexpr std::false_type always_false [[maybe_unused]] {}; |
| 64 | template <class Exception, typename Callable> void catching(const Callable& what) { |
| 65 | |
| 66 | if constexpr (std::is_same_v<Exception, void>) { |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 67 | what(); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 68 | } 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át | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 72 | } else if constexpr (std::is_same<Exception, std::logic_error>()) { |
| 73 | REQUIRE_THROWS_AS(what(), std::logic_error); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 74 | } 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át | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 80 | } |
| 81 | } |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 82 | } |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 83 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 84 | #if defined(yang_BACKEND) |
| 85 | class TestYangAccess : public YangAccess { |
| 86 | public: |
| 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 | |
| 99 | private: |
| 100 | void dumpToSysrepo() |
| 101 | { |
| 102 | { |
| 103 | std::ofstream of(testConfigFile); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 104 | of << dump(DataFormat::Xml); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 105 | } |
Jan Kundrát | 86840ec | 2021-01-27 15:38:08 +0100 | [diff] [blame] | 106 | auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running --module=example-schema"; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 107 | REQUIRE(std::system(command.c_str()) == 0); |
| 108 | } |
| 109 | }; |
| 110 | #endif |
| 111 | |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 112 | TEST_CASE("setting/getting values") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 113 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 114 | sr_log_stderr(SR_LL_DBG); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 115 | trompeloeil::sequence seq1; |
| 116 | MockRecorder mock; |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 117 | { |
| 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át | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 122 | SysrepoSubscription subscription("example-schema", &mock); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 123 | |
| 124 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 125 | SysrepoAccess datastore(Datastore::Running); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 126 | #elif defined(netconf_BACKEND) |
Václav Kubernát | d1beedc | 2020-09-07 12:09:05 +0200 | [diff] [blame] | 127 | const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET"); |
| 128 | NetconfAccess datastore(NETOPEER_SOCKET); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 129 | #elif defined(yang_BACKEND) |
| 130 | TestYangAccess datastore; |
| 131 | datastore.addSchemaDir(schemaDir); |
| 132 | datastore.addSchemaFile(exampleSchemaFile); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 133 | #else |
| 134 | #error "Unknown backend" |
| 135 | #endif |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 136 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 137 | |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 138 | SECTION("set leafInt8 to -128") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 139 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 140 | REQUIRE_CALL(mock, write("/example-schema:leafInt8", std::nullopt, "-128"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 141 | datastore.setLeaf("/example-schema:leafInt8", int8_t{-128}); |
| 142 | datastore.commitChanges(); |
| 143 | } |
| 144 | |
| 145 | SECTION("set leafInt16 to -32768") |
| 146 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 147 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "-32768"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 148 | datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768}); |
| 149 | datastore.commitChanges(); |
| 150 | } |
| 151 | |
| 152 | SECTION("set leafInt32 to -2147483648") |
| 153 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 154 | REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 155 | datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648}); |
| 156 | datastore.commitChanges(); |
| 157 | } |
| 158 | |
| 159 | SECTION("set leafInt64 to -50000000000") |
| 160 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 161 | REQUIRE_CALL(mock, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 162 | datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000}); |
| 163 | datastore.commitChanges(); |
| 164 | } |
| 165 | |
| 166 | SECTION("set leafUInt8 to 255") |
| 167 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 168 | REQUIRE_CALL(mock, write("/example-schema:leafUInt8", std::nullopt, "255"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 169 | datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255}); |
| 170 | datastore.commitChanges(); |
| 171 | } |
| 172 | |
| 173 | SECTION("set leafUInt16 to 65535") |
| 174 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 175 | REQUIRE_CALL(mock, write("/example-schema:leafUInt16", std::nullopt, "65535"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 176 | datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535}); |
| 177 | datastore.commitChanges(); |
| 178 | } |
| 179 | |
| 180 | SECTION("set leafUInt32 to 4294967295") |
| 181 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 182 | REQUIRE_CALL(mock, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 183 | datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295}); |
| 184 | datastore.commitChanges(); |
| 185 | } |
| 186 | |
| 187 | SECTION("set leafUInt64 to 50000000000") |
| 188 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 189 | REQUIRE_CALL(mock, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 190 | datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000}); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 191 | datastore.commitChanges(); |
| 192 | } |
| 193 | |
| 194 | SECTION("set leafEnum to coze") |
| 195 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 196 | REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "coze"s)); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 197 | datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"}); |
| 198 | datastore.commitChanges(); |
| 199 | } |
| 200 | |
| 201 | SECTION("set leafDecimal to 123.544") |
| 202 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 203 | REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.544"s)); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 204 | datastore.setLeaf("/example-schema:leafDecimal", 123.544); |
| 205 | datastore.commitChanges(); |
| 206 | } |
| 207 | |
Jan Kundrát | d287286 | 2020-06-18 21:02:00 +0200 | [diff] [blame] | 208 | 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át | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 223 | SECTION("set a non-existing leaf") |
| 224 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 225 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 226 | datastore.setLeaf("/example-schema:non-existing", "what"s); |
| 227 | }); |
| 228 | } |
| 229 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 230 | SECTION("create presence container") |
| 231 | { |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 232 | REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") == std::string::npos); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 233 | REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 234 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 235 | datastore.commitChanges(); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 236 | REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") != std::string::npos); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 239 | SECTION("create/delete a list instance") |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 240 | { |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 241 | { |
| 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 244 | datastore.createItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 245 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 250 | datastore.deleteItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 251 | datastore.commitChanges(); |
| 252 | } |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 255 | SECTION("deleting non-existing list keys") |
| 256 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 257 | catching<OnKeyNotFound>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 258 | datastore.deleteItem("/example-schema:person[name='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 259 | datastore.commitChanges(); |
| 260 | }); |
| 261 | } |
| 262 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 263 | SECTION("accessing non-existing schema nodes as a list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 264 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 265 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 266 | datastore.createItem("/example-schema:non-existing-list[xxx='blah']"); |
| 267 | datastore.commitChanges(); |
| 268 | }); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 269 | catching<OnInvalidSchemaPathDelete>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 270 | datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 271 | datastore.commitChanges(); |
| 272 | }); |
| 273 | } |
| 274 | |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 275 | SECTION("leafref pointing to a key of a list") |
| 276 | { |
| 277 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 278 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 284 | 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át | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 287 | datastore.commitChanges(); |
| 288 | } |
| 289 | |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 290 | std::string value; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 291 | SECTION("Dan") |
| 292 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 293 | value = "Dan"; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | SECTION("Elfi") |
| 297 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 298 | value = "Elfi"; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | SECTION("Kolafa") |
| 302 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 303 | 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át | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 309 | datastore.commitChanges(); |
| 310 | } |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 311 | REQUIRE(datastore.getItems("/example-schema:bossPerson") == DatastoreAccess::Tree{{"/example-schema:bossPerson", value}}); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 312 | } |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 313 | SECTION("bool values get correctly represented as bools") |
| 314 | { |
| 315 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 316 | REQUIRE_CALL(mock, write("/example-schema:down", std::nullopt, "true"s)); |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 317 | datastore.setLeaf("/example-schema:down", bool{true}); |
| 318 | datastore.commitChanges(); |
| 319 | } |
| 320 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 321 | DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}}; |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 322 | REQUIRE(datastore.getItems("/example-schema:down") == expected); |
| 323 | } |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 324 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 325 | SECTION("getting items from the whole module") |
| 326 | { |
| 327 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 328 | 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át | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 330 | datastore.setLeaf("/example-schema:up", bool{true}); |
| 331 | datastore.setLeaf("/example-schema:down", bool{false}); |
| 332 | datastore.commitChanges(); |
| 333 | } |
| 334 | |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 335 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 336 | {"/example-schema:up", bool{true}}, |
| 337 | {"/example-schema:down", bool{false}} |
| 338 | }; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 339 | REQUIRE(datastore.getItems("/example-schema:*") == expected); |
| 340 | } |
| 341 | |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 342 | SECTION("getItems returns correct datatypes") |
| 343 | { |
| 344 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 345 | REQUIRE_CALL(mock, write("/example-schema:leafEnum", std::nullopt, "lol"s)); |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 346 | datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"}); |
| 347 | datastore.commitChanges(); |
| 348 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 349 | DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}}; |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 350 | |
| 351 | REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected); |
| 352 | } |
| 353 | |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 354 | SECTION("getItems on a list") |
| 355 | { |
| 356 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 357 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 363 | 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át | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 366 | datastore.commitChanges(); |
| 367 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 368 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 369 | {"/example-schema:person[name='Jan']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 370 | {"/example-schema:person[name='Jan']/name", std::string{"Jan"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 371 | {"/example-schema:person[name='Michal']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 372 | {"/example-schema:person[name='Michal']/name", std::string{"Michal"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 373 | {"/example-schema:person[name='Petr']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 374 | {"/example-schema:person[name='Petr']/name", std::string{"Petr"}} |
| 375 | }; |
| 376 | |
| 377 | REQUIRE(datastore.getItems("/example-schema:person") == expected); |
| 378 | } |
| 379 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 380 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 388 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 389 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 399 | datastore.deleteItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 400 | datastore.commitChanges(); |
| 401 | } |
| 402 | expected = {}; |
| 403 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 404 | } |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 405 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 406 | SECTION("creating a non-existing schema node as a container") |
| 407 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 408 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 409 | datastore.createItem("/example-schema:non-existing-presence-container"); |
| 410 | datastore.commitChanges(); |
| 411 | }); |
| 412 | } |
| 413 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 414 | SECTION("deleting a non-existing schema node as a container or leaf") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 415 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 416 | catching<OnInvalidSchemaPathDelete>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 417 | datastore.deleteItem("/example-schema:non-existing-presence-container"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 418 | datastore.commitChanges(); |
| 419 | }); |
| 420 | } |
| 421 | |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 422 | 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át | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 428 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 429 | datastore.createItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 430 | 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át | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 437 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 438 | datastore.deleteItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 439 | datastore.commitChanges(); |
| 440 | } |
| 441 | expected = {}; |
| 442 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 445 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 450 | DatastoreAccess::Tree expected{ |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 451 | {"/example-schema:leafDecimal", 123.4}, |
| 452 | }; |
| 453 | REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected); |
| 454 | } |
| 455 | |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 456 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 461 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 462 | {"/example-schema:unionIntString", int32_t{10}}, |
| 463 | }; |
| 464 | REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected); |
| 465 | } |
| 466 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 467 | SECTION("identityref") |
| 468 | { |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 469 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 472 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 473 | {"/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át | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 486 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 491 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 492 | {"/example-schema:blob", binary_{"cHduegByIQ=="s}}, |
| 493 | }; |
| 494 | REQUIRE(datastore.getItems("/example-schema:blob") == expected); |
| 495 | } |
| 496 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 497 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 502 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 503 | {"/example-schema:dummy", empty_{}}, |
| 504 | }; |
| 505 | REQUIRE(datastore.getItems("/example-schema:dummy") == expected); |
| 506 | } |
| 507 | |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 508 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 513 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 514 | {"/example-schema:flags", bits_{{"carry", "sign"}}}, |
| 515 | }; |
| 516 | REQUIRE(datastore.getItems("/example-schema:flags") == expected); |
| 517 | } |
| 518 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 519 | #if not defined(yang_BACKEND) |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 520 | SECTION("operational data") |
| 521 | { |
| 522 | MockDataSupplier mockOpsData; |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 523 | OperationalDataSubscription opsDataSub("example-schema", "/example-schema:temperature", mockOpsData); |
Václav Kubernát | f90a0b5 | 2020-11-06 05:53:03 +0100 | [diff] [blame] | 524 | OperationalDataSubscription opsDataSub2("example-schema", "/example-schema:users", mockOpsData); |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 525 | DatastoreAccess::Tree expected; |
| 526 | std::string xpath; |
Václav Kubernát | f90a0b5 | 2020-11-06 05:53:03 +0100 | [diff] [blame] | 527 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 528 | SECTION("temperature") |
| 529 | { |
| 530 | expected = {{"/example-schema:temperature", int32_t{22}}}; |
| 531 | xpath = "/example-schema:temperature"; |
| 532 | } |
| 533 | |
Václav Kubernát | f90a0b5 | 2020-11-06 05:53:03 +0100 | [diff] [blame] | 534 | 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át | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 546 | REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected); |
| 547 | REQUIRE(datastore.getItems(xpath) == expected); |
| 548 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 549 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 550 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 551 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 556 | datastore.createItem("/example-schema:addresses[.='0.0.0.0']"); |
| 557 | datastore.createItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 558 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 567 | datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 568 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 576 | datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 577 | datastore.commitChanges(); |
| 578 | expected = {}; |
| 579 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 580 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 581 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 582 | SECTION("deleting a non-existing leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 583 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 584 | catching<OnKeyNotFound>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 585 | datastore.deleteItem("/example-schema:addresses[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 586 | datastore.commitChanges(); |
| 587 | }); |
| 588 | } |
| 589 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 590 | SECTION("accessing a non-existing schema node as a leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 591 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 592 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 593 | datastore.createItem("/example-schema:non-existing[.='non-existing']"); |
| 594 | datastore.commitChanges(); |
| 595 | }); |
| 596 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 597 | catching<OnInvalidSchemaPathDelete>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 598 | datastore.deleteItem("/example-schema:non-existing[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 599 | datastore.commitChanges(); |
| 600 | }); |
| 601 | } |
| 602 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 603 | 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át | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 617 | SECTION("moving leaflist instances") |
| 618 | { |
| 619 | DatastoreAccess::Tree expected; |
| 620 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 621 | 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át | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 626 | 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át | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 628 | datastore.createItem("/example-schema:protocols[.='http']"); |
| 629 | datastore.createItem("/example-schema:protocols[.='ftp']"); |
| 630 | datastore.createItem("/example-schema:protocols[.='pop3']"); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 631 | 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át | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 703 | SECTION("moving non-existing schema nodes") |
| 704 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 705 | catching<OnInvalidSchemaPathMove>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 706 | datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin); |
| 707 | datastore.commitChanges(); |
| 708 | }); |
| 709 | } |
| 710 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 711 | SECTION("moving list instances") |
| 712 | { |
| 713 | DatastoreAccess::Tree expected; |
| 714 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 715 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", std::nullopt, ""s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 716 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']/name", std::nullopt, "John"s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 717 | 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át | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 719 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", ""s, ""s)); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 720 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 721 | 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át | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 724 | 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át | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 806 | 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át | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 815 | {"/example-schema:leafInt32", 64} |
| 816 | }; |
Václav Kubernát | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 817 | // This tests if we at least get the data WE added. |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 818 | 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át | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 821 | } |
| 822 | |
Václav Kubernát | 36986c5 | 2020-06-25 10:30:05 +0200 | [diff] [blame] | 823 | 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át | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 829 | 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át | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 839 | waitForCompletionAndBitMore(seq1); |
| 840 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 841 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 842 | struct 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át | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 849 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 850 | if (session->get_context()->get_node(nullptr, xpath)->path(LYS_PATH_FIRST_PREFIX) == "/example-schema:ports/shutdown") { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 851 | 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át | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 857 | }; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 858 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 859 | struct 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át | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 866 | { |
| 867 | const auto nukes = "/example-schema:launch-nukes"s; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 868 | if (xpath == "/example-schema:noop"s || xpath == "/example-schema:fire"s) { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 869 | return SR_ERR_OK; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | if (xpath == nukes) { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 873 | 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át | f4b6a93 | 2020-07-09 10:34:19 +0200 | [diff] [blame] | 877 | if (val->xpath() == nukes + "/payload") { |
| 878 | continue; // ignore, container |
| 879 | } |
| 880 | if (val->xpath() == nukes + "/description") { |
| 881 | continue; // unused |
| 882 | } |
| 883 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 884 | if (val->xpath() == nukes + "/payload/kilotons") { |
| 885 | kilotons = val->data()->get_uint64(); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 886 | } 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 911 | TEST_CASE("rpc/action") |
| 912 | { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 913 | trompeloeil::sequence seq1; |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 914 | |
| 915 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 916 | auto datastore = std::make_shared<SysrepoAccess>(Datastore::Running); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 917 | #elif defined(netconf_BACKEND) |
Václav Kubernát | d1beedc | 2020-09-07 12:09:05 +0200 | [diff] [blame] | 918 | const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET"); |
| 919 | auto datastore = std::make_shared<NetconfAccess>(NETOPEER_SOCKET); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 920 | #elif defined(yang_BACKEND) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 921 | auto datastore = std::make_shared<YangAccess>(); |
| 922 | datastore->addSchemaDir(schemaDir); |
| 923 | datastore->addSchemaFile(exampleSchemaFile); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 924 | #else |
| 925 | #error "Unknown backend" |
| 926 | #endif |
| 927 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 928 | auto srConn = std::make_shared<sysrepo::Connection>(); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 929 | auto srSession = std::make_shared<sysrepo::Session>(srConn); |
| 930 | auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 931 | auto rpcCb = std::make_shared<RpcCb>(); |
| 932 | auto actionCb = std::make_shared<ActionCb>(); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 933 | sysrepo::Logs{}.set_stderr(SR_LL_INF); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 934 | SysrepoSubscription subscription("example-schema", nullptr); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 935 | // careful here, sysrepo insists on module_change CBs being registered before RPC CBs, otherwise there's a memleak |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 936 | 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át | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 940 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 941 | SECTION("rpc") |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 942 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 943 | 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át | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 947 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 948 | // 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 954 | SECTION("noop") |
| 955 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 956 | rpc = "/example-schema:noop"; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 957 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 960 | SECTION("small nuke") |
| 961 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 962 | rpc = "/example-schema:launch-nukes"; |
| 963 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 964 | {joinPaths(rpc, "description"), "dummy"s}, |
| 965 | {joinPaths(rpc, "payload/kilotons"), uint64_t{333'666}}, |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 966 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 967 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 968 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 972 | SECTION("small nuke") |
| 973 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 974 | rpc = "/example-schema:launch-nukes"; |
| 975 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 976 | {joinPaths(rpc, "description"), "dummy"s}, |
| 977 | {joinPaths(rpc, "payload/kilotons"), uint64_t{4}}, |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 978 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 979 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 980 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 988 | SECTION("with lists") |
| 989 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 990 | rpc = "/example-schema:launch-nukes"; |
| 991 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 992 | {joinPaths(rpc, "payload/kilotons"), uint64_t{6}}, |
| 993 | {joinPaths(rpc, "cities/targets[city='Prague']/city"), "Prague"s}, |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 994 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 995 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 996 | 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át | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 1009 | SECTION("with leafref") |
| 1010 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1011 | datastore->createItem("/example-schema:person[name='Colton']"); |
| 1012 | datastore->commitChanges(); |
| 1013 | |
| 1014 | rpc = "/example-schema:fire"; |
| 1015 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1016 | {joinPaths(rpc, "whom"), "Colton"s} |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1017 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1018 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1019 | proxyDatastore.setLeaf("/example-schema:fire/example-schema:whom", "Colton"s); |
| 1020 | } |
| 1021 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1022 | catching<OnExec>([&] { REQUIRE(datastore->execute(rpc, input) == output); }); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 1023 | catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); }); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1026 | SECTION("non-existing RPC") |
| 1027 | { |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1028 | catching<OnInvalidRpcPath>([&] { datastore->execute("/example-schema:non-existing", DatastoreAccess::Tree{}); }); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 1029 | } |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 1030 | |
| 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át | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 1037 | } |
| 1038 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1039 | SECTION("action") |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 1040 | { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1041 | 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át | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1045 | std::string path; |
| 1046 | DatastoreAccess::Tree input, output; |
| 1047 | |
| 1048 | output = { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1049 | {"success", true} |
| 1050 | }; |
| 1051 | datastore->createItem("/example-schema:ports[name='A']"); |
| 1052 | datastore->commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 1053 | SECTION("shutdown") |
| 1054 | { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1055 | 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át | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1064 | catching<OnExec>([&] { REQUIRE(datastore->execute(path, input) == output); }); |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1065 | catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); }); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 1068 | waitForCompletionAndBitMore(seq1); |
| 1069 | } |