Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016-2019 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Jan Kundrát <jan.kundrat@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "trompeloeil_doctest.h" |
| 9 | #include <boost/algorithm/string/predicate.hpp> |
| 10 | #include <map> |
| 11 | #include <sysrepo-cpp/Session.hpp> |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 12 | #include "test_log_setup.h" |
Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame] | 13 | #include "utils/sysrepo.h" |
Tomáš Pecka | 98ad18d | 2020-11-13 15:39:55 +0100 | [diff] [blame] | 14 | |
| 15 | /** @short Return a subtree from sysrepo, compacting the XPath */ |
| 16 | auto dataFromSysrepo(const std::shared_ptr<sysrepo::Session>& session, const std::string& xpath) |
| 17 | { |
| 18 | spdlog::get("main")->error("dataFrom {}", xpath); |
| 19 | std::map<std::string, std::string> res; |
| 20 | auto vals = session->get_items((xpath + "//*").c_str()); |
| 21 | REQUIRE(!!vals); |
| 22 | for (size_t i = 0; i < vals->val_cnt(); ++i) { |
| 23 | const auto& v = vals->val(i); |
| 24 | const auto briefXPath = std::string(v->xpath()).substr(boost::algorithm::ends_with(xpath, ":*") ? xpath.size() - 1 : xpath.size()); |
| 25 | res.emplace(briefXPath, v->val_to_string()); |
| 26 | } |
| 27 | return res; |
| 28 | } |
| 29 | |
Tomáš Pecka | 9971dcf | 2021-01-14 09:41:20 +0100 | [diff] [blame] | 30 | /** @short Return a subtree from specified sysrepo's datastore, compacting the XPath*/ |
| 31 | auto dataFromSysrepo(const std::shared_ptr<sysrepo::Session>& session, const std::string& xpath, sr_datastore_t datastore) |
| 32 | { |
| 33 | sr_datastore_t oldDatastore = session->session_get_ds(); |
| 34 | session->session_switch_ds(datastore); |
| 35 | |
| 36 | auto res = dataFromSysrepo(session, xpath); |
| 37 | |
| 38 | session->session_switch_ds(oldDatastore); |
| 39 | return res; |
| 40 | } |
| 41 | |
Tomáš Pecka | 55f64f8 | 2020-12-10 19:03:30 +0100 | [diff] [blame] | 42 | #define TEST_SYSREPO_INIT \ |
| 43 | auto srConn = std::make_shared<sysrepo::Connection>(); \ |
| 44 | auto srSess = std::make_shared<sysrepo::Session>(srConn); \ |
| 45 | auto srSubs = std::make_shared<sysrepo::Subscribe>(srSess); |
Tomáš Pecka | 749af2e | 2021-01-14 09:52:09 +0100 | [diff] [blame] | 46 | |
| 47 | #define TEST_SYSREPO_INIT_CLIENT \ |
| 48 | auto clientConn = std::make_shared<sysrepo::Connection>(); \ |
| 49 | auto client = std::make_shared<sysrepo::Session>(clientConn); |