Add & fix unittest of easy setup.
authorsy01.youn <sy01.youn@samsung.com>
Thu, 18 Aug 2016 08:20:17 +0000 (17:20 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 19 Aug 2016 09:13:35 +0000 (09:13 +0000)
- Add unit test cases
(1) Enrollee handles individual get request from client.
(2) Enrollee handles put request from client.
(3) ESSetCallbackForUserdata API unit test is added.

- Remove GetTargetNetworkInfoFromProvResource API in resourcehandler.
  . This API is not need anymore.

Change-Id: If634923b0daaf9760b78b3d7e853c823769db6ba
Signed-off-by: sy01.youn <sy01.youn@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10603
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/easy-setup/enrollee/src/resourcehandler.c
service/easy-setup/enrollee/src/resourcehandler.h
service/easy-setup/enrollee/unittests/ESEnrolleeTest.cpp
service/easy-setup/enrollee/unittests/ESMediatorSimulator.h

index 872a8b7..e22d6d4 100755 (executable)
@@ -107,15 +107,6 @@ void UnRegisterResourceEventCallBack()
     }
 }
 
-void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
-{
-    if (name != NULL && pass != NULL)
-    {
-        OICStrcpy(name, MAX_WEBLINKLEN, gWiFiResource.ssid);
-        OICStrcpy(pass, MAX_WEBLINKLEN, gWiFiResource.cred);
-    }
-}
-
 OCStackResult initProvResource(bool isSecured)
 {
     gProvResource.status = ES_STATE_INIT;
index cc9ae8d..c59f1b7 100755 (executable)
@@ -87,7 +87,6 @@ OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty);
 OCStackResult SetEnrolleeState(ESEnrolleeState esState);
 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode);
 
-void GetTargetNetworkInfoFromProvResource(char *, char *);
 void RegisterWifiRsrcEventCallBack(ESWiFiCB);
 void RegisterCloudRsrcEventCallBack(ESCloudCB);
 void RegisterDevConfRsrcEventCallBack(ESDevConfCB);
index 6232bce..b782a9a 100755 (executable)
@@ -88,6 +88,26 @@ public:
         std::cout << __func__ << std::endl;
     }
 
+    static void onGetWifiRsrc(const OCRepresentation& )
+    {
+        std::cout << __func__ << std::endl;
+    }
+
+    static void onGetCloudRsrc(const OCRepresentation& )
+    {
+        std::cout << __func__ << std::endl;
+    }
+
+    static void onGetDeviceConf(const OCRepresentation& )
+    {
+        std::cout << __func__ << std::endl;
+    }
+
+    static void onPutRequest(const OCRepresentation& )
+    {
+        std::cout << __func__ << std::endl;
+    }
+
     static void deviceProvisioningStatusCallback(std::shared_ptr< DevicePropProvisioningStatus >)
     {
         std::cout << __func__ << std::endl;
@@ -126,15 +146,30 @@ public:
         return ESInitEnrollee(false, resourcemMask, callbacks);
     }
 
+    ESResult startEnrolleeWithInvalidRsrcMask()
+    {
+        ESProvisioningCallbacks callbacks;
+        callbacks.WiFiProvCb = &EasysetupEnrolleeTest::WiFiProvCbInApp;
+        callbacks.DevConfProvCb = &EasysetupEnrolleeTest::DevConfProvCbInApp;
+        callbacks.CloudDataProvCb = &EasysetupEnrolleeTest::CloudDataCbInApp;
+
+        return ESInitEnrollee(false, (ESResourceMask)0, callbacks);
+    }
+
     ESResult setDeviceProperty()
     {
         ESDeviceProperty deviceProperty = {
-            {{WIFI_11G, WiFi_EOF}, WIFI_5G}, {"Test Device"}
+            {{WIFI_11G, WiFi_EOF}, WIFI_5G}, {"Test Device", "Test Model Number"}
         };
 
         return ESSetDeviceProperty(&deviceProperty);
     }
 
+    ESResult setCallabckForUserDataNULL()
+    {
+        return ESSetCallbackForUserdata(NULL, NULL);
+    }
+
 protected:
 
     void SetUp()
@@ -167,6 +202,13 @@ TEST_F(EasysetupEnrolleeTest, ESInitEnrolleeSuccess)
     ESTerminateEnrollee();
 }
 
