Added IotResourceServer class to replace IotResource and make unit testing easier.
authorLomtev Dmytro <d.lomtev@samsung.com>
Thu, 17 Aug 2017 08:06:29 +0000 (11:06 +0300)
committerLomtev Dmytro <d.lomtev@samsung.com>
Tue, 29 Aug 2017 05:47:32 +0000 (08:47 +0300)
13 files changed:
device_core/iotivity_lib/inc/iot_resource_server.h [new file with mode: 0644]
device_core/iotivity_lib/src/iot_resource_server.cpp [new file with mode: 0644]
device_core/nmdaemon/control_resource.cpp
device_core/nmdaemon/control_resource.h
device_core/nmdaemon/hub_resource.cpp
device_core/nmdaemon/hub_resource.h
device_core/nmdaemon/main_thread.cpp
device_core/utest/local_test_resources_init.cpp
device_core/utest/test_commandhandler.cpp
device_core/utest/test_controlresource.cpp
device_core/utest/test_hubresource.cpp [new file with mode: 0644]
device_core/utest/test_registrationmq.cpp
device_core/utest/utility_functions.cpp [new file with mode: 0644]

diff --git a/device_core/iotivity_lib/inc/iot_resource_server.h b/device_core/iotivity_lib/inc/iot_resource_server.h
new file mode 100644 (file)
index 0000000..3b90916
--- /dev/null
@@ -0,0 +1,112 @@
+#ifndef IOT_RESOURCE_SERVER_H
+#define IOT_RESOURCE_SERVER_H
+
+#include <OCApi.h>
+
+namespace NetworkManager
+{
+
+class IotRequestHandler
+{
+public:
+    /**
+     * @brief OCResource GET method handler
+     * @param representation [in] request body
+     * @param params         [in] query parameters
+     * @param response_body  [out] response body
+     * @return error code
+     */
+    virtual OCEntityHandlerResult getHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body)
+    {
+        return OC_EH_BAD_REQ;
+    }
+
+    /**
+    * @brief OCResource POST method handler
+    * @param representation [in] request body
+    * @param params         [in] query parameters
+    * @param response_body  [out] response body
+    * @return error code
+    */
+    virtual OCEntityHandlerResult postHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body)
+    {
+        return OC_EH_BAD_REQ;
+    }
+
+    /**
+    * @brief OCResource PUT method handler
+    * @param representation [in] request body
+    * @param params         [in] query parameters
+    * @param response_body  [out] response body
+    * @return error code
+    */
+    virtual OCEntityHandlerResult putHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body)
+    {
+        return OC_EH_BAD_REQ;
+    }
+
+    /**
+    * @brief OCResource DELETE method handler
+    * @param representation [in] request body
+    * @param params         [in] query parameters
+    * @param response_body  [out] response body
+    * @return error code
+    */
+    virtual OCEntityHandlerResult deleteHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body)
+    {
+        return OC_EH_BAD_REQ;
+    }
+
+    /**
+    * @brief OCResource OBSERV method handler
+    * @param representation [in] request body
+    * @param params         [in] query parameters
+    * @param obsInfo        [in] observation info
+    * @return error code
+    */
+    virtual OCEntityHandlerResult observHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, const OC::ObservationInfo& obsInfo)
+    {
+        return OC_EH_BAD_REQ;
+    }
+
+    virtual OCResourceHandle getHandle() = 0;
+
+    virtual ~IotRequestHandler() {}
+};
+
+
+/**
+ * @brief The IotResource class provides abstraction of IoTivity resource model
+ */
+class IotResourceServer
+{
+public:
+
+    IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type);
+
+    IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type, const std::vector<std::string>& interfaces);
+
+    /**
+     * @brief entityHandler - resource users request handler (called by OCF)
+     * @param request [in] request for resource
+     * @return error code or OC_EH_OK
+     */
+    OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request);
+
+    /**
+     * @brief getHandle - retruns handle used for publishing of the resource on resource directory
+     * @return handle
+     */
+    OCResourceHandle getHandle()
+    {
+        return m_handle;
+    }
+
+private:
+    OCResourceHandle m_handle;
+    IotRequestHandler* m_request_handler;
+};
+
+} // namespace NetworkManager
+
+#endif // IOT_RESOURCE_SERVER_H
diff --git a/device_core/iotivity_lib/src/iot_resource_server.cpp b/device_core/iotivity_lib/src/iot_resource_server.cpp
new file mode 100644 (file)
index 0000000..d63a790
--- /dev/null
@@ -0,0 +1,129 @@
+#include <OCPlatform.h>
+#include "iot_resource_server.h"
+#include "nmexceptions.h"
+#include "nmlib.h"
+#include "logging.h"
+#include "network_manager_tag.h"
+
+namespace PH = std::placeholders;
+
+namespace NetworkManager
+{
+
+IotResourceServer::IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type)
+    : IotResourceServer(request_handler, uri, type, {OC::DEFAULT_INTERFACE})
+{
+}
+
+IotResourceServer::IotResourceServer(IotRequestHandler* request_handler, const std::string& uri, const std::string& type, const std::vector<std::string>& interfaces)
+    : m_handle(nullptr), m_request_handler(request_handler)
+{
+    if (uri.empty() || type.empty() || interfaces.size() == 0 || request_handler == nullptr)
+    {
+        throw IoTInternalError("Register resource failed due to bad parameters", EC_BAD_PARAMETER);
+    }
+
+    std::string resource_uri = uri;
+
+    OCStackResult result =  OC::OCPlatform::registerResource(
+            m_handle,
+            resource_uri,
+            type,
+            interfaces[0],
+            std::bind(&IotResourceServer::entityHandler, this, PH::_1),
+            OC_DISCOVERABLE);
+
+    if (result != OC_STACK_OK)
+    {
+        throw IoTInternalError("Failed to register resource of type: " + type, result);
+    }
+
+    if (interfaces.size() > 1u)
+    {
+        auto it = interfaces.begin();
+
+        while (++it != interfaces.end())
+        {
+            OCStackResult result = OC::OCPlatform::bindInterfaceToResource(m_handle, *it);
+
+            if (result != OC_STACK_OK)
+            {
+                throw IoTInternalError("OCPlatform::bindInterfaceToResource returned error", result);
+            }
+        }
+    }
+}
+
+OCEntityHandlerResult IotResourceServer::entityHandler(std::shared_ptr<OC::OCResourceRequest> request)
+{
+    OCEntityHandlerResult res = OC_EH_ERROR;
+
+    if (request && m_request_handler != nullptr)
+    {
+        std::string method = request->getRequestType();
+        int requestFlag = request->getRequestHandlerFlag();
+        const OC::QueryParamsMap& params = request->getQueryParameters();
+        const OC::OCRepresentation& representation = request->getResourceRepresentation();
+
+        auto response = std::make_shared<OC::OCResourceResponse>();
+        response->setRequestHandle(request->getRequestHandle());
+        response->setResourceHandle(request->getResourceHandle());
+        OC::OCRepresentation response_body;
+
+
+        if (OC::RequestHandlerFlag::RequestFlag & requestFlag)
+        {
+            if (method == "GET")
+            {
+                res = m_request_handler->getHandler(representation, params, response_body);
+            }
+            else if (method == "POST")
+            {
+                res = m_request_handler->postHandler(representation, params, response_body);
+            }
+            else if (method == "PUT")
+            {
+                res = m_request_handler->putHandler(representation, params, response_body);
+            }
+            else if (method == "DELETE")
+            {
+                res = m_request_handler->deleteHandler(representation, params, response_body);
+            }
+        }
+
+        if (requestFlag & OC::RequestHandlerFlag::ObserverFlag)
+        {
+            const OC::ObservationInfo& observationInfo = request->getObservationInfo();
+            res = m_request_handler->observHandler(representation, params, observationInfo);
+        }
+
+        response->setResponseResult(res);
+
+        if (res == OC_EH_OK)
+        {
+            auto request_if = params.find("if");
+            response->setResourceRepresentation(response_body, (request_if != params.end() ? request_if->second : OC::DEFAULT_INTERFACE));
+        }
+        else
+        {
+            LOG_E(TAG, "Bad request: uri=[%s], method=[%s]", request->getResourceUri().c_str(), method.c_str());
+            LOG_D(TAG, "\tRequest query parameters:");
+
+            for (auto it = params.cbegin(); it != params.cend(); ++it)
+            {
+                LOG_D(TAG, "\t\t\"%s\"=\"%s\"", it->first.c_str(), it->second.c_str());
+            }
+        }
+
+        res = OC::OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
+    }
+    else
+    {
+        LOG_E(TAG, "Invalid request: uri=\"%s\"", request->getResourceUri().c_str());
+    }
+
+    return res;
+}
+
+
+} // namespace NetworkManager
index 4a8f3a9..c059097 100644 (file)
@@ -7,8 +7,6 @@
  *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
  * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
  */
