Skip to content

Commit

Permalink
test mock
Browse files Browse the repository at this point in the history
  • Loading branch information
cmuhammedrafi committed Dec 23, 2024
1 parent 3cde573 commit c5d87a0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 44 deletions.
2 changes: 1 addition & 1 deletion LegacyPlugin_WiFiManagerAPIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ namespace WPEFramework
Core::SystemInfo::SetEnvironment(_T("THUNDER_ACCESS"), (_T("127.0.0.1:9998")));
m_networkmanager = make_shared<WPEFramework::JSONRPC::SmartLinkType<WPEFramework::Core::JSON::IElement> >(_T(NETWORK_MANAGER_CALLSIGN), _T("org.rdk.Wifi"), query);

subscribeToEvents();
//subscribeToEvents();
return string();
}

Expand Down
148 changes: 111 additions & 37 deletions Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "NetworkManagerLogger.h"
#include "LegacyPlugin_WiFiManagerAPIs.h"
#include "ThunderPortability.h"
#include "INetworkManager.h"

using namespace std;
using namespace WPEFramework;
Expand All @@ -17,14 +18,12 @@ class WiFiManagerTest : public ::testing::Test {
Core::ProxyType<Plugin::WiFiManager> plugin;
Core::JSONRPC::Handler& handler;
DECL_CORE_JSONRPC_CONX connection;
Core::JSONRPC::Message message;
string response;
ServiceMock services;

std::string response;

WiFiManagerTest()
: plugin(Core::ProxyType<Plugin::WiFiManager>::Create())
, handler(*(plugin))
, INIT_CONX(1, 0)
, handler(*plugin)
, INIT_CONX(1, 0)
{

}
Expand All @@ -34,34 +33,6 @@ class WiFiManagerTest : public ::testing::Test {
}
};

class WiFiManagerInitializedTest : public WiFiManagerTest {
protected:
NiceMock<ServiceMock> service;
WiFiManagerInitializedTest()
{
EXPECT_EQ(string(""), plugin->Initialize(&service));
EXPECT_CALL(service, QueryInterfaceByCallsign(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const uint32_t, const string& name) -> void* {
EXPECT_EQ(name, string(_T("SecurityAgent")));
return nullptr;
}));
EXPECT_CALL(service, QueryInterfaceByCallsign(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const uint32_t, const string& name) -> void* {
EXPECT_EQ(name, string(_T("org.rdk.NetworkManager.1")));
return nullptr;
}));
}

virtual ~WiFiManagerInitializedTest() override
{
plugin->Deinitialize(&service);
}

};

TEST_F(WiFiManagerTest, TestedAPIsShouldExist)
{
Expand All @@ -80,10 +51,113 @@ TEST_F(WiFiManagerTest, TestedAPIsShouldExist)
EXPECT_EQ(Core::ERROR_NONE, handler.Exists(_T("getSupportedSecurityModes")));
}

