Authorization stub on service side
authorDmytro Logachev <d.logachev@samsung.com>
Thu, 13 Apr 2017 13:31:49 +0000 (16:31 +0300)
committerDmytro Logachev <d.logachev@samsung.com>
Thu, 13 Apr 2017 13:31:49 +0000 (16:31 +0300)
15 files changed:
iot-manager-service/.gitignore
iot-manager-service/inc/common_types.h [new file with mode: 0644]
iot-manager-service/inc/i_presenter.h [new file with mode: 0644]
iot-manager-service/inc/i_view.h [new file with mode: 0644]
iot-manager-service/inc/model.h [new file with mode: 0644]
iot-manager-service/inc/presenter.h [new file with mode: 0644]
iot-manager-service/inc/userinfo.h [new file with mode: 0644]
iot-manager-service/inc/utils/to_string.h [new file with mode: 0644]
iot-manager-service/inc/web_view.h [new file with mode: 0644]
iot-manager-service/src/model.cpp [new file with mode: 0644]
iot-manager-service/src/presenter.cpp [new file with mode: 0644]
iot-manager-service/src/service.cpp
iot-manager-service/src/web_view.cpp [new file with mode: 0644]
iot-manager-service/tests/test_stub.cpp [deleted file]
iot-manager-service/tests/web_view_test/web_view_tests.cpp [new file with mode: 0644]

index 435fd67..5270dee 100644 (file)
@@ -1,6 +1,8 @@
 .rds_delta
 .sdk_delta.info
 .sign
+.settings
 Debug
+Release
 
 /Test/
