replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / unittest / NSConsumerSimulator.h
index d12e30e..161a9ee 100644 (file)
@@ -34,11 +34,15 @@ private:
     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,7 +53,7 @@ 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);
@@ -59,18 +63,25 @@ public:
     {
         if (m_syncResource == nullptr)
         {
-            std::cout << "m_syncResource is null" << std::endl;
             return;
         }
 
         OC::OCRepresentation rep;
-        rep.setValue("PROVIDER_ID", providerID);
-        rep.setValue("MESSAGE_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);
     }
 
+    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)
@@ -81,50 +92,38 @@ 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;
-
         if(resource->uri() == "/notification")
         {
-            resource->get(OC::QueryParamsMap(),
-                    std::bind(&NSConsumerSimulator::onGet, this,
-                            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, resource),
-                    OC::QualityOfService::LowQos);
+            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("consumerid"), std::string("123456789012345678901234567890123456")));
+        map.insert(std::pair<std::string,std::string>(std::string("x.org.iotivity.ns.consumerid"),
+                std::string("123456789012345678901234567890123456")));
 
         try
         {
-            std::cout << "resourc : host " << resource->host() << std::endl;
-            std::cout << "resourc : uri " << resource->uri() << std::endl;
-            std::cout << " resource->connectivityType() " <<  resource->connectivityType() << std::endl;
-            std::cout << "resourc : getResourceInterfaces " << resource->getResourceInterfaces()[0] << std::endl;
-            std::cout << "resourc : getResourceTypes " << resource->getResourceTypes()[0] << std::endl;
-
-
-            std::vector<std::string> rts{"oic.r.notification"};
+            std::vector<std::string> rts{"x.org.iotivity.notification"};
 
-            std::shared_ptr<OC::OCResource> msgResource
+            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()));
 
-            msgResource->observe(OC::ObserveType::Observe, map,
+            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),
@@ -145,31 +144,90 @@ private:
                         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("MESSAGE_ID")
-                && rep.getValue<int>("MESSAGE_ID") != 1)
+        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.getValue<int>("ID") << std::endl;
-            std::cout << "TITLE : " << rep.getValueToString("TITLE") << std::endl;
-            std::cout << "CONTENT : " << rep.getValueToString("CONTENT") << std::endl;
-            m_messageFunc(int(rep.getValue<int>("MESSAGE_ID")),
-                          std::string(rep.getValueToString("TITLE")),
-                          std::string(rep.getValueToString("CONTENT")),
-                          std::string(rep.getValueToString("SOURCE")));
+            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")
         {
-            std::cout << "else if (rep.getUri() == sync) " << std::endl;
-            m_syncFunc(int(rep.getValue<int>("STATE")), int(rep.getValue<int>("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);
+    }
 };