-#include "utils.h"
-#include "OCPlatform.h"
 #include "control_resource.h"
 #include "logging.h"
 #include <cassert>
@@ -25,62 +23,16 @@ const std::string CTRL_RESOURCE_TYPE = "device.control";
 
 namespace NMD
 {
-ControlResource::ControlResource(/*ControlCallback&& cb*/ICommandHandler* handler)
-    : IotResource(CTRL_RESOURCE_URI, {CTRL_RESOURCE_TYPE}, {DEFAULT_INTERFACE})
+ControlResource::ControlResource(ICommandHandler* handler)
+    : m_resource(this, CTRL_RESOURCE_URI, CTRL_RESOURCE_TYPE)
     , m_handler(handler)
 {
     assert(nullptr != handler);
 }
 
-void ControlResource::setRepresentation(OCRepresentation& rep)
+OCEntityHandlerResult ControlResource::postHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& /*params*/, OC::OCRepresentation& /*response_body*/)
 {
-}
-
-OCEntityHandlerResult ControlResource::entityHandler(std::shared_ptr<OCResourceRequest> request)
-{
-    OCEntityHandlerResult res = OC_EH_ERROR;
-
-    if(request)
-    {
-        std::string rt = request->getRequestType();
-        int rf = request->getRequestHandlerFlag();
-
-        if(rf & RequestHandlerFlag::RequestFlag)
-        {
-            if(rt == "GET")
-            {
-            }
-            else if(rt == "PUT")
-            {
-            }
-            else if(rt == "POST")
-            {
-                LOG_D(TAG, "Control resource command received");
-                const OCRepresentation& representation = request->getResourceRepresentation();
-
-                if (!m_handler->process(representation))
-                {
-                    LOG_E(TAG, "Control resource handler returned false");
-                    return res;
-                }
-
-                LOG_E(TAG, "Control resource handler returned true");
-                if (OC_STACK_OK == sendRepresentation(request))
-                {
-                    res = OC_EH_OK;
-                }
-            }
-            else if(rt == "DELETE")
-            {
-            }
-        }
-    }
-    else
-    {
-        LOG_E(TAG, "Control resource: invalid request");
-    }
-
-    return res;
+    return m_handler->process(representation) ? OC_EH_OK : OC_EH_BAD_REQ;
 }
 
 } // namespace NMD