diff --git a/iot-manager-service/inc/common_types.h b/iot-manager-service/inc/common_types.h
new file mode 100644 (file)
index 0000000..f628893
--- /dev/null
@@ -0,0 +1,54 @@
+/**
+    @common_types.h
+    @brief Contains common types declarations.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef IOTSWC_COMMON_TYPES_H_
+#define IOTSWC_COMMON_TYPES_H_
+
+namespace iotswsec
+{
+
+/**
+    @brief Error codes enum.
+ */
+enum class ErrorCode
+{
+    Success = 0,                   ///< Success.
+    Error,                         ///< Some error occurred.
+    SessionExpired,                ///< Session is expired.
+    NotLoggedIn,                   ///< Not logged in.
+    IncorrectIdOrPsswd             ///< Incorrect id or password.
+};
+
+enum class SessionState
+{
+    Open = 0,
+    Closed,
+    Expired
+};
+
+/**
+    @brief Message processing results for UI communication.
+ */
+enum class MessageProcessResult: int
+{
+    Success = 0,                     ///< Message processing succeed.
+    Error,                           ///< In case of any other error.
+    ErrorUnregisteredCommand,        ///< Can`t process message, unregistered command.
+    ErrorBadMessageFormat,           ///< Can`t process message, wrong message format.
+    ErrorProcessingFailed,           ///< Can`t process message, internal message handler error.
+    ErrorInternalConnectionFailed,   ///< Can`t process message, internal service configuration error.
+       IncorrectIdOrPsswd,              ///< Invalid used id or password.
+       NotLoggedIn
+};
+
+}
+
+#endif /* IOTSWC_COMMON_TYPES_H_ */
diff --git a/iot-manager-service/inc/i_presenter.h b/iot-manager-service/inc/i_presenter.h
new file mode 100644 (file)
index 0000000..7936e10
--- /dev/null
@@ -0,0 +1,44 @@
+/**
+    @i_presenter.h
+    @brief Contains interface for creating readable model presentation for view.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef IOTSWC_I_PRESENTER_H_
+#define IOTSWC_I_PRESENTER_H_
+
+#include "userinfo.h"
+#include "common_types.h"
+
+#include <memory>
+
+namespace iotswsec
+{
+class IPresenter
+{
+public:
+    virtual ~IPresenter()
+    {}
+
+    //TODO: define interface.
+
+    virtual ErrorCode LogIn(const UserInfo& userInfo, const std::string& psswd) = 0;
+
+    virtual ErrorCode LogOut() = 0;
+
+    virtual SessionState GetSessionState() = 0;
+
+private:
+};
+
+typedef std::weak_ptr<IPresenter>   IPresenterWPtr;
+typedef std::shared_ptr<IPresenter> IPresenterPtr;
+
+}
+
+#endif /* IOTSWC_I_PRESENTER_H_ */
diff --git a/iot-manager-service/inc/i_view.h b/iot-manager-service/inc/i_view.h
new file mode 100644 (file)
index 0000000..eceb7c3
--- /dev/null
@@ -0,0 +1,39 @@
+/**
+    @i_view.h
+    @brief Interface for reverse interaction between presenter and bound UI.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef IOTSWC_I_VIEW_H_
+#define IOTSWC_I_VIEW_H_
+
+#include <memory>
+#include <vector>
+
+namespace iotswsec
+{
+
+/**
+    @brief Interface for reverse interaction between presenter and bound UI.
+ */
+class IView
+{
+public:
+    //TODO: define interface of this function;
+    virtual void Update() = 0;
+
+    virtual ~IView()
+    {}
+};
+
+using IViewPtr = std::shared_ptr<IView>;
+using ViewList =std::vector<IViewPtr>;
+
+}
+
+#endif /* IOTSWC_I_VIEW_H_ */
diff --git a/iot-manager-service/inc/model.h b/iot-manager-service/inc/model.h
new file mode 100644 (file)
index 0000000..9a53972
--- /dev/null
@@ -0,0 +1,46 @@
+/**
+    @model.h
+    @brief Contains declaration of application model.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef MODEL_H_
+#define MODEL_H_
+
+#include "common_types.h"
+#include "userinfo.h"
+
+#include <memory>
+
+namespace iotswsec
+{
+class Model
+{
+public:
+    Model():
+        m_sessionState(false)
+    {}
+
+    virtual ~Model()
+    {}
+
+    virtual ErrorCode LogIn(const UserInfo& userInfo, const std::string& psswd);
+
+    virtual ErrorCode LogOut();
+
+    virtual SessionState GetSessionState();
+private:
+    bool m_sessionState;
+};
+
+typedef std::shared_ptr<Model>  ModelPtr;
+typedef std::weak_ptr<Model>    ModelWPtr;
+
+}
+
+#endif /* MODEL_H_ */
diff --git a/iot-manager-service/inc/presenter.h b/iot-manager-service/inc/presenter.h
new file mode 100644 (file)
index 0000000..4c21247
--- /dev/null
@@ -0,0 +1,53 @@
+/**
+    @presenter.h
+    @brief Contains declaration of class which implements Ipresenter interface.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef IOTSWC_PRESENTER_H_
+#define IOTSWC_PRESENTER_H_
+
+#include "i_presenter.h"
+#include "i_view.h"
+#include "model.h"
+
+namespace iotswsec
+{
+
+class Presenter : public IPresenter, /*public IModelObserver,*/ public std::enable_shared_from_this<Presenter>
+{
+public:
+    Presenter():
+        m_model(nullptr),
+        m_views()
+    {}
+
+    /**
+        @brief Sets model to work with.
+        @param[in] model - model to work with.
+    */
+    void SetModel(const ModelPtr& model);
+
+    /**
+        @brief Adds view to work with.
+        @param[in] view - view to work with.
+    */
+    void AddView(const IViewPtr& view);
+
+    ErrorCode LogIn(const UserInfo& userInfo, const std::string& psswd) override;
+
+    ErrorCode LogOut() override;
+
+    SessionState GetSessionState() override;
+private:
+    ModelPtr m_model;
+    ViewList m_views;
+};
+}
+
+#endif /* IOTSWC_PRESENTER_H_ */
diff --git a/iot-manager-service/inc/userinfo.h b/iot-manager-service/inc/userinfo.h
new file mode 100644 (file)
index 0000000..7b8d3ce
--- /dev/null
@@ -0,0 +1,70 @@
+/**
+    @userinfo.h
+    @brief Contains declaration for UserInfo class.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef IOTSWC_USERINFO_H_
+#define IOTSWC_USERINFO_H_
+
+#include <string>
+
+class UserInfo
+{
+public:
+    UserInfo()
+    {}
+
+const std::string& GetFirstName() const
+{
+    return m_firstName;
+}
+
+void SetFirstName(const std::string& firstName)
+{
+    m_firstName = firstName;
+}
+
+const std::string& GetMail() const
+{
+    return m_mail;
+}
+
+void SetMail(const std::string& mail)
+{
+    m_mail = mail;
+}
+
+const std::string& GetSecondName() const
+{
+    return m_secondName;
+}
+
+void SetSecondName(const std::string& secondName)
+{
+    m_secondName = secondName;
+}
+
+const std::string& GetUserLogin() const
+{
+    return m_userLogin;
+}
+
+void SetUserLogin(const std::string& userLogin)
+{
+    m_userLogin = userLogin;
+}
+
+private:
+    std::string m_firstName;
+    std::string m_secondName;
+    std::string m_userLogin;
+    std::string m_mail;
+};
+
+#endif /* IOTSWC_USERINFO_H_ */
diff --git a/iot-manager-service/inc/utils/to_string.h b/iot-manager-service/inc/utils/to_string.h
new file mode 100644 (file)
index 0000000..7ba29a1
--- /dev/null
@@ -0,0 +1,76 @@
+/**
+    @file to_string.h
+    @brief Contains utility functions for transforming enums to strings.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Aug 26, 2015
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2015. All rights reserved.
+**/
+#ifndef TO_STRING_H_
+#define TO_STRING_H_
+
+namespace iotswsec
+{
+namespace impl
+{
+
+/**
+    @brief Class implements transforming any variables to std::string.
+*/
+template <class T, bool isEnum>
+class to_string_impl
+{
+public:
+
+    /**
+        @brief Transforms any embedded variables to string.
+        @param[in] val - object for transforming.
+        @return string representation of object.
+    */
+    static std::string to_string(T val)
+    {
+        return std::to_string(val);
+    }
+};
+
+/**
+    @brief Class transforms enums members to strings.
+*/
+template <class T>
+class to_string_impl<T, true>
+{
+public:
+
+    /**
+        @brief Transforms enum value to string.
+        @param[in] val - enum value.
+        @return string representation of enum member.
+    */
+    static std::string to_string(T val)
+    {
+        return std::to_string(static_cast<int>(val));
+    }
+};
+
+} // namespace impl
+} // namespace iotswsec
+
+namespace std
+{
+
+/**
+    @brief Redefinition of std::to_string function.
+    @param[in] val - any transformable to string object.
+    @return string representation of object.
+*/
+template <class T>
+std::string to_string(T val)
+{
+    return iotswsec::impl::to_string_impl<T, std::is_enum<T>::value>::to_string(val);
+}
+
+} // namespace std
+
+#endif // TO_STRING_H_
diff --git a/iot-manager-service/inc/web_view.h b/iot-manager-service/inc/web_view.h
new file mode 100644 (file)
index 0000000..c560cba
--- /dev/null
@@ -0,0 +1,78 @@
+/**
+    @web_view.h
+    @brief 
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#ifndef IOTSWC_WEB_VIEW_H_
+#define IOTSWC_WEB_VIEW_H_
+
+#include "common_types.h"
+#include "i_view.h"
+#include "i_presenter.h"
+
+#include "_internal/Source/JSONNode.h"
+#include <string>
+#include <unordered_map>
+
+namespace iotswsec
+{
+
+typedef void (*UpdateCallback)(const std::string&, const std::string&);
+
+class WebView : public IView
+{
+public:
+
+    /**
+        @brief WebView c-tor.
+        @param[in] presenter - presenter to work with.
+        @param[in] updateCallback - function providing reverse interaction.
+     */
+    WebView(const IPresenterPtr& presenter, UpdateCallback updateCallback);
+
+    //IView implementation.
+    void Update() override;
+
+    /**
+        @brief Processes request from UI.
+        @param[in] command - command to process.
+        @param[in] attachedObject - parameters for command in JSON representation.
+     */
+    std::string ProcessRequest(const std::string& command, const JSONNode& attachedObject);
+
+    typedef std::function<MessageProcessResult(const JSONNode&, JSONNode&, std::string&)> Callback;
+
+    /**
+        @brief Registers a callback for requests.
+        @param[in] command  - callback key.
+        @param[in] callback - class method which is called by key.
+       */
+    void RegisterCallback(const std::string& command, const Callback& callback);
+
+    MessageProcessResult LogIn(const JSONNode& request, JSONNode& response,
+            std::string& errDescription) const;
+
+    MessageProcessResult LogOut(const JSONNode& request, JSONNode& response,
+            std::string& errDescription) const;
+
+    MessageProcessResult GetSessionState(const JSONNode& request, JSONNode& response,
+            std::string& errDescription) const;
+
+private:
+
+    typedef std::unordered_map<std::string, Callback> CallbackMap;
+
+    IPresenterWPtr   m_presenter;       ///< Requests handler.
+    CallbackMap      m_callbackMap;     ///< Callback map.
+    UpdateCallback   m_updateCallback;  ///< Reverse callback for UpdateView
+};
+
+}
+
+#endif /* IOTSWC_WEB_VIEW_H_ */
diff --git a/iot-manager-service/src/model.cpp b/iot-manager-service/src/model.cpp
new file mode 100644 (file)
index 0000000..568796d
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+    @model.cpp
+    @brief Contains implementation of application model.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#include "model.h"
+
+namespace iotswsec
+{
+
+ErrorCode Model::LogIn(const UserInfo& userInfo, const std::string& psswd)
+{
+    if (userInfo.GetUserLogin().compare("iotgod")
+        || psswd.compare("1234"))
+    {
+        return ErrorCode::IncorrectIdOrPsswd;
+    }
+    m_sessionState = true;
+
+    return ErrorCode::Success;
+}
+
+ErrorCode Model::LogOut()
+{
+    if (!m_sessionState)
+    {
+       return ErrorCode::NotLoggedIn;
+    }
+
+    m_sessionState = false;
+    return ErrorCode::Success;
+}
+
+SessionState Model::GetSessionState()
+{
+    //TODO: implement
+    return m_sessionState ? SessionState::Open : SessionState::Closed;
+}
+
+}
+
diff --git a/iot-manager-service/src/presenter.cpp b/iot-manager-service/src/presenter.cpp
new file mode 100644 (file)
index 0000000..384856d
--- /dev/null
@@ -0,0 +1,45 @@
+/**
+    @presenter.cpp
+    @brief Contains implementation of class which implements IPresenter interface.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#include "presenter.h"
+
+namespace iotswsec
+{
+
+void Presenter::SetModel(const ModelPtr& model)
+{
+    m_model = model;
+}
+
+/**
+    @brief Adds view to work with.
+    @param[in] view - view to work with.
+*/
+void Presenter::AddView(const IViewPtr& view)
+{
+    m_views.push_back(view);
+}
+
+ErrorCode Presenter::LogIn(const UserInfo& userInfo, const std::string& psswd)
+{
+    return m_model->LogIn(userInfo, psswd);
+}
+
+ErrorCode Presenter::LogOut()
+{
+    return m_model->LogOut();
+}
+
+SessionState Presenter::GetSessionState()
+{
+    return m_model->GetSessionState();
+}
+}
index f91322b..ff8ca30 100644 (file)
@@ -9,35 +9,71 @@
     @par Copyright: (c) Samsung Electronics Co, Ltd 2015. All rights reserved.
 **/
 
