diff --git a/README.md b/README.md index 2b77b69..5bc9bb3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # wsjcpp-jsonrpc20 -[![Build Status](https://api.travis-ci.com/wsjcpp/wsjcpp-jsonrpc20.svg?branch=master)](https://travis-ci.com/wsjcpp/wsjcpp-jsonrpc20) +[![Build Status](https://api.travis-ci.com/wsjcpp/wsjcpp-jsonrpc20.svg?branch=master)](https://travis-ci.com/wsjcpp/wsjcpp-jsonrpc20) [![Total alerts](https://img.shields.io/lgtm/alerts/g/wsjcpp/wsjcpp-jsonrpc20.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/wsjcpp/wsjcpp-jsonrpc20/alerts/) [![Language grade: C/C++](https://img.shields.io/lgtm/grade/cpp/g/wsjcpp/wsjcpp-jsonrpc20.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/wsjcpp/wsjcpp-jsonrpc20/context:cpp) [![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/wsjcpp/wsjcpp-jsonrpc20.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/wsjcpp/wsjcpp-jsonrpc20/context:python) C++ Implementation for JsonRPC 2.0 (oriented on websockets) diff --git a/example-of-exported/py3/API.md b/example-of-exported/py3/API.md index 9ede0d2..01c4757 100644 --- a/example-of-exported/py3/API.md +++ b/example-of-exported/py3/API.md @@ -1,8 +1,8 @@ # SomeClient Python Library Automatically generated by wsjcpp-jsonrpc20. -* Version: v0.0.2 -* Date: Sun, 13 Sep 2020 19:26:47 GMT +* Version: v0.0.3 +* Date: Fri, 18 Sep 2020 03:27:31 GMT Example connect/disconnect: ``` diff --git a/example-of-exported/py3/libwsjcppjson20client/SomeClient.py b/example-of-exported/py3/libwsjcppjson20client/SomeClient.py index ad29fcc..a1b4f02 100644 --- a/example-of-exported/py3/libwsjcppjson20client/SomeClient.py +++ b/example-of-exported/py3/libwsjcppjson20client/SomeClient.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- ### This file was automatically generated by wsjcpp-jsonrpc20 -### Version: v0.0.2 -### Date: Sun, 13 Sep 2020 19:26:47 GMT +### Version: v0.0.3 +### Date: Fri, 18 Sep 2020 03:27:31 GMT import asyncio import websocket @@ -13,7 +13,7 @@ class SomeClient: __ws = None __url = None - __cli_version = 'v0.0.2' + __cli_version = 'v0.0.3' __loop = None __token = None __connecting = False @@ -45,7 +45,7 @@ def hasConnection(self): def getToken(self): return self.__token def setToken(self, token): - if self.__token == None: + if self.__token is None: self.__token = token else: print('ERROR: Token can be set only once') @@ -56,10 +56,10 @@ def close(self): self.__ws = None def receiveIncomingMesssages(self): - if self.__ws == None: + if self.__ws is None: return None # TODO has not connection while True: - if self.__ws == None: + if self.__ws is None: return None # TODO has not connection ready = select.select([self.__ws], [], [], 1) if ready[0]: @@ -83,7 +83,7 @@ async def __looper(self, messageId): max_time = 5*10 # 5 seconds counter_time = 0 while True: - if self.__ws == None: + if self.__ws is None: return None # TODO has not connection for inmsg in self.__incomingMesssages: if inmsg['id'] == messageId: @@ -130,7 +130,7 @@ def __sendCommand(self, req): return result def preprocessIncomeJson(self, jsonIn): - if jsonIn == None: + if jsonIn is None: return jsonIn if jsonIn['method'] == 'auth_logoff' and 'result' in jsonIn: self.__token = None @@ -157,12 +157,12 @@ def auth_login(self, login, password, client_app_name = None, client_app_version """ if not self.hasConnection(): return None reqJson = self.generateBaseCommand('auth_login') - if login == None: + if login is None: raise Exception('Parameter "login" expected (lib: SomeClient.auth_login)') if not isinstance(login, str): raise Exception('Parameter "login" expected datatype "str" (lib: SomeClient.auth_login )') reqJson['params']['login'] = login - if password == None: + if password is None: raise Exception('Parameter "password" expected (lib: SomeClient.auth_login)') if not isinstance(password, str): raise Exception('Parameter "password" expected datatype "str" (lib: SomeClient.auth_login )') @@ -207,7 +207,7 @@ def auth_token(self, token, client_app_name = None, client_app_version = None): """ if not self.hasConnection(): return None reqJson = self.generateBaseCommand('auth_token') - if token == None: + if token is None: raise Exception('Parameter "token" expected (lib: SomeClient.auth_token)') if not isinstance(token, str): raise Exception('Parameter "token" expected datatype "str" (lib: SomeClient.auth_token )') @@ -242,7 +242,7 @@ def game_create(self, uuid, cost, public, name = None, age = None, activated = N """ if not self.hasConnection(): return None reqJson = self.generateBaseCommand('game_create') - if uuid == None: + if uuid is None: raise Exception('Parameter "uuid" expected (lib: SomeClient.game_create)') if not isinstance(uuid, str): raise Exception('Parameter "uuid" expected datatype "str" (lib: SomeClient.game_create )') @@ -251,7 +251,7 @@ def game_create(self, uuid, cost, public, name = None, age = None, activated = N if not isinstance(name, str): raise Exception('Parameter "name" expected datatype "str" (lib: SomeClient.game_create )') reqJson['params']['name'] = name - if cost == None: + if cost is None: raise Exception('Parameter "cost" expected (lib: SomeClient.game_create)') if not isinstance(cost, int): raise Exception('Parameter "cost" expected datatype "int" (lib: SomeClient.game_create )') @@ -260,7 +260,7 @@ def game_create(self, uuid, cost, public, name = None, age = None, activated = N if not isinstance(age, int): raise Exception('Parameter "age" expected datatype "int" (lib: SomeClient.game_create )') reqJson['params']['age'] = age - if public == None: + if public is None: raise Exception('Parameter "public" expected (lib: SomeClient.game_create)') if not isinstance(public, bool): raise Exception('Parameter "public" expected datatype "bool" (lib: SomeClient.game_create )') diff --git a/example-of-exported/py3/libwsjcppjson20client/__init__.py b/example-of-exported/py3/libwsjcppjson20client/__init__.py index c482e4d..b589702 100644 --- a/example-of-exported/py3/libwsjcppjson20client/__init__.py +++ b/example-of-exported/py3/libwsjcppjson20client/__init__.py @@ -1 +1 @@ -from .SomeClient import * +from .SomeClient import SomeClient diff --git a/example-of-exported/py3/setup.py b/example-of-exported/py3/setup.py index 7190229..46b7c77 100644 --- a/example-of-exported/py3/setup.py +++ b/example-of-exported/py3/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='libwsjcppjson20client', - version='v0.0.2', + version='v0.0.3', packages=['libwsjcppjson20client'], install_requires=['websocket-client>=0.56.0', 'requests>=2.21.0'], keywords=['wsjcpp-jsonrpc20', 'wsjcpp', 'wsjcpp-jsonrpc20', 'example-python-client'], diff --git a/src.wsjcpp/CMakeLists.txt b/src.wsjcpp/CMakeLists.txt index f5b8447..3a217c9 100644 --- a/src.wsjcpp/CMakeLists.txt +++ b/src.wsjcpp/CMakeLists.txt @@ -1,7 +1,7 @@ # Automaticly generated by wsjcpp@v0.1.7 cmake_minimum_required(VERSION 3.0) -add_definitions(-DWSJCPP_APP_VERSION="v0.0.2") +add_definitions(-DWSJCPP_APP_VERSION="v0.0.3") add_definitions(-DWSJCPP_APP_NAME="wsjcpp-jsonrpc20") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") @@ -17,7 +17,7 @@ set (WSJCPP_SOURCES "") find_package(Threads REQUIRED) list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) -# wsjcpp-core:v0.1.7 +# wsjcpp-core:v0.2.0 list (APPEND WSJCPP_INCLUDE_DIRS "./src.wsjcpp/wsjcpp_core/") list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp") list (APPEND WSJCPP_SOURCES "./src.wsjcpp/wsjcpp_core/wsjcpp_core.h") diff --git a/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest b/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest new file mode 100644 index 0000000..30db9d5 --- /dev/null +++ b/src.wsjcpp/wsjcpp_core/generate.WsjcppUnitTest @@ -0,0 +1,103 @@ +#!/usr/bin/wsjcpp-safe-scripting + +# log_info rootdir +# log_info script_filename + +make_dir "./unit-tests.wsjcpp" +make_dir "./unit-tests.wsjcpp/src" + +var user_class_name +set_value user_class_name arg1 +normalize_class_name user_class_name +var class_name +set_value class_name "UnitTest" +concat class_name user_class_name + +var base_filename +convert_CamelCase_to_snake_case class_name base_filename +# log_info base_filename + +var filename_cpp +concat filename_cpp "./unit-tests.wsjcpp/src/" base_filename ".cpp" + +var filename_h +concat filename_h "./unit-tests.wsjcpp/src/" base_filename ".h" + +var ifndef_header +set_value ifndef_header base_filename +concat ifndef_header "_H" + +to_upper_case ifndef_header + +var content_header +concat content_header "#ifndef " ifndef_header " +#define " ifndef_header " + +#include + +class " class_name " : public WsjcppUnitTestBase { + public: + " class_name "(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; +}; + +#endif // " ifndef_header + + +var content_source +concat content_source " +#include \"" base_filename ".h\" +#include + +// --------------------------------------------------------------------- +// " class_name " + +REGISTRY_WSJCPP_UNIT_TEST(" class_name ") + +" class_name "::" class_name "() + : WsjcppUnitTestBase(\"" class_name "\") { +} + +// --------------------------------------------------------------------- + +bool " class_name "::doBeforeTest() { + // nothing + return true; +} + +// --------------------------------------------------------------------- + +void " class_name "::executeTest() { + compare(\"Not implemented\", true, false); + // TODO unit test source code here +} + +// --------------------------------------------------------------------- + +bool " class_name "::doAfterTest() { + // nothing + return true; +} + +" + +var file_source +concat file_source "src/" filename_cpp + +write_file filename_h content_header +write_file filename_cpp content_source + +log_info " +====== +Generated class: + - " class_name " +Generated files: + - " filename_h " + - " filename_cpp " +====== +" + +wsjcpp_yml_unit_test_add user_class_name filename_h +wsjcpp_yml_unit_test_add user_class_name filename_cpp \ No newline at end of file diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml b/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml index 32119e7..9539ed7 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml +++ b/src.wsjcpp/wsjcpp_core/wsjcpp.hold.yml @@ -3,7 +3,7 @@ cmake_cxx_standard: 11 cmake_minimum_required: 3.0 name: wsjcpp-core -version: v0.1.7 +version: v0.2.0 description: Basic Utils for wsjcpp issues: https://github.com/wsjcpp/wsjcpp-core/issues repositories: @@ -33,6 +33,9 @@ distribution: - source-file: "src/wsjcpp_unit_tests_main.cpp" target-file: "wsjcpp_unit_tests_main.cpp" type: "unit-tests" + - source-file: "scripts.wsjcpp/generate.WsjcppUnitTest" + target-file: "generate.WsjcppUnitTest" + type: "safe-scripting-generate" - source-file: "scripts.wsjcpp/generate.Class" target-file: "generate.Class" type: "safe-scripting-generate" @@ -79,3 +82,9 @@ unit-tests: description: "Test basic resources" - name: "ListOfDirs" description: "Check list of directories" + - name: "FilePermissions" + description: "" + - name: "StringPadding" + description: "" + - name: "DateTimeFormat" + description: "" diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp index 188fda4..c92c433 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp @@ -1,6 +1,7 @@ #include "wsjcpp_core.h" #include #include +#include #include #include #include @@ -16,10 +17,237 @@ #include #include #include -#include -#include +// #include #include #include +#include + +// --------------------------------------------------------------------- +// WsjcppFilePermissions + +WsjcppFilePermissions::WsjcppFilePermissions() { + // default permissions + m_bOwnerReadFlag = true; + m_bOwnerWriteFlag = true; + m_bOwnerExecuteFlag = false; + m_bGroupReadFlag = false; + m_bGroupWriteFlag = false; + m_bGroupExecuteFlag = false; + m_bOtherReadFlag = true; + m_bOtherWriteFlag = false; + m_bOtherExecuteFlag = false; +} + +WsjcppFilePermissions::WsjcppFilePermissions( + bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag, + bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag, + bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag +) { + m_bOwnerReadFlag = bOwnerReadFlag; + m_bOwnerWriteFlag = bOwnerWriteFlag; + m_bOwnerExecuteFlag = bOwnerExecuteFlag; + m_bGroupReadFlag = bGroupReadFlag; + m_bGroupWriteFlag = bGroupWriteFlag; + m_bGroupExecuteFlag = bGroupExecuteFlag; + m_bOtherReadFlag = bOtherReadFlag; + m_bOtherWriteFlag = bOtherWriteFlag; + m_bOtherExecuteFlag = bOtherExecuteFlag; +} + +WsjcppFilePermissions::WsjcppFilePermissions(uint16_t nFilePermission) { + + // owner + m_bOwnerReadFlag = nFilePermission & 0x0400; + m_bOwnerWriteFlag = nFilePermission & 0x0200; + m_bOwnerExecuteFlag = nFilePermission & 0x0100; + + // group + m_bGroupReadFlag = nFilePermission & 0x0040; + m_bGroupWriteFlag = nFilePermission & 0x0020; + m_bGroupExecuteFlag = nFilePermission & 0x0010; + + // for other + m_bOtherReadFlag = nFilePermission & 0x0004; + m_bOtherWriteFlag = nFilePermission & 0x0002; + m_bOtherExecuteFlag = nFilePermission & 0x0001; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerReadFlag(bool bOwnerReadFlag) { + m_bOwnerReadFlag = bOwnerReadFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOwnerReadFlag() const { + return m_bOwnerReadFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerWriteFlag(bool bOwnerWriteFlag) { + m_bOwnerWriteFlag = bOwnerWriteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOwnerWriteFlag() const { + return m_bOwnerWriteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerExecuteFlag(bool bOwnerExecuteFlag) { + m_bOwnerExecuteFlag = bOwnerExecuteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOwnerExecuteFlag() const { + return m_bOwnerExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag) { + m_bOwnerReadFlag = bOwnerReadFlag; + m_bOwnerWriteFlag = bOwnerWriteFlag; + m_bOwnerExecuteFlag = bOwnerExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupReadFlag(bool bGroupReadFlag) { + m_bGroupReadFlag = bGroupReadFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getGroupReadFlag() const { + return m_bGroupReadFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupWriteFlag(bool bGroupWriteFlag) { + m_bGroupWriteFlag = bGroupWriteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getGroupWriteFlag() const { + return m_bGroupWriteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupExecuteFlag(bool bGroupExecuteFlag) { + m_bGroupExecuteFlag = bGroupExecuteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getGroupExecuteFlag() const { + return m_bGroupExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag) { + m_bGroupReadFlag = bGroupReadFlag; + m_bGroupWriteFlag = bGroupWriteFlag; + m_bGroupExecuteFlag = bGroupExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherReadFlag(bool bOtherReadFlag) { + m_bOtherReadFlag = bOtherReadFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOtherReadFlag() const { + return m_bOtherReadFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherWriteFlag(bool bOtherWriteFlag) { + m_bOtherWriteFlag = bOtherWriteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOtherWriteFlag() const { + return m_bOtherWriteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherExecuteFlag(bool bOtherExecuteFlag) { + m_bOtherExecuteFlag = bOtherExecuteFlag; +} + +// --------------------------------------------------------------------- + +bool WsjcppFilePermissions::getOtherExecuteFlag() const { + return m_bOtherExecuteFlag; +} + +// --------------------------------------------------------------------- + +void WsjcppFilePermissions::setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag) { + m_bOtherReadFlag = bOtherReadFlag; + m_bOtherWriteFlag = bOtherWriteFlag; + m_bOtherExecuteFlag = bOtherExecuteFlag; +} + +// --------------------------------------------------------------------- + +std::string WsjcppFilePermissions::toString() const { + std::string sRet = "-"; + + // owner + sRet += m_bOwnerReadFlag ? "r" : "-"; + sRet += m_bOwnerWriteFlag ? "w" : "-"; + sRet += m_bOwnerExecuteFlag ? "x" : "-"; + + // group + sRet += m_bGroupReadFlag ? "r" : "-"; + sRet += m_bGroupWriteFlag ? "w" : "-"; + sRet += m_bGroupExecuteFlag ? "x" : "-"; + + // for other + sRet += m_bOtherReadFlag ? "r" : "-"; + sRet += m_bOtherWriteFlag ? "w" : "-"; + sRet += m_bOtherExecuteFlag ? "x" : "-"; + + return sRet; +} + +// --------------------------------------------------------------------- + +uint16_t WsjcppFilePermissions::toUInt16() const { + uint16_t nRet = 0x0; + // owner + nRet |= m_bOwnerReadFlag ? 0x0400 : 0x0; + nRet |= m_bOwnerWriteFlag ? 0x0200 : 0x0; + nRet |= m_bOwnerExecuteFlag ? 0x0100 : 0x0; + + // group + nRet += m_bGroupReadFlag ? 0x0040 : 0x0; + nRet += m_bGroupWriteFlag ? 0x0020 : 0x0; + nRet += m_bGroupExecuteFlag ? 0x0010 : 0x0; + + // for other + nRet += m_bOtherReadFlag ? 0x0004 : 0x0; + nRet += m_bOtherWriteFlag ? 0x0002 : 0x0; + nRet += m_bOtherExecuteFlag ? 0x0001 : 0x0; + return nRet; +} + // --------------------------------------------------------------------- // WsjcppCore @@ -139,28 +367,35 @@ std::string WsjcppCore::getCurrentDirectory() { // --------------------------------------------------------------------- -long WsjcppCore::currentTime_milliseconds() { +long WsjcppCore::getCurrentTimeInMilliseconds() { long nTimeStart = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); return nTimeStart; } // --------------------------------------------------------------------- -long WsjcppCore::currentTime_seconds() { +long WsjcppCore::getCurrentTimeInSeconds() { long nTimeStart = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); return nTimeStart; } // --------------------------------------------------------------------- -std::string WsjcppCore::currentTime_logformat() { - long nTimeStart = WsjcppCore::currentTime_milliseconds(); +std::string WsjcppCore::getCurrentTimeForFilename() { + long nTimeStart = WsjcppCore::getCurrentTimeInSeconds(); + return WsjcppCore::formatTimeForFilename(nTimeStart); +} + +// --------------------------------------------------------------------- + +std::string WsjcppCore::getCurrentTimeForLogFormat() { + long nTimeStart = WsjcppCore::getCurrentTimeInMilliseconds(); std::string sMilliseconds = std::to_string(int(nTimeStart % 1000)); nTimeStart = nTimeStart / 1000; std::time_t tm_ = long(nTimeStart); - // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -171,11 +406,14 @@ std::string WsjcppCore::currentTime_logformat() { // --------------------------------------------------------------------- -std::string WsjcppCore::threadId() { +std::string WsjcppCore::getThreadId() { + + static_assert(sizeof(std::thread::id)==sizeof(uint64_t),"this function only works if size of thead::id is equal to the size of uint_64"); std::thread::id this_id = std::this_thread::get_id(); + uint64_t val = *((uint64_t*) &this_id); std::stringstream stream; - stream << std::hex << this_id; - return "0x" + std::string(stream.str()); + stream << "0x" << std::setw(16) << std::setfill('0') << std::hex << val; + return std::string(stream.str()); } // --------------------------------------------------------------------- @@ -183,8 +421,8 @@ std::string WsjcppCore::threadId() { std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) { std::time_t tm_ = long(nTimeInSec); // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); - + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -194,19 +432,15 @@ std::string WsjcppCore::formatTimeForWeb(long nTimeInSec) { return std::string(buf); } -// --------------------------------------------------------------------- -std::string WsjcppCore::currentTime_forFilename() { - long nTimeStart = WsjcppCore::currentTime_seconds(); - return WsjcppCore::formatTimeForFilename(nTimeStart); -} // --------------------------------------------------------------------- std::string WsjcppCore::formatTimeForFilename(long nTimeInSec) { std::time_t tm_ = long(nTimeInSec); // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -221,7 +455,8 @@ std::string WsjcppCore::formatTimeUTC(int nTimeInSec) { // datetime std::time_t tm_ = long(nTimeInSec); // struct tm tstruct = *localtime(&tm_); - struct tm tstruct = *gmtime ( &tm_ ); + struct tm tstruct; + gmtime_r(&tm_, &tstruct); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format @@ -723,8 +958,6 @@ bool WsjcppCore::recoursiveCopyFiles(const std::string& sSourceDir, const std::s return true; } - - // --------------------------------------------------------------------- bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) { @@ -755,6 +988,93 @@ bool WsjcppCore::recoursiveRemoveDir(const std::string& sDir) { return true; } +// --------------------------------------------------------------------- + +bool WsjcppCore::setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError) { + + mode_t m; + + // owner + m |= filePermissions.getOwnerReadFlag() ? S_IRUSR : 0x0; + m |= filePermissions.getOwnerWriteFlag() ? S_IWUSR : 0x0; + m |= filePermissions.getOwnerExecuteFlag() ? S_IXUSR : 0x0; + + // group + m |= filePermissions.getGroupReadFlag() ? S_IRGRP : 0x0; + m |= filePermissions.getGroupWriteFlag() ? S_IWGRP : 0x0; + m |= filePermissions.getGroupExecuteFlag() ? S_IXGRP : 0x0; + + // for other + m |= filePermissions.getOtherReadFlag() ? S_IROTH : 0x0; + m |= filePermissions.getOtherWriteFlag() ? S_IWOTH : 0x0; + m |= filePermissions.getOtherExecuteFlag() ? S_IXOTH : 0x0; + + if (chmod(sFilePath.c_str(), m) != 0) { + sError = "Could not change permissions for: '" + sFilePath + "'"; + return false; + } + return true; +} + +// --------------------------------------------------------------------- + +bool WsjcppCore::getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError) { + if (!WsjcppCore::fileExists(sFilePath)) { + sError = "File '" + sFilePath + "' - not found"; + return false; + } + + struct stat fileStat; + if (stat(sFilePath.c_str(), &fileStat) < 0) { + sError = "Could not get info about file '" + sFilePath + "'."; + return false; + } + + mode_t m = fileStat.st_mode; + + // S_ISDIR(fileStat.st_mode)) ? "d" : "-" + + // owner + filePermissions.setOwnerReadFlag(m & S_IRUSR); + filePermissions.setOwnerWriteFlag(m & S_IWUSR); + filePermissions.setOwnerExecuteFlag(m & S_IXUSR); + + + // group + filePermissions.setGroupReadFlag(m & S_IRGRP); + filePermissions.setGroupWriteFlag(m & S_IWGRP); + filePermissions.setGroupExecuteFlag(m & S_IXGRP); + + // for other + filePermissions.setOtherReadFlag(m & S_IROTH); + filePermissions.setOtherWriteFlag(m & S_IWOTH); + filePermissions.setOtherExecuteFlag(m & S_IXOTH); + + return true; +} + +// --------------------------------------------------------------------- + +std::string WsjcppCore::doPadLeft(const std::string& sIn, char cWhat, int nLength) { + std::string sRet; + int nPadLen = nLength - sIn.length(); + for (int i = 0; i < nPadLen; i++) { + sRet += cWhat; + } + return sRet + sIn; +} + +// --------------------------------------------------------------------- + +std::string WsjcppCore::doPadRight(const std::string& sIn, char cWhat, int nLength) { + std::string sRet; + int nPadLen = nLength - sIn.length(); + for (int i = 0; i < nPadLen; i++) { + sRet += cWhat; + } + return sIn + sRet; +} + // --------------------------------------------------------------------- // WsjcppLog @@ -771,7 +1091,7 @@ WsjcppLogGlobalConf::WsjcppLogGlobalConf() { // --------------------------------------------------------------------- void WsjcppLogGlobalConf::doLogRotateUpdateFilename(bool bForce) { - long t = WsjcppCore::currentTime_seconds(); + long t = WsjcppCore::getCurrentTimeInSeconds(); long nEverySeconds = logRotationPeriodInSeconds; // rotate log if started now or if time left more then 1 day if (logStartTime == 0 || t - logStartTime > nEverySeconds || bForce) { logStartTime = t; @@ -869,7 +1189,7 @@ void WsjcppLog::add(WsjcppColorModifier &clr, const std::string &sType, const st std::lock_guard lock(WsjcppLog::g_WSJCPP_LOG_GLOBAL_CONF.logMutex); WsjcppColorModifier def(WsjcppColorCode::FG_DEFAULT); - std::string sLogMessage = WsjcppCore::currentTime_logformat() + ", " + WsjcppCore::threadId() + std::string sLogMessage = WsjcppCore::getCurrentTimeForLogFormat() + ", " + WsjcppCore::getThreadId() + " [" + sType + "] " + sTag + ": " + sMessage; std::cout << clr << sLogMessage << def << std::endl; diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_core.h b/src.wsjcpp/wsjcpp_core/wsjcpp_core.h index b990460..49142e5 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_core.h +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_core.h @@ -8,6 +8,58 @@ #include #include +class WsjcppFilePermissions { + public: + WsjcppFilePermissions(); + WsjcppFilePermissions( + bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag, + bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag, + bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag + ); + WsjcppFilePermissions(uint16_t nFilePermission); + + // owner flags + void setOwnerReadFlag(bool bOwnerReadFlag); + bool getOwnerReadFlag() const; + void setOwnerWriteFlag(bool bOwnerWriteFlag); + bool getOwnerWriteFlag() const; + void setOwnerExecuteFlag(bool bOwnerExecuteFlag); + bool getOwnerExecuteFlag() const; + void setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag); + + // group flags + void setGroupReadFlag(bool bGroupReadFlag); + bool getGroupReadFlag() const; + void setGroupWriteFlag(bool bGroupWriteFlag); + bool getGroupWriteFlag() const; + void setGroupExecuteFlag(bool bGroupExecuteFlag); + bool getGroupExecuteFlag() const; + void setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag); + + // for other flags + void setOtherReadFlag(bool bOtherReadFlag); + bool getOtherReadFlag() const; + void setOtherWriteFlag(bool bOtherWriteFlag); + bool getOtherWriteFlag() const; + void setOtherExecuteFlag(bool bOtherExecuteFlag); + bool getOtherExecuteFlag() const; + void setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag); + + std::string toString() const; + uint16_t toUInt16() const; + + private: + bool m_bOwnerReadFlag; + bool m_bOwnerWriteFlag; + bool m_bOwnerExecuteFlag; + bool m_bGroupReadFlag; + bool m_bGroupWriteFlag; + bool m_bGroupExecuteFlag; + bool m_bOtherReadFlag; + bool m_bOtherWriteFlag; + bool m_bOtherExecuteFlag; +}; + class WsjcppCore { public: static bool init( @@ -22,11 +74,11 @@ class WsjcppCore { static std::string extractFilename(const std::string &sPath); static std::string getCurrentDirectory(); - static long currentTime_milliseconds(); - static long currentTime_seconds(); - static std::string currentTime_forFilename(); - static std::string currentTime_logformat(); - static std::string threadId(); + static long getCurrentTimeInMilliseconds(); + static long getCurrentTimeInSeconds(); + static std::string getCurrentTimeForFilename(); + static std::string getCurrentTimeForLogFormat(); + static std::string getThreadId(); static std::string formatTimeForWeb(long nTimeInSec); static std::string formatTimeForFilename(long nTimeInSec); static std::string formatTimeUTC(int nTimeInSec); @@ -71,6 +123,13 @@ class WsjcppCore { static bool recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir); static bool recoursiveRemoveDir(const std::string& sDir); + + static bool setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError); + static bool getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError); + + static std::string doPadLeft(const std::string& sIn, char cWhat, int nLength); + static std::string doPadRight(const std::string& sIn, char cWhat, int nLength); + }; diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h b/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h index efba363..b741687 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_resources_manager.h @@ -9,10 +9,10 @@ class WsjcppResourceFile { public: WsjcppResourceFile(); - virtual const std::string &getFilename() = 0; - virtual const std::string &getPackAs() = 0; - virtual const int getBufferSize() = 0; - virtual const char *getBuffer() = 0; + virtual const std::string &getFilename() const = 0; + virtual const std::string &getPackAs() const = 0; + virtual int getBufferSize() const = 0; + virtual const char *getBuffer() const = 0; }; extern std::vector *g_pWsjcppResourceFiles; diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp index d675dac..1b5e281 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.cpp @@ -1,33 +1,82 @@ #include "wsjcpp_unit_tests.h" +#include + +// --------------------------------------------------------------------- +// WsjcppUnitTestBase WsjcppUnitTestBase::WsjcppUnitTestBase(const std::string &sTestName) { m_sTestName = sTestName; TAG = m_sTestName; + m_bTestResult = true; WsjcppUnitTests::addUnitTest(sTestName, this); } // --------------------------------------------------------------------- -std::string WsjcppUnitTestBase::name() { +std::string WsjcppUnitTestBase::getName() { return m_sTestName; } // --------------------------------------------------------------------- -void WsjcppUnitTestBase::compareS(bool &bTestSuccess, const std::string &sPoint, +void WsjcppUnitTestBase::ok(const std::string &sSuccessMessage) { + // print obly success message + WsjcppLog::ok(TAG, sSuccessMessage); +} + +// --------------------------------------------------------------------- + +void WsjcppUnitTestBase::fail(const std::string &sFailedMessage) { + WsjcppLog::err(TAG, sFailedMessage); + m_bTestResult = false; +} + +// --------------------------------------------------------------------- + +bool WsjcppUnitTestBase::runTest() { + WsjcppLog::info(TAG, "Start unit-test"); + WsjcppLog::info(TAG, "Do before unit-test"); + if (!doBeforeTest()) { + fail("Problem with before unit-test"); + return false; + } + WsjcppLog::info(TAG, "Execute unit-test"); + try { + executeTest(); + } catch(const std::exception& e) { + fail(e.what()); + } catch(...) { + + } + if (m_bTestResult) { + ok("Test passed."); + } else { + fail("Test failed."); + } + WsjcppLog::info(TAG, "Do after unit-test"); + if (!doAfterTest()) { + fail("Problem with after unit-test"); + } + WsjcppLog::info(TAG, "End unit-test"); + return m_bTestResult; +} + +// --------------------------------------------------------------------- + +bool WsjcppUnitTestBase::compareS(const std::string &sMark, const std::string &sValue, const std::string &sExpected) { if (sValue != sExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + sExpected + "', but got '" + sValue + "'"); - bTestSuccess = false; + fail(" {" + sMark + "} Expected '" + sExpected + "', but got '" + sValue + "'"); + return false; } + return true; } // --------------------------------------------------------------------- -bool WsjcppUnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected) { +bool WsjcppUnitTestBase::compareN(const std::string &sMark, int nValue, int nExpected) { if (nValue != nExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); - bTestSuccess = false; + fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); return false; } return true; @@ -35,10 +84,9 @@ bool WsjcppUnitTestBase::compareN(bool &bTestSuccess, const std::string &sPoint, // --------------------------------------------------------------------- -bool WsjcppUnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected) { - if (nValue != nExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); - bTestSuccess = false; +bool WsjcppUnitTestBase::compareD(const std::string &sMark, double nValue, double nExpected) { + if (abs(nValue - nExpected) > std::numeric_limits::epsilon()) { + fail(" {" + sMark + "} Expected '" + std::to_string(nExpected) + "', but got '" + std::to_string(nValue) + "'"); return false; } return true; @@ -46,11 +94,12 @@ bool WsjcppUnitTestBase::compareD(bool &bTestSuccess, const std::string &sPoint, // --------------------------------------------------------------------- -void WsjcppUnitTestBase::compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected) { +bool WsjcppUnitTestBase::compareB(const std::string &sMark, bool bValue, bool bExpected) { if (bValue != bExpected) { - WsjcppLog::err(TAG, " {" + sPoint + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'"); - bTestSuccess = false; + fail(" {" + sMark + "} Expected '" + (bExpected ? "true" : "false") + "', but got '" + (bValue ? "true" : "false") + "'"); + return false; } + return true; } // --------------------------------------------------------------------- @@ -71,7 +120,7 @@ void WsjcppUnitTests::addUnitTest(const std::string &sTestName, WsjcppUnitTestBa bool bFound = false; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* p = g_pWsjcppUnitTests->at(i); - if (p->name() == sTestName) { + if (p->getName() == sTestName) { bFound = true; } } diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h index b3213db..2f2a2b3 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests.h @@ -1,25 +1,38 @@ #ifndef WSJCPP_UNIT_TESTS_H #define WSJCPP_UNIT_TESTS_H -#include -#include #include +#include class WsjcppUnitTestBase { public: WsjcppUnitTestBase(const std::string &sTestName); - std::string name(); - virtual void init() = 0; - virtual bool run() = 0; + std::string getName(); + void ok(const std::string &sSuccessMessage); + void fail(const std::string &sFailedMessage); + bool runTest(); + + virtual bool doBeforeTest() = 0; + virtual void executeTest() = 0; + virtual bool doAfterTest() = 0; protected: std::string TAG; - void compareS(bool &bTestSuccess, const std::string &sPoint, const std::string &sValue, const std::string &sExpected); - bool compareN(bool &bTestSuccess, const std::string &sPoint, int nValue, int nExpected); - bool compareD(bool &bTestSuccess, const std::string &sPoint, double nValue, double nExpected); - void compareB(bool &bTestSuccess, const std::string &sPoint, bool bValue, bool bExpected); - + bool compareS(const std::string &sMark, const std::string &sValue, const std::string &sExpected); + bool compareN(const std::string &sMark, int nValue, int nExpected); + bool compareD(const std::string &sMark, double nValue, double nExpected); + bool compareB(const std::string &sMark, bool bValue, bool bExpected); + template bool compare(const std::string &sMark, T1 tGotValue, T2 tExpectedValue) { + if (tGotValue != tExpectedValue) { + std::stringstream ss; + ss << " {mark: " << sMark << "} Expected '" << tExpectedValue << "', but got '" << tGotValue << "'"; + fail(ss.str()); + return false; + } + return true; + } private: + bool m_bTestResult; std::string m_sTestName; }; diff --git a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp index 3d66672..c66e9d2 100644 --- a/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp +++ b/src.wsjcpp/wsjcpp_core/wsjcpp_unit_tests_main.cpp @@ -36,16 +36,17 @@ int main(int argc, char** argv) { int nSuccess = 0; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - std::string sTestName = pUnitTest->name(); - WsjcppLog::info(TAG, "Run test " + sTestName); - if (pUnitTest->run()) { - WsjcppLog::ok(sTestName, "Test passed"); + if (pUnitTest->runTest()) { nSuccess++; - } else { - WsjcppLog::err(sTestName, "Test failed"); } } - WsjcppLog::info(TAG, "Passed tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); + if (nSuccess == nAll) { + WsjcppLog::ok(TAG, "All unit-tests passed " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); + } else { + WsjcppLog::warn(TAG, "Passed unit-tests " + std::to_string(nSuccess) + " / " + std::to_string(nAll)); + WsjcppLog::err(TAG, std::to_string(nAll - nSuccess) + " unit-test(s) failed."); + } + bool bResult = nSuccess == nAll; return bResult ? 0 : -1; } else if (argc == 2) { @@ -54,7 +55,7 @@ int main(int argc, char** argv) { std::string sOutput = "\nList of unit-tests:\n"; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - sOutput += " - " + pUnitTest->name() + "\n"; + sOutput += " - " + pUnitTest->getName() + "\n"; } WsjcppLog::info(TAG, sOutput); return -1; @@ -70,15 +71,11 @@ int main(int argc, char** argv) { bool bTestFound = false; for (int i = 0; i < g_pWsjcppUnitTests->size(); i++) { WsjcppUnitTestBase* pUnitTest = g_pWsjcppUnitTests->at(i); - if (pUnitTest->name() == sArg3) { + if (pUnitTest->getName() == sArg3) { bTestFound = true; - WsjcppLog::info(TAG, "Run test " + pUnitTest->name()); - bool bResult = pUnitTest->run(); + bool bResult = pUnitTest->runTest(); if (bResult) { - WsjcppLog::ok(TAG, pUnitTest->name() + " Test passed"); nSuccess++; - } else { - WsjcppLog::err(TAG, pUnitTest->name() + " Test failed"); } } } diff --git a/src/wsjcpp_jsonrpc20.cpp b/src/wsjcpp_jsonrpc20.cpp index 6f87545..034e0b6 100644 --- a/src/wsjcpp_jsonrpc20.cpp +++ b/src/wsjcpp_jsonrpc20.cpp @@ -5,31 +5,47 @@ // --------------------------------------------------------------------- // WsjcppJsonRpc20Error - -WsjcppJsonRpc20Error::WsjcppJsonRpc20Error(int nErrorCode, const std::string &sErrorMessage) { +WsjcppJsonRpc20Error::WsjcppJsonRpc20Error( + int nErrorCode, + const std::string &sErrorMessage, + const std::vector> &vErrorContext +) { m_nErrorCode = nErrorCode; m_sErrorMessage = sErrorMessage; + m_vErrorContext = vErrorContext; } // --------------------------------------------------------------------- -int WsjcppJsonRpc20Error::getErrorCode() { +int WsjcppJsonRpc20Error::getErrorCode() const { return m_nErrorCode; } // --------------------------------------------------------------------- -std::string WsjcppJsonRpc20Error::getErrorMessage() { +std::string WsjcppJsonRpc20Error::getErrorMessage() const { return m_sErrorMessage; } // --------------------------------------------------------------------- +const std::vector> &WsjcppJsonRpc20Error::getErrorContext() const { + return m_vErrorContext; +} + +// --------------------------------------------------------------------- + nlohmann::json WsjcppJsonRpc20Error::toJson() { nlohmann::json jsonRet; jsonRet["code"] = m_nErrorCode; jsonRet["message"] = m_sErrorMessage; + if (m_vErrorContext.size() > 0) { + jsonRet["context"] = nlohmann::json(); + for (int i = 0; i < m_vErrorContext.size(); i++) { + jsonRet["context"][m_vErrorContext[i].first] = m_vErrorContext[i].second; + } + } return jsonRet; - // {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null} } // --------------------------------------------------------------------- @@ -702,7 +718,7 @@ WsjcppJsonRpc20Request::WsjcppJsonRpc20Request( m_bResponseSend = false; m_pWebSocketClient = pClient; m_pServer = pWebSocketServer; - m_sMethod = "unknown_method"; + m_sMethodName = "unknown_method"; m_sId = "unknown_id"; } @@ -713,30 +729,55 @@ bool WsjcppJsonRpc20Request::parseIncomeData(const std::string &sIncomeData) { this->fail(WsjcppJsonRpc20Error(400, "WRONG_JSON")); return false; } + m_jsonRequest = nlohmann::json::parse(sIncomeData); if (m_jsonRequest["method"].is_string()) { - m_sMethod = m_jsonRequest["method"]; + m_sMethodName = m_jsonRequest["method"]; } - if (m_jsonRequest["id"].is_string()) { m_sId = m_jsonRequest["id"]; } - if (m_sMethod == "unknown_method") { - this->fail(WsjcppJsonRpc20Error(404, "NOT_FOUND_METHOD_IN_REQUEST")); + if (m_sMethodName == "unknown_method") { + this->fail(WsjcppJsonRpc20Error(400, "NOT_FOUND_METHOD_IN_REQUEST")); return false; } if (m_sId == "unknown_id") { - this->fail(WsjcppJsonRpc20Error(404, "NOT_FOUND_ID_IN_REQUEST")); + this->fail(WsjcppJsonRpc20Error(400, "NOT_FOUND_ID_IN_REQUEST")); return false; } + if (m_jsonRequest.find("jsonrpc") == m_jsonRequest.end()) { + this->fail(WsjcppJsonRpc20Error(400, "NOT_FOUND_FIELD_JSONRPC_IN_REQUEST")); + return false; + } else { + if (!m_jsonRequest["jsonrpc"].is_string()) { + this->fail(WsjcppJsonRpc20Error(400, "FIELD_JSONRPC_EXPECTED_AS_STRING_IN_REQUEST")); + return false; + } else { + std::string sJsonRpc = m_jsonRequest["jsonrpc"]; + if (sJsonRpc != "2.0") { + this->fail(WsjcppJsonRpc20Error(400, "FIELD_JSONRPC_EXPECTED_2_DOT_0_IN_REQUEST")); + return false; + } + } + } + + if (m_jsonRequest.find("params") != m_jsonRequest.end()) { + // std::cout << m_jsonRequest.dump() << std::endl; + m_jsonParams = m_jsonRequest["params"]; + if (!m_jsonParams.is_object()) { + this->fail(WsjcppJsonRpc20Error(400, "FIELD_PARAMS_EXPECTED_AS_OBJECT_IN_REQUEST")); + return false; + } + } + // m_pWsjcppJsonRpc20UserSession = m_pServer->findUserSession(m_pClient); - WsjcppLog::info(TAG, "[WS] >>> " + m_sMethod + ":" + m_sId); + WsjcppLog::info(TAG, "[WS] >>> " + m_sMethodName + ":" + m_sId); return true; } @@ -785,7 +826,7 @@ std::string WsjcppJsonRpc20Request::getInputString(const std::string &sParamName int WsjcppJsonRpc20Request::getInputInteger(const std::string &sParamName, int nDefaultValue) { // TODO check by input defs int nRet = nDefaultValue; - if (m_jsonRequest[sParamName].is_number()) { + if (m_jsonRequest[sParamName].is_number_integer()) { nRet = m_jsonRequest[sParamName]; } return nRet; @@ -793,6 +834,28 @@ int WsjcppJsonRpc20Request::getInputInteger(const std::string &sParamName, int n // --------------------------------------------------------------------- +bool WsjcppJsonRpc20Request::getInputBoolean(const std::string &sParamName, bool defaultValue) { + // TODO check by input defs + bool bRet = defaultValue; + if (m_jsonRequest[sParamName].is_boolean()) { + bRet = m_jsonRequest[sParamName]; + } + return bRet; +} + +// --------------------------------------------------------------------- + +nlohmann::json WsjcppJsonRpc20Request::getInputJson(const std::string &sParamName, nlohmann::json defaultValue) { + // TODO check by input defs + nlohmann::json jsonRet = defaultValue; + if (m_jsonRequest[sParamName].is_structured()) { + jsonRet = m_jsonRequest[sParamName]; + } + return jsonRet; +} + +// --------------------------------------------------------------------- + WsjcppJsonRpc20UserSession *WsjcppJsonRpc20Request::getUserSession() { return m_pWebSocketClient->getUserSession(); } @@ -832,7 +895,7 @@ void WsjcppJsonRpc20Request::done(nlohmann::json& jsonResponseResult) { // {"jsonrpc": "2.0", "method": "some" "result": {"hello": 5}, "id": "9"} nlohmann::json jsonResponse; jsonResponse["jsonrpc"] = "2.0"; - jsonResponse["method"] = m_sMethod; + jsonResponse["method"] = m_sMethodName; jsonResponse["id"] = m_sId; jsonResponse["result"] = jsonResponseResult; m_pWebSocketClient->sendTextMessage(jsonResponse.dump()); @@ -849,7 +912,7 @@ void WsjcppJsonRpc20Request::fail(WsjcppJsonRpc20Error error) { // {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null} nlohmann::json jsonResponse; jsonResponse["jsonrpc"] = "2.0"; - jsonResponse["method"] = m_sMethod; + jsonResponse["method"] = m_sMethodName; jsonResponse["id"] = m_sId; jsonResponse["error"] = error.toJson(); m_pWebSocketClient->sendTextMessage(jsonResponse.dump()); @@ -865,10 +928,181 @@ std::string WsjcppJsonRpc20Request::getId() { // --------------------------------------------------------------------- -std::string WsjcppJsonRpc20Request::getMethod() { - return m_sMethod; +std::string WsjcppJsonRpc20Request::getMethodName() { + return m_sMethodName; } +// --------------------------------------------------------------------- + +bool WsjcppJsonRpc20Request::checkInputParams(const std::vector& vParamDef) { + // if (vParamDef.size() == 0) { + // return true; + // } + + for (int i = 0; i < vParamDef.size(); i++) { + WsjcppJsonRpc20ParamDef paramDef = vParamDef[i]; + + + bool bParamExists = m_jsonParams.find(paramDef.getName()) != m_jsonParams.end(); + if (paramDef.isRequired() && !bParamExists) { + this->fail(WsjcppJsonRpc20Error(400, "PARAMETER_REQUIRED", { + {"method", this->getMethodName()}, + {"param_name", paramDef.getName()} + })); + return false; + } + + // TODO validators + if (bParamExists) { + if (paramDef.isString()) { + if(!m_jsonParams[paramDef.getName()].is_string()) { + this->fail(WsjcppJsonRpc20Error(400, "DATATYPE_OF_PARAMETER_EXPECTED_STRING", { + {"method", m_sMethodName}, + {"param_name", paramDef.getName()} + })); + return false; + } + + std::string sVal = m_jsonParams[paramDef.getName()]; + std::string sError; + const std::vector vValidators = paramDef.listOfStringValidators(); + for (int i = 0; i < vValidators.size(); i++) { + if (!vValidators[i]->isValid(sVal, sError)) { + this->fail(WsjcppJsonRpc20Error(400, "INVALID_PARAM_STRING_VALUE", { + {"method", m_sMethodName}, + {"param_name", paramDef.getName()}, + {"description", sError} + })); + return false; + } + } + + } else if (paramDef.isBool() && !m_jsonParams[paramDef.getName()].is_boolean()) { + this->fail(WsjcppJsonRpc20Error(400, "DATATYPE_OF_PARAMETER_EXPECTED_BOOLEAN", { + {"method", m_sMethodName}, + {"param_name", paramDef.getName()} + })); + return false; + } else if (paramDef.isInteger()) { + + if (!m_jsonParams[paramDef.getName()].is_number_integer()) { + this->fail(WsjcppJsonRpc20Error(400, "DATATYPE_OF_PARAMETER_EXPECTED_INTEGER", { + {"method", m_sMethodName}, + {"param_name", paramDef.getName()} + })); + return false; + } + + int nVal = m_jsonParams[paramDef.getName()]; + std::string sError; + const std::vector vValidators = paramDef.listOfIntegerValidators(); + for (int i = 0; i < vValidators.size(); i++) { + if (!vValidators[i]->isValid(nVal, sError)) { + this->fail(WsjcppJsonRpc20Error(400, "INVALID_PARAM_INTEGER_VALUE", { + {"method", m_sMethodName}, + {"param_name", paramDef.getName()}, + {"description", sError} + })); + return false; + } + } + + } else if (paramDef.isJson()) { + if (!m_jsonParams[paramDef.getName()].is_structured()) { + this->fail(WsjcppJsonRpc20Error(400, "DATATYPE_OF_PARAMETER_EXPECTED_JSON_OBJECT", { + {"method", m_sMethodName}, + {"param_name", paramDef.getName()} + })); + return false; + } + nlohmann::json jsonVal = m_jsonParams[paramDef.getName()]; + std::string sError; + + const std::vector vValidators = paramDef.listOfJsonValidators(); + for (int i = 0; i < vValidators.size(); i++) { + if (!vValidators[i]->isValid(jsonVal, sError)) { + this->fail(WsjcppJsonRpc20Error(400, "INVALID_PARAM_JSON_VALUE", { + {"method", m_sMethodName}, + {"param_name", paramDef.getName()}, + {"description", sError} + })); + return false; + } + } + } + } + } + + // check extra params + for (auto it = m_jsonParams.begin(); it != m_jsonParams.end(); ++it) { + std::string sKey = it.key(); + bool bFound = false; + for (int i = 0; i < vParamDef.size(); i++) { + if (vParamDef[i].getName() == sKey) { + bFound = true; + } + } + if (!bFound) { + this->fail(WsjcppJsonRpc20Error(400, "EXTRA_PARAMETER_FOUND", { + {"method", m_sMethodName}, + {"param_name", sKey} + })); + return false; + } + } + + /*try { + + for (WsjcppJsonRpc20ParamDef inDef : pCmdHandler->inputs()) { + auto itJsonParamName = jsonMessage.find(inDef.getName()); + + + if (itJsonParamName != endJson) { + if (itJsonParamName->is_null()) { + error = WsjcppJsonRpc20Error(400, "Parameter '" + inDef.getName() + "' could not be null"); + return false; + } + + if (inDef.isInteger()) { + if (!itJsonParamName->is_number()) { + error = WsjcppJsonRpc20Error(400, "Parameter '" + inDef.getName() + "' must be integer"); + return false; + } + } + + if (inDef.isString()) { + std::string sVal = itJsonParamName->get_ref(); + std::string sError; + const std::vector vValidators = inDef.listOfStringValidators(); + for (int i = 0; i < vValidators.size(); i++) { + if (!vValidators[i]->isValid(sVal, sError)) { + error = WsjcppJsonRpc20Error(400, "Wrong param '" + inDef.getName() + "': " + sError); + return false; + } + } + } + + if (inDef.isInteger()) { + int nVal = *itJsonParamName; + std::string sError; + const std::vector vValidators = inDef.listOfIntegerValidators(); + for (int i = 0; i < vValidators.size(); i++) { + if (!vValidators[i]->isValid(nVal, sError)) { + error = WsjcppJsonRpc20Error(400, "Wrong param '" + inDef.getName() + "': " + sError); + return false; + } + } + } + } + } + return true; + } catch(std::exception const &e) { + error = WsjcppJsonRpc20Error(500, "InternalServerError"); + WsjcppLog::err(TAG, std::string("validateInputParameters, ") + e.what()); + return false; + }*/ + return true; +} // --------------------------------------------------------------------- // WsjcppJsonRpc20HandlerBase @@ -926,29 +1160,29 @@ bool WsjcppJsonRpc20HandlerBase::haveAdminAccess() const { // --------------------------------------------------------------------- // TODO write unit-test for this -bool WsjcppJsonRpc20HandlerBase::checkAccess(WsjcppJsonRpc20Request *pRequest, WsjcppJsonRpc20Error& error) const { +bool WsjcppJsonRpc20HandlerBase::checkAccess(WsjcppJsonRpc20Request *pRequest) const { WsjcppJsonRpc20UserSession *pUserSession = pRequest->getUserSession(); if (!haveUnauthorizedAccess()) { if (pUserSession == nullptr) { - error = WsjcppJsonRpc20Error(401, "NOT_AUTHORIZED_REQUEST"); + pRequest->fail(WsjcppJsonRpc20Error(401, "NOT_AUTHORIZED_REQUEST")); return false; } // access user if (pUserSession->isUser() && !haveUserAccess()) { - error = WsjcppJsonRpc20Error(403, "ACCESS_DENY_FOR_USER"); + pRequest->fail(WsjcppJsonRpc20Error(403, "ACCESS_DENY_FOR_USER")); return false; } // access tester if (pUserSession->isTester() && !haveTesterAccess()) { - error = WsjcppJsonRpc20Error(403, "ACCESS_DENY_FOR_TESTER"); + pRequest->fail(WsjcppJsonRpc20Error(403, "ACCESS_DENY_FOR_TESTER")); return false; } // access admin if (pUserSession->isAdmin() && !haveAdminAccess()) { - error = WsjcppJsonRpc20Error(403, "ACCESS_DENY_FOR_ADMIN"); + pRequest->fail(WsjcppJsonRpc20Error(403, "ACCESS_DENY_FOR_ADMIN")); return false; } } @@ -958,8 +1192,8 @@ bool WsjcppJsonRpc20HandlerBase::checkAccess(WsjcppJsonRpc20Request *pRequest, W // --------------------------------------------------------------------- -const std::vector &WsjcppJsonRpc20HandlerBase::inputs() { - return m_vInputs; +const std::vector &WsjcppJsonRpc20HandlerBase::getParamsDef() { + return m_vParamsDef; } // --------------------------------------------------------------------- @@ -1016,8 +1250,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::requireStringParam(const st this->validateParamName(sName); WsjcppJsonRpc20ParamDef pStringDef(sName, sDescription); pStringDef.string_().required(); - m_vInputs.push_back(pStringDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pStringDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1026,8 +1260,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::optionalStringParam(const s this->validateParamName(sName); WsjcppJsonRpc20ParamDef pStringDef(sName, sDescription); pStringDef.string_().optional(); - m_vInputs.push_back(pStringDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pStringDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1036,8 +1270,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::requireIntegerParam(const s this->validateParamName(sName); WsjcppJsonRpc20ParamDef pIntegerDef(sName, sDescription); pIntegerDef.integer_().required(); - m_vInputs.push_back(pIntegerDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pIntegerDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1046,8 +1280,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::optionalIntegerParam(const this->validateParamName(sName); WsjcppJsonRpc20ParamDef pIntegerDef(sName, sDescription); pIntegerDef.integer_().optional(); - m_vInputs.push_back(pIntegerDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pIntegerDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1056,8 +1290,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::requireBooleanParam(const s this->validateParamName(sName); WsjcppJsonRpc20ParamDef pBooleanDef(sName, sDescription); pBooleanDef.bool_().required(); - m_vInputs.push_back(pBooleanDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pBooleanDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1066,8 +1300,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::optionalBooleanParam(const this->validateParamName(sName); WsjcppJsonRpc20ParamDef pBooleanDef(sName, sDescription); pBooleanDef.bool_().optional(); - m_vInputs.push_back(pBooleanDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pBooleanDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1076,8 +1310,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::requireJsonParam(const std: this->validateParamName(sName); WsjcppJsonRpc20ParamDef pJsonDef(sName, sDescription); pJsonDef.json_().required(); - m_vInputs.push_back(pJsonDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pJsonDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1086,8 +1320,8 @@ WsjcppJsonRpc20ParamDef &WsjcppJsonRpc20HandlerBase::optionalJsonParam(const std this->validateParamName(sName); WsjcppJsonRpc20ParamDef pJsonDef(sName, sDescription); pJsonDef.json_().optional(); - m_vInputs.push_back(pJsonDef); - return m_vInputs[m_vInputs.size()-1]; + m_vParamsDef.push_back(pJsonDef); + return m_vParamsDef[m_vParamsDef.size()-1]; } // --------------------------------------------------------------------- @@ -1097,8 +1331,8 @@ void WsjcppJsonRpc20HandlerBase::validateParamName(const std::string &sName) { if (!std::regex_match(sName, rxName)) { WsjcppLog::throw_err(TAG, "Parameter '" + sName + "' in method '" + m_sMethodName + "' has invalid format. Expected '[a-z]+[0-9a-z_]*'"); } - for (int i = 0; i < m_vInputs.size(); i++) { - if (m_vInputs[i].getName() == sName) { + for (int i = 0; i < m_vParamsDef.size(); i++) { + if (m_vParamsDef[i].getName() == sName) { WsjcppLog::throw_err(TAG, "Parameter '" + sName + "' in method '" + m_sMethodName + "' already exists"); } } @@ -1198,7 +1432,7 @@ void WsjcppJsonRpc20HandlerServerApi::handle(WsjcppJsonRpc20Request *pRequest) { jsonHandler["access"] = jsonAccess; nlohmann::json jsonInputs = nlohmann::json::array(); - std::vector ins = pHandler->inputs(); + std::vector ins = pHandler->getParamsDef(); if (ins.size() > 0) { for (unsigned int i = 0; i < ins.size(); i++) { jsonInputs.push_back(ins[i].toJson()); diff --git a/src/wsjcpp_jsonrpc20.h b/src/wsjcpp_jsonrpc20.h index e7faefe..0116072 100644 --- a/src/wsjcpp_jsonrpc20.h +++ b/src/wsjcpp_jsonrpc20.h @@ -10,17 +10,22 @@ // --------------------------------------------------------------------- // WsjcppJsonRpc20Error -// must be this json: -// {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request."}, "id": null} class WsjcppJsonRpc20Error { public: - WsjcppJsonRpc20Error(int nErrorCode, const std::string &sErrorMessage); - int getErrorCode(); - std::string getErrorMessage(); + // WsjcppJsonRpc20Error(int nErrorCode, const std::string &sErrorMessage); + WsjcppJsonRpc20Error( + int nErrorCode, + const std::string &sErrorMessage, + const std::vector> &vErrorContext = {} + ); + int getErrorCode() const; + std::string getErrorMessage() const; + const std::vector> &getErrorContext() const; nlohmann::json toJson(); private: std::string m_sErrorMessage; int m_nErrorCode; + std::vector> m_vErrorContext; }; // --------------------------------------------------------------------- @@ -133,6 +138,10 @@ class FakeWebSocketClient : public WsjcppJsonRpc20WebSocketClient { return m_sLastTextMessage; } + void clearLastTextMessage() { + m_sLastTextMessage = ""; + } + private: std::string m_sLastTextMessage; }; @@ -161,10 +170,8 @@ class WsjcppJsonRpc20WebSocketServer { std::map m_mapClients; }; -/*! - * WsjcppJsonRpc20ParamDef - helper api for define input params and descrip it for docs. - * */ -// {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3} +// --------------------------------------------------------------------- +// WsjcppJsonRpc20ParamDef - helper api for define input params and descrip it for docs. class WsjcppJsonRpc20ParamDef { public: @@ -243,9 +250,13 @@ class WsjcppJsonRpc20Request { bool hasInputParam(const std::string &sParamName); std::string getInputString(const std::string &sParamName, const std::string &sDefaultValue); int getInputInteger(const std::string &sParamName, int defaultValue); + bool getInputBoolean(const std::string &sParamName, bool defaultValue); + nlohmann::json getInputJson(const std::string &sParamName, nlohmann::json defaultValue); std::string getId(); - std::string getMethod(); + std::string getMethodName(); + + bool checkInputParams(const std::vector& vParamDef); void done(nlohmann::json& jsonResponseResult); void fail(WsjcppJsonRpc20Error error); @@ -256,8 +267,9 @@ class WsjcppJsonRpc20Request { WsjcppJsonRpc20WebSocketServer *m_pServer; nlohmann::json m_jsonRequest; + nlohmann::json m_jsonParams; std::string m_sId; - std::string m_sMethod; + std::string m_sMethodName; bool m_bResponseSend; }; @@ -276,11 +288,8 @@ class WsjcppJsonRpc20HandlerBase { bool haveUserAccess() const; bool haveTesterAccess() const; bool haveAdminAccess() const; - bool checkAccess( - WsjcppJsonRpc20Request *pRequest, - WsjcppJsonRpc20Error& error - ) const; - const std::vector &inputs(); + bool checkAccess(WsjcppJsonRpc20Request *pRequest) const; + const std::vector &getParamsDef(); virtual void handle(WsjcppJsonRpc20Request *pRequest) = 0; @@ -308,8 +317,7 @@ class WsjcppJsonRpc20HandlerBase { private: void validateParamName(const std::string &sName); - std::vector m_vInputs; // TODO rename to m_vParams to std::map - // std::map *m_vWsjcppJsonRpc20ParamDefs; + std::vector m_vParamsDef; std::string m_sActivatedFromVersion; std::string m_sDeprecatedFromVersion; bool m_bAccessUnauthorized; diff --git a/src/wsjcpp_jsonrpc20_export_cli_python.cpp b/src/wsjcpp_jsonrpc20_export_cli_python.cpp index 491c3c4..9014912 100644 --- a/src/wsjcpp_jsonrpc20_export_cli_python.cpp +++ b/src/wsjcpp_jsonrpc20_export_cli_python.cpp @@ -34,9 +34,9 @@ class PyCodeLine{ ~PyCodeLine() { if (m_pParent == NULL) { - std::cout << "destruct root \n"; + // std::cout << "destruct root \n"; } else { - std::cout << "destruct something else [" << m_sLine << "]\n"; + // std::cout << "destruct something else [" << m_sLine << "]\n"; } } @@ -86,6 +86,7 @@ class PyCodeBuilder { ~PyCodeBuilder() { // std::cout << "destruct something else [" << m_pCurr->getLine() << "]\n"; + delete m_pCurr; } PyCodeBuilder &add(const std::string &sLine) { @@ -349,7 +350,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportAPImd() { apimd.open(sFilename); - long nSec = WsjcppCore::currentTime_seconds(); + long nSec = WsjcppCore::getCurrentTimeInSeconds(); apimd << "# " + m_sClassName + " Python Library \n\n"; apimd << "Automatically generated by " << m_sAppName << ". \n"; @@ -388,7 +389,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportAPImd() { std::string pythonTemplate = ""; - std::vector vVin = pWsjcppJsonRpc20HandlerBase->inputs(); + std::vector vVin = pWsjcppJsonRpc20HandlerBase->getParamsDef(); for (int i = 0; i < vVin.size(); i++) { WsjcppJsonRpc20ParamDef inDef = vVin[i]; std::string nameIn = std::string(inDef.getName()); @@ -429,7 +430,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportInitPy() { WsjcppLog::info(TAG, "Prepare __init__.py " + sFilename); std::ofstream __init__; __init__.open (sFilename); - __init__ << "from ." << m_sClassName << " import *\n"; + __init__ << "from ." << m_sClassName << " import " << m_sClassName << "\n"; WsjcppLog::ok(TAG, "Done: " + sFilename); return true; } @@ -476,7 +477,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() { std::ofstream clientpy; clientpy.open (sFilename); - long nSec = WsjcppCore::currentTime_seconds(); + long nSec = WsjcppCore::getCurrentTimeInSeconds(); PyCodeBuilder builder; builder @@ -534,7 +535,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() { .add("return self.__token") .end() .sub("def setToken(self, token):") - .sub("if self.__token == None:") + .sub("if self.__token is None:") .add("self.__token = token") .end() .sub("else:") @@ -551,11 +552,11 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() { .end() .add("") .sub("def receiveIncomingMesssages(self):") - .sub("if self.__ws == None:") + .sub("if self.__ws is None:") .add("return None # TODO has not connection") .end() .sub("while True:") - .sub("if self.__ws == None:") + .sub("if self.__ws is None:") .add("return None # TODO has not connection") .end() .add("ready = select.select([self.__ws], [], [], 1)") @@ -586,7 +587,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() { .add("max_time = 5*10 # 5 seconds") .add("counter_time = 0") .sub("while True:") - .sub("if self.__ws == None:") + .sub("if self.__ws is None:") .add("return None # TODO has not connection") .end() .sub("for inmsg in self.__incomingMesssages:") @@ -651,7 +652,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() { // prepare login / logoff builder .sub("def preprocessIncomeJson(self, jsonIn):") - .sub("if jsonIn == None:") + .sub("if jsonIn is None:") .add("return jsonIn") .end() ; @@ -678,7 +679,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() { for (; it != g_pWsjcppJsonRpc20HandlerList->end(); ++it) { std::string sMethod = it->first; WsjcppJsonRpc20HandlerBase* pWsjcppJsonRpc20HandlerBase = it->second; - std::vector vParams = pWsjcppJsonRpc20HandlerBase->inputs(); + std::vector vParams = pWsjcppJsonRpc20HandlerBase->getParamsDef(); std::string sRequestParams = ""; if (vParams.size() > 0) { @@ -746,7 +747,7 @@ bool WsjcppJsonRpc20ExportCliPython::exportClientPy() { std::string sParamName = paramDef.getName(); if (paramDef.isRequired()) { builder - .sub("if " + sParamName + " == None: ") + .sub("if " + sParamName + " is None: ") .add("raise Exception('Parameter \"" + sParamName + "\" expected (lib: " + m_sClassName + "." + sMethod + ")')") .end(); exportCliPythonAddCheckDataTypeOfParam(builder, paramDef, m_sClassName, sMethod); diff --git a/unit-tests.wsjcpp/CMakeLists.txt b/unit-tests.wsjcpp/CMakeLists.txt index c15af9c..f220a38 100644 --- a/unit-tests.wsjcpp/CMakeLists.txt +++ b/unit-tests.wsjcpp/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) project(unit-tests C CXX) -add_definitions(-DWSJCPP_APP_VERSION="ut-v0.0.2") +add_definitions(-DWSJCPP_APP_VERSION="ut-v0.0.3") add_definitions(-DWSJCPP_APP_NAME="unit-tests-wsjcpp-jsonrpc20") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") @@ -19,7 +19,7 @@ set (WSJCPP_SOURCES "") find_package(Threads REQUIRED) list (APPEND WSJCPP_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) -# wsjcpp-core:v0.1.7 +# wsjcpp-core:v0.2.0 list (APPEND WSJCPP_INCLUDE_DIRS "../src.wsjcpp/wsjcpp_core/") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_core/wsjcpp_core.h") @@ -38,7 +38,7 @@ list (APPEND WSJCPP_INCLUDE_DIRS "../src.wsjcpp/wsjcpp_validators/") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_validators/wsjcpp_validators.h") list (APPEND WSJCPP_SOURCES "../src.wsjcpp/wsjcpp_validators/wsjcpp_validators.cpp") -# wsjcpp-jsonrpc20:v0.0.2 +# wsjcpp-jsonrpc20:v0.0.3 list (APPEND WSJCPP_INCLUDE_DIRS "../src") list (APPEND WSJCPP_SOURCES "../src/wsjcpp_jsonrpc20.h") list (APPEND WSJCPP_SOURCES "../src/wsjcpp_jsonrpc20.cpp") @@ -53,6 +53,10 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_json_rpc20_param list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.cpp") list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_server_api.h") list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_server_api.cpp") +list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_parsing_request_params.h") +list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_parsing_request_params.cpp") +list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_parse_income_data.h") +list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_request_parse_income_data.cpp") include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.user-custom.txt) diff --git a/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.cpp b/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.cpp index 6d51bdd..7442d2f 100644 --- a/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.cpp +++ b/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.cpp @@ -1,8 +1,19 @@ -#include "unit_test_json_rpc20_param_def.h" +#include #include #include #include +// --------------------------------------------------------------------- +// UnitTestJsonRpc20ParamDef + +class UnitTestJsonRpc20ParamDef : public WsjcppUnitTestBase { + public: + UnitTestJsonRpc20ParamDef(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; +}; + REGISTRY_WSJCPP_UNIT_TEST(UnitTestJsonRpc20ParamDef) UnitTestJsonRpc20ParamDef::UnitTestJsonRpc20ParamDef() @@ -11,66 +22,66 @@ UnitTestJsonRpc20ParamDef::UnitTestJsonRpc20ParamDef() // --------------------------------------------------------------------- -void UnitTestJsonRpc20ParamDef::init() { +bool UnitTestJsonRpc20ParamDef::doBeforeTest() { // nothing + return true; } // --------------------------------------------------------------------- -bool UnitTestJsonRpc20ParamDef::run() { - bool bTestSuccess = true; +void UnitTestJsonRpc20ParamDef::executeTest() { { WsjcppJsonRpc20ParamDef paramDef; - compareS(bTestSuccess, "Empty paramDef - name", paramDef.getName(), ""); - compareS(bTestSuccess, "Empty paramDef - getType", paramDef.getType(), ""); - compareS(bTestSuccess, "Empty paramDef - getRestrict", paramDef.getRestrict(), ""); - compareS(bTestSuccess, "Empty paramDef - getDescription", paramDef.getDescription(), ""); - compareB(bTestSuccess, "Empty paramDef - isRequired", paramDef.isRequired(), false); - compareB(bTestSuccess, "Empty paramDef - isOptional", paramDef.isOptional(), false); - compareB(bTestSuccess, "Empty paramDef - isInteger", paramDef.isInteger(), false); - compareB(bTestSuccess, "Empty paramDef - isString", paramDef.isString(), false); - compareB(bTestSuccess, "Empty paramDef - isBool", paramDef.isBool(), false); - compareB(bTestSuccess, "Empty paramDef - isJson", paramDef.isJson(), false); + compareS("Empty paramDef - name", paramDef.getName(), ""); + compareS("Empty paramDef - getType", paramDef.getType(), ""); + compareS("Empty paramDef - getRestrict", paramDef.getRestrict(), ""); + compareS("Empty paramDef - getDescription", paramDef.getDescription(), ""); + compareB("Empty paramDef - isRequired", paramDef.isRequired(), false); + compareB("Empty paramDef - isOptional", paramDef.isOptional(), false); + compareB("Empty paramDef - isInteger", paramDef.isInteger(), false); + compareB("Empty paramDef - isString", paramDef.isString(), false); + compareB("Empty paramDef - isBool", paramDef.isBool(), false); + compareB("Empty paramDef - isJson", paramDef.isJson(), false); nlohmann::json js_empty; js_empty["description"] = ""; js_empty["name"] = ""; js_empty["restrict"] = ""; js_empty["type"] = ""; - compareS(bTestSuccess, "Empty paramDef - toJson", paramDef.toJson().dump(), js_empty.dump()); + compareS("Empty paramDef - toJson", paramDef.toJson().dump(), js_empty.dump()); - compareN(bTestSuccess, "Empty paramDef - listOfStringValidators", paramDef.listOfStringValidators().size(), 0); - compareN(bTestSuccess, "Empty paramDef - listOfIntegerValidators", paramDef.listOfIntegerValidators().size(), 0); - compareN(bTestSuccess, "Empty paramDef - listOfJsonValidators", paramDef.listOfJsonValidators().size(), 0); + compareN("Empty paramDef - listOfStringValidators", paramDef.listOfStringValidators().size(), 0); + compareN("Empty paramDef - listOfIntegerValidators", paramDef.listOfIntegerValidators().size(), 0); + compareN("Empty paramDef - listOfJsonValidators", paramDef.listOfJsonValidators().size(), 0); } { WsjcppJsonRpc20ParamDef paramDef2("arg2", "Some argument222222"); paramDef2.string_().required() .addValidator(new WsjcppValidatorURL()); - compareS(bTestSuccess, "paramDef2 - name", paramDef2.getName(), "arg2"); - compareS(bTestSuccess, "paramDef2 - getType", paramDef2.getType(), "string"); - compareS(bTestSuccess, "paramDef2 - getRestrict", paramDef2.getRestrict(), "required"); - compareS(bTestSuccess, "paramDef2 - getDescription", paramDef2.getDescription(), "Some argument222222"); - compareB(bTestSuccess, "paramDef2 - isRequired", paramDef2.isRequired(), true); - compareB(bTestSuccess, "paramDef2 - isOptional", paramDef2.isOptional(), false); - compareB(bTestSuccess, "paramDef2 - isInteger", paramDef2.isInteger(), false); - compareB(bTestSuccess, "paramDef2 - isString", paramDef2.isString(), true); + compareS("paramDef2 - name", paramDef2.getName(), "arg2"); + compareS("paramDef2 - getType", paramDef2.getType(), "string"); + compareS("paramDef2 - getRestrict", paramDef2.getRestrict(), "required"); + compareS("paramDef2 - getDescription", paramDef2.getDescription(), "Some argument222222"); + compareB("paramDef2 - isRequired", paramDef2.isRequired(), true); + compareB("paramDef2 - isOptional", paramDef2.isOptional(), false); + compareB("paramDef2 - isInteger", paramDef2.isInteger(), false); + compareB("paramDef2 - isString", paramDef2.isString(), true); - compareB(bTestSuccess, "paramDef2 - isJson", paramDef2.isJson(), false); - compareB(bTestSuccess, "paramDef2 - isBool", paramDef2.isBool(), false); + compareB("paramDef2 - isJson", paramDef2.isJson(), false); + compareB("paramDef2 - isBool", paramDef2.isBool(), false); nlohmann::json js2; js2["description"] = "Some argument222222"; js2["name"] = "arg2"; js2["restrict"] = "required"; js2["type"] = "string"; - compareS(bTestSuccess, "paramDef2 - toJson", paramDef2.toJson().dump(), js2.dump()); + compareS("paramDef2 - toJson", paramDef2.toJson().dump(), js2.dump()); - compareN(bTestSuccess, "paramDef2 - listOfStringValidators", paramDef2.listOfStringValidators().size(), 1); - compareN(bTestSuccess, "paramDef2 - listOfIntegerValidators", paramDef2.listOfIntegerValidators().size(), 0); - compareN(bTestSuccess, "paramDef2 - listOfJsonValidators", paramDef2.listOfJsonValidators().size(), 0); + compareN("paramDef2 - listOfStringValidators", paramDef2.listOfStringValidators().size(), 1); + compareN("paramDef2 - listOfIntegerValidators", paramDef2.listOfIntegerValidators().size(), 0); + compareN("paramDef2 - listOfJsonValidators", paramDef2.listOfJsonValidators().size(), 0); } { @@ -79,84 +90,87 @@ bool UnitTestJsonRpc20ParamDef::run() { .addValidator(new WsjcppValidatorIntegerMinValue(1)) .addValidator(new WsjcppValidatorIntegerMaxValue(1000)) ; - compareS(bTestSuccess, "paramDef3 - name", paramDef3.getName(), "arg3"); - compareS(bTestSuccess, "paramDef3 - getType", paramDef3.getType(), "integer"); - compareS(bTestSuccess, "paramDef3 - getRestrict", paramDef3.getRestrict(), "optional"); - compareS(bTestSuccess, "paramDef3 - getDescription", paramDef3.getDescription(), "Some argument3"); - compareB(bTestSuccess, "paramDef3 - isRequired", paramDef3.isRequired(), false); - compareB(bTestSuccess, "paramDef3 - isOptional", paramDef3.isOptional(), true); - compareB(bTestSuccess, "paramDef3 - isInteger", paramDef3.isInteger(), true); - compareB(bTestSuccess, "paramDef3 - isString", paramDef3.isString(), false); - compareB(bTestSuccess, "paramDef3 - isJson", paramDef3.isJson(), false); - compareB(bTestSuccess, "paramDef3 - isBool", paramDef3.isBool(), false); + compareS("paramDef3 - name", paramDef3.getName(), "arg3"); + compareS("paramDef3 - getType", paramDef3.getType(), "integer"); + compareS("paramDef3 - getRestrict", paramDef3.getRestrict(), "optional"); + compareS("paramDef3 - getDescription", paramDef3.getDescription(), "Some argument3"); + compareB("paramDef3 - isRequired", paramDef3.isRequired(), false); + compareB("paramDef3 - isOptional", paramDef3.isOptional(), true); + compareB("paramDef3 - isInteger", paramDef3.isInteger(), true); + compareB("paramDef3 - isString", paramDef3.isString(), false); + compareB("paramDef3 - isJson", paramDef3.isJson(), false); + compareB("paramDef3 - isBool", paramDef3.isBool(), false); nlohmann::json js3; js3["description"] = "Some argument3"; js3["name"] = "arg3"; js3["restrict"] = "optional"; js3["type"] = "integer"; - compareS(bTestSuccess, "paramDef3 - toJson", paramDef3.toJson().dump(), js3.dump()); + compareS("paramDef3 - toJson", paramDef3.toJson().dump(), js3.dump()); - compareN(bTestSuccess, "paramDef3 - listOfStringValidators", paramDef3.listOfStringValidators().size(), 0); - compareN(bTestSuccess, "paramDef3 - listOfIntegerValidators", paramDef3.listOfIntegerValidators().size(), 2); - compareN(bTestSuccess, "paramDef3 - listOfJsonValidators", paramDef3.listOfJsonValidators().size(), 0); + compareN("paramDef3 - listOfStringValidators", paramDef3.listOfStringValidators().size(), 0); + compareN("paramDef3 - listOfIntegerValidators", paramDef3.listOfIntegerValidators().size(), 2); + compareN("paramDef3 - listOfJsonValidators", paramDef3.listOfJsonValidators().size(), 0); } { WsjcppJsonRpc20ParamDef paramDef4("arg4_1ddd", "Some argument4"); paramDef4.json_().optional(); - compareS(bTestSuccess, "paramDef4 - name", paramDef4.getName(), "arg4_1ddd"); - compareS(bTestSuccess, "paramDef4 - getType", paramDef4.getType(), "json"); - compareS(bTestSuccess, "paramDef4 - getRestrict", paramDef4.getRestrict(), "optional"); - compareS(bTestSuccess, "paramDef4 - getDescription", paramDef4.getDescription(), "Some argument4"); - compareB(bTestSuccess, "paramDef4 - isRequired", paramDef4.isRequired(), false); - compareB(bTestSuccess, "paramDef4 - isOptional", paramDef4.isOptional(), true); - compareB(bTestSuccess, "paramDef4 - isInteger", paramDef4.isInteger(), false); - compareB(bTestSuccess, "paramDef4 - isString", paramDef4.isString(), false); - compareB(bTestSuccess, "paramDef4 - isJson", paramDef4.isJson(), true); - compareB(bTestSuccess, "paramDef4 - isBool", paramDef4.isBool(), false); + compareS("paramDef4 - name", paramDef4.getName(), "arg4_1ddd"); + compareS("paramDef4 - getType", paramDef4.getType(), "json"); + compareS("paramDef4 - getRestrict", paramDef4.getRestrict(), "optional"); + compareS("paramDef4 - getDescription", paramDef4.getDescription(), "Some argument4"); + compareB("paramDef4 - isRequired", paramDef4.isRequired(), false); + compareB("paramDef4 - isOptional", paramDef4.isOptional(), true); + compareB("paramDef4 - isInteger", paramDef4.isInteger(), false); + compareB("paramDef4 - isString", paramDef4.isString(), false); + compareB("paramDef4 - isJson", paramDef4.isJson(), true); + compareB("paramDef4 - isBool", paramDef4.isBool(), false); nlohmann::json js4; js4["description"] = "Some argument4"; js4["name"] = "arg4_1ddd"; js4["restrict"] = "optional"; js4["type"] = "json"; - compareS(bTestSuccess, "paramDef4 - toJson", paramDef4.toJson().dump(), js4.dump()); + compareS("paramDef4 - toJson", paramDef4.toJson().dump(), js4.dump()); - compareN(bTestSuccess, "paramDef4 - listOfStringValidators", paramDef4.listOfStringValidators().size(), 0); - compareN(bTestSuccess, "paramDef4 - listOfIntegerValidators", paramDef4.listOfIntegerValidators().size(), 0); - compareN(bTestSuccess, "paramDef4 - listOfJsonValidators", paramDef4.listOfJsonValidators().size(), 0); + compareN("paramDef4 - listOfStringValidators", paramDef4.listOfStringValidators().size(), 0); + compareN("paramDef4 - listOfIntegerValidators", paramDef4.listOfIntegerValidators().size(), 0); + compareN("paramDef4 - listOfJsonValidators", paramDef4.listOfJsonValidators().size(), 0); } { WsjcppJsonRpc20ParamDef paramDef5("arg5_0", "Some argument5"); paramDef5.bool_().required(); - compareS(bTestSuccess, "paramDef5 - name", paramDef5.getName(), "arg5_0"); - compareS(bTestSuccess, "paramDef5 - getType", paramDef5.getType(), "boolean"); - compareS(bTestSuccess, "paramDef5 - getRestrict", paramDef5.getRestrict(), "required"); - compareS(bTestSuccess, "paramDef5 - getDescription", paramDef5.getDescription(), "Some argument5"); - compareB(bTestSuccess, "paramDef5 - isRequired", paramDef5.isRequired(), true); - compareB(bTestSuccess, "paramDef5 - isOptional", paramDef5.isOptional(), false); - compareB(bTestSuccess, "paramDef5 - isInteger", paramDef5.isInteger(), false); - compareB(bTestSuccess, "paramDef5 - isString", paramDef5.isString(), false); - compareB(bTestSuccess, "paramDef5 - isJson", paramDef5.isJson(), false); - compareB(bTestSuccess, "paramDef5 - isBool", paramDef5.isBool(), true); + compareS("paramDef5 - name", paramDef5.getName(), "arg5_0"); + compareS("paramDef5 - getType", paramDef5.getType(), "boolean"); + compareS("paramDef5 - getRestrict", paramDef5.getRestrict(), "required"); + compareS("paramDef5 - getDescription", paramDef5.getDescription(), "Some argument5"); + compareB("paramDef5 - isRequired", paramDef5.isRequired(), true); + compareB("paramDef5 - isOptional", paramDef5.isOptional(), false); + compareB("paramDef5 - isInteger", paramDef5.isInteger(), false); + compareB("paramDef5 - isString", paramDef5.isString(), false); + compareB("paramDef5 - isJson", paramDef5.isJson(), false); + compareB("paramDef5 - isBool", paramDef5.isBool(), true); nlohmann::json js5; js5["description"] = "Some argument5"; js5["name"] = "arg5_0"; js5["restrict"] = "required"; js5["type"] = "boolean"; - compareS(bTestSuccess, "paramDef5 - toJson", paramDef5.toJson().dump(), js5.dump()); + compareS("paramDef5 - toJson", paramDef5.toJson().dump(), js5.dump()); - compareN(bTestSuccess, "paramDef5 - listOfStringValidators", paramDef5.listOfStringValidators().size(), 0); - compareN(bTestSuccess, "paramDef5 - listOfIntegerValidators", paramDef5.listOfIntegerValidators().size(), 0); - compareN(bTestSuccess, "paramDef5 - listOfJsonValidators", paramDef5.listOfJsonValidators().size(), 0); + compareN("paramDef5 - listOfStringValidators", paramDef5.listOfStringValidators().size(), 0); + compareN("paramDef5 - listOfIntegerValidators", paramDef5.listOfIntegerValidators().size(), 0); + compareN("paramDef5 - listOfJsonValidators", paramDef5.listOfJsonValidators().size(), 0); } - - - return bTestSuccess; } +// --------------------------------------------------------------------- + +bool UnitTestJsonRpc20ParamDef::doAfterTest() { + // nothing + return true; +} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.h b/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.h index 3947e90..397e005 100644 --- a/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.h +++ b/unit-tests.wsjcpp/src/unit_test_json_rpc20_param_def.h @@ -1,15 +1,5 @@ #ifndef UNIT_TEST_JSON_RPC20_PARAM_DEF_H #define UNIT_TEST_JSON_RPC20_PARAM_DEF_H -#include - -// Description: TODO -class UnitTestJsonRpc20ParamDef : public WsjcppUnitTestBase { - public: - UnitTestJsonRpc20ParamDef(); - virtual void init(); - virtual bool run(); -}; - #endif // UNIT_TEST_JSON_RPC20_PARAM_DEF_H diff --git a/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.cpp b/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.cpp index afe3bcd..f34d05c 100644 --- a/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.cpp +++ b/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.cpp @@ -3,6 +3,9 @@ #include #include +// --------------------------------------------------------------------- +// UnitTestJsonRpc20UserSession + REGISTRY_WSJCPP_UNIT_TEST(UnitTestJsonRpc20UserSession) UnitTestJsonRpc20UserSession::UnitTestJsonRpc20UserSession() @@ -11,70 +14,70 @@ UnitTestJsonRpc20UserSession::UnitTestJsonRpc20UserSession() // --------------------------------------------------------------------- -void UnitTestJsonRpc20UserSession::init() { +bool UnitTestJsonRpc20UserSession::doBeforeTest() { // nothing + return true; } // --------------------------------------------------------------------- -bool UnitTestJsonRpc20UserSession::run() { - bool bTestSuccess = true; +void UnitTestJsonRpc20UserSession::executeTest() { WsjcppJsonRpc20UserSession session0; - compareB(bTestSuccess, "is admin (0)", session0.isAdmin(), false); - compareB(bTestSuccess, "is user (0)", session0.isUser(), false); - compareB(bTestSuccess, "is tester (0)", session0.isTester(), false); - compareB(bTestSuccess, "has role (0)", session0.hasRole(), false); + compareB("is admin (0)", session0.isAdmin(), false); + compareB("is user (0)", session0.isUser(), false); + compareB("is tester (0)", session0.isTester(), false); + compareB("has role (0)", session0.hasRole(), false); - compareN(bTestSuccess, "User Id (0)", session0.getUserId(), 0); + compareN("User Id (0)", session0.getUserId(), 0); - compareS(bTestSuccess, "User Uuid (0)", session0.getUserUuid(), ""); - compareS(bTestSuccess, "User Role (0)", session0.getUserRole(), ""); - compareS(bTestSuccess, "User Name (0)", session0.getUserName(), ""); - compareS(bTestSuccess, "User Email (0)", session0.getUserEmail(), ""); + compareS("User Uuid (0)", session0.getUserUuid(), ""); + compareS("User Role (0)", session0.getUserRole(), ""); + compareS("User Name (0)", session0.getUserName(), ""); + compareS("User Email (0)", session0.getUserEmail(), ""); - compareS(bTestSuccess, "Session Uuid (0)", session0.getSessionUuid(), ""); - compareN(bTestSuccess, "Session Created (0)", session0.getSessionCreated(), 0); - compareN(bTestSuccess, "Session Updated (0)", session0.getSessionUpdated(), 0); - compareN(bTestSuccess, "Session Expire At (0)", session0.getSessionExpireAt(), 0); - compareS(bTestSuccess, "Session Custom (0)", session0.getSessionCustom().dump(), "null"); + compareS("Session Uuid (0)", session0.getSessionUuid(), ""); + compareN("Session Created (0)", session0.getSessionCreated(), 0); + compareN("Session Updated (0)", session0.getSessionUpdated(), 0); + compareN("Session Expire At (0)", session0.getSessionExpireAt(), 0); + compareS("Session Custom (0)", session0.getSessionCustom().dump(), "null"); // test fill role session0.setUserRole("admin"); - compareB(bTestSuccess, "is admin (1)", session0.isAdmin(), true); - compareB(bTestSuccess, "is user (1)", session0.isUser(), false); - compareB(bTestSuccess, "is tester (1)", session0.isTester(), false); - compareB(bTestSuccess, "has role (1)", session0.hasRole(), true); - compareS(bTestSuccess, "User Role (1)", session0.getUserRole(), "admin"); + compareB("is admin (1)", session0.isAdmin(), true); + compareB("is user (1)", session0.isUser(), false); + compareB("is tester (1)", session0.isTester(), false); + compareB("has role (1)", session0.hasRole(), true); + compareS("User Role (1)", session0.getUserRole(), "admin"); session0.setUserRole("user"); - compareB(bTestSuccess, "is admin (2)", session0.isAdmin(), false); - compareB(bTestSuccess, "is user (2)", session0.isUser(), true); - compareB(bTestSuccess, "is tester (2)", session0.isTester(), false); - compareB(bTestSuccess, "has role (2)", session0.hasRole(), true); - compareS(bTestSuccess, "User Role (2)", session0.getUserRole(), "user"); + compareB("is admin (2)", session0.isAdmin(), false); + compareB("is user (2)", session0.isUser(), true); + compareB("is tester (2)", session0.isTester(), false); + compareB("has role (2)", session0.hasRole(), true); + compareS("User Role (2)", session0.getUserRole(), "user"); session0.setUserRole("tester"); - compareB(bTestSuccess, "is admin (3)", session0.isAdmin(), false); - compareB(bTestSuccess, "is user (3)", session0.isUser(), false); - compareB(bTestSuccess, "is tester (3)", session0.isTester(), true); - compareB(bTestSuccess, "has role (3)", session0.hasRole(), true); - compareS(bTestSuccess, "User Role (3)", session0.getUserRole(), "tester"); + compareB("is admin (3)", session0.isAdmin(), false); + compareB("is user (3)", session0.isUser(), false); + compareB("is tester (3)", session0.isTester(), true); + compareB("has role (3)", session0.hasRole(), true); + compareS("User Role (3)", session0.getUserRole(), "tester"); std::string sUserRole = "custom0"; session0.setUserRole(sUserRole); - compareB(bTestSuccess, "is admin (4)", session0.isAdmin(), false); - compareB(bTestSuccess, "is user (4)", session0.isUser(), false); - compareB(bTestSuccess, "is tester (4)", session0.isTester(), false); - compareB(bTestSuccess, "has role (4)", session0.hasRole(), true); - compareS(bTestSuccess, "User Role (4)", session0.getUserRole(), sUserRole); + compareB("is admin (4)", session0.isAdmin(), false); + compareB("is user (4)", session0.isUser(), false); + compareB("is tester (4)", session0.isTester(), false); + compareB("has role (4)", session0.hasRole(), true); + compareS("User Role (4)", session0.getUserRole(), sUserRole); int nUserId = 1000; std::string sUserUuid = "b5bd63ce-0405-4ef5-8473-552f2729e7e9"; std::string sUserName = "user some name"; std::string sUserEmail = "user@user.usr"; std::string sSessionUuid = "580ec846-4d3f-47b1-bb05-a54f1dd7fdbe"; - long nCreated = WsjcppCore::currentTime_milliseconds(); + long nCreated = WsjcppCore::getCurrentTimeInMilliseconds(); long nUpdated = nCreated + 3000; long nExpiredAt = nCreated + 24*60*60*1000; nlohmann::json jsonCustom; @@ -91,46 +94,46 @@ bool UnitTestJsonRpc20UserSession::run() { session0.setSessionExpireAt(nExpiredAt); session0.setSessionCustom(jsonCustom); - compareN(bTestSuccess, "User Id (5)", session0.getUserId(), nUserId); - compareS(bTestSuccess, "User Uuid (6)", session0.getUserUuid(), sUserUuid); - compareS(bTestSuccess, "User Name (7)", session0.getUserName(), sUserName); - compareS(bTestSuccess, "User Email (8)", session0.getUserEmail(), sUserEmail); - compareS(bTestSuccess, "Session Uuid (9)", session0.getSessionUuid(), sSessionUuid); - compareN(bTestSuccess, "Session Created (10)", session0.getSessionCreated(), nCreated); - compareN(bTestSuccess, "Session Updated (11)", session0.getSessionUpdated(), nUpdated); - compareN(bTestSuccess, "Session Expire At (12)", session0.getSessionExpireAt(), nExpiredAt); - compareS(bTestSuccess, "Session Custom (13)", session0.getSessionCustom().dump(), sJsonCustom); + compareN("User Id (5)", session0.getUserId(), nUserId); + compareS("User Uuid (6)", session0.getUserUuid(), sUserUuid); + compareS("User Name (7)", session0.getUserName(), sUserName); + compareS("User Email (8)", session0.getUserEmail(), sUserEmail); + compareS("Session Uuid (9)", session0.getSessionUuid(), sSessionUuid); + compareN("Session Created (10)", session0.getSessionCreated(), nCreated); + compareN("Session Updated (11)", session0.getSessionUpdated(), nUpdated); + compareN("Session Expire At (12)", session0.getSessionExpireAt(), nExpiredAt); + compareS("Session Custom (13)", session0.getSessionCustom().dump(), sJsonCustom); // test full json nlohmann::json jsonSession = session0.toJson(); WsjcppLog::info(TAG, jsonSession.dump()); - compareS(bTestSuccess, "toJson Session uuid (14)", jsonSession["uuid"], sSessionUuid); - compareN(bTestSuccess, "toJson Session created (14)", jsonSession["created"], nCreated); - compareN(bTestSuccess, "toJson Session updated (14)", jsonSession["updated"], nUpdated); - compareN(bTestSuccess, "toJson Session expire_at (14)", jsonSession["expire_at"], nExpiredAt); + compareS("toJson Session uuid (14)", jsonSession["uuid"], sSessionUuid); + compareN("toJson Session created (14)", jsonSession["created"], nCreated); + compareN("toJson Session updated (14)", jsonSession["updated"], nUpdated); + compareN("toJson Session expire_at (14)", jsonSession["expire_at"], nExpiredAt); - compareN(bTestSuccess, "toJson Session user.id (14)", jsonSession["user"]["id"], nUserId); - compareS(bTestSuccess, "toJson Session user.id (14)", jsonSession["user"]["uuid"], sUserUuid); - compareS(bTestSuccess, "toJson Session user.id (14)", jsonSession["user"]["name"], sUserName); - compareS(bTestSuccess, "toJson Session user.id (14)", jsonSession["user"]["role"], sUserRole); - compareS(bTestSuccess, "toJson Session user.id (14)", jsonSession["user"]["email"], sUserEmail); + compareN("toJson Session user.id (14)", jsonSession["user"]["id"], nUserId); + compareS("toJson Session user.id (14)", jsonSession["user"]["uuid"], sUserUuid); + compareS("toJson Session user.id (14)", jsonSession["user"]["name"], sUserName); + compareS("toJson Session user.id (14)", jsonSession["user"]["role"], sUserRole); + compareS("toJson Session user.id (14)", jsonSession["user"]["email"], sUserEmail); WsjcppJsonRpc20UserSession session1(jsonSession); - compareB(bTestSuccess, "session1 is admin (0)", session1.isAdmin(), false); - compareB(bTestSuccess, "session1 is user (0)", session1.isUser(), false); - compareB(bTestSuccess, "session1 is tester (0)", session1.isTester(), false); - compareB(bTestSuccess, "session1 has role (0)", session1.hasRole(), true); - compareN(bTestSuccess, "session1 User Id (0)", session1.getUserId(), nUserId); - compareS(bTestSuccess, "session1 User Uuid (0)", session1.getUserUuid(), sUserUuid); - compareS(bTestSuccess, "session1 User Role (0)", session1.getUserRole(), sUserRole); - compareS(bTestSuccess, "session1 User Name (0)", session1.getUserName(), sUserName); - compareS(bTestSuccess, "session1 User Email (0)", session1.getUserEmail(), sUserEmail); - compareS(bTestSuccess, "session1 Session Uuid (0)", session1.getSessionUuid(), sSessionUuid); - compareN(bTestSuccess, "session1 Session Created (0)", session1.getSessionCreated(), nCreated); - compareN(bTestSuccess, "session1 Session Updated (0)", session1.getSessionUpdated(), nUpdated); - compareN(bTestSuccess, "session1 Session Expire At (0)", session1.getSessionExpireAt(), nExpiredAt); - compareS(bTestSuccess, "session1 Session Custom (0)", session1.getSessionCustom().dump(), sJsonCustom); + compareB("session1 is admin (0)", session1.isAdmin(), false); + compareB("session1 is user (0)", session1.isUser(), false); + compareB("session1 is tester (0)", session1.isTester(), false); + compareB("session1 has role (0)", session1.hasRole(), true); + compareN("session1 User Id (0)", session1.getUserId(), nUserId); + compareS("session1 User Uuid (0)", session1.getUserUuid(), sUserUuid); + compareS("session1 User Role (0)", session1.getUserRole(), sUserRole); + compareS("session1 User Name (0)", session1.getUserName(), sUserName); + compareS("session1 User Email (0)", session1.getUserEmail(), sUserEmail); + compareS("session1 Session Uuid (0)", session1.getSessionUuid(), sSessionUuid); + compareN("session1 Session Created (0)", session1.getSessionCreated(), nCreated); + compareN("session1 Session Updated (0)", session1.getSessionUpdated(), nUpdated); + compareN("session1 Session Expire At (0)", session1.getSessionExpireAt(), nExpiredAt); + compareS("session1 Session Custom (0)", session1.getSessionCustom().dump(), sJsonCustom); // check rewrite data WsjcppJsonRpc20UserSession session2; @@ -147,21 +150,25 @@ bool UnitTestJsonRpc20UserSession::run() { nlohmann::json jsonSessionEmpty; session2.fillFrom(jsonSessionEmpty); - compareB(bTestSuccess, "session2: is admin (0)", session2.isAdmin(), false); - compareB(bTestSuccess, "session2: is user (0)", session2.isUser(), false); - compareB(bTestSuccess, "session2: is tester (0)", session2.isTester(), false); - compareB(bTestSuccess, "session2: has role (0)", session2.hasRole(), false); - compareN(bTestSuccess, "session2: User Id (0)", session2.getUserId(), 0); - compareS(bTestSuccess, "session2: User Uuid (0)", session2.getUserUuid(), ""); - compareS(bTestSuccess, "session2: User Role (0)", session2.getUserRole(), ""); - compareS(bTestSuccess, "session2: User Name (0)", session2.getUserName(), ""); - compareS(bTestSuccess, "session2: User Email (0)", session2.getUserEmail(), ""); - compareS(bTestSuccess, "session2: Session Uuid (0)", session2.getSessionUuid(), ""); - compareN(bTestSuccess, "session2: Session Created (0)", session2.getSessionCreated(), 0); - compareN(bTestSuccess, "session2: Session Updated (0)", session2.getSessionUpdated(), 0); - compareN(bTestSuccess, "session2: Session Expire At (0)", session2.getSessionExpireAt(), 0); - compareS(bTestSuccess, "session2: Session Custom (0)", session2.getSessionCustom().dump(), "null"); - - return bTestSuccess; + compare("session2: is admin (0)", session2.isAdmin(), false); + compare("session2: is user (0)", session2.isUser(), false); + compare("session2: is tester (0)", session2.isTester(), false); + compare("session2: has role (0)", session2.hasRole(), false); + compare("session2: User Id (0)", session2.getUserId(), 0); + compare("session2: User Uuid (0)", session2.getUserUuid(), ""); + compare("session2: User Role (0)", session2.getUserRole(), ""); + compare("session2: User Name (0)", session2.getUserName(), ""); + compare("session2: User Email (0)", session2.getUserEmail(), ""); + compare("session2: Session Uuid (0)", session2.getSessionUuid(), ""); + compare("session2: Session Created (0)", session2.getSessionCreated(), 0); + compare("session2: Session Updated (0)", session2.getSessionUpdated(), 0); + compare("session2: Session Expire At (0)", session2.getSessionExpireAt(), 0); + compare("session2: Session Custom (0)", session2.getSessionCustom().dump(), "null"); } +// --------------------------------------------------------------------- + +bool UnitTestJsonRpc20UserSession::doAfterTest() { + // nothing + return true; +} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.h b/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.h index 67f3260..1f21535 100644 --- a/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.h +++ b/unit-tests.wsjcpp/src/unit_test_json_rpc20_user_session.h @@ -3,12 +3,12 @@ #include -// Description: TODO class UnitTestJsonRpc20UserSession : public WsjcppUnitTestBase { public: UnitTestJsonRpc20UserSession(); - virtual void init(); - virtual bool run(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; }; #endif // UNIT_TEST_JSON_RPC20_USER_SESSION_H diff --git a/unit-tests.wsjcpp/src/unit_test_parsing_request_params.cpp b/unit-tests.wsjcpp/src/unit_test_parsing_request_params.cpp new file mode 100644 index 0000000..fb753f3 --- /dev/null +++ b/unit-tests.wsjcpp/src/unit_test_parsing_request_params.cpp @@ -0,0 +1,438 @@ +#include "unit_test_parsing_request_params.h" +#include +#include +#include + +// --------------------------------------------------------------------- +// CustomObjValidator + +class CustomObjValidator : public WsjcppValidatorJsonBase { + + public: + CustomObjValidator() : WsjcppValidatorJsonBase("custom_json") { + // nothing + }; + + virtual bool isValid(const nlohmann::json &jsonValue, std::string &sError) override { + if (jsonValue.find("subname") == jsonValue.end()) { + sError = "Expected subname in struct"; + return false; + } + return true; + }; +}; + +// --------------------------------------------------------------------- +// WsjcppJsonRpc20HandlerGameCreate + +class WsjcppJsonRpc20HandlerGameCreate : public WsjcppJsonRpc20HandlerBase { + public: + WsjcppJsonRpc20HandlerGameCreate(); + virtual void handle(WsjcppJsonRpc20Request *pRequest) override; +}; + +// --------------------------------------------------------------------- + +REGISTRY_WSJCPP_JSONRPC20_HANDLER(WsjcppJsonRpc20HandlerGameCreate) + +WsjcppJsonRpc20HandlerGameCreate::WsjcppJsonRpc20HandlerGameCreate() +: WsjcppJsonRpc20HandlerBase("game_create", "Some example of description") { + TAG = "WsjcppJsonRpc20HandlerGameCreate"; + setAccessUnauthorized(false); + setAccessUser(true); + setAccessTester(true); + setAccessAdmin(true); + setActivatedFromVersion("v0.0.2"); + // void setDeprecatedFromVersion(""); + + // description of input params + requireStringParam("uuid1", "object uuid") + .addValidator(new WsjcppValidatorUUID()); + optionalStringParam("name", "Name of object") + .addValidator(new WsjcppValidatorStringLength(3,10)); + + requireIntegerParam("cost", "Name of object") + .addValidator(new WsjcppValidatorIntegerMinValue(3)) + .addValidator(new WsjcppValidatorIntegerMaxValue(1000)); + optionalIntegerParam("age", "Name of object") + .addValidator(new WsjcppValidatorIntegerMinValue(0)); + + requireBooleanParam("public", "True if object is public"); + optionalBooleanParam("activated", "If object can handle"); + + optionalJsonParam("custom", "Some custom json") + .addValidator(new CustomObjValidator()); +} + +// --------------------------------------------------------------------- + +void WsjcppJsonRpc20HandlerGameCreate::handle(WsjcppJsonRpc20Request *pRequest) { + WsjcppLog::err(TAG, "Not implemented"); + // TODO + pRequest->fail(WsjcppJsonRpc20Error(501, "NOT_IMPLEMENTED")); +} + +// --------------------------------------------------------------------- +// UnitTestParsingRequestParams + +REGISTRY_WSJCPP_UNIT_TEST(UnitTestParsingRequestParams) + +UnitTestParsingRequestParams::UnitTestParsingRequestParams() + : WsjcppUnitTestBase("UnitTestParsingRequestParams") { +} + +// --------------------------------------------------------------------- + +bool UnitTestParsingRequestParams::doBeforeTest() { + // nothing + return true; +} + +// --------------------------------------------------------------------- + +void UnitTestParsingRequestParams::executeTest() { + + WsjcppJsonRpc20WebSocketServer *pWebSocketServer = new WsjcppJsonRpc20WebSocketServer(); + FakeWebSocketClient *pFakeClient = new FakeWebSocketClient(); + WsjcppJsonRpc20WebSocketClient *pClient = pFakeClient; + WsjcppJsonRpc20HandlerBase *pHandlergameCreate = WsjcppJsonRpc20::findJsonRpc20Handler("game_create"); + const std::vector &vParamsDef = pHandlergameCreate->getParamsDef(); + + // checkInputParams - paramer required + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams0 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams0 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams0 error.message", sErrorMessage, "PARAMETER_REQUIRED"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams0 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams0 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams0 error.context.param_name", sErrorContextParamName, "uuid1"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams0 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams0 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - paramer datatype string + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = 1; + + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams1 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams1 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams1 error.message", sErrorMessage, "DATATYPE_OF_PARAMETER_EXPECTED_STRING"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams1 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams1 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams1 error.context.param_name", sErrorContextParamName, "uuid1"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams1 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams1 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - parameter data type int + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = "9eb658c0-c217-4386-8688-f86c72e077c5"; + jsonRequest["params"]["cost"] = "111"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams2 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams2 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams2 error.message", sErrorMessage, "DATATYPE_OF_PARAMETER_EXPECTED_INTEGER"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams2 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams2 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams2 error.context.param_name", sErrorContextParamName, "cost"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams2 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams2 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - parameter data type boolean + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = "9eb658c0-c217-4386-8688-f86c72e077c5"; + jsonRequest["params"]["name"] = "1111"; + jsonRequest["params"]["cost"] = 3; + jsonRequest["params"]["age"] = 1; + jsonRequest["params"]["public"] = "true"; + jsonRequest["params"]["activated"] = false; + jsonRequest["params"]["custom"] = nlohmann::json::object(); + + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams3 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams3 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams3 error.message", sErrorMessage, "DATATYPE_OF_PARAMETER_EXPECTED_BOOLEAN"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams3 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams3 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams3 error.context.param_name", sErrorContextParamName, "public"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams3 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams3 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - parameter data type json + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = "9eb658c0-c217-4386-8688-f86c72e077c5"; + jsonRequest["params"]["name"] = "1111"; + jsonRequest["params"]["cost"] = 3; + jsonRequest["params"]["age"] = 1; + jsonRequest["params"]["public"] = true; + jsonRequest["params"]["activated"] = false; + jsonRequest["params"]["custom"] = "some"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams4 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams4 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams4 error.message", sErrorMessage, "DATATYPE_OF_PARAMETER_EXPECTED_JSON_OBJECT"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams4 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams4 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams4 error.context.param_name", sErrorContextParamName, "custom"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams4 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams4 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - extra field + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = "9eb658c0-c217-4386-8688-f86c72e077c5"; + jsonRequest["params"]["name"] = "1111"; + jsonRequest["params"]["cost"] = 3; + jsonRequest["params"]["age"] = 1; + jsonRequest["params"]["public"] = true; + jsonRequest["params"]["activated"] = false; + jsonRequest["params"]["custom"] = nlohmann::json::object(); + jsonRequest["params"]["custom"]["subname"] = "111"; + jsonRequest["params"]["ollala"] = "some"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams5 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams5 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams5 error.message", sErrorMessage, "EXTRA_PARAMETER_FOUND"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams5 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams5 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams5 error.context.param_name", sErrorContextParamName, "ollala"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams5 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams5 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - string validator + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = "9eb658c0-c217-4386-8688- "; + jsonRequest["params"]["name"] = "1111"; + jsonRequest["params"]["cost"] = 3; + jsonRequest["params"]["age"] = 1; + jsonRequest["params"]["public"] = true; + jsonRequest["params"]["activated"] = false; + jsonRequest["params"]["custom"] = nlohmann::json::object(); + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams6 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams6 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams6 error.message", sErrorMessage, "INVALID_PARAM_STRING_VALUE"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams6 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams6 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams6 error.context.param_name", sErrorContextParamName, "uuid1"); + std::string sErrorContextDescription = jsonResponse["error"]["context"]["description"]; + compare("checkInputParams6 error.context.description", sErrorContextDescription, + "uuid - Value must match regular expression ^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams6 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams6 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - int validator + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = "9eb658c0-c217-4386-8688-f86c72e077c5"; + jsonRequest["params"]["name"] = "1111"; + jsonRequest["params"]["cost"] = 1; + jsonRequest["params"]["age"] = 1; + jsonRequest["params"]["public"] = true; + jsonRequest["params"]["activated"] = false; + jsonRequest["params"]["custom"] = nlohmann::json::object(); + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams7 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams7 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams7 error.message", sErrorMessage, "INVALID_PARAM_INTEGER_VALUE"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams7 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams7 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams7 error.context.param_name", sErrorContextParamName, "cost"); + std::string sErrorContextDescription = jsonResponse["error"]["context"]["description"]; + compare("checkInputParams7 error.context.description", sErrorContextDescription, + "Value must be more or equal then 3"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams7 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams7 method", sMethod, "game_create"); + delete pRequest; + } + + // checkInputParams - json validator + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + jsonRequest["params"]["uuid1"] = "9eb658c0-c217-4386-8688-f86c72e077c5"; + jsonRequest["params"]["name"] = "1111"; + jsonRequest["params"]["cost"] = 3; + jsonRequest["params"]["age"] = 1; + jsonRequest["params"]["public"] = true; + jsonRequest["params"]["activated"] = false; + jsonRequest["params"]["custom"] = nlohmann::json::object(); + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compare("checkInputParams8 parseIncomeData", pRequest->parseIncomeData(sRequest), true); + compare("checkInputParams8 result", pRequest->checkInputParams(vParamsDef), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse = " << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compare("checkInputParams8 error.message", sErrorMessage, "INVALID_PARAM_JSON_VALUE"); + int nErrorCode = jsonResponse["error"]["code"]; + compare("checkInputParams8 error.code", nErrorCode, 400); + std::string sErrorContextMethod = jsonResponse["error"]["context"]["method"]; + compare("checkInputParams8 error.context.method", sErrorContextMethod, "game_create"); + std::string sErrorContextParamName = jsonResponse["error"]["context"]["param_name"]; + compare("checkInputParams8 error.context.param_name", sErrorContextParamName, "custom"); + std::string sErrorContextDescription = jsonResponse["error"]["context"]["description"]; + compare("checkInputParams8 error.context.description", sErrorContextDescription, + "Expected subname in struct"); + std::string sId = jsonResponse["id"]; + compare("checkInputParams8 id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compare("checkInputParams8 method", sMethod, "game_create"); + delete pRequest; + } + + delete pHandlergameCreate; + delete pWebSocketServer; + delete pFakeClient; +} + +// --------------------------------------------------------------------- + +bool UnitTestParsingRequestParams::doAfterTest() { + // nothing + return true; +} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_parsing_request_params.h b/unit-tests.wsjcpp/src/unit_test_parsing_request_params.h new file mode 100644 index 0000000..7a5f099 --- /dev/null +++ b/unit-tests.wsjcpp/src/unit_test_parsing_request_params.h @@ -0,0 +1,15 @@ +#ifndef UNIT_TEST_PARSING_REQUEST_PARAMS_H +#define UNIT_TEST_PARSING_REQUEST_PARAMS_H + +#include + +class UnitTestParsingRequestParams : public WsjcppUnitTestBase { + public: + UnitTestParsingRequestParams(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; +}; + +#endif // UNIT_TEST_PARSING_REQUEST_PARAMS_H + diff --git a/unit-tests.wsjcpp/src/unit_test_request_parse_income_data.cpp b/unit-tests.wsjcpp/src/unit_test_request_parse_income_data.cpp new file mode 100644 index 0000000..97fed55 --- /dev/null +++ b/unit-tests.wsjcpp/src/unit_test_request_parse_income_data.cpp @@ -0,0 +1,211 @@ +#include "unit_test_request_parse_income_data.h" +#include +#include +#include + +REGISTRY_WSJCPP_UNIT_TEST(UnitTestRequestParseIncomeData) + +UnitTestRequestParseIncomeData::UnitTestRequestParseIncomeData() + : WsjcppUnitTestBase("UnitTestRequestParseIncomeData") { +} + +// --------------------------------------------------------------------- + +bool UnitTestRequestParseIncomeData::doBeforeTest() { + // nothing + return true; +} + +// --------------------------------------------------------------------- + +void UnitTestRequestParseIncomeData::executeTest() { + + WsjcppJsonRpc20WebSocketServer *pWebSocketServer = new WsjcppJsonRpc20WebSocketServer(); + FakeWebSocketClient *pFakeClient = new FakeWebSocketClient(); + WsjcppJsonRpc20WebSocketClient *pClient = pFakeClient; + + // parseIncomeData empty - wrong json + { + pFakeClient->clearLastTextMessage(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData empty", pRequest->parseIncomeData(""), false); + std::string sResponse0 = pFakeClient->getLastTextMessage(); + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse0); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compareS("parseIncomeData empty. error.message", sErrorMessage, "WRONG_JSON"); + int nErrorCode = jsonResponse["error"]["code"]; + compareN("parseIncomeData empty. error.code", nErrorCode, 400); + std::string sId = jsonResponse["id"]; + compareS("parseIncomeData empty. id", sId, "unknown_id"); + std::string sMethod = jsonResponse["method"]; + compareS("parseIncomeData empty. method", sMethod, "unknown_method"); + delete pRequest; + } + + // missing 'id' in request + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData missing id", pRequest->parseIncomeData(sRequest), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compareS("parseIncomeData missing id. error.message", sErrorMessage, "NOT_FOUND_ID_IN_REQUEST"); + int nErrorCode = jsonResponse["error"]["code"]; + compareN("parseIncomeData missing id. error.code", nErrorCode, 400); + std::string sId = jsonResponse["id"]; + compareS("parseIncomeData missing id. id", sId, "unknown_id"); + std::string sMethod = jsonResponse["method"]; + compareS("parseIncomeData missing id. method", sMethod, "game_create"); + delete pRequest; + } + + // missing 'method' in request + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["id"] = "id1"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData missing method", pRequest->parseIncomeData(sRequest), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compareS("parseIncomeData missing method. error. message", sErrorMessage, "NOT_FOUND_METHOD_IN_REQUEST"); + int nErrorCode = jsonResponse["error"]["code"]; + compareN("parseIncomeData missing method. error. code", nErrorCode, 400); + std::string sId = jsonResponse["id"]; + compareS("parseIncomeData missing method. id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compareS("parseIncomeData missing method. method", sMethod, "unknown_method"); + delete pRequest; + } + + // missing 'jsonrpc' in request + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["method"] = "game_create"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData missing jsonrpc", pRequest->parseIncomeData(sRequest), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compareS("parseIncomeData missing jsonrpc. error.message", sErrorMessage, "NOT_FOUND_FIELD_JSONRPC_IN_REQUEST"); + int nErrorCode = jsonResponse["error"]["code"]; + compareN("parseIncomeData missing jsonrpc. error.code", nErrorCode, 400); + std::string sId = jsonResponse["id"]; + compareS("parseIncomeData missing jsonrpc. id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compareS("parseIncomeData missing jsonrpc. method", sMethod, "game_create"); + delete pRequest; + } + + // field 'jsonrpc' is non string in request + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = 2; + jsonRequest["method"] = "game_create"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData jsonrpc is non string", pRequest->parseIncomeData(sRequest), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compareS("parseIncomeData jsonrpc is non string. error.message", sErrorMessage, "FIELD_JSONRPC_EXPECTED_AS_STRING_IN_REQUEST"); + int nErrorCode = jsonResponse["error"]["code"]; + compareN("parseIncomeData jsonrpc is non string. error.code", nErrorCode, 400); + std::string sId = jsonResponse["id"]; + compareS("parseIncomeData jsonrpc is non string. id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compareS("parseIncomeData jsonrpc is non string. method", sMethod, "game_create"); + delete pRequest; + } + + // field 'jsonrpc' is no "2.0" in request + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.1"; + jsonRequest["method"] = "game_create"; + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData jsonrpc is no 2.0", pRequest->parseIncomeData(sRequest), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compareS("parseIncomeData jsonrpc is no 2.0. error.message", sErrorMessage, "FIELD_JSONRPC_EXPECTED_2_DOT_0_IN_REQUEST"); + int nErrorCode = jsonResponse["error"]["code"]; + compareN("parseIncomeData jsonrpc is no 2.0. error.code", nErrorCode, 400); + std::string sId = jsonResponse["id"]; + compareS("parseIncomeData jsonrpc is no 2.0. id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compareS("parseIncomeData jsonrpc is no 2.0. method", sMethod, "game_create"); + delete pRequest; + } + + // field 'params' wrong + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::array(); + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData field params wrong", pRequest->parseIncomeData(sRequest), false); + std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << sResponse << std::endl; + nlohmann::json jsonResponse = nlohmann::json::parse(sResponse); + std::string sErrorMessage = jsonResponse["error"]["message"]; + compareS("parseIncomeData field params wrong. error.message", sErrorMessage, "FIELD_PARAMS_EXPECTED_AS_OBJECT_IN_REQUEST"); + int nErrorCode = jsonResponse["error"]["code"]; + compareN("parseIncomeData field params wrong. error.code", nErrorCode, 400); + std::string sId = jsonResponse["id"]; + compareS("parseIncomeData field params wrong. id", sId, "id1"); + std::string sMethod = jsonResponse["method"]; + compareS("parseIncomeData field params wrong. method", sMethod, "game_create"); + delete pRequest; + } + + // success parsing + { + pFakeClient->clearLastTextMessage(); + nlohmann::json jsonRequest; + jsonRequest["id"] = "id1"; + jsonRequest["jsonrpc"] = "2.0"; + jsonRequest["method"] = "game_create"; + jsonRequest["params"] = nlohmann::json::object(); + std::string sRequest = jsonRequest.dump(); + WsjcppJsonRpc20Request *pRequest = new WsjcppJsonRpc20Request(pClient, pWebSocketServer); + compareB("parseIncomeData success parsing", pRequest->parseIncomeData(sRequest), true); + std::string sResponse = pFakeClient->getLastTextMessage(); + compareS("parseIncomeData success parsing. error must be empty", sResponse, ""); + delete pRequest; + } + + delete pWebSocketServer; + delete pFakeClient; +} + +// --------------------------------------------------------------------- + +bool UnitTestRequestParseIncomeData::doAfterTest() { + // nothing + return true; +} diff --git a/unit-tests.wsjcpp/src/unit_test_request_parse_income_data.h b/unit-tests.wsjcpp/src/unit_test_request_parse_income_data.h new file mode 100644 index 0000000..31412b4 --- /dev/null +++ b/unit-tests.wsjcpp/src/unit_test_request_parse_income_data.h @@ -0,0 +1,15 @@ +#ifndef UNIT_TEST_REQUEST_PARSE_INCOME_DATA_H +#define UNIT_TEST_REQUEST_PARSE_INCOME_DATA_H + +#include + +class UnitTestRequestParseIncomeData : public WsjcppUnitTestBase { + public: + UnitTestRequestParseIncomeData(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; +}; + +#endif // UNIT_TEST_REQUEST_PARSE_INCOME_DATA_H + diff --git a/unit-tests.wsjcpp/src/unit_test_request_server_api.cpp b/unit-tests.wsjcpp/src/unit_test_request_server_api.cpp index 3612e6c..cbba7ad 100644 --- a/unit-tests.wsjcpp/src/unit_test_request_server_api.cpp +++ b/unit-tests.wsjcpp/src/unit_test_request_server_api.cpp @@ -3,6 +3,9 @@ #include #include +// --------------------------------------------------------------------- +// UnitTestRequestServerApi + REGISTRY_WSJCPP_UNIT_TEST(UnitTestRequestServerApi) UnitTestRequestServerApi::UnitTestRequestServerApi() @@ -11,14 +14,14 @@ UnitTestRequestServerApi::UnitTestRequestServerApi() // --------------------------------------------------------------------- -void UnitTestRequestServerApi::init() { +bool UnitTestRequestServerApi::doBeforeTest() { // nothing + return true; } // --------------------------------------------------------------------- -bool UnitTestRequestServerApi::run() { - bool bTestSuccess = true; +void UnitTestRequestServerApi::executeTest() { WsjcppJsonRpc20WebSocketServer *pWebSocketServer = new WsjcppJsonRpc20WebSocketServer(); FakeWebSocketClient *pFakeClient = new FakeWebSocketClient(); @@ -31,43 +34,60 @@ bool UnitTestRequestServerApi::run() { requestJson["jsonrpc"] = "2.0"; requestJson["method"] = "server_api"; requestJson["id"] = "id1"; - requestJson["params"] = nlohmann::json::array(); - pRequest->parseIncomeData(requestJson.dump()); + + std::string sRequest = requestJson.dump(); + compareB("Response: check method", pRequest->parseIncomeData(sRequest), true); + pHandlerServerApi->handle(pRequest); std::string sResponse = pFakeClient->getLastTextMessage(); + // std::cout << "sResponse: " << sResponse << std::endl; nlohmann::json respJson = nlohmann::json::parse(sResponse); std::string sMethod = respJson["method"]; std::string sId = respJson["id"]; std::string sJsonRpc = respJson["jsonrpc"]; - compareS(bTestSuccess, "Response: check method", sMethod, "server_api"); - compareS(bTestSuccess, "Response: check id", sId, "id1"); - compareS(bTestSuccess, "Response: check jsonrpc", sJsonRpc, "2.0"); + compareS("Response: check method", sMethod, "server_api"); + compareS("Response: check id", sId, "id1"); + compareS("Response: check jsonrpc", sJsonRpc, "2.0"); - int nDataLength = respJson["result"]["data_length"]; + std::string sVersion = respJson["result"]["version"]; - compareS(bTestSuccess, "Response: check result.version", sVersion, std::string(WSJCPP_APP_VERSION)); - compareN(bTestSuccess, "Response: check result.data_length", nDataLength, 1); + compareS("Response: check result.version", sVersion, std::string(WSJCPP_APP_VERSION)); - - nlohmann::json data0Json = respJson["result"]["data"][0]; + int nDataLength = respJson["result"]["data_length"]; + int nExpectedDataLength = respJson["result"]["data"].size(); + compareN("Response: check result.data_length", nDataLength, nExpectedDataLength); + + int nId = 1; + for (int i = 0; i < nExpectedDataLength; i++) { + std::string sMethodName0 = respJson["result"]["data"][i]["method"]; + if (sMethodName0 == "server_api") { + nId = i; + } + } + + nlohmann::json data0Json = respJson["result"]["data"][nId]; bool bAccessUnauthorized = data0Json["access"]["unauthorized"]; bool bAccessUser = data0Json["access"]["user"]; bool bAccessTester = data0Json["access"]["tester"]; bool bAccessAdmin = data0Json["access"]["admin"]; - compareB(bTestSuccess, "Response: check result.data[0].access.unauthorized", bAccessUnauthorized, true); - compareB(bTestSuccess, "Response: check result.data[0].access.user", bAccessUser, true); - compareB(bTestSuccess, "Response: check result.data[0].access.tester", bAccessTester, true); - compareB(bTestSuccess, "Response: check result.data[0].access.admin", bAccessAdmin, true); + compareB("Response: check result.data[0].access.unauthorized", bAccessUnauthorized, true); + compareB("Response: check result.data[0].access.user", bAccessUser, true); + compareB("Response: check result.data[0].access.tester", bAccessTester, true); + compareB("Response: check result.data[0].access.admin", bAccessAdmin, true); sMethod = data0Json["method"]; - compareS(bTestSuccess, "Response: check result.data[0].method", sMethod, "server_api"); + compareS("Response: check result.data[0].method", sMethod, "server_api"); std::string sDescription = data0Json["description"]; - compareS(bTestSuccess, "Response: check result.data[0].method", sDescription, pHandlerServerApi->getDescription()); - - return bTestSuccess; + compareS("Response: check result.data[0].method", sDescription, pHandlerServerApi->getDescription()); } +// --------------------------------------------------------------------- + +bool UnitTestRequestServerApi::doAfterTest() { + // nothing + return true; +} \ No newline at end of file diff --git a/unit-tests.wsjcpp/src/unit_test_request_server_api.h b/unit-tests.wsjcpp/src/unit_test_request_server_api.h index 062d76f..6ab7fd7 100644 --- a/unit-tests.wsjcpp/src/unit_test_request_server_api.h +++ b/unit-tests.wsjcpp/src/unit_test_request_server_api.h @@ -3,12 +3,12 @@ #include -// Description: TODO class UnitTestRequestServerApi : public WsjcppUnitTestBase { public: UnitTestRequestServerApi(); - virtual void init(); - virtual bool run(); + virtual bool doBeforeTest() override; + virtual void executeTest() override; + virtual bool doAfterTest() override; }; #endif // UNIT_TEST_REQUEST_SERVER_API_H diff --git a/wsjcpp.yml b/wsjcpp.yml index 7200b56..bdeb333 100644 --- a/wsjcpp.yml +++ b/wsjcpp.yml @@ -1,5 +1,5 @@ name: "wsjcpp-jsonrpc20" -version: "v0.0.2" +version: v0.0.3 cmake_minimum_required: "3.0" cmake_cxx_standard: "11" description: "Implementation of JsonRPC 2.0" @@ -14,7 +14,7 @@ keywords: dependencies: - name: "wsjcpp-core" - version: "v0.1.7" + version: "v0.2.0" url: "https://github.com/wsjcpp/wsjcpp-core:master" origin: "https://github.com/" installation-dir: "./src.wsjcpp/wsjcpp_core" @@ -54,3 +54,7 @@ unit-tests: description: "Test" - name: "RequestServerApi" description: "" + - name: "ParsingRequestParams" + description: "" + - name: "RequestParseIncomeData" + description: ""