replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / unittest / NSConsumerSimulator.h
index f1177ba..161a9ee 100644 (file)
 class NSConsumerSimulator
 {
 private:
-    std::function<void(const std::string&, const std::string&, const std::string&)> m_messageFunc;
-    std::function<void(int, const std::string&)> m_syncFunc;
+    std::function<void(const int&, const std::string&, const std::string&,
+            const std::string&)> m_messageFunc;
+    std::function<void(const int&, const int&)> m_syncFunc;
 
     std::shared_ptr<OC::OCResource> m_syncResource;
+    std::shared_ptr<OC::OCResource> m_msgResource;
+    std::shared_ptr<OC::OCResource> m_topicResource;
 
+    bool isTopicPost;
 
 public:
     NSConsumerSimulator()
     : m_messageFunc(), m_syncFunc(),
-      m_syncResource() { };
+      m_syncResource(), isTopicPost(false) { };
     ~NSConsumerSimulator() = default;
 
     NSConsumerSimulator(const NSConsumerSimulator &) = delete;
@@ -49,29 +53,38 @@ public:
 
     void findProvider()
     {
-        OC::OCPlatform::findResource("", std::string("/oic/res?rt=oic.r.notification"),
+        OC::OCPlatform::findResource("", std::string("/oic/res?rt=x.org.iotivity.notification"),
                 OCConnectivityType::CT_DEFAULT,
                 std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
                 OC::QualityOfService::LowQos);
     }
 
-    void syncToProvider(int & type, const std::string & id)
+    void syncToProvider(int & type, const int & id, const std::string & providerID)
     {
         if (m_syncResource == nullptr)
         {
-            std::cout << "m_syncResource is null" << std::endl;
             return;
         }
 
         OC::OCRepresentation rep;
-        rep.setValue("ID", id);
-        rep.setValue("STATE", type);
+        rep.setValue("x.org.iotivity.ns.providerid", providerID);
+        rep.setValue("x.org.iotivity.ns.messageid", id);
+        rep.setValue("x.org.iotivity.ns.state", type);
 
         m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
     }
 
-    void setCallback(const std::function<void(const std::string&, const std::string&, const std::string&)> & messageFunc,
-            const std::function<void(int, const std::string&)> & syncFunc)
+    bool cancelObserves()
+    {
+        if(!msgResourceCancelObserve(OC::QualityOfService::HighQos) &&
+                !syncResourceCancelObserve(OC::QualityOfService::HighQos))
+            return true;
+        return false;
+    }
+
+    void setCallback(std::function<void(const int&, const std::string&,
+            const std::string&, const std::string&)> messageFunc,
+            const std::function<void(const int&, const int&)> & syncFunc)
     {
         m_messageFunc = messageFunc;
         m_syncFunc = syncFunc;
@@ -79,68 +92,142 @@ public:
 
 private:
     static void onPost(const OC::HeaderOptions &/*headerOption*/,
-                const OC::OCRepresentation & /*rep*/ , const int eCode)
+                const OC::OCRepresentation & /*rep*/ , const int /*eCode*/)
     {
-        std::cout << __func__ << " result : " << eCode << std::endl;
     }
     void findResultCallback(std::shared_ptr<OC::OCResource> resource)
     {
-        std::cout << __func__ << " " << resource->host() << std::endl;
-        resource->get(OC::QueryParamsMap(),
-                std::bind(&NSConsumerSimulator::onGet, this,
-                        std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, resource),
-                OC::QualityOfService::LowQos);
+        if(resource->uri() == "/notification")
+        {
+            resource->get(std::string("x.org.iotivity.notification"), std::string("oic.if.baseline"),
+                    OC::QueryParamsMap(), std::bind(&NSConsumerSimulator::onGet, this,
+                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+                            resource), OC::QualityOfService::LowQos);
+        }
     }
     void onGet(const OC::HeaderOptions &/*headerOption*/,
-            const OC::OCRepresentation & rep , const int eCode,
+            const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
             std::shared_ptr<OC::OCResource> resource)
     {
-        std::cout << __func__ << " " << rep.getHost() << " result : " << eCode << std::endl;
+        OC::QueryParamsMap map;
+        map.insert(std::pair<std::string,std::string>(std::string("x.org.iotivity.ns.consumerid"),
+                std::string("123456789012345678901234567890123456")));
 
-        std::shared_ptr<OC::OCResource> msgResource
-            = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/message",
-                    resource->connectivityType(), false, resource->getResourceTypes(),
-                    resource->getResourceInterfaces());
+        try
+        {
+            std::vector<std::string> rts{"x.org.iotivity.notification"};
+
+            m_msgResource
+                = OC::OCPlatform::constructResourceObject(
+                        std::string(resource->host()), std::string(resource->uri() + "/message"),
+                        OCConnectivityType(resource->connectivityType()), true, rts,
+                        std::vector<std::string>(resource->getResourceInterfaces()));
+
+            m_msgResource->observe(OC::ObserveType::Observe, map,
+                            std::bind(&NSConsumerSimulator::onObserve, this,
+                                    std::placeholders::_1, std::placeholders::_2,
+                                    std::placeholders::_3, std::placeholders::_4, resource),
+                            OC::QualityOfService::LowQos);
+        }
+        catch(std::exception & e)
+        {
+            std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
+        }
         m_syncResource
             = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
-                    resource->connectivityType(), false, resource->getResourceTypes(),
+                    resource->connectivityType(), true, resource->getResourceTypes(),
                     resource->getResourceInterfaces());
 
-        msgResource->observe(OC::ObserveType::Observe, OC::QueryParamsMap(),
-                std::bind(&NSConsumerSimulator::onObserve, this,
-                        std::placeholders::_1, std::placeholders::_2,
-                        std::placeholders::_3, std::placeholders::_4, resource),
-                OC::QualityOfService::LowQos);
-        m_syncResource->observe(OC::ObserveType::Observe, OC::QueryParamsMap(),
+        m_syncResource->observe(OC::ObserveType::Observe, map,
                 std::bind(&NSConsumerSimulator::onObserve, this,
                         std::placeholders::_1, std::placeholders::_2,
                         std::placeholders::_3, std::placeholders::_4, resource),
                 OC::QualityOfService::LowQos);
 
+
+        m_topicResource
+            = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
+                    resource->connectivityType(), true, resource->getResourceTypes(),
+                    resource->getResourceInterfaces());
+
     }
     void onObserve(const OC::HeaderOptions &/*headerOption*/,
-            const OC::OCRepresentation &rep , const int &eCode, const int &,
+            const OC::OCRepresentation &rep , const int & /*eCode*/, const int &,
             std::shared_ptr<OC::OCResource> )
     {
-        std::cout << __func__ << " " << rep.getHost() << " result : " << eCode;
-        std::cout << " uri : " << rep.getUri() << std::endl;
-
-        if (rep.getUri() == "/notification/message" && rep.hasAttribute("ID")
-                && rep.getValueToString("ID") != "0000-0000-0000-0000")
+        if (rep.getUri() == "/notification/message" && rep.hasAttribute("x.org.iotivity.ns.messageid")
+                && rep.getValue<int>("x.org.iotivity.ns.messageid") != 1)
         {
-            std::cout << "ID : " << rep.getValueToString("ID") << std::endl;
-            std::cout << "TITLE : " << rep.getValueToString("TITLE") << std::endl;
-            std::cout << "CONTENT : " << rep.getValueToString("CONTENT") << std::endl;
-            m_messageFunc(std::string(rep.getValueToString("ID")),
-                          std::string(rep.getValueToString("TITLE")),
-                          std::string(rep.getValueToString("CONTENT")));
+            m_messageFunc(int(rep.getValue<int>("x.org.iotivity.ns.messageid")),
+                          std::string(rep.getValueToString("x.org.iotivity.ns.title")),
+                          std::string(rep.getValueToString("x.org.iotivity.ns.contenttext")),
+                          std::string(rep.getValueToString("x.org.iotivity.ns.source")));
+
+            if(rep.getValue<int>("x.org.iotivity.ns.messageid") == 3)
+            {
+                m_topicResource->get(std::string("x.org.iotivity.notification"),
+                        std::string("oic.if.baseline"), OC::QueryParamsMap(),
+                        std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
+                                std::placeholders::_2, std::placeholders::_3, m_topicResource),
+                                OC::QualityOfService::LowQos);
+            }
         }
         else if (rep.getUri() == "/notification/sync")
         {
-            m_syncFunc(int(rep.getValue<int>("STATE")),
-                       std::string(rep.getValueToString("ID")));
+            m_syncFunc(int(rep.getValue<int>("x.org.iotivity.ns.state")), int(rep.getValue<int>("x.org.iotivity.ns.messageid")));
         }
     }
