From: Cédric Aguerre Date: Fri, 7 Aug 2015 16:34:38 +0000 (+0200) Subject: add simple test X-Git-Tag: gde-v0.1~20 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=e35cd524b5ad09e8141474220b5e294768d24ef9;p=modules%2Fgde.git add simple test --- diff --git a/projects/GDE_API_CPP/api/CMakeLists.txt b/projects/GDE_API_CPP/api/CMakeLists.txt index 48e3fb4..525cf76 100644 --- a/projects/GDE_API_CPP/api/CMakeLists.txt +++ b/projects/GDE_API_CPP/api/CMakeLists.txt @@ -9,3 +9,4 @@ FIND_PACKAGE(POCO REQUIRED) INCLUDE_DIRECTORIES(${Poco_INCLUDE_DIRS}) ADD_SUBDIRECTORY(src) +ADD_SUBDIRECTORY(tests) diff --git a/projects/GDE_API_CPP/api/src/CommandResultTO.cpp b/projects/GDE_API_CPP/api/src/CommandResultTO.cpp index 011bd62..a225786 100644 --- a/projects/GDE_API_CPP/api/src/CommandResultTO.cpp +++ b/projects/GDE_API_CPP/api/src/CommandResultTO.cpp @@ -9,6 +9,7 @@ gde::CommandResultTO::CommandResultTO(const CommandResultTO& cto) gde::CommandResultTO gde::CommandResultTO::fromJson(std::string json) { + std::cout << "CommandResultTO: " << json << std::endl; Poco::JSON::Object::Ptr object = JsonFormatter::parse(json); int code = JsonFormatter::extract(object, "code"); std::string message = JsonFormatter::extract(object, "message"); diff --git a/projects/GDE_API_CPP/api/src/CommandTO.cpp b/projects/GDE_API_CPP/api/src/CommandTO.cpp index 92d19b9..6b5ea89 100644 --- a/projects/GDE_API_CPP/api/src/CommandTO.cpp +++ b/projects/GDE_API_CPP/api/src/CommandTO.cpp @@ -63,7 +63,10 @@ gde::CommandTO::fromJson(std::string json) std::string data = JsonFormatter::extract(object, "data"); ParametersMap parameters; - Poco::JSON::Object::Ptr objParameters = JsonFormatter::extract(object, "parameters"); + //Poco::JSON::Object::Ptr objParameters = JsonFormatter::extract(object, "parameters"); + Poco::Dynamic::Var varParameters = object->get("parameters"); + Poco::JSON::Object::Ptr objParameters = varParameters.extract(); + for (Poco::JSON::Object::ConstIterator itr = objParameters->begin(); itr != objParameters->end(); ++itr) { std::string paramName = itr->first; std::string paramValue = itr->second; diff --git a/projects/GDE_API_CPP/api/src/HttpConnection.cpp b/projects/GDE_API_CPP/api/src/HttpConnection.cpp index 5a2e16d..9054e03 100644 --- a/projects/GDE_API_CPP/api/src/HttpConnection.cpp +++ b/projects/GDE_API_CPP/api/src/HttpConnection.cpp @@ -47,7 +47,10 @@ gde::HttpConnection::_processRequest(const HTTPRequest& request, std::string message = ""; std::string data = ""; + std::cout << "processRequest: body = " << requestBody << std::endl; + try { + std::cout << "Sending request...\n"; std::ostream& os = _session.sendRequest((HTTPRequest&)request); // Send request body, if any if (!requestBody.empty()) { @@ -55,6 +58,7 @@ gde::HttpConnection::_processRequest(const HTTPRequest& request, Poco::StreamCopier::copyStream((std::istream&)iss, os); } + std::cout << "Getting response...\n"; // Get response HTTPResponse response; std::istream& rs = _session.receiveResponse(response); @@ -62,6 +66,7 @@ gde::HttpConnection::_processRequest(const HTTPRequest& request, std::ostringstream responseBody; Poco::StreamCopier::copyStream(rs, responseBody); // responseBody contains a CommandResultTO in json format + std::cout << "Returning result...\n"; return CommandResultTO::fromJson(responseBody.str()); } catch (Poco::Exception& e) { std::cerr << e.displayText() << std::endl; diff --git a/projects/GDE_API_CPP/api/src/JsonFormatter.cpp b/projects/GDE_API_CPP/api/src/JsonFormatter.cpp index 2bee2a5..f043375 100644 --- a/projects/GDE_API_CPP/api/src/JsonFormatter.cpp +++ b/projects/GDE_API_CPP/api/src/JsonFormatter.cpp @@ -28,3 +28,7 @@ gde::JsonFormatter::stringify(const Poco::JSON::Object& object) { object.stringify(jsonStream); return jsonStream.str(); } + +// Declare methods for the supported template type parameters +template int gde::JsonFormatter::extract(Poco::JSON::Object::Ptr object, const std::string& varName); +template std::string gde::JsonFormatter::extract(Poco::JSON::Object::Ptr object, const std::string& varName); diff --git a/projects/GDE_API_CPP/api/src/User.hpp b/projects/GDE_API_CPP/api/src/User.hpp index 131508b..577f4e2 100644 --- a/projects/GDE_API_CPP/api/src/User.hpp +++ b/projects/GDE_API_CPP/api/src/User.hpp @@ -9,6 +9,8 @@ namespace gde { friend class UserService; public: + ~User() {} + inline int getId() const { return _id; } inline void setId(int id) { this->_id = id; } @@ -21,7 +23,6 @@ namespace gde { private: User(int id=-1, std::string name="", std::string password="") : _id(id), _name(name), _password(password) {} - ~User() {} User(const User&); // non copyable User& operator=(const User&); // non copyable diff --git a/projects/GDE_API_CPP/api/src/UserService.cpp b/projects/GDE_API_CPP/api/src/UserService.cpp index 2465ed9..20fc079 100644 --- a/projects/GDE_API_CPP/api/src/UserService.cpp +++ b/projects/GDE_API_CPP/api/src/UserService.cpp @@ -20,8 +20,9 @@ enum { FIND_USER }; +std::string gde::UserService::_servletName = "http://localhost:8080/GDE-war/UserService"; + gde::UserService::UserService() - : _servletName("/UserService") { } @@ -29,7 +30,7 @@ gde::UserService::~UserService() { } -gde::User +const gde::User gde::UserService::createUser(std::string name, std::string password) { // build JSON string for CommandTO data @@ -37,6 +38,7 @@ gde::UserService::createUser(std::string name, std::string password) obj.set("name", Poco::Dynamic::Var(name)); obj.set("password", Poco::Dynamic::Var(password)); std::string data = JsonFormatter::stringify(obj); + std::cout << "createUser: command = " << data << std::endl; CommandTO cto(CREATE_USER, data); CommandResultTO crto = gde::HttpConnection(Poco::URI(_servletName)).doPost(cto); diff --git a/projects/GDE_API_CPP/api/src/UserService.hpp b/projects/GDE_API_CPP/api/src/UserService.hpp index 33ca6b8..0a143bb 100644 --- a/projects/GDE_API_CPP/api/src/UserService.hpp +++ b/projects/GDE_API_CPP/api/src/UserService.hpp @@ -10,7 +10,7 @@ namespace gde { class UserService { public: - User createUser(std::string name, std::string password); + static const User createUser(std::string name, std::string password); void createUser(); void deleteUser(); @@ -29,7 +29,7 @@ namespace gde { UserService& operator=(const UserService&); // non copyable private: - std::string _servletName; + static std::string _servletName; }; }; diff --git a/projects/GDE_API_CPP/api/tests/CMakeLists.txt b/projects/GDE_API_CPP/api/tests/CMakeLists.txt new file mode 100644 index 0000000..cd80d75 --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/CMakeLists.txt @@ -0,0 +1,20 @@ + +INCLUDE_DIRECTORIES( + ${Poco_INCLUDE_DIRS} + ${PROJECT_SOURCE_DIR}/src +) + +LINK_DIRECTORIES( + ${Poco_LIBRARY_DIRS} +# ${CMAKE_INSTALL_DIR/lib} +) + +SET(_link_LIBRARIES + #PocoNet + #PocoJSON + GdeApiCpp +) + +ADD_EXECUTABLE(simple_test simple_test.cpp) +TARGET_LINK_LIBRARIES(simple_test ${_link_LIBRARIES}) +INSTALL(TARGETS simple_test EXPORT ${PROJECT_NAME}TargetGroup DESTINATION tests) diff --git a/projects/GDE_API_CPP/api/tests/simple_test.cpp b/projects/GDE_API_CPP/api/tests/simple_test.cpp new file mode 100644 index 0000000..c913b8b --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/simple_test.cpp @@ -0,0 +1,14 @@ +#include + +#include +#include + +int +main() +{ + const gde::User& user = gde::UserService::createUser("toto", "toto_passwd"); + + std::cout << "Name: " << user.getName() << std::endl; + std::cout << "Password: " << user.getPassword() << std::endl; + +}