Skip to content

Commit 067a131

Browse files
committed
feat: Singleton class mock test support for all modules
1 parent 6b936cb commit 067a131

File tree

11 files changed

+72
-51
lines changed

11 files changed

+72
-51
lines changed

languages/cpp/src/shared/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ option(FIREBOLT_ENABLE_STATIC_LIB "Create Firebolt library as Static library" OF
2727
option(ENABLE_BIDIRECTIONAL "Enable bidirectional communication over WS" OFF)
2828
option(ENABLE_TESTS "Build openrpc native test" ON)
2929
option(ENABLE_UNIT_TESTS "Enable unit test" ON)
30+
option(ENABLE_MOCK_TESTS "Enable mock test" OFF)
3031
option(ENABLE_COVERAGE "Enable code coverage build." ON)
3132
option(ENABLE_INTERACTIVE_APP "Enable interactive application" ON)
3233
option(FIREBOLT_PLAIN_LOG "Disable log coloring" OFF)

languages/cpp/src/shared/src/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ list(APPEND SOURCES
2828
Transport/Transport.cpp
2929
Accessor/Accessor.cpp
3030
Async/Async.cpp
31+
Properties/Properties.cpp
3132
)
3233

3334
if (ENABLE_BIDIRECTIONAL)
@@ -38,6 +39,10 @@ endif ()
3839

3940
add_library(${TARGET} ${FIREBOLT_LIBRARY_TYPE} ${SOURCES})
4041

42+
if(ENABLE_MOCK_TESTS)
43+
add_compile_definitions(MOCK_TEST)
44+
endif()
45+
4146
if(ENABLE_UNIT_TESTS)
4247
target_compile_definitions(FireboltSDK PRIVATE UNIT_TEST)
4348
endif()
@@ -60,6 +65,7 @@ target_link_libraries(${TARGET}
6065
target_include_directories(${TARGET}
6166
PRIVATE
6267
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
68+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../test/include>
6369
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
6470
)
6571

languages/cpp/src/shared/src/Gateway/Gateway.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,10 @@ namespace FireboltSDK
5757

5858
void TransportUpdated(Transport<WPEFramework::Core::JSON::IElement>* transport);
5959

60-
#ifdef UNIT_TEST
6160
void UpdateGateway(std::unique_ptr<GatewayImpl> mockGateway)
6261
{
6362
implementation = std::move(mockGateway);
6463
}
65-
#endif
6664

6765
template <typename RESPONSE>
6866
Firebolt::Error Request(const std::string &method, const JsonObject &parameters, RESPONSE &response)

languages/cpp/src/shared/src/Gateway/unidi/gateway_impl.h

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,29 @@
2525

2626
#include "../common.h"
2727

28+
#ifndef MOCK_TEST
2829
#include "Transport/Transport.h"
30+
#else
31+
#include "IGatewayMock.h"
32+
#endif
2933

3034
#include <string>
3135
#include <stdio.h>
3236

33-
#ifdef UNIT_TEST
34-
#include "IGateway.h"
35-
#endif
36-
3737
namespace FireboltSDK
3838
{
3939

4040
using EventCallback = std::function<void(const std::string & /* eventName */, const JsonObject & /* parameters */, Firebolt::Error /* error */)>;
4141

42-
#ifndef UNIT_TEST
42+
#ifndef MOCK_TEST
4343
class GatewayImpl
44-
#else
45-
class GatewayImpl : public IGateway
46-
#endif
4744
{
4845

4946
Transport<WPEFramework::Core::JSON::IElement> *transport;
47+
#else
48+
class GatewayImpl : public IGatewayMock
49+
{
50+
#endif
5051

5152
public:
5253
GatewayImpl()
@@ -56,10 +57,10 @@ namespace FireboltSDK
5657
public:
5758
void TransportUpdated(Transport<WPEFramework::Core::JSON::IElement> *transport)
5859
{
59-
this->transport = transport;
60+
this->transport = transport;
6061
}
6162

62-
#ifndef UNIT_TEST
63+
#ifndef MOCK_TEST
6364
template <typename RESPONSE>
6465
Firebolt::Error Request(const std::string &method, const JsonObject &parameters, RESPONSE &response)
6566
{
@@ -69,25 +70,6 @@ namespace FireboltSDK
6970
}
7071
return transport->Invoke(method, parameters, response);
7172
}
72-
#else
73-
Firebolt::Error Request(const std::string &method, const JsonObject &parameters, FireboltSDK::JSON::String &response)
74-
{
75-
if (transport == nullptr)
76-
{
77-
return Firebolt::Error::NotConnected;
78-
}
79-
return transport->Invoke(method, parameters, response);
80-
}
81-
82-
Firebolt::Error Request(const std::string &method, const JsonObject &parameters, Firebolt::Authentication::JsonData_Token &response)
83-
{
84-
if (transport == nullptr)
85-
{
86-
return Firebolt::Error::NotConnected;
87-
}
88-
return transport->Invoke(method, parameters, response);
89-
}
90-
9173
#endif
9274

9375
Firebolt::Error Response(unsigned id, const std::string &method, const JsonObject &response)
@@ -126,3 +108,4 @@ namespace FireboltSDK
126108
}
127109
};
128110
}
111+

languages/cpp/src/shared/src/IGateway.h renamed to languages/cpp/src/shared/src/Properties/Properties.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19-
#ifdef UNIT_TEST
19+
#include "Properties.h"
2020

21-
#include "TypesPriv.h"
22-
23-
namespace Firebolt
21+
namespace FireboltSDK
2422
{
25-
namespace Authentication
23+
24+
std::unique_ptr<Properties> Properties::instance = nullptr;
25+
26+
Properties &Properties::Instance()
2627
{
27-
class JsonData_Token;
28+
if (instance == nullptr)
29+
{
30+
instance = std::make_unique<Properties>();
31+
}
32+
return *instance;
2833
}
29-
}
3034

31-
class IGateway
32-
{
33-
public:
34-
virtual ~IGateway() = default;
35-
virtual Firebolt::Error Request(const std::string &method, const JsonObject &parameters, FireboltSDK::JSON::String &result) = 0;
36-
virtual Firebolt::Error Request(const std::string &method, const JsonObject &parameters, Firebolt::Authentication::JsonData_Token &result) = 0;
37-
};
38-
#endif
35+
void Properties::Dispose()
36+
{
37+
ASSERT(instance != nullptr);
38+
if (instance != nullptr)
39+
{
40+
instance = nullptr;
41+
}
42+
}
43+
}