+
+    void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
+            const OC::OCRepresentation & rep , const int /*eCode*/,
+            std::shared_ptr<OC::OCResource> /*resource*/)
+    {
+
+        if(!isTopicPost)
+        {
+            isTopicPost = true;
+            OC::OCRepresentation postRep;
+
+            std::vector<OC::OCRepresentation> topicArr =
+                            rep.getValue<std::vector<OC::OCRepresentation>>("x.org.iotivity.ns.topiclist");
+
+            std::vector<OC::OCRepresentation> postTopicArr;
+
+            for(std::vector<OC::OCRepresentation>::iterator it = topicArr.begin();
+                    it != topicArr.end(); ++it)
+            {
+                /* std::cout << *it; ... */
+                OC::OCRepresentation topic = *it;
+                OC::OCRepresentation postTopic;
+
+                postTopic.setValue("x.org.iotivity.ns.topicname", topic.getValueToString("x.org.iotivity.ns.topicname"));
+                postTopic.setValue("x.org.iotivity.ns.topicstate", (int) topic.getValue<int>("x.org.iotivity.ns.topicstate"));
+
+                postTopicArr.push_back(topic);
+
+//                std::cout << "tName : " << tName << std::endl;
+//                std::cout << "tState : " << tState << std::endl;
+            }
+
+            postRep.setValue<std::vector<OC::OCRepresentation>>
+                ("x.org.iotivity.ns.topiclist", postTopicArr);
+
+            OC::QueryParamsMap map;
+            map.insert(std::pair<std::string,std::string>(std::string("x.org.iotivity.ns.consumerid"),
+                    std::string("123456789012345678901234567890123456")));
+            m_topicResource->post(postRep, map, &onPost, OC::QualityOfService::LowQos);
+        }
+    }
+
+    OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
+    {
+        return m_msgResource->cancelObserve(qos);
+    }
+
+    OCStackResult syncResourceCancelObserve(OC::QualityOfService qos)
+    {
+        return m_syncResource->cancelObserve(qos);
+    }
 };