Skip to content

Commit e0089ec

Browse files
committed
Added unit-test parse_income_data and complitedt unittest for request_params. Also fixing and renaming
1 parent 2a6f774 commit e0089ec

9 files changed

+793
-143
lines changed

src/wsjcpp_jsonrpc20.cpp

Lines changed: 260 additions & 39 deletions
Large diffs are not rendered by default.

src/wsjcpp_jsonrpc20.h

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
// ---------------------------------------------------------------------
1111
// WsjcppJsonRpc20Error
1212

13-
// must be this json:
14-
// {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null}
1513
class WsjcppJsonRpc20Error {
1614
public:
17-
WsjcppJsonRpc20Error(int nErrorCode, const std::string &sErrorMessage);
18-
int getErrorCode();
19-
std::string getErrorMessage();
15+
// WsjcppJsonRpc20Error(int nErrorCode, const std::string &sErrorMessage);
16+
WsjcppJsonRpc20Error(
17+
int nErrorCode,
18+
const std::string &sErrorMessage,
19+
const std::vector<std::pair<std::string,std::string>> &vErrorContext = {}
20+
);
21+
int getErrorCode() const;
22+
std::string getErrorMessage() const;
23+
const std::vector<std::pair<std::string,std::string>> &getErrorContext() const;
2024
nlohmann::json toJson();
2125
private:
2226
std::string m_sErrorMessage;
2327
int m_nErrorCode;
28+
std::vector<std::pair<std::string,std::string>> m_vErrorContext;
2429
};
2530

2631
// ---------------------------------------------------------------------
@@ -133,6 +138,10 @@ class FakeWebSocketClient : public WsjcppJsonRpc20WebSocketClient {
133138
return m_sLastTextMessage;
134139
}
135140

141+
void clearLastTextMessage() {
142+
m_sLastTextMessage = "";
143+
}
144+
136145
private:
137146
std::string m_sLastTextMessage;
138147
};
@@ -161,10 +170,8 @@ class WsjcppJsonRpc20WebSocketServer {
161170
std::map<void *, WsjcppJsonRpc20WebSocketClient *> m_mapClients;
162171
};
163172

164-
/*!
165-
* WsjcppJsonRpc20ParamDef - helper api for define input params and descrip it for docs.
166-
* */
167-
// {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
173+
// ---------------------------------------------------------------------
174+
// WsjcppJsonRpc20ParamDef - helper api for define input params and descrip it for docs.
168175

169176
class WsjcppJsonRpc20ParamDef {
170177
public:
@@ -243,9 +250,13 @@ class WsjcppJsonRpc20Request {
243250
bool hasInputParam(const std::string &sParamName);
244251
std::string getInputString(const std::string &sParamName, const std::string &sDefaultValue);
245252
int getInputInteger(const std::string &sParamName, int defaultValue);
253+
bool getInputBoolean(const std::string &sParamName, bool defaultValue);
254+
nlohmann::json getInputJson(const std::string &sParamName, nlohmann::json defaultValue);
246255

247256
std::string getId();
248-
std::string getMethod();
257+
std::string getMethodName();
258+
259+
bool checkInputParams(const std::vector<WsjcppJsonRpc20ParamDef>& vParamDef);
249260

250261
void done(nlohmann::json& jsonResponseResult);
251262
void fail(WsjcppJsonRpc20Error error);
@@ -256,8 +267,9 @@ class WsjcppJsonRpc20Request {
256267
WsjcppJsonRpc20WebSocketServer *m_pServer;
257268

258269
nlohmann::json m_jsonRequest;
270+
nlohmann::json m_jsonParams;
259271
std::string m_sId;
260-
std::string m_sMethod;
272+
std::string m_sMethodName;
261273
bool m_bResponseSend;
262274
};
263275

@@ -276,11 +288,8 @@ class WsjcppJsonRpc20HandlerBase {
276288
bool haveUserAccess() const;
277289
bool haveTesterAccess() const;
278290
bool haveAdminAccess() const;
279-
bool checkAccess(
280-
WsjcppJsonRpc20Request *pRequest,
281-
WsjcppJsonRpc20Error& error
282-
) const;
283-
const std::vector<WsjcppJsonRpc20ParamDef> &inputs();
291+
bool checkAccess(WsjcppJsonRpc20Request *pRequest) const;
292+
const std::vector<WsjcppJsonRpc20ParamDef> &getParamsDef();
284293

285294
virtual void handle(WsjcppJsonRpc20Request *pRequest) = 0;
286295

@@ -308,8 +317,7 @@ class WsjcppJsonRpc20HandlerBase {
308317
private:
309318
void validateParamName(const std::string &sName);
310319

311-
std::vector<WsjcppJsonRpc20ParamDef> m_vInputs; // TODO rename to m_vParams to std::map
312-
// std::map<std::string, WsjcppJsonRpc20ParamDef*> *m_vWsjcppJsonRpc20ParamDefs;
320+
std::vector<WsjcppJsonRpc20ParamDef> m_vParamsDef;
313321
std::string m_sActivatedFromVersion;
314322
std::string m_sDeprecatedFromVersion;
315323
bool m_bAccessUnauthorized;

src/wsjcpp_jsonrpc20_export_cli_python.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportAPImd() {
388388

389389
std::string pythonTemplate = "";
390390

391-
std::vector<WsjcppJsonRpc20ParamDef> vVin = pWsjcppJsonRpc20HandlerBase->inputs();
391+
std::vector<WsjcppJsonRpc20ParamDef> vVin = pWsjcppJsonRpc20HandlerBase->getParamsDef();
392392
for (int i = 0; i < vVin.size(); i++) {
393393
WsjcppJsonRpc20ParamDef inDef = vVin[i];
394394
std::string nameIn = std::string(inDef.getName());
@@ -678,7 +678,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() {
678678
for (; it != g_pWsjcppJsonRpc20HandlerList->end(); ++it) {
679679
std::string sMethod = it->first;
680680
WsjcppJsonRpc20HandlerBase* pWsjcppJsonRpc20HandlerBase = it->second;
681-
std::vector<WsjcppJsonRpc20ParamDef> vParams = pWsjcppJsonRpc20HandlerBase->inputs();
681+
std::vector<WsjcppJsonRpc20ParamDef> vParams = pWsjcppJsonRpc20HandlerBase->getParamsDef();
682682

683683
std::string sRequestParams = "";
684684
if (vParams.size() > 0) {

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_server_a
5555
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_server_api.cpp")
5656
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_parsing_request_params.h")
5757
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_parsing_request_params.cpp")
58+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_parse_income_data.h")
59+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_parse_income_data.cpp")
5860

5961
include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt)
6062

0 commit comments

Comments
 (0)