-#include <tizen.h>
-#include <service_app.h>
 #include "ui_connector.h"
 #include "utils/log.h"
+#include "utils/to_string.h"
+#include "presenter.h"
+#include "web_view.h"
+#include "model.h"
+
+#include "libjson.h"
+
+#include <tizen.h>
+#include <service_app.h>
 
 const std::string RemotePortId    = "WEB_APP";
 const std::string RemoteAppId     = "lFI9TOMZjd.iotmanagerdashboard";
 const std::string LocalPortId     = "NATIVE_APP";
 
-static std::shared_ptr<iotswsec::UIConnector> connectorPtr; ///< Pointer to UIConnector
+static std::shared_ptr<iotswsec::Presenter>   presenterPtr;   ///< Pointer to model instance
+static std::shared_ptr<iotswsec::UIConnector> connectorPtr;   ///< Pointer to UIConnector
+static std::shared_ptr<iotswsec::WebView>     webViewPtr;     ///< Pointer to web view instance
+static std::shared_ptr<iotswsec::Model>       modelPtr;       ///< Pointer to model instance
 
 
 void UIConnectorWebViewCallback(const iotswsec::UIRemotePort& port, const std::string& command,
                                 const std::string& data)
 {
     LOG_DBG("BEGIN");
+    std::string response;
 
-    LOG_DBG("Receive msg from WEB app. command: %s; data: %s", command.c_str(), data.c_str());
-    std::string msg = "response to the command: " + command;
-    std::string response = "{\"error_code\": 0, \"data\": {\"msg\": \"" + msg + "\"}}";
-    //TODO: message handler here
+    if (webViewPtr != NULL)
+    {
+        JSONNode request = libjson::parse(data);
+
+        response = webViewPtr->ProcessRequest(command, request);
+    }
+    else
+    {
+        LOG_ERR("Error:connector error");
+        response = std::to_string(iotswsec::MessageProcessResult::ErrorInternalConnectionFailed);
+    }
 
     connectorPtr->SendMessage(iotswsec::UIRemotePort(RemoteAppId, RemotePortId), command.c_str(), response.c_str());
     LOG_DBG("END");
 }
 