+TEST_F(EasysetupEnrolleeTest, ESInitEnrolleeFailedInvalidRsrcMask)
+{
+    ESResult ret = startEnrolleeWithInvalidRsrcMask();
+    EXPECT_EQ(ret, ES_ERROR);
+    ESTerminateEnrollee();
+}
+
 TEST_F(EasysetupEnrolleeTest, ESInitEnrolleeFailedByWiFiCbIsNull)
 {
     ESResourceMask resourcemMask = (ESResourceMask)(ES_WIFI_RESOURCE |
@@ -410,4 +452,93 @@ TEST_F(EasysetupEnrolleeTest, CloudServerProperiesProvisionedWithSuccess)
     ESTerminateEnrollee();
 }
 
+TEST_F(EasysetupEnrolleeTest, GetWifiRsrcTest)
+{
+    bool isRepFlag = false;
+    mocks.ExpectCallFunc(onGetWifiRsrc).Do(
+        [& isRepFlag](const OCRepresentation& /*rep*/)
+        {
+
+            isRepFlag = true;
+        });
+
+    ESResult ret = startEnrollee();
+    g_mediatorSimul.getWifiRsrc(onGetWifiRsrc);
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+
+    EXPECT_EQ(ret, ES_OK);
+
+    ESTerminateEnrollee();
+}
 
+TEST_F(EasysetupEnrolleeTest, GetCloudRsrcTest)
+{
+    bool isRepFlag = false;
+    mocks.ExpectCallFunc(onGetCloudRsrc).Do(
+        [& isRepFlag](const OCRepresentation& /*rep*/)
+        {
+            isRepFlag = true;
+        });
+
+    ESResult ret = startEnrollee();
+    g_mediatorSimul.getCloudRsrc(onGetCloudRsrc);
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+
+    EXPECT_EQ(ret, ES_OK);
+
+    ESTerminateEnrollee();
+}
+
+TEST_F(EasysetupEnrolleeTest, GetDevConfTest)
+{
+    bool isRepFlag = false;
+    mocks.ExpectCallFunc(onGetDeviceConf).Do(
+        [& isRepFlag](const OCRepresentation& /*rep*/)
+        {
+            isRepFlag = true;
+        });
+
+    ESResult ret = startEnrollee();
+    ret = setDeviceProperty();
+
+    g_mediatorSimul.getDevConfiguration(onGetDeviceConf);
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+
+    EXPECT_EQ(ret, ES_OK);
+
+    ESTerminateEnrollee();
+}
+
+TEST_F(EasysetupEnrolleeTest, PutRequestTest)
+{
+    bool isRepFlag = false;
+    mocks.ExpectCallFunc(onPutRequest).Do(
+        [& isRepFlag](const OCRepresentation& /*rep*/)
+        {
+            isRepFlag = true;
+        });
+
+    ESResult ret = startEnrollee();
+
+    g_mediatorSimul.putProvRsrc(onPutRequest);
+
+    std::unique_lock< std::mutex > lock{ mutexForCondition };
+    responseCon.wait_for(lock, g_waitForResponse);
+
+    EXPECT_EQ(ret, ES_OK);
+
+    ESTerminateEnrollee();
+}
+
+TEST_F(EasysetupEnrolleeTest, ESSetCallabckForUserDataFailed)
+{
+    ESResult ret = setCallabckForUserDataNULL();
+    EXPECT_EQ(ret, ES_ERROR);
+    ESTerminateEnrollee();
+}
index aa43055..41a0b62 100755 (executable)
 #include "OCPlatform.h"
 
 #define PROV_RESOURCE_TYPE "ocf.wk.prov"
+#define WIFI_RESOURCE_TYPE "ocf.wk.wifi"
+#define DEV_RESOURCE_TYPE  "ocf.wk.devconf"
+#define CLOUD_RESOURCE_TYPE "ocf.wk.cloudserver"
 
 using namespace OIC::Service;
 
 class ESMediatorSimulator
 {
 private:
+    std::shared_ptr<RemoteEnrollee> m_remoteEnrollee;
     std::function<void(std::shared_ptr<OC::OCResource> resource)> m_discoveryCb;
     std::function<void(std::shared_ptr< GetConfigurationStatus > status)> m_getConfigurationCb;
     std::function<void(std::shared_ptr< GetEnrolleeStatus >)> m_getStatusCb;
     std::function<void(std::shared_ptr< DevicePropProvisioningStatus >)> m_DevicePropProvisioningCb;
     std::function<void(std::shared_ptr< CloudPropProvisioningStatus >)> m_CloudPropProvisioningCb;
-
-    std::shared_ptr<RemoteEnrollee> m_remoteEnrollee;
+    std::function<void(const OCRepresentation&)> m_getWifiCb;
+    std::function<void(const OCRepresentation&)> m_getCloudCb;
+    std::function<void(const OCRepresentation&)> m_getDevConfCb;
+    std::function<void(const OCRepresentation&)> m_provPutCb;
 
 public:
     ESMediatorSimulator()
     : m_remoteEnrollee(), m_discoveryCb(), m_getConfigurationCb(), m_getStatusCb(),
-    m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb()
+    m_DevicePropProvisioningCb(), m_CloudPropProvisioningCb(),
+    m_getWifiCb(), m_getCloudCb(), m_getDevConfCb(), m_provPutCb()
     {
     };
     ~ESMediatorSimulator() = default;
@@ -85,6 +92,36 @@ public:
                                                                     this, std::placeholders::_1));
     }
 
