From 6b936cbdd3aa93321d5241f6f739b1d41ef3071a Mon Sep 17 00:00:00 2001 From: AdityaKasar Date: Tue, 25 Feb 2025 11:23:15 +0530 Subject: [PATCH 1/2] feat: Singleton class mock test support --- .../cpp/src/shared/src/Gateway/Gateway.h | 7 +++ .../shared/src/Gateway/unidi/gateway_impl.h | 53 +++++++++++++++---- languages/cpp/src/shared/src/IGateway.h | 38 +++++++++++++ 3 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 languages/cpp/src/shared/src/IGateway.h diff --git a/languages/cpp/src/shared/src/Gateway/Gateway.h b/languages/cpp/src/shared/src/Gateway/Gateway.h index 23360017..6af7e34c 100644 --- a/languages/cpp/src/shared/src/Gateway/Gateway.h +++ b/languages/cpp/src/shared/src/Gateway/Gateway.h @@ -57,6 +57,13 @@ namespace FireboltSDK void TransportUpdated(Transport* transport); +#ifdef UNIT_TEST + void UpdateGateway(std::unique_ptr mockGateway) + { + implementation = std::move(mockGateway); + } +#endif + template Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, RESPONSE &response) { diff --git a/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h b/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h index 17177afc..481f23d6 100644 --- a/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h +++ b/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h @@ -30,15 +30,23 @@ #include #include +#ifdef UNIT_TEST +#include "IGateway.h" +#endif + namespace FireboltSDK { using EventCallback = std::function; +#ifndef UNIT_TEST class GatewayImpl +#else + class GatewayImpl : public IGateway +#endif { - Transport* transport; + Transport *transport; public: GatewayImpl() @@ -46,52 +54,75 @@ namespace FireboltSDK } public: - void TransportUpdated(Transport* transport) + void TransportUpdated(Transport *transport) { - this->transport = transport; + this->transport = transport; } +#ifndef UNIT_TEST template Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, RESPONSE &response) { - if (transport == nullptr) { + if (transport == nullptr) + { + return Firebolt::Error::NotConnected; + } + return transport->Invoke(method, parameters, response); + } +#else + Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, FireboltSDK::JSON::String &response) + { + if (transport == nullptr) + { return Firebolt::Error::NotConnected; } return transport->Invoke(method, parameters, response); } + Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, Firebolt::Authentication::JsonData_Token &response) + { + if (transport == nullptr) + { + return Firebolt::Error::NotConnected; + } + return transport->Invoke(method, parameters, response); + } + +#endif + Firebolt::Error Response(unsigned id, const std::string &method, const JsonObject &response) { return Firebolt::Error::General; } template - Firebolt::Error Subscribe(const string& event, const string& parameters, RESPONSE& response) + Firebolt::Error Subscribe(const string &event, const string ¶meters, RESPONSE &response) { - if (transport == nullptr) { + if (transport == nullptr) + { return Firebolt::Error::NotConnected; } return transport->Subscribe(event, parameters, response); } - Firebolt::Error Unsubscribe(const string& event, const string& parameters) + Firebolt::Error Unsubscribe(const string &event, const string ¶meters) { - if (transport == nullptr) { + if (transport == nullptr) + { return Firebolt::Error::NotConnected; } return transport->Unsubscribe(event, parameters); } template - Firebolt::Error RegisterProviderInterface(const std::string &method, const PARAMETERS ¶meters, const CALLBACK& callback, void* usercb) + Firebolt::Error RegisterProviderInterface(const std::string &method, const PARAMETERS ¶meters, const CALLBACK &callback, void *usercb) { return Firebolt::Error::General; } - Firebolt::Error UnregisterProviderInterface(const std::string &interface, const std::string &method, void* usercb) + Firebolt::Error UnregisterProviderInterface(const std::string &interface, const std::string &method, void *usercb) { return Firebolt::Error::General; } }; } - diff --git a/languages/cpp/src/shared/src/IGateway.h b/languages/cpp/src/shared/src/IGateway.h new file mode 100644 index 00000000..e5d29fbf --- /dev/null +++ b/languages/cpp/src/shared/src/IGateway.h @@ -0,0 +1,38 @@ +/* + * Copyright 2023 Comcast Cable Communications Management, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifdef UNIT_TEST + +#include "TypesPriv.h" + +namespace Firebolt +{ + namespace Authentication + { + class JsonData_Token; + } +} + +class IGateway +{ +public: + virtual ~IGateway() = default; + virtual Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, FireboltSDK::JSON::String &result) = 0; + virtual Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, Firebolt::Authentication::JsonData_Token &result) = 0; +}; +#endif From 067a1315f93586733295882ac28fc0bced2c3eb2 Mon Sep 17 00:00:00 2001 From: AdityaKasar Date: Fri, 14 Mar 2025 23:33:55 +0530 Subject: [PATCH 2/2] feat: Singleton class mock test support for all modules --- languages/cpp/src/shared/CMakeLists.txt | 1 + languages/cpp/src/shared/src/CMakeLists.txt | 6 +++ .../cpp/src/shared/src/Gateway/Gateway.h | 2 - .../shared/src/Gateway/unidi/gateway_impl.h | 41 ++++++------------- .../{IGateway.h => Properties/Properties.cpp} | 35 +++++++++------- .../src/shared/src/Properties/Properties.h | 27 ++++++++++++ languages/cpp/src/shared/src/TypesPriv.h | 1 + .../json-types/additionalProperties.cpp | 2 +- .../json-types/object-empty-property.h | 2 +- languages/cpp/templates/json-types/tuple.cpp | 2 +- languages/cpp/templates/methods/property.cpp | 4 +- 11 files changed, 72 insertions(+), 51 deletions(-) rename languages/cpp/src/shared/src/{IGateway.h => Properties/Properties.cpp} (59%) diff --git a/languages/cpp/src/shared/CMakeLists.txt b/languages/cpp/src/shared/CMakeLists.txt index df69cbad..8b7cca48 100644 --- a/languages/cpp/src/shared/CMakeLists.txt +++ b/languages/cpp/src/shared/CMakeLists.txt @@ -27,6 +27,7 @@ option(FIREBOLT_ENABLE_STATIC_LIB "Create Firebolt library as Static library" OF option(ENABLE_BIDIRECTIONAL "Enable bidirectional communication over WS" OFF) option(ENABLE_TESTS "Build openrpc native test" ON) option(ENABLE_UNIT_TESTS "Enable unit test" ON) +option(ENABLE_MOCK_TESTS "Enable mock test" OFF) option(ENABLE_COVERAGE "Enable code coverage build." ON) option(ENABLE_INTERACTIVE_APP "Enable interactive application" ON) option(FIREBOLT_PLAIN_LOG "Disable log coloring" OFF) diff --git a/languages/cpp/src/shared/src/CMakeLists.txt b/languages/cpp/src/shared/src/CMakeLists.txt index a7e2ef90..5b9b961b 100644 --- a/languages/cpp/src/shared/src/CMakeLists.txt +++ b/languages/cpp/src/shared/src/CMakeLists.txt @@ -28,6 +28,7 @@ list(APPEND SOURCES Transport/Transport.cpp Accessor/Accessor.cpp Async/Async.cpp + Properties/Properties.cpp ) if (ENABLE_BIDIRECTIONAL) @@ -38,6 +39,10 @@ endif () add_library(${TARGET} ${FIREBOLT_LIBRARY_TYPE} ${SOURCES}) +if(ENABLE_MOCK_TESTS) + add_compile_definitions(MOCK_TEST) +endif() + if(ENABLE_UNIT_TESTS) target_compile_definitions(FireboltSDK PRIVATE UNIT_TEST) endif() @@ -60,6 +65,7 @@ target_link_libraries(${TARGET} target_include_directories(${TARGET} PRIVATE $ + $ $ ) diff --git a/languages/cpp/src/shared/src/Gateway/Gateway.h b/languages/cpp/src/shared/src/Gateway/Gateway.h index 6af7e34c..5c5146d4 100644 --- a/languages/cpp/src/shared/src/Gateway/Gateway.h +++ b/languages/cpp/src/shared/src/Gateway/Gateway.h @@ -57,12 +57,10 @@ namespace FireboltSDK void TransportUpdated(Transport* transport); -#ifdef UNIT_TEST void UpdateGateway(std::unique_ptr mockGateway) { implementation = std::move(mockGateway); } -#endif template Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, RESPONSE &response) diff --git a/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h b/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h index 481f23d6..f60737dd 100644 --- a/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h +++ b/languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h @@ -25,28 +25,29 @@ #include "../common.h" +#ifndef MOCK_TEST #include "Transport/Transport.h" +#else +#include "IGatewayMock.h" +#endif #include #include -#ifdef UNIT_TEST -#include "IGateway.h" -#endif - namespace FireboltSDK { using EventCallback = std::function; -#ifndef UNIT_TEST +#ifndef MOCK_TEST class GatewayImpl -#else - class GatewayImpl : public IGateway -#endif { Transport *transport; +#else + class GatewayImpl : public IGatewayMock + { +#endif public: GatewayImpl() @@ -56,10 +57,10 @@ namespace FireboltSDK public: void TransportUpdated(Transport *transport) { - this->transport = transport; + this->transport = transport; } -#ifndef UNIT_TEST +#ifndef MOCK_TEST template Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, RESPONSE &response) { @@ -69,25 +70,6 @@ namespace FireboltSDK } return transport->Invoke(method, parameters, response); } -#else - Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, FireboltSDK::JSON::String &response) - { - if (transport == nullptr) - { - return Firebolt::Error::NotConnected; - } - return transport->Invoke(method, parameters, response); - } - - Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, Firebolt::Authentication::JsonData_Token &response) - { - if (transport == nullptr) - { - return Firebolt::Error::NotConnected; - } - return transport->Invoke(method, parameters, response); - } - #endif Firebolt::Error Response(unsigned id, const std::string &method, const JsonObject &response) @@ -126,3 +108,4 @@ namespace FireboltSDK } }; } + diff --git a/languages/cpp/src/shared/src/IGateway.h b/languages/cpp/src/shared/src/Properties/Properties.cpp similarity index 59% rename from languages/cpp/src/shared/src/IGateway.h rename to languages/cpp/src/shared/src/Properties/Properties.cpp index e5d29fbf..43c6f090 100644 --- a/languages/cpp/src/shared/src/IGateway.h +++ b/languages/cpp/src/shared/src/Properties/Properties.cpp @@ -16,23 +16,28 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifdef UNIT_TEST +#include "Properties.h" -#include "TypesPriv.h" - -namespace Firebolt +namespace FireboltSDK { - namespace Authentication + + std::unique_ptr Properties::instance = nullptr; + + Properties &Properties::Instance() { - class JsonData_Token; + if (instance == nullptr) + { + instance = std::make_unique(); + } + return *instance; } -} -class IGateway -{ -public: - virtual ~IGateway() = default; - virtual Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, FireboltSDK::JSON::String &result) = 0; - virtual Firebolt::Error Request(const std::string &method, const JsonObject ¶meters, Firebolt::Authentication::JsonData_Token &result) = 0; -}; -#endif + void Properties::Dispose() + { + ASSERT(instance != nullptr); + if (instance != nullptr) + { + instance = nullptr; + } + } +} \ No newline at end of file diff --git a/languages/cpp/src/shared/src/Properties/Properties.h b/languages/cpp/src/shared/src/Properties/Properties.h index 73dd2101..add8b279 100644 --- a/languages/cpp/src/shared/src/Properties/Properties.h +++ b/languages/cpp/src/shared/src/Properties/Properties.h @@ -22,9 +22,17 @@ #include "Event/Event.h" #include "Gateway/Gateway.h" +#ifdef MOCK_TEST +#include "IPropertiesMock.h" +#endif + namespace FireboltSDK { +#ifndef MOCK_TEST class Properties { +#else + class Properties : public IPropertiesMock{ +#endif public: Properties(const Properties&) = delete; Properties& operator= (const Properties&) = delete; @@ -32,10 +40,26 @@ namespace FireboltSDK { Properties() = default; ~Properties() = default; + static Properties &Instance(); + static void Dispose(); + + void UpdateProperties(std::unique_ptr mockProperties) + { + instance = std::move(mockProperties); + } + + private: + static std::unique_ptr instance; + Properties(std::unique_ptr instance); + public: + +#ifndef MOCK_TEST + template static Firebolt::Error Get(const string& propertyName, RESPONSETYPE& response) { + std::cout << "Properties::Get()\n"; JsonObject parameters; return Gateway::Instance().Request(propertyName, parameters, response); } @@ -43,8 +67,11 @@ namespace FireboltSDK { template static Firebolt::Error Get(const string& propertyName, const PARAMETERS& parameters, RESPONSETYPE& response) { + std::cout << "Properties::Get()\n"; return Gateway::Instance().Request(propertyName, parameters, response); } + +#endif template static Firebolt::Error Set(const string& propertyName, const PARAMETERS& parameters) diff --git a/languages/cpp/src/shared/src/TypesPriv.h b/languages/cpp/src/shared/src/TypesPriv.h index 224f0552..9646df17 100644 --- a/languages/cpp/src/shared/src/TypesPriv.h +++ b/languages/cpp/src/shared/src/TypesPriv.h @@ -19,6 +19,7 @@ #pragma once #include +#include namespace FireboltSDK { namespace JSON { diff --git a/languages/cpp/templates/json-types/additionalProperties.cpp b/languages/cpp/templates/json-types/additionalProperties.cpp index 3466cdce..8ef85554 100644 --- a/languages/cpp/templates/json-types/additionalProperties.cpp +++ b/languages/cpp/templates/json-types/additionalProperties.cpp @@ -1 +1 @@ - using ${title} = WPEFramework::Core::JSON::VariantContainer; \ No newline at end of file + class ${title} : public WPEFramework::Core::JSON::VariantContainer {}; \ No newline at end of file diff --git a/languages/cpp/templates/json-types/object-empty-property.h b/languages/cpp/templates/json-types/object-empty-property.h index 94be02b1..6cfb28bf 100644 --- a/languages/cpp/templates/json-types/object-empty-property.h +++ b/languages/cpp/templates/json-types/object-empty-property.h @@ -1 +1 @@ - using ${title} = WPEFramework::Core::JSON::VariantContainer; + class ${title} : public WPEFramework::Core::JSON::VariantContainer {}; diff --git a/languages/cpp/templates/json-types/tuple.cpp b/languages/cpp/templates/json-types/tuple.cpp index f734872b..e2b6d3e7 100644 --- a/languages/cpp/templates/json-types/tuple.cpp +++ b/languages/cpp/templates/json-types/tuple.cpp @@ -1 +1 @@ - using ${title} = WPEFramework::Core::JSON::ArrayType<${json.type}>; \ No newline at end of file + class ${title} : public WPEFramework::Core::JSON::ArrayType<${json.type}> {}; \ No newline at end of file diff --git a/languages/cpp/templates/methods/property.cpp b/languages/cpp/templates/methods/property.cpp index ce38ebd0..f28d728b 100644 --- a/languages/cpp/templates/methods/property.cpp +++ b/languages/cpp/templates/methods/property.cpp @@ -6,8 +6,8 @@ ${if.params}${method.params.serialization}${end.if.params} ${method.result.json} jsonResult; ${method.result.initialization} - ${if.params}Firebolt::Error status = FireboltSDK::Properties::Get(method, jsonParameters, jsonResult);${end.if.params} - ${if.params.empty}Firebolt::Error status = FireboltSDK::Properties::Get(method, jsonResult);${end.if.params.empty} + ${if.params}Firebolt::Error status = FireboltSDK::Properties::Instance().Get(method, jsonParameters, jsonResult);${end.if.params} + ${if.params.empty}Firebolt::Error status = FireboltSDK::Properties::Instance().Get(method, jsonResult);${end.if.params.empty} if (status == Firebolt::Error::None) { ${method.result.instantiation} }