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 "yang_schema.hpp" |
| 12 | #include "proxy_datastore.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; |
| 17 | using OnInvalidSchemaPathDelete = void; |
| 18 | using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 19 | using OnInvalidRpcPath = sysrepo::sysrepo_exception; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 20 | using OnKeyNotFound = void; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 21 | using OnRPC = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 22 | #elif defined(netconf_BACKEND) |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 23 | using OnInvalidSchemaPathCreate = std::runtime_error; |
| 24 | using OnInvalidSchemaPathDelete = std::runtime_error; |
| 25 | using OnInvalidSchemaPathMove = std::runtime_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 26 | using OnInvalidRpcPath = std::runtime_error; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 27 | using OnKeyNotFound = std::runtime_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 28 | using OnRPC = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 29 | #include "netconf_access.hpp" |
| 30 | #include "netopeer_vars.hpp" |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 31 | #elif defined(yang_BACKEND) |
| 32 | #include <fstream> |
| 33 | #include "yang_access.hpp" |
| 34 | #include "yang_access_test_vars.hpp" |
| 35 | using OnInvalidSchemaPathCreate = DatastoreException; |
| 36 | using OnInvalidSchemaPathDelete = DatastoreException; |
| 37 | using OnInvalidSchemaPathMove = DatastoreException; |
| 38 | using OnInvalidRpcPath = DatastoreException; |
| 39 | using OnKeyNotFound = DatastoreException; |
| 40 | using OnRPC = std::logic_error; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 41 | #else |
| 42 | #error "Unknown backend" |
| 43 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 44 | #include "pretty_printers.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 45 | #include "sysrepo_subscription.hpp" |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 46 | #include "utils.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 47 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 48 | using namespace std::literals::string_literals; |
| 49 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 50 | class MockRecorder : public trompeloeil::mock_interface<Recorder> { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 51 | public: |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 52 | IMPLEMENT_MOCK3(write); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 55 | class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> { |
| 56 | public: |
| 57 | IMPLEMENT_CONST_MOCK1(get_data); |
| 58 | }; |
| 59 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 60 | namespace { |
| 61 | template <class ...> constexpr std::false_type always_false [[maybe_unused]] {}; |
| 62 | template <class Exception, typename Callable> void catching(const Callable& what) { |
| 63 | |
| 64 | if constexpr (std::is_same_v<Exception, void>) { |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 65 | what(); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 66 | } else if constexpr (std::is_same<Exception, std::runtime_error>()) { |
| 67 | // cannot use REQUIRE_THROWS_AS(..., Exception) directly because that one |
| 68 | // needs an extra `typename` deep in the bowels of doctest |
| 69 | REQUIRE_THROWS_AS(what(), std::runtime_error); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 70 | } else if constexpr (std::is_same<Exception, std::logic_error>()) { |
| 71 | REQUIRE_THROWS_AS(what(), std::logic_error); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 72 | } else if constexpr (std::is_same<Exception, DatastoreException>()) { |
| 73 | REQUIRE_THROWS_AS(what(), DatastoreException); |
| 74 | } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) { |
| 75 | REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception); |
| 76 | } else { |
| 77 | static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623 |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 78 | } |
| 79 | } |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 80 | } |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 81 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 82 | #if defined(yang_BACKEND) |
| 83 | class TestYangAccess : public YangAccess { |
| 84 | public: |
| 85 | void commitChanges() override |
| 86 | { |
| 87 | YangAccess::commitChanges(); |
| 88 | dumpToSysrepo(); |
| 89 | } |
| 90 | |
| 91 | void copyConfig(const Datastore source, const Datastore destination) override |
| 92 | { |
| 93 | YangAccess::copyConfig(source, destination); |
| 94 | dumpToSysrepo(); |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | void dumpToSysrepo() |
| 99 | { |
| 100 | { |
| 101 | std::ofstream of(testConfigFile); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 102 | of << dump(DataFormat::Xml); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 103 | } |
| 104 | auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running example-schema"; |
| 105 | REQUIRE(std::system(command.c_str()) == 0); |
| 106 | } |
| 107 | }; |
| 108 | #endif |
| 109 | |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 110 | TEST_CASE("setting/getting values") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 111 | { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 112 | trompeloeil::sequence seq1; |
| 113 | MockRecorder mock; |
Václav Kubernát | ab612e9 | 2019-11-26 19:51:31 +0100 | [diff] [blame] | 114 | SysrepoSubscription subscription("example-schema", &mock); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 115 | |
| 116 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 715c85c | 2020-04-14 01:46:08 +0200 | [diff] [blame] | 117 | SysrepoAccess datastore("netconf-cli-test", Datastore::Running); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 118 | #elif defined(netconf_BACKEND) |
| 119 | NetconfAccess datastore(NETOPEER_SOCKET_PATH); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 120 | #elif defined(yang_BACKEND) |
| 121 | TestYangAccess datastore; |
| 122 | datastore.addSchemaDir(schemaDir); |
| 123 | datastore.addSchemaFile(exampleSchemaFile); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 124 | #else |
| 125 | #error "Unknown backend" |
| 126 | #endif |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 127 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 128 | |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 129 | SECTION("set leafInt8 to -128") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 130 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 131 | 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] | 132 | datastore.setLeaf("/example-schema:leafInt8", int8_t{-128}); |
| 133 | datastore.commitChanges(); |
| 134 | } |
| 135 | |
| 136 | SECTION("set leafInt16 to -32768") |
| 137 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 138 | 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] | 139 | datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768}); |
| 140 | datastore.commitChanges(); |
| 141 | } |
| 142 | |
| 143 | SECTION("set leafInt32 to -2147483648") |
| 144 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 145 | 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] | 146 | datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648}); |
| 147 | datastore.commitChanges(); |
| 148 | } |
| 149 | |
| 150 | SECTION("set leafInt64 to -50000000000") |
| 151 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 152 | 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] | 153 | datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000}); |
| 154 | datastore.commitChanges(); |
| 155 | } |
| 156 | |
| 157 | SECTION("set leafUInt8 to 255") |
| 158 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 159 | 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] | 160 | datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255}); |
| 161 | datastore.commitChanges(); |
| 162 | } |
| 163 | |
| 164 | SECTION("set leafUInt16 to 65535") |
| 165 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 166 | 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] | 167 | datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535}); |
| 168 | datastore.commitChanges(); |
| 169 | } |
| 170 | |
| 171 | SECTION("set leafUInt32 to 4294967295") |
| 172 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 173 | 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] | 174 | datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295}); |
| 175 | datastore.commitChanges(); |
| 176 | } |
| 177 | |
| 178 | SECTION("set leafUInt64 to 50000000000") |
| 179 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 180 | 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] | 181 | datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000}); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 182 | datastore.commitChanges(); |
| 183 | } |
| 184 | |
| 185 | SECTION("set leafEnum to coze") |
| 186 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 187 | 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] | 188 | datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"}); |
| 189 | datastore.commitChanges(); |
| 190 | } |
| 191 | |
| 192 | SECTION("set leafDecimal to 123.544") |
| 193 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 194 | 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] | 195 | datastore.setLeaf("/example-schema:leafDecimal", 123.544); |
| 196 | datastore.commitChanges(); |
| 197 | } |
| 198 | |
Jan Kundrát | d287286 | 2020-06-18 21:02:00 +0200 | [diff] [blame] | 199 | SECTION("set a string, then delete it") |
| 200 | { |
| 201 | REQUIRE_CALL(mock, write("/example-schema:leafString", std::nullopt, "blah"s)); |
| 202 | datastore.setLeaf("/example-schema:leafString", "blah"s); |
| 203 | datastore.commitChanges(); |
| 204 | DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}}; |
| 205 | REQUIRE(datastore.getItems("/example-schema:leafString") == expected); |
| 206 | |
| 207 | REQUIRE_CALL(mock, write("/example-schema:leafString", "blah"s, std::nullopt)); |
| 208 | datastore.deleteItem("/example-schema:leafString"); |
| 209 | datastore.commitChanges(); |
| 210 | expected.clear(); |
| 211 | REQUIRE(datastore.getItems("/example-schema:leafString") == expected); |
| 212 | } |
| 213 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 214 | SECTION("set a non-existing leaf") |
| 215 | { |
| 216 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 217 | datastore.setLeaf("/example-schema:non-existing", "what"s); |
| 218 | }); |
| 219 | } |
| 220 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 221 | SECTION("create presence container") |
| 222 | { |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 223 | 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] | 224 | REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 225 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 226 | datastore.commitChanges(); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 227 | 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] | 228 | } |
| 229 | |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 230 | SECTION("create/delete a list instance") |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 231 | { |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 232 | { |
| 233 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s)); |
| 234 | 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] | 235 | datastore.createItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 236 | datastore.commitChanges(); |
| 237 | } |
| 238 | { |
| 239 | REQUIRE_CALL(mock, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt)); |
| 240 | 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] | 241 | datastore.deleteItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 242 | datastore.commitChanges(); |
| 243 | } |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 246 | SECTION("deleting non-existing list keys") |
| 247 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 248 | catching<OnKeyNotFound>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 249 | datastore.deleteItem("/example-schema:person[name='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 250 | datastore.commitChanges(); |
| 251 | }); |
| 252 | } |
| 253 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 254 | SECTION("accessing non-existing schema nodes as a list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 255 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 256 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 257 | datastore.createItem("/example-schema:non-existing-list[xxx='blah']"); |
| 258 | datastore.commitChanges(); |
| 259 | }); |
| 260 | catching<OnInvalidSchemaPathDelete>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 261 | datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 262 | datastore.commitChanges(); |
| 263 | }); |
| 264 | } |
| 265 | |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 266 | SECTION("leafref pointing to a key of a list") |
| 267 | { |
| 268 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 269 | REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']", std::nullopt, ""s)); |
| 270 | REQUIRE_CALL(mock, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s)); |
| 271 | REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']", std::nullopt, ""s)); |
| 272 | REQUIRE_CALL(mock, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s)); |
| 273 | REQUIRE_CALL(mock, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s)); |
| 274 | 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] | 275 | datastore.createItem("/example-schema:person[name='Dan']"); |
| 276 | datastore.createItem("/example-schema:person[name='Elfi']"); |
| 277 | datastore.createItem("/example-schema:person[name='Kolafa']"); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 278 | datastore.commitChanges(); |
| 279 | } |
| 280 | |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 281 | std::string value; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 282 | SECTION("Dan") |
| 283 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 284 | value = "Dan"; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | SECTION("Elfi") |
| 288 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 289 | value = "Elfi"; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | SECTION("Kolafa") |
| 293 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 294 | value = "Kolafa"; |
| 295 | } |
| 296 | |
| 297 | datastore.setLeaf("/example-schema:bossPerson", value); |
| 298 | { |
| 299 | REQUIRE_CALL(mock, write("/example-schema:bossPerson", std::nullopt, value)); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 300 | datastore.commitChanges(); |
| 301 | } |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 302 | 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] | 303 | } |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 304 | SECTION("bool values get correctly represented as bools") |
| 305 | { |
| 306 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 307 | 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] | 308 | datastore.setLeaf("/example-schema:down", bool{true}); |
| 309 | datastore.commitChanges(); |
| 310 | } |
| 311 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 312 | DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}}; |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 313 | REQUIRE(datastore.getItems("/example-schema:down") == expected); |
| 314 | } |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 315 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 316 | SECTION("getting items from the whole module") |
| 317 | { |
| 318 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 319 | REQUIRE_CALL(mock, write("/example-schema:up", std::nullopt, "true"s)); |
| 320 | 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] | 321 | datastore.setLeaf("/example-schema:up", bool{true}); |
| 322 | datastore.setLeaf("/example-schema:down", bool{false}); |
| 323 | datastore.commitChanges(); |
| 324 | } |
| 325 | |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 326 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 327 | // Sysrepo always returns containers when getting values, but |
| 328 | // libnetconf does not. This is fine by the YANG standard: |
| 329 | // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore, |
| 330 | // NetconfAccess implementation actually only iterates over leafs, |
| 331 | // so even if libnetconf did include containers, they wouldn't get |
| 332 | // shown here anyway. With sysrepo2, this won't be necessary, |
| 333 | // because it'll use the same data structure as libnetconf, so the |
| 334 | // results will be consistent. |
| 335 | #ifdef sysrepo_BACKEND |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 336 | {"/example-schema:inventory", special_{SpecialValue::Container}}, |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 337 | {"/example-schema:lol", special_{SpecialValue::Container}}, |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 338 | #endif |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 339 | {"/example-schema:up", bool{true}}, |
| 340 | {"/example-schema:down", bool{false}}}; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 341 | REQUIRE(datastore.getItems("/example-schema:*") == expected); |
| 342 | } |
| 343 | |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 344 | SECTION("getItems returns correct datatypes") |
| 345 | { |
| 346 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 347 | 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] | 348 | datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"}); |
| 349 | datastore.commitChanges(); |
| 350 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 351 | DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}}; |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 352 | |
| 353 | REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected); |
| 354 | } |
| 355 | |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 356 | SECTION("getItems on a list") |
| 357 | { |
| 358 | { |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 359 | REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']", std::nullopt, ""s)); |
| 360 | REQUIRE_CALL(mock, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s)); |
| 361 | REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']", std::nullopt, ""s)); |
| 362 | REQUIRE_CALL(mock, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s)); |
| 363 | REQUIRE_CALL(mock, write("/example-schema:person[name='Petr']", std::nullopt, ""s)); |
| 364 | 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] | 365 | datastore.createItem("/example-schema:person[name='Jan']"); |
| 366 | datastore.createItem("/example-schema:person[name='Michal']"); |
| 367 | datastore.createItem("/example-schema:person[name='Petr']"); |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 368 | datastore.commitChanges(); |
| 369 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 370 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 371 | {"/example-schema:person[name='Jan']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 372 | {"/example-schema:person[name='Jan']/name", std::string{"Jan"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 373 | {"/example-schema:person[name='Michal']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 374 | {"/example-schema:person[name='Michal']/name", std::string{"Michal"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 375 | {"/example-schema:person[name='Petr']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 376 | {"/example-schema:person[name='Petr']/name", std::string{"Petr"}} |
| 377 | }; |
| 378 | |
| 379 | REQUIRE(datastore.getItems("/example-schema:person") == expected); |
| 380 | } |
| 381 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 382 | SECTION("presence containers") |
| 383 | { |
| 384 | DatastoreAccess::Tree expected; |
| 385 | // Make sure it's not there before we create it |
| 386 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 387 | |
| 388 | { |
| 389 | REQUIRE_CALL(mock, write("/example-schema:pContainer", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 390 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 391 | datastore.commitChanges(); |
| 392 | } |
| 393 | expected = { |
| 394 | {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}} |
| 395 | }; |
| 396 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 397 | |
| 398 | // Make sure it's not there after we delete it |
| 399 | { |
| 400 | REQUIRE_CALL(mock, write("/example-schema:pContainer", ""s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 401 | datastore.deleteItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 402 | datastore.commitChanges(); |
| 403 | } |
| 404 | expected = {}; |
| 405 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 406 | } |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 407 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 408 | SECTION("creating a non-existing schema node as a container") |
| 409 | { |
| 410 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 411 | datastore.createItem("/example-schema:non-existing-presence-container"); |
| 412 | datastore.commitChanges(); |
| 413 | }); |
| 414 | } |
| 415 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 416 | 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] | 417 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 418 | catching<OnInvalidSchemaPathDelete>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 419 | datastore.deleteItem("/example-schema:non-existing-presence-container"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 420 | datastore.commitChanges(); |
| 421 | }); |
| 422 | } |
| 423 | |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 424 | SECTION("nested presence container") |
| 425 | { |
| 426 | DatastoreAccess::Tree expected; |
| 427 | // Make sure it's not there before we create it |
| 428 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 429 | { |
| 430 | REQUIRE_CALL(mock, write("/example-schema:inventory", std::nullopt, ""s)); |
| 431 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 432 | datastore.createItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 433 | datastore.commitChanges(); |
| 434 | } |
| 435 | expected = { |
| 436 | {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}} |
| 437 | }; |
| 438 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 439 | { |
| 440 | REQUIRE_CALL(mock, write("/example-schema:inventory", ""s, std::nullopt)); |
| 441 | REQUIRE_CALL(mock, write("/example-schema:inventory/stuff", ""s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 442 | datastore.deleteItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 443 | datastore.commitChanges(); |
| 444 | } |
| 445 | expected = {}; |
| 446 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 449 | SECTION("floats") |
| 450 | { |
| 451 | datastore.setLeaf("/example-schema:leafDecimal", 123.4); |
| 452 | REQUIRE_CALL(mock, write("/example-schema:leafDecimal", std::nullopt, "123.4"s)); |
| 453 | datastore.commitChanges(); |
| 454 | DatastoreAccess::Tree expected { |
| 455 | {"/example-schema:leafDecimal", 123.4}, |
| 456 | }; |
| 457 | REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected); |
| 458 | } |
| 459 | |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 460 | SECTION("unions") |
| 461 | { |
| 462 | datastore.setLeaf("/example-schema:unionIntString", int32_t{10}); |
| 463 | REQUIRE_CALL(mock, write("/example-schema:unionIntString", std::nullopt, "10"s)); |
| 464 | datastore.commitChanges(); |
| 465 | DatastoreAccess::Tree expected { |
| 466 | {"/example-schema:unionIntString", int32_t{10}}, |
| 467 | }; |
| 468 | REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected); |
| 469 | } |
| 470 | |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 471 | SECTION("identityref") { |
| 472 | datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"}); |
| 473 | REQUIRE_CALL(mock, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s)); |
| 474 | datastore.commitChanges(); |
| 475 | DatastoreAccess::Tree expected { |
| 476 | {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}}, |
| 477 | }; |
| 478 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 479 | |
| 480 | datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"}); |
| 481 | REQUIRE_CALL(mock, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s)); |
| 482 | datastore.commitChanges(); |
| 483 | expected = { |
| 484 | {"/example-schema:beast", identityRef_{"example-schema", "Whale"}}, |
| 485 | }; |
| 486 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 487 | } |
| 488 | |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 489 | SECTION("binary") |
| 490 | { |
| 491 | datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s}); |
| 492 | REQUIRE_CALL(mock, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s)); |
| 493 | datastore.commitChanges(); |
| 494 | DatastoreAccess::Tree expected { |
| 495 | {"/example-schema:blob", binary_{"cHduegByIQ=="s}}, |
| 496 | }; |
| 497 | REQUIRE(datastore.getItems("/example-schema:blob") == expected); |
| 498 | } |
| 499 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 500 | SECTION("empty") |
| 501 | { |
| 502 | datastore.setLeaf("/example-schema:dummy", empty_{}); |
| 503 | REQUIRE_CALL(mock, write("/example-schema:dummy", std::nullopt, ""s)); |
| 504 | datastore.commitChanges(); |
| 505 | DatastoreAccess::Tree expected { |
| 506 | {"/example-schema:dummy", empty_{}}, |
| 507 | }; |
| 508 | REQUIRE(datastore.getItems("/example-schema:dummy") == expected); |
| 509 | } |
| 510 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 511 | #if not defined(yang_BACKEND) |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 512 | SECTION("operational data") |
| 513 | { |
| 514 | MockDataSupplier mockOpsData; |
| 515 | OperationalDataSubscription opsDataSub("/example-schema:temperature", mockOpsData); |
| 516 | DatastoreAccess::Tree expected; |
| 517 | std::string xpath; |
| 518 | SECTION("temperature") |
| 519 | { |
| 520 | expected = {{"/example-schema:temperature", int32_t{22}}}; |
| 521 | xpath = "/example-schema:temperature"; |
| 522 | } |
| 523 | |
| 524 | REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected); |
| 525 | REQUIRE(datastore.getItems(xpath) == expected); |
| 526 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 527 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 528 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 529 | SECTION("leaf list") |
| 530 | { |
| 531 | DatastoreAccess::Tree expected; |
| 532 | REQUIRE_CALL(mock, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s)); |
| 533 | 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] | 534 | datastore.createItem("/example-schema:addresses[.='0.0.0.0']"); |
| 535 | datastore.createItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 536 | datastore.commitChanges(); |
| 537 | expected = { |
| 538 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 539 | {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s}, |
| 540 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 541 | }; |
| 542 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 543 | |
| 544 | 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] | 545 | datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 546 | datastore.commitChanges(); |
| 547 | expected = { |
| 548 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 549 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 550 | }; |
| 551 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 552 | |
| 553 | 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] | 554 | datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 555 | datastore.commitChanges(); |
| 556 | expected = {}; |
| 557 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 558 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 559 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 560 | SECTION("deleting a non-existing leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 561 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 562 | catching<OnKeyNotFound>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 563 | datastore.deleteItem("/example-schema:addresses[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 564 | datastore.commitChanges(); |
| 565 | }); |
| 566 | } |
| 567 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 568 | SECTION("accessing a non-existing schema node as a leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 569 | { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 570 | catching<OnInvalidSchemaPathCreate>([&]{ |
| 571 | datastore.createItem("/example-schema:non-existing[.='non-existing']"); |
| 572 | datastore.commitChanges(); |
| 573 | }); |
| 574 | |
| 575 | catching<OnInvalidSchemaPathDelete>([&]{ |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 576 | datastore.deleteItem("/example-schema:non-existing[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 577 | datastore.commitChanges(); |
| 578 | }); |
| 579 | } |
| 580 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 581 | SECTION("copying data from startup refreshes the data") |
| 582 | { |
| 583 | { |
| 584 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
| 585 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", std::nullopt, "123"s)); |
| 586 | datastore.setLeaf("/example-schema:leafInt16", int16_t{123}); |
| 587 | datastore.commitChanges(); |
| 588 | } |
| 589 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}}); |
| 590 | REQUIRE_CALL(mock, write("/example-schema:leafInt16", "123"s, std::nullopt)); |
| 591 | datastore.copyConfig(Datastore::Startup, Datastore::Running); |
| 592 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
| 593 | } |
| 594 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 595 | SECTION("moving leaflist instances") |
| 596 | { |
| 597 | DatastoreAccess::Tree expected; |
| 598 | { |
| 599 | // sysrepo does this twice for some reason, it's possibly a bug |
| 600 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "http"s)).TIMES(2); |
| 601 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s)); |
| 602 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s)); |
| 603 | REQUIRE_CALL(mock, write("/example-schema:protocols", "http"s, "ftp"s)); |
| 604 | REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "pop3"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 605 | datastore.createItem("/example-schema:protocols[.='http']"); |
| 606 | datastore.createItem("/example-schema:protocols[.='ftp']"); |
| 607 | datastore.createItem("/example-schema:protocols[.='pop3']"); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 608 | datastore.commitChanges(); |
| 609 | expected = { |
| 610 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 611 | {"/example-schema:protocols[.='http']", "http"s}, |
| 612 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 613 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 614 | }; |
| 615 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 616 | } |
| 617 | |
| 618 | std::string sourcePath; |
| 619 | SECTION("begin") |
| 620 | { |
| 621 | REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s)); |
| 622 | sourcePath = "/example-schema:protocols[.='pop3']"; |
| 623 | datastore.moveItem(sourcePath, yang::move::Absolute::Begin); |
| 624 | datastore.commitChanges(); |
| 625 | expected = { |
| 626 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 627 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 628 | {"/example-schema:protocols[.='http']", "http"s}, |
| 629 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 630 | }; |
| 631 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 632 | } |
| 633 | |
| 634 | SECTION("end") |
| 635 | { |
| 636 | sourcePath = "/example-schema:protocols[.='http']"; |
| 637 | REQUIRE_CALL(mock, write("/example-schema:protocols", "pop3"s, "http"s)); |
| 638 | datastore.moveItem(sourcePath, yang::move::Absolute::End); |
| 639 | datastore.commitChanges(); |
| 640 | expected = { |
| 641 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 642 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 643 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 644 | {"/example-schema:protocols[.='http']", "http"s}, |
| 645 | }; |
| 646 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 647 | } |
| 648 | |
| 649 | SECTION("after") |
| 650 | { |
| 651 | sourcePath = "/example-schema:protocols[.='http']"; |
| 652 | REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s)); |
| 653 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}}); |
| 654 | datastore.commitChanges(); |
| 655 | expected = { |
| 656 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 657 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 658 | {"/example-schema:protocols[.='http']", "http"s}, |
| 659 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 660 | }; |
| 661 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 662 | } |
| 663 | |
| 664 | SECTION("before") |
| 665 | { |
| 666 | sourcePath = "/example-schema:protocols[.='http']"; |
| 667 | REQUIRE_CALL(mock, write("/example-schema:protocols", "ftp"s, "http"s)); |
| 668 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}}); |
| 669 | datastore.commitChanges(); |
| 670 | expected = { |
| 671 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 672 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 673 | {"/example-schema:protocols[.='http']", "http"s}, |
| 674 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 675 | }; |
| 676 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 677 | } |
| 678 | } |
| 679 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 680 | SECTION("moving non-existing schema nodes") |
| 681 | { |
| 682 | catching<OnInvalidSchemaPathMove>([&]{ |
| 683 | datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin); |
| 684 | datastore.commitChanges(); |
| 685 | }); |
| 686 | } |
| 687 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 688 | SECTION("moving list instances") |
| 689 | { |
| 690 | DatastoreAccess::Tree expected; |
| 691 | { |
| 692 | // sysrepo does this twice for some reason, it's possibly a bug |
| 693 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", std::nullopt, ""s)).TIMES(2); |
| 694 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']/name", std::nullopt, "John"s)); |
| 695 | REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", std::nullopt, ""s)); |
| 696 | REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']", ""s, ""s)); |
| 697 | REQUIRE_CALL(mock, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s)); |
| 698 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s)); |
| 699 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s)); |
| 700 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", ""s, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 701 | datastore.createItem("/example-schema:players[name='John']"); |
| 702 | datastore.createItem("/example-schema:players[name='Eve']"); |
| 703 | datastore.createItem("/example-schema:players[name='Adam']"); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 704 | datastore.commitChanges(); |
| 705 | expected = { |
| 706 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 707 | {"/example-schema:players[name='John']/name", "John"s}, |
| 708 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 709 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 710 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 711 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 712 | }; |
| 713 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 714 | } |
| 715 | |
| 716 | std::string sourcePath; |
| 717 | SECTION("begin") |
| 718 | { |
| 719 | sourcePath = "/example-schema:players[name='Adam']"; |
| 720 | REQUIRE_CALL(mock, write("/example-schema:players[name='Adam']", std::nullopt, ""s)); |
| 721 | datastore.moveItem(sourcePath, yang::move::Absolute::Begin); |
| 722 | datastore.commitChanges(); |
| 723 | expected = { |
| 724 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 725 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 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 | }; |
| 731 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 732 | } |
| 733 | |
| 734 | SECTION("end") |
| 735 | { |
| 736 | sourcePath = "/example-schema:players[name='John']"; |
| 737 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s)); |
| 738 | datastore.moveItem(sourcePath, yang::move::Absolute::End); |
| 739 | datastore.commitChanges(); |
| 740 | expected = { |
| 741 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 742 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 743 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 744 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 745 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 746 | {"/example-schema:players[name='John']/name", "John"s}, |
| 747 | }; |
| 748 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 749 | } |
| 750 | |
| 751 | SECTION("after") |
| 752 | { |
| 753 | sourcePath = "/example-schema:players[name='John']"; |
| 754 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s)); |
| 755 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}}); |
| 756 | datastore.commitChanges(); |
| 757 | expected = { |
| 758 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 759 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 760 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 761 | {"/example-schema:players[name='John']/name", "John"s}, |
| 762 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 763 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 764 | }; |
| 765 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 766 | } |
| 767 | |
| 768 | SECTION("before") |
| 769 | { |
| 770 | sourcePath = "/example-schema:players[name='John']"; |
| 771 | REQUIRE_CALL(mock, write("/example-schema:players[name='John']", ""s, ""s)); |
| 772 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}}); |
| 773 | datastore.commitChanges(); |
| 774 | expected = { |
| 775 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 776 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 777 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 778 | {"/example-schema:players[name='John']/name", "John"s}, |
| 779 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 780 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 781 | }; |
| 782 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 783 | } |
| 784 | } |
| 785 | |
Václav Kubernát | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 786 | SECTION("getting /") |
| 787 | { |
| 788 | { |
| 789 | REQUIRE_CALL(mock, write("/example-schema:leafInt32", std::nullopt, "64"s)); |
| 790 | datastore.setLeaf("/example-schema:leafInt32", 64); |
| 791 | datastore.commitChanges(); |
| 792 | } |
| 793 | |
| 794 | DatastoreAccess::Tree expected{ |
| 795 | // Sysrepo always returns containers when getting values, but |
| 796 | // libnetconf does not. This is fine by the YANG standard: |
| 797 | // https://tools.ietf.org/html/rfc7950#section-7.5.7 Furthermore, |
| 798 | // NetconfAccess implementation actually only iterates over leafs, |
| 799 | // so even if libnetconf did include containers, they wouldn't get |
| 800 | // shown here anyway. With sysrepo2, this won't be necessary, |
| 801 | // because it'll use the same data structure as libnetconf, so the |
| 802 | // results will be consistent. |
| 803 | #ifdef sysrepo_BACKEND |
| 804 | {"/example-schema:inventory", special_{SpecialValue::Container}}, |
| 805 | {"/example-schema:lol", special_{SpecialValue::Container}}, |
| 806 | #endif |
| 807 | {"/example-schema:leafInt32", 64}}; |
| 808 | auto items = datastore.getItems("/"); |
| 809 | // This tests if we at least get the data WE added. |
| 810 | REQUIRE(std::all_of(expected.begin(), expected.end(), [items] (const auto& item) { return std::find(items.begin(), items.end(), item) != items.end(); })); |
| 811 | } |
| 812 | |
Václav Kubernát | 36986c5 | 2020-06-25 10:30:05 +0200 | [diff] [blame] | 813 | SECTION("setting and removing without commit") |
| 814 | { |
| 815 | datastore.setLeaf("/example-schema:leafInt32", 64); |
| 816 | datastore.deleteItem("/example-schema:leafInt32"); |
| 817 | } |
| 818 | |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 819 | SECTION("two key lists") |
| 820 | { |
| 821 | REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']", std::nullopt, ""s)); |
| 822 | REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s)); |
| 823 | REQUIRE_CALL(mock, write("/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s)); |
| 824 | datastore.createItem("/example-schema:point[x='12'][y='10']"); |
| 825 | datastore.commitChanges(); |
| 826 | REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:point") != std::string::npos); |
| 827 | } |
| 828 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 829 | waitForCompletionAndBitMore(seq1); |
| 830 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 831 | |
| 832 | class RpcCb: public sysrepo::Callback { |
| 833 | int rpc(const char *xpath, const ::sysrepo::S_Vals input, ::sysrepo::S_Vals_Holder output, void *) override |
| 834 | { |
| 835 | const auto nukes = "/example-schema:launch-nukes"s; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 836 | if (xpath == "/example-schema:noop"s || xpath == "/example-schema:fire"s) { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 837 | return SR_ERR_OK; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | if (xpath == nukes) { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 841 | uint64_t kilotons = 0; |
| 842 | bool hasCities = false; |
| 843 | for (size_t i = 0; i < input->val_cnt(); ++i) { |
| 844 | const auto& val = input->val(i); |
Václav Kubernát | f4b6a93 | 2020-07-09 10:34:19 +0200 | [diff] [blame] | 845 | if (val->xpath() == nukes + "/payload") { |
| 846 | continue; // ignore, container |
| 847 | } |
| 848 | if (val->xpath() == nukes + "/description") { |
| 849 | continue; // unused |
| 850 | } |
| 851 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 852 | if (val->xpath() == nukes + "/payload/kilotons") { |
| 853 | kilotons = val->data()->get_uint64(); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 854 | } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) { |
| 855 | hasCities = true; |
| 856 | } else { |
| 857 | throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath()); |
| 858 | } |
| 859 | } |
| 860 | if (kilotons == 333'666) { |
| 861 | // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>. |
| 862 | return SR_ERR_OK; |
| 863 | } |
| 864 | auto buf = output->allocate(2); |
| 865 | size_t i = 0; |
| 866 | buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666}); |
| 867 | buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons)); |
| 868 | if (hasCities) { |
| 869 | buf = output->reallocate(output->val_cnt() + 2); |
| 870 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London"); |
| 871 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin"); |
| 872 | } |
| 873 | return SR_ERR_OK; |
| 874 | } |
| 875 | throw std::runtime_error("unrecognized RPC"); |
| 876 | } |
| 877 | }; |
| 878 | |
| 879 | TEST_CASE("rpc") { |
| 880 | trompeloeil::sequence seq1; |
| 881 | auto srConn = std::make_shared<sysrepo::Connection>("netconf-cli-test-rpc"); |
| 882 | auto srSession = std::make_shared<sysrepo::Session>(srConn); |
| 883 | auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession); |
| 884 | auto cb = std::make_shared<RpcCb>(); |
| 885 | sysrepo::Logs{}.set_stderr(SR_LL_INF); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 886 | auto doNothingCb = std::make_shared<sysrepo::Callback>(); |
| 887 | srSubscription->module_change_subscribe("example-schema", doNothingCb, nullptr, SR_SUBSCR_CTX_REUSE); |
| 888 | // careful here, sysrepo insists on module_change CBs being registered before RPC CBs, otherwise there's a memleak |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 889 | srSubscription->rpc_subscribe("/example-schema:noop", cb, nullptr, SR_SUBSCR_CTX_REUSE); |
| 890 | srSubscription->rpc_subscribe("/example-schema:launch-nukes", cb, nullptr, SR_SUBSCR_CTX_REUSE); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 891 | srSubscription->rpc_subscribe("/example-schema:fire", cb, nullptr, SR_SUBSCR_CTX_REUSE); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 892 | |
| 893 | #ifdef sysrepo_BACKEND |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 894 | auto datastore = std::make_shared<SysrepoAccess>("netconf-cli-test", Datastore::Running); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 895 | #elif defined(netconf_BACKEND) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 896 | auto datastore = std::make_shared<NetconfAccess>(NETOPEER_SOCKET_PATH); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 897 | #elif defined(yang_BACKEND) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 898 | auto datastore = std::make_shared<YangAccess>(); |
| 899 | datastore->addSchemaDir(schemaDir); |
| 900 | datastore->addSchemaFile(exampleSchemaFile); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 901 | #else |
| 902 | #error "Unknown backend" |
| 903 | #endif |
| 904 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 905 | auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) { |
| 906 | return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema())); |
| 907 | }; |
| 908 | |
| 909 | ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore); |
| 910 | |
| 911 | // ProxyDatastore cannot easily read DatastoreAccess::Tree, so we need to set the input via create/setLeaf/etc. |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 912 | SECTION("valid") |
| 913 | { |
| 914 | std::string rpc; |
| 915 | DatastoreAccess::Tree input, output; |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 916 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 917 | SECTION("noop") { |
| 918 | rpc = "/example-schema:noop"; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 919 | proxyDatastore.initiateRpc(rpc); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | SECTION("small nuke") { |
| 923 | rpc = "/example-schema:launch-nukes"; |
| 924 | input = { |
| 925 | {"description", "dummy"s}, |
| 926 | {"payload/kilotons", uint64_t{333'666}}, |
| 927 | }; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 928 | proxyDatastore.initiateRpc(rpc); |
| 929 | proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{333'666}); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 930 | // no data are returned |
| 931 | } |
| 932 | |
| 933 | SECTION("small nuke") { |
| 934 | rpc = "/example-schema:launch-nukes"; |
| 935 | input = { |
| 936 | {"description", "dummy"s}, |
| 937 | {"payload/kilotons", uint64_t{4}}, |
| 938 | }; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 939 | proxyDatastore.initiateRpc(rpc); |
| 940 | proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{4}); |
| 941 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 942 | output = { |
| 943 | {"blast-radius", uint32_t{33'666}}, |
| 944 | {"actual-yield", uint64_t{5}}, |
| 945 | }; |
| 946 | } |
| 947 | |
| 948 | SECTION("with lists") { |
| 949 | rpc = "/example-schema:launch-nukes"; |
| 950 | input = { |
| 951 | {"payload/kilotons", uint64_t{6}}, |
| 952 | {"cities/targets[city='Prague']/city", "Prague"s}, |
| 953 | }; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 954 | proxyDatastore.initiateRpc(rpc); |
| 955 | proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{6}); |
| 956 | proxyDatastore.createItem("/example-schema:launch-nukes/example-schema:cities/example-schema:targets[city='Prague']"); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 957 | output = { |
| 958 | {"blast-radius", uint32_t{33'666}}, |
| 959 | {"actual-yield", uint64_t{7}}, |
| 960 | {"damaged-places", special_{SpecialValue::PresenceContainer}}, |
| 961 | {"damaged-places/targets[city='London']", special_{SpecialValue::List}}, |
| 962 | {"damaged-places/targets[city='London']/city", "London"s}, |
| 963 | {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}}, |
| 964 | {"damaged-places/targets[city='Berlin']/city", "Berlin"s}, |
| 965 | }; |
| 966 | } |
| 967 | |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 968 | SECTION("with leafref") { |
| 969 | datastore->createItem("/example-schema:person[name='Colton']"); |
| 970 | datastore->commitChanges(); |
| 971 | |
| 972 | rpc = "/example-schema:fire"; |
| 973 | input = { |
| 974 | {"whom", "Colton"s} |
| 975 | }; |
| 976 | proxyDatastore.initiateRpc(rpc); |
| 977 | proxyDatastore.setLeaf("/example-schema:fire/example-schema:whom", "Colton"s); |
| 978 | } |
| 979 | |
| 980 | catching<OnRPC>([&] {REQUIRE(datastore->executeRpc(rpc, input) == output);}); |
| 981 | catching<OnRPC>([&] {REQUIRE(proxyDatastore.executeRpc() == output);}); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 982 | } |
| 983 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 984 | SECTION("non-existing RPC") |
| 985 | { |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 986 | catching<OnInvalidRpcPath>([&] {datastore->executeRpc("/example-schema:non-existing", DatastoreAccess::Tree{});}); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 989 | waitForCompletionAndBitMore(seq1); |
| 990 | } |