+//provides reverse interaction between web and service
+void WebViewCallback(const std::string& command, const std::string& message)
+{
+    connectorPtr->SendMessage(iotswsec::UIRemotePort(RemoteAppId, RemotePortId), command.c_str(), message.c_str());
+}
+
 bool service_app_create(void *data)
 {
     LOG_DBG("NativeService Created.");
+
+    // Initialize presenter.
+    presenterPtr = std::make_shared<iotswsec::Presenter>();
+    // Initialize model.
+    modelPtr = std::make_shared<iotswsec::Model>();
+    // Bind model to presenter.
+    presenterPtr->SetModel(modelPtr);
+    // Initialize view.
+    webViewPtr = std::make_shared<iotswsec::WebView>(presenterPtr, WebViewCallback);
+    // Bind view to presenter.
+    presenterPtr->AddView(webViewPtr);
+
     // Bind UIConnector
     connectorPtr = iotswsec::UIConnector::Create(LocalPortId);
     connectorPtr->SetReciveCallback(UIConnectorWebViewCallback);
diff --git a/iot-manager-service/src/web_view.cpp b/iot-manager-service/src/web_view.cpp
new file mode 100644 (file)
index 0000000..c4451d9
--- /dev/null
@@ -0,0 +1,188 @@
+/**
+    @web_view.cpp
+    @brief Contains implementation of class for WEB UI interaction.
+    @author Dmytro Logachev (d.logachev@samsung.com)
+    @date Created Apr 12, 2017
+    @par In Samsung Ukraine R&D Center (SRK) under a contract between
+    @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
+    @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
+    @par Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+**/
+
+#include "web_view.h"
+#include "utils/to_string.h"
+#include "utils/log.h"
+
+namespace iotswsec
+{
+
+//Command names:
+const std::string LogInCommand        = "Authorization.login";
+const std::string LogOutCommand       = "Authorization.logout";
+const std::string SessionStateCommand = "Authorization.session.get";
+
+//node names:
+const std::string ErrorCodeNodeName           = "error_code";
+const std::string DataNodeName                = "data";
+const std::string ErrDescNodeName             = "description";
+
+inline JSONNode SetJSONNodeName(const std::string& name, JSONNode&& node)
+{
+    node.set_name(name);
+
+    return node;
+}
+
+WebView::WebView(const IPresenterPtr& presenter, UpdateCallback updateCallback):
+    m_presenter(presenter),
+    m_callbackMap(),
+    m_updateCallback(updateCallback)
+{
+    Callback logInCallback = std::bind(&WebView::LogIn, this, std::placeholders::_1,
+             std::placeholders::_2, std::placeholders::_3);
+    RegisterCallback(LogInCommand, logInCallback);
+    Callback logOutCallback = std::bind(&WebView::LogOut, this, std::placeholders::_1,
+                 std::placeholders::_2, std::placeholders::_3);
+    RegisterCallback(LogOutCommand, logOutCallback);
+    Callback getSessionStateCallback = std::bind(&WebView::GetSessionState, this, std::placeholders::_1,
+                     std::placeholders::_2, std::placeholders::_3);
+    RegisterCallback(SessionStateCommand, getSessionStateCallback);
+
+}
+
+void WebView::Update()
+{}
+
+std::string WebView::ProcessRequest(const std::string& command, const JSONNode& attachedObject)
+{
+    LOG_INFO("command: %s", command.c_str());
+    LOG_INFO("attached object: %s", attachedObject.write().c_str());
+
+    JSONNode response(JSON_NODE);
+
+    auto callbackIterator = m_callbackMap.find(command);
+
+    if (callbackIterator == m_callbackMap.end())
+    {
+        LOG_ERR("Command %s, has no according callback", command.c_str());
+        response.push_back(JSONNode(ErrorCodeNodeName,  std::to_string(MessageProcessResult::ErrorUnregisteredCommand)));
+    }
+    else
+    {
+        JSONNode data = SetJSONNodeName(DataNodeName, JSONNode(JSON_NODE));
+        std::string errDescription;
+
+        MessageProcessResult result = callbackIterator->second(attachedObject, data, errDescription);
+
+        response.push_back(JSONNode(ErrorCodeNodeName, std::stoi(std::to_string(result))));
+
+        if (result != MessageProcessResult::Success)
+        {
+            response.push_back(JSONNode(ErrDescNodeName, errDescription));
+        }
+
+        response.push_back(data);
+    }
+
+    return response.write(); //Response string
+}
+
+void WebView::RegisterCallback(const std::string& command, const Callback& callback)
+{
+    m_callbackMap.insert(CallbackMap::value_type(command, callback));
+}
+
+MessageProcessResult WebView::LogIn(const JSONNode& request, JSONNode& response, std::string& errDescription) const
+{
+    //Expected JSON:
+    // "data" : {"login" : login, "password" : password}
+
+    IPresenterPtr presenter = m_presenter.lock();
+
+    if (!presenter)
+    {
+        return MessageProcessResult::ErrorInternalConnectionFailed;
+    }
+
+    auto iteratorLogin = request.find("login");
+    auto iteratorPsswd = request.find("password");
+
+    if (iteratorLogin != request.end() &&
+        iteratorPsswd != request.end())
+    {
+        UserInfo userInfo;
+        userInfo.SetUserLogin(iteratorLogin->as_string());
+
+        if (presenter->LogIn(userInfo, iteratorPsswd->as_string()) != ErrorCode::Success)
+        {
+            errDescription = "Wrong credentials";
+            return MessageProcessResult::IncorrectIdOrPsswd;
+        }
+
+        return MessageProcessResult::Success;
+    }
+
+    return MessageProcessResult::ErrorBadMessageFormat;
+}
+
+MessageProcessResult WebView::LogOut(const JSONNode& request, JSONNode& response, std::string& errDescription) const
+{
+    //Expected JSON:
+    // "data" : {}
+    IPresenterPtr presenter = m_presenter.lock();
+
+    if (!presenter)
+    {
+        return MessageProcessResult::ErrorInternalConnectionFailed;
+    }
+
+    if (presenter->LogOut() != ErrorCode::Success)
+    {
+        return MessageProcessResult::NotLoggedIn;
+    }
+
+    return MessageProcessResult::Success;
+}
+
+MessageProcessResult WebView::GetSessionState(const JSONNode& request, JSONNode& response, std::string& errDescription) const
+{
+    //Expected JSON:
+    // "data" : {}
+    //Expected response in case of open session:
+    // "data" : {"state" : 0 }
+    IPresenterPtr presenter = m_presenter.lock();
+
+    if (!presenter)
+    {
+        return MessageProcessResult::ErrorInternalConnectionFailed;
+    }
+
+    SessionState state = presenter->GetSessionState();
+    int webState = 1;
+
+    switch (state)
+    {
+    case SessionState::Open:
+    {
+        webState = 0;
+        break;
+    }
+    case SessionState::Closed:
+    {
+        break;
+    }
+    default:
+    {
+        webState = 1;
+        break;
+    }
+    }
+
+    response.push_back(JSONNode("state", webState));
+
+    return MessageProcessResult::Success;
+}
+
+
+} //namespace iotswsec
+
diff --git a/iot-manager-service/tests/test_stub.cpp b/iot-manager-service/tests/test_stub.cpp
deleted file mode 100644 (file)
index 89a9f50..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-#include "gtest/gtest.h"
-
-TEST(StubTest, TestOne)
-{
-       EXPECT_TRUE(false);
-       ASSERT_TRUE(true);
-}
diff --git a/iot-manager-service/tests/web_view_test/web_view_tests.cpp b/iot-manager-service/tests/web_view_test/web_view_tests.cpp
new file mode 100644 (file)
index 0000000..a591c4b
--- /dev/null
@@ -0,0 +1,146 @@
+#include "web_view.h"
+
+#include "libjson.h"
+#include "gtest/gtest.h"
+
+namespace iotswsec
+{
+
+class PresenterMock: public IPresenter
+{
+public:
+
+    ErrorCode LogIn(const UserInfo& userInfo, const std::string& psswd) override
+    {
+        if (userInfo.GetUserLogin().compare("iotgod")
+            || psswd.compare("iotivity"))
+        {
+            return ErrorCode::IncorrectIdOrPsswd;
+        }
+        m_sessionState = true;
+
+        return ErrorCode::Success;
+    }
+
+    ErrorCode LogOut() override
+    {
+        if (!m_sessionState)
+        {
+            return ErrorCode::NotLoggedIn;
+        }
+
+        m_sessionState = false;
+        return ErrorCode::Success;
+    }
+
+    SessionState GetSessionState() override
+    {
+        return m_sessionState ? SessionState::Open : SessionState::Closed;
+    }
+private:
+    bool m_sessionState;
+};
+
+//provides reverse interaction between web and service
+static void WebViewCallbackStub(const std::string& command,
+        const std::string& message)
+{
+}
+
+class WebViewTest: public ::testing::Test
+{
+protected:
+    void SetUp() override
+    {
+        presenterPtr = std::make_shared<iotswsec::PresenterMock>();
+        webViewPtr = std::make_shared < iotswsec::WebView
+                > (presenterPtr, WebViewCallbackStub);
+    }
+
+    std::shared_ptr<iotswsec::IPresenter> presenterPtr; ///< Pointer to presenter instance
+    std::shared_ptr<iotswsec::WebView> webViewPtr; ///< Pointer to web view instance
+
+};
+
+TEST_F(WebViewTest, LogInTest1)
+{
+    std::string requestBody(
+            "\"data\":{\"login\":\"iotgod\",\"password\":\"iotivity\"}");
+    JSONNode request(JSON_NODE);
+    request.push_back(JSONNode("login", "iotgod"));
+    request.push_back(JSONNode("password", "iotivity"));
+    JSONNode response;
+    std::string errDesc;
+    MessageProcessResult result = webViewPtr->LogIn(request, response, errDesc);
+
+    EXPECT_EQ(MessageProcessResult::Success, result);
+}
+
+TEST_F(WebViewTest, LogInTest2)
+{
+    JSONNode request(JSON_NODE);
+    request.push_back(JSONNode("login", "sm1_else"));
+    request.push_back(JSONNode("password", "smth_else"));
+    JSONNode response;
+    std::string errDesc;
+    MessageProcessResult result = webViewPtr->LogIn(request, response, errDesc);
+
+    EXPECT_EQ(MessageProcessResult::IncorrectIdOrPsswd, result);
+}
+
+TEST_F(WebViewTest, LogOutTest1)
+{
+    JSONNode request(JSON_NODE);
+    JSONNode response;
+    std::string errDesc;
+    MessageProcessResult result = webViewPtr->LogOut(request, response, errDesc);
+
+    EXPECT_EQ(MessageProcessResult::NotLoggedIn, result);
+}
+
+TEST_F(WebViewTest, LogOutTest2)
+{
+    JSONNode request(JSON_NODE);
+    request.push_back(JSONNode("login", "iotgod"));
+    request.push_back(JSONNode("password", "iotivity"));
+    JSONNode response;
+    std::string errDesc;
+    webViewPtr->LogIn(request, response, errDesc);
+
+    MessageProcessResult result = webViewPtr->LogOut(request, response, errDesc);
+
+    EXPECT_EQ(MessageProcessResult::Success, result);
+}
+
+TEST_F(WebViewTest, GetSessionState1)
+{
+    JSONNode request(JSON_NODE);
+    request.push_back(JSONNode("login", "iotgod"));
+    request.push_back(JSONNode("password", "iotivity"));
+    JSONNode response;
+    std::string errDesc;
+    webViewPtr->LogIn(request, response, errDesc);
+
+    MessageProcessResult result = webViewPtr->GetSessionState(request, response, errDesc);
+
+    EXPECT_EQ(MessageProcessResult::Success, result);
+}
+
+TEST_F(WebViewTest, GetSessionState2)
+{
+    JSONNode request(JSON_NODE);
+    request.push_back(JSONNode("login", "iotgod"));
+    request.push_back(JSONNode("password", "iotivity"));
+    JSONNode response;
+    std::string errDesc;
+    webViewPtr->LogIn(request, response, errDesc);
+
+    JSONNode stateRequest;
+    JSONNode stateResponse;
+    MessageProcessResult result = webViewPtr->GetSessionState(stateRequest, stateResponse, errDesc);
+
+    EXPECT_EQ(MessageProcessResult::Success, result);
+    EXPECT_EQ(std::string("{\"state\":0}"), stateResponse.write());
+}
+
+} //namespace iotswsec