From 30d704588fa7eb9d32f66296ec5f6784f082869e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= Date: Mon, 2 Dec 2024 20:11:32 +0100 Subject: [PATCH 16/44] tests: make NotificationWatcher reusable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Organization: Wires We will need some parts of this in yang push tests Change-Id: I72e60d993f75bbc848af096ac325a75fa3dea61a Signed-off-by: Mattias Walström --- tests/event_watchers.cpp | 28 +++++++++++++++++++++++++ tests/event_watchers.h | 13 ++++++++++++ tests/restconf-notifications.cpp | 35 -------------------------------- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/tests/event_watchers.cpp b/tests/event_watchers.cpp index c696bc5..bde96ec 100644 --- a/tests/event_watchers.cpp +++ b/tests/event_watchers.cpp @@ -1,3 +1,4 @@ +#include #include "UniqueResource.h" #include "event_watchers.h" @@ -50,3 +51,30 @@ sysrepo::Subscription datastoreNewStateSubscription(sysrepo::Session& session, D 0, sysrepo::SubscribeOptions::DoneOnly); } + +RestconfNotificationWatcher::RestconfNotificationWatcher(const libyang::Context& ctx) + : ctx(ctx) + , dataFormat(libyang::DataFormat::JSON) +{ +} + +void RestconfNotificationWatcher::setDataFormat(const libyang::DataFormat dataFormat) +{ + this->dataFormat = dataFormat; +} + +void RestconfNotificationWatcher::operator()(const std::string& msg) const +{ + spdlog::trace("Client received data: {}", msg); + auto notifDataNode = ctx.parseOp(msg, + dataFormat, + dataFormat == libyang::DataFormat::JSON ? libyang::OperationType::NotificationRestconf : libyang::OperationType::NotificationNetconf); + + // parsing nested notifications does not return the data tree root node but the notification data node + auto dataRoot = notifDataNode.op; + while (dataRoot->parent()) { + dataRoot = *dataRoot->parent(); + } + + data(*dataRoot->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::Shrink)); +} diff --git a/tests/event_watchers.h b/tests/event_watchers.h index 5b2429c..b6f2dd2 100644 --- a/tests/event_watchers.h +++ b/tests/event_watchers.h @@ -32,3 +32,16 @@ static DatastoreChangesMock testMockForUntrackedModuleWrites; #define SUBSCRIBE_MODULE(SUBNAME, SESSION, MODULE) \ ALLOW_CALL(testMockForUntrackedModuleWrites, change(trompeloeil::_)); \ auto SUBNAME = datastoreChangesSubscription(SESSION, testMockForUntrackedModuleWrites, MODULE); + +struct RestconfNotificationWatcher { + libyang::Context ctx; + libyang::DataFormat dataFormat; + + RestconfNotificationWatcher(const libyang::Context& ctx); + void setDataFormat(const libyang::DataFormat dataFormat); + void operator()(const std::string& msg) const; + + MAKE_CONST_MOCK1(data, void(const std::string&)); +}; + +#define EXPECT_NOTIFICATION(DATA, SEQ) expectations.emplace_back(NAMED_REQUIRE_CALL(netconfWatcher, data(DATA)).IN_SEQUENCE(SEQ)); diff --git a/tests/restconf-notifications.cpp b/tests/restconf-notifications.cpp index d873512..6c8c51a 100644 --- a/tests/restconf-notifications.cpp +++ b/tests/restconf-notifications.cpp @@ -16,45 +16,10 @@ static const auto SERVER_PORT = "10088"; #include "tests/aux-utils.h" #include "tests/pretty_printers.h" -#define EXPECT_NOTIFICATION(DATA, SEQ) expectations.emplace_back(NAMED_REQUIRE_CALL(netconfWatcher, data(DATA)).IN_SEQUENCE(SEQ)); #define SEND_NOTIFICATION(DATA) notifSession.sendNotification(*ctx.parseOp(DATA, libyang::DataFormat::JSON, libyang::OperationType::NotificationYang).op, sysrepo::Wait::No); using namespace std::chrono_literals; -struct RestconfNotificationWatcher { - libyang::Context ctx; - libyang::DataFormat dataFormat; - - RestconfNotificationWatcher(const libyang::Context& ctx) - : ctx(ctx) - , dataFormat(libyang::DataFormat::JSON) - { - } - - void setDataFormat(const libyang::DataFormat dataFormat) - { - this->dataFormat = dataFormat; - } - - void operator()(const std::string& msg) const - { - spdlog::trace("Client received data: {}", msg); - auto notifDataNode = ctx.parseOp(msg, - dataFormat, - dataFormat == libyang::DataFormat::JSON ? libyang::OperationType::NotificationRestconf : libyang::OperationType::NotificationNetconf); - - // parsing nested notifications does not return the data tree root node but the notification data node - auto dataRoot = notifDataNode.op; - while (dataRoot->parent()) { - dataRoot = *dataRoot->parent(); - } - - data(*dataRoot->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::Shrink)); - } - - MAKE_CONST_MOCK1(data, void(const std::string&)); -}; - struct SSEClient { std::shared_ptr client; boost::asio::deadline_timer t; -- 2.43.0