index 5dc779e..74c9f12 100644 (file)
 #ifndef __IOT_CONTROL_RESOURCE_H__
 #define __IOT_CONTROL_RESOURCE_H__
 
-#include <memory>
-#include <string>
-#include <functional>
-#include "OCApi.h"
-#include "iot_resource.h"
+#include "iot_resource_server.h"
 #include "icommandhandler.h"
 
 namespace NMD
 {
 
-class ControlResource : public NetworkManager::IotResource
+class ControlResource : public NetworkManager::IotRequestHandler //IotResource
 {
 public:
     /**
@@ -31,16 +27,17 @@ public:
 
     ControlResource(const ControlResource& obj) = delete;
 
-    virtual ~ControlResource() = default;
-
     ControlResource& operator=(const ControlResource& obj) = delete;
 
-    void setRepresentation(OC::OCRepresentation& rep) override;
-
-    OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request) override;
+    OCEntityHandlerResult postHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body) override;
 
+    OCResourceHandle getHandle() override
+    {
+        m_resource.getHandle();
+    }
 private:
     ICommandHandler* m_handler;
+    NetworkManager::IotResourceServer m_resource;
 };
 
 } // namespace NMD
index 8e6f20a..4a31854 100644 (file)
@@ -1,17 +1,13 @@
-#include <iostream>
 #include <fstream>
-#include <memory>
 #include <cassert>
 #include <future>
 
-#include "OCPlatform.h"
 #include <boost/archive/text_oarchive.hpp>
 #include <boost/archive/text_iarchive.hpp>
 
 #include "nmlib.h"
 #include "hub_resource.h"
 #include "resource_callbacks.h"
-#include "iotdevice.h"
 #include "iotdevice_impl.h"
 #include "nmexceptions.h"
 #include "logging.h"
@@ -34,7 +30,7 @@ HubResource::HubResource(
         std::shared_ptr<ProxyThread> proxy_thread,
         const std::string& _devices_file_path_name
     )
-    : IotResource(HUB_RESOURCE_URI, {HUB_RESORCE_TYPE}, {DEFAULT_INTERFACE})
+    : m_resource(this, HUB_RESOURCE_URI, HUB_RESORCE_TYPE)
     , m_devices_file_path_name(_devices_file_path_name)
     , m_iotivity(iotivity)
     , m_proxy_thread(proxy_thread)
@@ -43,11 +39,6 @@ HubResource::HubResource(
     assert(m_iotivity);
 
     loadOwnedDevicesInfo();
-    m_representation.setValue("devices", "");
-}
-
-void HubResource::setRepresentation(OCRepresentation& rep)
-{
 }
 
 void HubResource::findDevices()
@@ -79,68 +70,51 @@ void HubResource::unownAll()
     }
 }
 
-OCEntityHandlerResult HubResource::entityHandler(std::shared_ptr<OCResourceRequest> request)
+OCEntityHandlerResult HubResource::getHandler(const OCRepresentation& /*request_body*/, const OC::QueryParamsMap& params, OCRepresentation& response_body)
 {
-    OCEntityHandlerResult res = OC_EH_ERROR;
+    OCEntityHandlerResult res = OC_EH_BAD_REQ;
+    auto param_iterator = params.find("intention");
 
-    if(request)
-    {
-        std::string method = request->getRequestType();
+    if (param_iterator == params.cend()) return res;
 
-        if(RequestHandlerFlag::RequestFlag & request->getRequestHandlerFlag())
-        {
-            if(method == "GET")
-            {
-                std::string intention = request->getQueryParameters().find("intention")->second;
+    std::string intention = param_iterator->second;
 
-                if(intention == "owned" || intention == "unowned")
-                {
-                    std::string dev_list{"[]"};
+    if ( intention == "owned" || intention == "unowned")
+    {
+        std::string dev_list{"[]"};
 
-                    if (m_enabled)
-                    {
-                        dev_list = makeDeviceList((intention == "owned") ? m_owned_devices: m_unowned_devices);
-                    }
+        if (m_enabled)
+        {
+            dev_list = makeDeviceList((intention == "owned") ? m_owned_devices: m_unowned_devices);
+        }
 
-                    m_representation.setValue("devices", dev_list);
+        response_body.setValue("devices", dev_list);
+    }
 
-                    if(sendRepresentation(request) == OC_STACK_OK)
-                        res = OC_EH_OK;
-                }
-                else
-                {
-                    res = OC_EH_BAD_REQ;
-                }
-            }
-            else if(method == "POST")
-            {
-                std::string id = request->getQueryParameters().find("uid")->second;
-                std::string state = request->getQueryParameters().find("state")->second;
+    return res;
+}
 
-                if(state == "own")
-                {
-                    m_proxy_thread->addAction(std::async(std::launch::deferred, &HubResource::ownDevice, this, id));
+OCEntityHandlerResult HubResource::postHandler(const OC::OCRepresentation& /*request_body*/, const OC::QueryParamsMap& params, OCRepresentation& /*response_body*/)
+{
+    OCEntityHandlerResult res = OC_EH_BAD_REQ;
+    auto id_iterator = params.find("uid");
+    auto state_iterator = params.find("state");
 
-                    if(sendRepresentation(request) == OC_STACK_OK)
-                        res = OC_EH_OK;
-                }
-                else if(state == "unown")
-                {
-                    m_proxy_thread->addAction(std::async(std::launch::deferred, &HubResource::unownDevice, this, id));
+    if (id_iterator != params.cend() && state_iterator != params.cend())
+    {
+        const std::string& state = state_iterator->second;
+        const std::string& id = id_iterator->second;
 
-                    if(sendRepresentation(request) == OC_STACK_OK)
-                        res = OC_EH_OK;
-                }
-                else
-                {
-                    res = OC_EH_BAD_REQ;
-                }
-            }
+        if (state == "own")
+        {
+            m_proxy_thread->addAction(std::async(std::launch::deferred, &HubResource::ownDevice, this, id));
+            res = OC_EH_OK;
+        }
+        else if (state == "unown")
+        {
+            m_proxy_thread->addAction(std::async(std::launch::deferred, &HubResource::unownDevice, this, id));
+            res = OC_EH_OK;
         }
-    }
-    else
-    {
-        LOG_E(TAG, "HubResource: Invalid request");
     }
 
     return res;
index f975fc4..0406dd4 100644 (file)
@@ -1,26 +1,24 @@
 #ifndef __IOT_HUB_RESOURCE_H__
 #define __IOT_HUB_RESOURCE_H__
 
-#include <memory>
 #include <string>
 #include <mutex>
 
-#include "OCApi.h"
-
 #include "iotivity.h"
-#include "iot_resource.h"
+#include "iot_resource_server.h"
 #include "proxythread.h"
 
 namespace NMD
 {
 
-class HubResource : public NetworkManager::IotResource
+class HubResource : public NetworkManager::IotRequestHandler /*NetworkManager::IotResource*/
 {
 public:
 
     /**
      * @brief Hub resource uri
      */
+
     static const std::string HUB_RESOURCE_URI;
     /**
      * @brief Hub resource type
@@ -45,30 +43,12 @@ public:
     HubResource(const HubResource& obj) = delete;
 
     /**
-     * @brief Destructor
-     */
-    virtual ~HubResource() = default;
-
-    /**
      * @brief Have no assign operator
      * @return
      */
     HubResource& operator=(const HubResource& obj) = delete;
 
     /**
-     * @brief Sets resource representation (not used)
-     * @param rep [in] New representation to set
-     */
-    void setRepresentation(OC::OCRepresentation& rep) override;
-
-    /**
-     * @brief Request handler callback (called by IoTivity framework)
-     * @param request [in] request object
-     * @return error code
-     */
-    OCEntityHandlerResult entityHandler(std::shared_ptr<OC::OCResourceRequest> request) override;
-
-    /**
      * @brief Find owned and unowned primitive devices
      */
     void findDevices();
@@ -96,6 +76,15 @@ public:
         return m_enabled;
     }
 
+    OCEntityHandlerResult getHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body) override;
+
+    OCEntityHandlerResult postHandler(const OC::OCRepresentation& representation, const OC::QueryParamsMap& params, OC::OCRepresentation& response_body) override;
+
+    OCResourceHandle getHandle() override
+    {
+        m_resource.getHandle();
+    }
+
 private:
 
     /**
@@ -140,6 +129,7 @@ private:
     static const int m_find_devices_interval = 10;
     static const int m_own_devices_interval = 10;
 
+    NetworkManager::IotResourceServer m_resource;
     std::string m_devices_file_path_name;
     NetworkManager::IoTivity* m_iotivity;
     std::shared_ptr<ProxyThread> m_proxy_thread;
index d63993d..a8f8241 100644 (file)
@@ -146,11 +146,6 @@ void MainThread::routine()
         {
             hub = std::make_shared<HubResource>(iotivity, proxy_thread, hub_file_path_name);
 
-            if(OC_STACK_OK != hub->registerResource())
-            {
-                throw std::runtime_error("register hub resource failed");
-            }
-
             rhandles.push_back(hub->getHandle());
 
             report_hub_resorce = std::make_shared<ReportResource>(*report_handler, proxy_thread);
@@ -164,11 +159,6 @@ void MainThread::routine()
         CommandHandler command_handler(iotivity, hub, report_handler, policy_handler, proxy_thread, g_working_mode, this);
         ControlResource control(&command_handler);
 
-        if(OC_STACK_OK != control.registerResource())
-        {
-            throw std::runtime_error("Register control resource failed");
-        }
-
         if (with_cloud)
         {
             rhandles.push_back(control.getHandle());
index 250543b..45a6cab 100644 (file)
@@ -73,27 +73,16 @@ int child_process_routine()
         {
             archive << i.uuid << i.name << i.model << i.type;
         }
-    }
-    catch(std::exception& e)
-    {
-        std::cout << "Exception: " << e.what() << std::endl;
-        return -1;
-    }
 
-    HubResource hub_resource(iotivity, std::shared_ptr<ProxyThread>{}, saved_devices_path);
-    if (OC_STACK_OK != hub_resource.registerResource())
-    {
-        std::cout << "register hub resource failed" << std::endl;
-        return -1;
-    }
-
-    HandlerMock hm;
+        HubResource hub_resource(iotivity, std::shared_ptr<ProxyThread>{}, saved_devices_path);
 
-    ControlResource res(&hm);
+        HandlerMock hm;
 
-    if (OC_STACK_OK != res.registerResource())
+        ControlResource res(&hm);
+    }
+    catch(std::exception& e)
     {
-        std::cout << "register control resource failed" << std::endl;
+        std::cout << "Exception: " << e.what() << std::endl;
         return -1;
     }
 
index 6832c52..9c29f94 100644 (file)
@@ -156,7 +156,6 @@ TEST_F(test_commandhandler_fixture, test_unOwnTask_hub)
         std::shared_ptr<PolicyHandler> ph = std::make_shared<PolicyHandlerMock>();
 
         proxy->start();
-        hub->registerResource();
 
         CommandHandler handler(iot, hub, rh, ph, proxy, WorkingMode::Hub, &main_thread);
 
@@ -235,7 +234,6 @@ TEST(test_commandhandler, test_uninstallTask)
         std::shared_ptr<ReportHandler> rh = std::make_shared<ReportHandlerMock>();
         std::shared_ptr<PolicyHandler> ph = std::make_shared<PolicyHandlerMock>();
         ThreadBase mt;
-        hub->registerResource();
 
         CommandHandler handler(iot, hub, rh, ph, proxy, WorkingMode::Standard, &mt);
 
index 1475f49..09033d3 100644 (file)
@@ -7,91 +7,48 @@
  *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
  * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
  */
+#include <OCApi.h>
 #include <iostream>
-#include <gtest/gtest.h>
+#include <gmock/gmock.h>
 #include "control_resource.h"
-#include <OCApi.h>
-#include <OCPlatform.h>
-#include "iotivity.h"
-#include <mutex>
-#include <condition_variable>
-#include <chrono>
-#include "device_commands.h"
 
 using namespace NMD;
 using namespace OC;
 using namespace NetworkManager;
 
-class HandlerMock : public ICommandHandler
+class HandlerMock: public ICommandHandler
 {
 public:
-    bool process(const OCRepresentation& rep) override
-    {
-        return true;
-    }
+    MOCK_METHOD1(process, bool (const OC::OCRepresentation& command));
+    virtual ~HandlerMock() {};
 };
 
 /**
  * @brief TEST for ControlResource used in device unowning usecase
- * 1. IoTivity initialization
- * 2. Create and register ControlResource
- * 3. ControlResource discovery
- * 4. Post "unown" request
- * 4. Check ControlResource entityHandler called and posted state is "unown"
+ * 1. Create ControlResource object
+ * 2. test getHandle returns non null value
+ * 3. test postHandler calls internal CommandHandler methods
  */
 TEST(test_ControlResource, test_all)
 {
     try
     {
-        IoTivity* iot = IoTivity::getInstance();
-        bool cb_called = false;
-
-        std::shared_ptr<OCResource> control;
-        std::mutex mtx;
-        std::unique_lock<std::mutex> lock(mtx);
-        std::condition_variable cv;
-        bool flag = false;
-
-        auto find_result = OCPlatform::findResource("", OC_RSRVD_WELL_KNOWN_URI, CT_DEFAULT,
-            [&](std::shared_ptr<OCResource> res){
-                std::unique_lock<std::mutex> lock(mtx);
-                if (res && !control)
-                {
-                    for (auto type: res->getResourceTypes())
-                    {
-                        if (type == "device.control" && res->sid() == iot->getDeviceID())
-                        {
-                            control = res;
-                            flag = true;
-                            cv.notify_one();
-                            break;
-                        }
-                    }
-                }
-            });
-
-        ASSERT_EQ(OC_STACK_OK, find_result) << "OCPlatform::findResource failed with code " << int(find_result);
-
-        cv.wait_for(lock, std::chrono::seconds(1), [&flag] { return flag; });
-
-        ASSERT_TRUE(flag) << "Resource not found";
-
-
-        OCRepresentation rep;
-        rep.setValue("command", int(DeviceCommands::UNOWN));
+        OCRepresentation rep, response;
+        rep.setValue("testval", 1);
         QueryParamsMap params;
-        flag = false;
+        HandlerMock handler;
+        ::testing::InSequence dummy;
 
-        control->post(rep, params,
-                   [&cv, &flag](const HeaderOptions&, const OCRepresentation&, const int)
-        {
-            flag = true;
-            cv.notify_all();
-        });
+        EXPECT_CALL(handler, process(::testing::Eq(rep)))
+                .Times(2)
+                .WillOnce(::testing::Return(true))
+                .WillOnce(::testing::Return(false));
 
-        cv.wait_for(lock, std::chrono::seconds(1), [&flag]{ return flag; });
+        ControlResource control_resource(&handler);
+        EXPECT_NE(nullptr, control_resource.getHandle());
+        EXPECT_EQ(OC_EH_OK, control_resource.postHandler(rep, params, response));
+        EXPECT_EQ(OC_EH_BAD_REQ, control_resource.postHandler(rep, params, response));
 
-        ASSERT_TRUE(flag);
     }
     catch (std::exception& e)
     {
diff --git a/device_core/utest/test_hubresource.cpp b/device_core/utest/test_hubresource.cpp
new file mode 100644 (file)
index 0000000..f0957f4
--- /dev/null
@@ -0,0 +1,32 @@
+/**
+ * @brief  Tests for HubResource
+ * @date   Created 16.08.2017
+ * @author Created 2017 in Samsung Ukraine R&D Center (SURC) under a contract
+ *         between LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine)
+ *         and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea).
+ *         Copyright: (c) Samsung Electronics Co, Ltd 2017. All rights reserved.
+ * @author Mail to: <A HREF="mailto:d.lomtev@samsung.com">Dmytro Lomtev, d.lomtev@samsung.com</A>
+ */
+#include <OCApi.h>
+#include <OCPlatform.h>
+#include <iostream>
+#include <gmock/gmock.h>
+#include "hub_resource.h"
+#include "iotivity.h"
+
+using namespace NMD;
+using namespace OC;
+using namespace NetworkManager;
+
+
+TEST(test_HubResource, test_all)
+{
+    try
+    {
+        IoTivity* iot = IoTivity::getInstance();
+    }
+    catch (std::exception& e)
+    {
+        FAIL() << "Exception: " << e.what();
+    }
+}
index d9c740b..38b2a2e 100644 (file)
@@ -36,72 +36,6 @@ public:
     MOCK_METHOD1(unsubscribe, void (const std::string& subTopic));
 };
 
-namespace OC
-{
-
-bool operator==(const OCRepresentation& lhs, const OCRepresentation& rhs)
-{
-    for (auto it = lhs.begin(); it != lhs.end(); ++it)
-    {
-        const OCRepresentation::AttributeItem& rhi = rhs[it->attrname()];
-
-        if (rhi.type() != it->type())
-        {
-            return false;
-        }
-
-        if (rhi.type() != AttributeType::Null)
-        {
-            switch(rhi.type())
-            {
-            case AttributeType::Null:
-                break;
-            case AttributeType::Integer:
-                if (rhi.getValue<int>() != it->getValue<int>()) return false;
-                break;
-            case AttributeType::Double:
-                if (rhi.getValue<double>() != it->getValue<double>()) return false;
-                break;
-            case AttributeType::Boolean:
-                if (rhi.getValue<bool>() != it->getValue<bool>()) return false;
-                break;
-            case AttributeType::String:
-                if (rhi.getValue<std::string>() != it->getValue<std::string>()) return false;
-                break;
-            case AttributeType::OCRepresentation:
-                if (!(rhi.getValue<OCRepresentation>() == it->getValue<OCRepresentation>())) return false;
-                break;
-            case AttributeType::Vector:
-                std::cout << "Failed to compare vectors of unknown type" << std::endl;
-                return false;
-            case AttributeType::Binary:
-            {
-                auto vr = rhi.getValue<std::vector<uint8_t>>();
-                auto vl = it->getValue<std::vector<uint8_t>>();
-                if (vr.size() != vl.size()) return false;
-                auto itr = vr.cbegin();
-                auto itl = vl.cbegin();
-                for (; itr != vr.cend(); ++itr, ++itl)
-                {
-                    if (*itr != *itl) return false;
-                }
-                break;
-            }
-            case AttributeType::OCByteString:
-            {
-                OCByteString sr = rhi.getValue<OCByteString>();
-                OCByteString sl = it->getValue<OCByteString>();
-                if (sr.len != sl.len || 0 != memcmp(sr.bytes, sl.bytes, sr.len)) return false;
-                break;
-            }
-            }
-        }
-    }
-    return true;
-}
-
-}
-
 /**
  * @brief TEST for RegistrationMQ::reg method
  * 1. Test registration of parent/standalone device
diff --git a/device_core/utest/utility_functions.cpp b/device_core/utest/utility_functions.cpp
new file mode 100644 (file)
index 0000000..402f551
--- /dev/null
@@ -0,0 +1,68 @@
+
+#include <OCApi.h>
+
+namespace OC
+{
+
+bool operator==(const OCRepresentation& lhs, const OCRepresentation& rhs)
+{
+    for (auto it = lhs.begin(); it != lhs.end(); ++it)
+    {
+        const OCRepresentation::AttributeItem& rhi = rhs[it->attrname()];
+
+        if (rhi.type() != it->type())
+        {
+            return false;
+        }
+
+        if (rhi.type() != AttributeType::Null)
+        {
+            switch(rhi.type())
+            {
+            case AttributeType::Null:
+                break;
+            case AttributeType::Integer:
+                if (rhi.getValue<int>() != it->getValue<int>()) return false;
+                break;
+            case AttributeType::Double:
+                if (rhi.getValue<double>() != it->getValue<double>()) return false;
+                break;
+            case AttributeType::Boolean:
+                if (rhi.getValue<bool>() != it->getValue<bool>()) return false;
+                break;
+            case AttributeType::String:
+                if (rhi.getValue<std::string>() != it->getValue<std::string>()) return false;
+                break;
+            case AttributeType::OCRepresentation:
+                if (!(rhi.getValue<OCRepresentation>() == it->getValue<OCRepresentation>())) return false;
+                break;
+            case AttributeType::Vector:
+                std::cout << "Failed to compare vectors of unknown type" << std::endl;
+                return false;
+            case AttributeType::Binary:
+            {
+                auto vr = rhi.getValue<std::vector<uint8_t>>();
+                auto vl = it->getValue<std::vector<uint8_t>>();
+                if (vr.size() != vl.size()) return false;
+                auto itr = vr.cbegin();
+                auto itl = vl.cbegin();
+                for (; itr != vr.cend(); ++itr, ++itl)
+                {
+                    if (*itr != *itl) return false;
+                }
+                break;
+            }
+            case AttributeType::OCByteString:
+            {
+                OCByteString sr = rhi.getValue<OCByteString>();
+                OCByteString sl = it->getValue<OCByteString>();
+                if (sr.len != sl.len || 0 != memcmp(sr.bytes, sl.bytes, sr.len)) return false;
+                break;
+            }
+            }
+        }
+    }
+    return true;
+}
+
+} // namespace OC