TEST_F(WiFiManagerInitializedTest, startScan)
namespace WPEFramework
{
namespace Exchange
{
class MockINetworkManager : public INetworkManager {
public:
MOCK_METHOD(uint32_t, GetAvailableInterfaces, (IInterfaceDetailsIterator*& interfaces), (override));
MOCK_METHOD(uint32_t, GetPrimaryInterface, (std::string& interface), (override));
MOCK_METHOD(uint32_t, SetPrimaryInterface, (const std::string& interface), (override));
MOCK_METHOD(uint32_t, SetInterfaceState, (const std::string& interface, const bool enabled), (override));
MOCK_METHOD(uint32_t, GetInterfaceState, (const std::string& interface, bool& enabled), (override));
MOCK_METHOD(uint32_t, GetIPSettings, (std::string& interface, const std::string& ipversion, IPAddress& address), (override));
MOCK_METHOD(uint32_t, SetIPSettings, (const std::string& interface, const IPAddress& address), (override));
MOCK_METHOD(uint32_t, GetStunEndpoint, (std::string& endpoint, uint32_t& port, uint32_t& timeout, uint32_t& cacheLifetime), (const, override));
MOCK_METHOD(uint32_t, SetStunEndpoint, (const std::string& endpoint, const uint32_t port, const uint32_t timeout, const uint32_t cacheLifetime), (override));
MOCK_METHOD(uint32_t, GetConnectivityTestEndpoints, (IStringIterator*& endpoints), (const, override));
MOCK_METHOD(uint32_t, SetConnectivityTestEndpoints, (IStringIterator* const endpoints), (override));
MOCK_METHOD(uint32_t, IsConnectedToInternet, (std::string& ipversion, InternetStatus& status), (override));
MOCK_METHOD(uint32_t, GetCaptivePortalURI, (std::string& uri), (const, override));
MOCK_METHOD(uint32_t, StartConnectivityMonitoring, (const uint32_t interval), (override));
MOCK_METHOD(uint32_t, StopConnectivityMonitoring, (), (const, override));
MOCK_METHOD(uint32_t, GetPublicIP, (std::string& ipversion, std::string& ipaddress), (override));
MOCK_METHOD(uint32_t, Ping, (const std::string& ipversion, const std::string& endpoint, const uint32_t count, const uint16_t timeout, const std::string& guid, std::string& response), (override));
MOCK_METHOD(uint32_t, Trace, (const std::string& ipversion, const std::string& endpoint, const uint32_t nqueries, const std::string& guid, std::string& response), (override));

// WiFi Specific Methods
MOCK_METHOD(uint32_t, StartWiFiScan, (const std::string& frequency, IStringIterator* const ssids), (override));
MOCK_METHOD(uint32_t, StopWiFiScan, (), (override));
MOCK_METHOD(uint32_t, GetKnownSSIDs, (IStringIterator*& ssids), (override));
MOCK_METHOD(uint32_t, AddToKnownSSIDs, (const WiFiConnectTo& ssid), (override));
MOCK_METHOD(uint32_t, RemoveKnownSSID, (const std::string& ssid), (override));
MOCK_METHOD(uint32_t, WiFiConnect, (const WiFiConnectTo& ssid), (override));
MOCK_METHOD(uint32_t, WiFiDisconnect, (), (override));
MOCK_METHOD(uint32_t, GetConnectedSSID, (WiFiSSIDInfo& ssidInfo), (override));
MOCK_METHOD(uint32_t, StartWPS, (const WiFiWPS& method, const std::string& pin), (override));
MOCK_METHOD(uint32_t, StopWPS, (), (override));
MOCK_METHOD(uint32_t, GetWifiState, (WiFiState& state), (override));
MOCK_METHOD(uint32_t, GetWiFiSignalStrength, (std::string& ssid, std::string& strength, WiFiSignalQuality& quality), (override));
MOCK_METHOD(uint32_t, GetSupportedSecurityModes, (ISecurityModeIterator*& modes), (const, override));

// Logging Methods
MOCK_METHOD(uint32_t, SetLogLevel, (const Logging& level), (override));
MOCK_METHOD(uint32_t, GetLogLevel, (Logging& level), (override));

// Configuration Method
MOCK_METHOD(uint32_t, Configure, (const std::string& configLine), (override));

// Notification Methods
MOCK_METHOD(uint32_t, Register, (INetworkManager::INotification* notification), (override));
MOCK_METHOD(uint32_t, Unregister, (INetworkManager::INotification* notification), (override));

// Notification Event Methods
MOCK_METHOD(void, onInterfaceStateChange, (const InterfaceState state, const std::string interface), (override));
MOCK_METHOD(void, onActiveInterfaceChange, (const std::string prevActiveInterface, const std::string currentActiveInterface), (override));
MOCK_METHOD(void, onIPAddressChange, (const std::string interface, const std::string ipversion, const std::string ipaddress, const IPStatus status), (override));
MOCK_METHOD(void, onInternetStatusChange, (const InternetStatus prevState, const InternetStatus currState), (override));
MOCK_METHOD(void, onAvailableSSIDs, (const std::string jsonOfScanResults), (override));
MOCK_METHOD(void, onWiFiStateChange, (const WiFiState state), (override));
MOCK_METHOD(void, onWiFiSignalStrengthChange, (const std::string ssid, const std::string strength, const WiFiSignalQuality quality), (override));
};
}
}

TEST_F(WiFiManagerTest, connect)
{
EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("startScan"), _T("{}"), response));
EXPECT_EQ(response, string("{\"success\":false}"));
NiceMock<ServiceMock> service;
WPEFramework::Exchange::MockINetworkManager NetworkManagerService;

EXPECT_CALL(service, QueryInterfaceByCallsign(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const uint32_t, const string& name) -> void* {
EXPECT_EQ(name, string(_T("org.rdk.NetworkManager.1")));
return nullptr;
}));

EXPECT_EQ(string(""), plugin->Initialize(&service));

EXPECT_EQ(Core::ERROR_GENERAL, handler.Invoke(connection, _T("connect"), _T("{}"), response));
EXPECT_EQ(response, string(""));

EXPECT_CALL(service, QueryInterfaceByCallsign(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const uint32_t, const string& name) -> void* {
EXPECT_EQ(name, string(_T("org.rdk.NetworkManager.1")));
return nullptr;
}));

EXPECT_EQ(Core::ERROR_UNAVAILABLE, handler.Invoke(connection, _T("connect"), _T("{\"ssid\":\"testssid\"}"), response));
EXPECT_EQ(response, string(""));

EXPECT_CALL(service, QueryInterfaceByCallsign(::testing::_, ::testing::_))
.Times(1)
.WillOnce(::testing::Invoke(
[&](const uint32_t, const string& name) -> void* {
EXPECT_EQ(name, string(_T("org.rdk.NetworkManager.1")));
return &NetworkManagerService;
}));

ON_CALL(NetworkManagerService, WiFiConnect(::testing::_))
.WillByDefault(::testing::Return(Core::ERROR_NONE));

EXPECT_EQ(Core::ERROR_UNAVAILABLE, handler.Invoke(connection, _T("connect"), _T("{\"ssid\":\"testssid\"}"), response));
EXPECT_EQ(response, string(""));

plugin->Deinitialize(&service);
}


Expand Down
6 changes: 0 additions & 6 deletions Tests/unit_test/unit_tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@ FetchContent_Declare(
FetchContent_MakeAvailable(googletest)

add_executable(${UNIT_TEST}
Tests/unit_test/test_WiFiSignalStrengthMonitor.cpp
Tests/unit_test/test_NetworkManagerConnectivity.cpp
Tests/unit_test/test_NetworkManagerStunClient.cpp
Tests/unit_test/test_LegacyPlugin_WiFiManagerAPIs.cpp
Tests/mocks/thunder/Module.cpp
WiFiSignalStrengthMonitor.cpp
NetworkManagerLogger.cpp
NetworkManagerConnectivity.cpp
NetworkManagerStunClient.cpp
LegacyPlugin_WiFiManagerAPIs.cpp
)

Expand Down

0 comments on commit c5d87a0

Please sign in to comment.