+    void getWifiRsrc(std::function<void(const OCRepresentation& rep)> cb)
+    {
+        m_getWifiCb = cb;
+        std::string uri = std::string("/oic/res?rt=") + WIFI_RESOURCE_TYPE;
+        OC::OCPlatform::findResource("", uri,
+                OCConnectivityType::CT_DEFAULT,
+                std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetWifiRsrc,
+                                                                    this, std::placeholders::_1));
+    }
+
+    void getCloudRsrc(std::function<void(const OCRepresentation& rep)> cb)
+    {
+        m_getCloudCb = cb;
+        std::string uri = std::string("/oic/res?rt=") + CLOUD_RESOURCE_TYPE;
+        OC::OCPlatform::findResource("", uri,
+                OCConnectivityType::CT_DEFAULT,
+                std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetCloudRsrc,
+                                                                    this, std::placeholders::_1));
+    }
+
+    void getDevConfiguration(std::function<void(const OCRepresentation& rep)> cb)
+    {
+        m_getDevConfCb = cb;
+        std::string uri = std::string("/oic/res?rt=") + DEV_RESOURCE_TYPE;
+        OC::OCPlatform::findResource("", uri,
+                OCConnectivityType::CT_DEFAULT,
+                std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToGetDevConf,
+                                                                    this, std::placeholders::_1));
+    }
+
     void getStatus(std::function<void(std::shared_ptr< GetEnrolleeStatus >)> cb)
     {
         m_getStatusCb = cb;
@@ -118,6 +155,17 @@ public:
                                                                     this, std::placeholders::_1));
     }
 
+    void putProvRsrc(std::function<void(const OCRepresentation& rep)> cb)
+    {
+        m_provPutCb = cb;
+        m_remoteEnrollee = NULL;
+        std::string uri = std::string("/oic/res?rt=") + PROV_RESOURCE_TYPE;
+        OC::OCPlatform::findResource("", uri,
+                OCConnectivityType::CT_DEFAULT,
+                std::bind(&ESMediatorSimulator::discoverRemoteEnrolleeCbToPutProvRsrc,
+                                                                    this, std::placeholders::_1));
+    }
+
 private:
     bool isValidResourceToTest(std::shared_ptr<OC::OCResource> resource)
     {
@@ -194,6 +242,118 @@ private:
         }
     }
 
