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 23360017..5c5146d4 100644 --- a/languages/cpp/src/shared/src/Gateway/Gateway.h +++ b/languages/cpp/src/shared/src/Gateway/Gateway.h @@ -57,6 +57,11 @@ namespace FireboltSDK void TransportUpdated(Transport* transport); + void UpdateGateway(std::unique_ptr mockGateway) + { + implementation = std::move(mockGateway); + } + 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..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,7 +25,11 @@ #include "../common.h" +#ifndef MOCK_TEST #include "Transport/Transport.h" +#else +#include "IGatewayMock.h" +#endif #include #include @@ -35,10 +39,15 @@ namespace FireboltSDK using EventCallback = std::function; +#ifndef MOCK_TEST class GatewayImpl { - Transport* transport; + Transport *transport; +#else + class GatewayImpl : public IGatewayMock + { +#endif public: GatewayImpl() @@ -46,19 +55,22 @@ namespace FireboltSDK } public: - void TransportUpdated(Transport* transport) + void TransportUpdated(Transport *transport) { this->transport = transport; } +#ifndef MOCK_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); } +#endif Firebolt::Error Response(unsigned id, const std::string &method, const JsonObject &response) { @@ -66,29 +78,31 @@ namespace FireboltSDK } 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/Properties/Properties.cpp b/languages/cpp/src/shared/src/Properties/Properties.cpp new file mode 100644 index 00000000..43c6f090 --- /dev/null +++ b/languages/cpp/src/shared/src/Properties/Properties.cpp @@ -0,0 +1,43 @@ +/* + * 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 + */ + +#include "Properties.h" + +namespace FireboltSDK +{ + + std::unique_ptr Properties::instance = nullptr; + + Properties &Properties::Instance() + { + if (instance == nullptr) + { + instance = std::make_unique(); + } + return *instance; + } + + 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} }