languages/cpp/src/shared/src/Properties/Properties.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,56 @@
2222
#include "Event/Event.h"
2323
#include "Gateway/Gateway.h"
2424

25+
#ifdef MOCK_TEST
26+
#include "IPropertiesMock.h"
27+
#endif
28+
2529
namespace FireboltSDK {
2630

31+
#ifndef MOCK_TEST
2732
class Properties {
33+
#else
34+
class Properties : public IPropertiesMock{
35+
#endif
2836
public:
2937
Properties(const Properties&) = delete;
3038
Properties& operator= (const Properties&) = delete;
3139

3240
Properties() = default;
3341
~Properties() = default;
3442

43+
static Properties &Instance();
44+
static void Dispose();
45+
46+
void UpdateProperties(std::unique_ptr<Properties> mockProperties)
47+
{
48+
instance = std::move(mockProperties);
49+
}
50+
51+
private:
52+
static std::unique_ptr<Properties> instance;
53+
Properties(std::unique_ptr<Properties> instance);
54+
3555
public:
56+
57+
#ifndef MOCK_TEST
58+
3659
template <typename RESPONSETYPE>
3760
static Firebolt::Error Get(const string& propertyName, RESPONSETYPE& response)
3861
{
62+
std::cout << "Properties::Get()\n";
3963
JsonObject parameters;
4064
return Gateway::Instance().Request<RESPONSETYPE>(propertyName, parameters, response);
4165
}
4266

4367
template <typename PARAMETERS, typename RESPONSETYPE>
4468
static Firebolt::Error Get(const string& propertyName, const PARAMETERS& parameters, RESPONSETYPE& response)
4569
{
70+
std::cout << "Properties::Get()\n";
4671
return Gateway::Instance().Request(propertyName, parameters, response);
4772
}
73+
74+
#endif
4875

4976
template <typename PARAMETERS>
5077
static Firebolt::Error Set(const string& propertyName, const PARAMETERS& parameters)

languages/cpp/src/shared/src/TypesPriv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020

2121
#include <string>
22+
#include <core/JSON.h>
2223

2324
namespace FireboltSDK {
2425
namespace JSON {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
using ${title} = WPEFramework::Core::JSON::VariantContainer;
1+
class ${title} : public WPEFramework::Core::JSON::VariantContainer {};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
using ${title} = WPEFramework::Core::JSON::VariantContainer;
1+
class ${title} : public WPEFramework::Core::JSON::VariantContainer {};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
using ${title} = WPEFramework::Core::JSON::ArrayType<${json.type}>;
1+
class ${title} : public WPEFramework::Core::JSON::ArrayType<${json.type}> {};

0 commit comments

Comments
 (0)