+    void getWifiRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode)
+    {
+        try
+        {
+            if(eCode == OC_STACK_OK)
+            {
+                std::cout << "GET request was successful" << std::endl;
+                std::cout << "Resource URI: " << rep.getUri() << std::endl;
+                if(m_getWifiCb != NULL)
+                {
+                    m_getWifiCb(rep);
+                    m_getWifiCb = NULL;
+                }
+            }
+            else
+            {
+                std::cout << "onGET Response error: " << eCode << std::endl;
+                std::exit(-1);
+            }
+        }
+        catch(std::exception& e)
+        {
+            std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
+        }
+    }
+
+    void discoverRemoteEnrolleeCbToGetWifiRsrc(std::shared_ptr<OC::OCResource> resource)
+    {
+        if(!resource->getResourceTypes().at(0).compare(WIFI_RESOURCE_TYPE) && m_getWifiCb)
+        {
+            QueryParamsMap test;
+            resource->get(test, std::bind(
+                    &ESMediatorSimulator::getWifiRsrcCallback, this, std::placeholders::_1,
+                                            std::placeholders::_2, std::placeholders::_3));
+        }
+    }
+
+    void getCloudRsrcCallback(const HeaderOptions& , const OCRepresentation& rep, const int eCode)
+    {
+        try
+        {
+            if(eCode == OC_STACK_OK)
+            {
+                std::cout << "GET request was successful" << std::endl;
+                std::cout << "Resource URI: " << rep.getUri() << std::endl;
+                if(m_getCloudCb != NULL)
+                {
+                    m_getCloudCb(rep);
+                    m_getCloudCb = NULL;
+                }
+            }
+            else
+            {
+                std::cout << "onGET Response error: " << eCode << std::endl;
+                std::exit(-1);
+            }
+        }
+        catch(std::exception& e)
+        {
+            std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
+        }
+    }
+
+
+    void discoverRemoteEnrolleeCbToGetCloudRsrc(std::shared_ptr<OC::OCResource> resource)
+    {
+        if(!resource->getResourceTypes().at(0).compare(CLOUD_RESOURCE_TYPE) && m_getCloudCb)
+        {
+            QueryParamsMap test;
+            resource->get(test, std::bind(
+                    &ESMediatorSimulator::getCloudRsrcCallback, this, std::placeholders::_1,
+                                            std::placeholders::_2, std::placeholders::_3));
+        }
+    }
+
+    void getDevConfCallback(const HeaderOptions&, const OCRepresentation& rep, const int eCode)
+    {
+        try
+        {
+            if(eCode == OC_STACK_OK)
+            {
+                std::cout << "GET request was successful" << std::endl;
+                std::cout << "Resource URI: " << rep.getUri() << std::endl;
+                if(m_getDevConfCb != NULL)
+                {
+                    m_getDevConfCb(rep);
+                    m_getDevConfCb = NULL;
+                }
+            }
+            else
+            {
+                std::cout << "onGET Response error: " << eCode << std::endl;
+                std::exit(-1);
+            }
+        }
+        catch(std::exception& e)
+        {
+            std::cout << "Exception: " << e.what() << " in onGet" << std::endl;
+        }
+    }
+
+    void discoverRemoteEnrolleeCbToGetDevConf(std::shared_ptr<OC::OCResource> resource)
+    {
+        if(!resource->getResourceTypes().at(0).compare(DEV_RESOURCE_TYPE) && m_getDevConfCb)
+        {
+            QueryParamsMap test;
+            resource->get(test, std::bind(
+                    &ESMediatorSimulator::getDevConfCallback, this, std::placeholders::_1,
+                                            std::placeholders::_2, std::placeholders::_3));
+        }
+    }
+
     void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
     {
         if(m_getStatusCb != NULL)
@@ -294,6 +454,37 @@ private:
             }
         }
     }
+
+    void putProvRsrcCallabck(const HeaderOptions&, const OCRepresentation& rep, const int )
+    {
+        cout << "putProvRsrcCallback is called" << endl;
+
+        if(m_provPutCb != NULL){
+            m_provPutCb(rep);
+            m_provPutCb = NULL;
+        }
+
+    }
+
+    void discoverRemoteEnrolleeCbToPutProvRsrc(std::shared_ptr<OC::OCResource> resource)
+    {
+        m_remoteEnrollee = EasySetup::getInstance()->createRemoteEnrollee(resource);
+
+        if(m_remoteEnrollee != NULL)
+        {
+            if(m_provPutCb)
+            {
+                OCRepresentation rep;
+                QueryParamsMap test;
+                resource->put(rep, test, std::bind(
+                        &ESMediatorSimulator::putProvRsrcCallabck, this, std::placeholders::_1,
+                                                std::placeholders::_2, std::placeholders::_3));
+                }
+
+        }
+
+    }
+
 };