replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / unittest / NSProviderSimulator.h
index 36a8f54..eebae7e 100644 (file)
@@ -22,6 +22,7 @@
 #define _NS_PROVIDER_SIMULATOR_H_
 
 #include <iostream>
+#include <memory>
 
 #include "OCPlatform.h"
 #include "OCApi.h"
@@ -34,15 +35,33 @@ namespace
         NS_NOTIFICATION,
         NS_MESSAGE,
         NS_SYNC,
+        NS_TOPIC,
+    };
+    enum class messageType
+    {
+        NS_ALLOW = 1,
+        NS_DENY,
+        NS_TOPIC,
     };
 }
 
 class NSProviderSimulator
 {
+public:
+    enum class TopicAllowState
+    {
+        DENY,
+        ALLOW
+    };
+    typedef std::pair<std::string, TopicAllowState> NS_TopicState;
+    typedef std::map<std::string, TopicAllowState> NS_TopicStateList;
+
+    typedef std::list<std::string> NS_TopicList;
 private:
     OCResourceHandle m_notificationHandle;
     OCResourceHandle m_messageHandle;
     OCResourceHandle m_syncHandle;
+    OCResourceHandle m_topicHandle;
     OC::OCRepresentation m_syncRep;
     OC::OCRepresentation m_messageRep;
     int m_accepter;
@@ -50,17 +69,26 @@ private:
     std::string m_notificationUri;
     std::string m_messageUri;
     std::string m_syncUri;
+    std::string m_topicUri;
+    NS_TopicList m_topicList;
+    NS_TopicStateList m_allowedTopicList;
 
     OC::ObservationIds m_syncObservers;
 
 public:
     NSProviderSimulator()
-    : m_notificationHandle(), m_messageHandle(), m_syncHandle(),
+    : m_notificationHandle(), m_messageHandle(), m_syncHandle(), m_topicHandle(),
       m_syncRep(), m_messageRep(), m_accepter(0),
       m_notificationUri(std::string("/notification")),
       m_messageUri(std::string("/message")),
       m_syncUri(std::string("/sync")),
-      m_syncObservers() { };
+      m_topicUri(std::string("/topic")),
+      m_topicList(),
+      m_allowedTopicList(),
+      m_syncObservers()
+    {
+
+    };
 
     ~NSProviderSimulator() = default;
 
@@ -94,9 +122,13 @@ private:
                 {
                     std::string msgUri = m_notificationUri + m_messageUri;
                     std::string syncUri = m_notificationUri + m_syncUri;
-                    rep.setValue("ACCEPTER", m_accepter);
-                    rep.setValue("MESSAGE_URI", msgUri);
-                    rep.setValue("SYNC_URI", syncUri);
+                    std::string topicUri = m_notificationUri + m_topicUri;
+                    std::string providerId = "123456789012345678901234567890123456";
+                    rep.setValue("x.org.iotivity.ns.subcontrollability", m_accepter);
+                    rep.setValue("x.org.iotivity.ns.messageuri", msgUri);
+                    rep.setValue("x.org.iotivity.ns.syncuri", syncUri);
+                    rep.setValue("x.org.iotivity.ns.topicuri", topicUri);
+                    rep.setValue("x.org.iotivity.ns.providerid", providerId);
                 }
                 else if (type == requestType::NS_SYNC)
                 {
@@ -106,6 +138,35 @@ private:
                 {
                     rep = m_messageRep;
                 }
+                else if (type == requestType::NS_TOPIC)
+                {
+                    if (m_allowedTopicList.empty())
+                    {
+                        std::for_each (m_topicList.begin(), m_topicList.end(),
+                            [this](const std::string & topic)
+                            {
+                                m_allowedTopicList.insert(
+                                        std::make_pair<std::string, TopicAllowState>(
+                                                std::string(topic), TopicAllowState::DENY));
+                            }
+                        );
+                    }
+
+                    std::vector<OC::OCRepresentation> topicArr;
+
+                    std::for_each (m_allowedTopicList.begin(), m_allowedTopicList.end(),
+                        [& topicArr](const NS_TopicState & topicState)
+                        {
+                            OC::OCRepresentation topic;
+                            topic.setValue("x.org.iotivity.ns.topicname", topicState.first);
+                            topic.setValue("x.org.iotivity.ns.topicstate", (int) topicState.second);
+                            topicArr.push_back(topic);
+                        }
+                    );
+
+                    rep.setValue<std::vector<OC::OCRepresentation>>
+                        ("x.org.iotivity.ns.topiclist", topicArr);
+                }
                 else
                 {
                     return NULL;
@@ -115,19 +176,39 @@ private:
                 return response;
             }
 
-            else if (request == "POST" && type == requestType::NS_SYNC)
+            else if (request == "POST")
             {
-                m_syncRep = requests->getResourceRepresentation();
+                if (type == requestType::NS_SYNC)
+                {
+                    m_syncRep = requests->getResourceRepresentation();
 
-                std::cout << "Receive POST at Sync" << std::endl;
-                std::cout << "Sync Id : " << m_syncRep.getValueToString("ID") << std::endl;
-                std::cout << "Sync State : " << m_syncRep.getValueToString("STATE") << std::endl;
+                    std::cout << "Receive POST for Sync" << std::endl;
+                    std::cout << "provider Id : " << m_syncRep.getValueToString("x.org.iotivity.ns.providerid") << std::endl;
+                    std::cout << "Sync State : " << m_syncRep.getValueToString("x.org.iotivity.ns.state") << std::endl;
 
-                response->setResourceRepresentation(m_syncRep);
+                    response->setResourceRepresentation(m_syncRep);
 
-                OC::OCPlatform::notifyListOfObservers(m_syncHandle, m_syncObservers, response);
+                    OC::OCPlatform::notifyAllObservers(m_syncHandle);
 
-                return response;
+                    return response;
+                }
+                else if (type == requestType::NS_TOPIC)
+                {
+                    auto receivePayload =
+                            requests->getResourceRepresentation()
+                            .getValue<std::vector<OC::OCRepresentation>>("x.org.iotivity.ns.topiclist");
+
+                    std::for_each (receivePayload.begin(), receivePayload.end(),
+                          [this](const OC::OCRepresentation & rep)
+                          {
+                              auto tmp = m_allowedTopicList.find(rep.getValueToString("x.org.iotivity.ns.topicname"));
+                              if (tmp != m_allowedTopicList.end())
+                              {
+                                  tmp->second = (TopicAllowState) rep.getValue<int>("x.org.iotivity.ns.topicstate");
+                              }
+                          }
+                    );
+                }
             }
         }
 
@@ -150,6 +231,25 @@ private:
                         observationInfo.obsId), m_syncObservers.end());
             }
         }
+        else if (type == requestType::NS_MESSAGE)
+        {
+            OC::OCRepresentation rep;
+            std::string providerId = "123456789012345678901234567890123456";
+            rep.setValue<int>("x.org.iotivity.ns.messageid", (int)messageType::NS_ALLOW);
+            rep.setValue("x.org.iotivity.ns.providerid", providerId);
+
+            auto response = std::make_shared<OC::OCResourceResponse>();
+            response->setRequestHandle(requests->getRequestHandle());
+            response->setResourceHandle(requests->getResourceHandle());
+            response->setErrorCode(200);
+            response->setResponseResult(OC_EH_OK);
+            response->setResourceRepresentation(rep);
+
+            OC::ObservationIds ids;
+            ids.push_back(requests->getObservationInfo().obsId);
+
+            OC::OCPlatform::notifyListOfObservers(m_messageHandle, ids, response);
+        }
     }
 
     OCEntityHandlerResult entityHandler(
@@ -199,30 +299,80 @@ public:
         OC::OCPlatform::notifyAllObservers(m_messageHandle);
     }
 
-    void notifyMessage(const std::string & id, const std::string & title, const std::string & content)
+    void notifyMessage(const uint64_t & id, const std::string & title, const std::string & content)
     {
         setMessage(id, title, content);
         notifyMessage();
     }
 
-    void sendRead(const std::string & id)
+    void sendRead(const uint64_t & id)
     {
-        m_syncRep.setValue("ID", id);
-        m_syncRep.setValue("STATE", (int)0);
+        std::string providerId = "123456789012345678901234567890123456";
+        m_syncRep.setValue<int>("x.org.iotivity.ns.messageid", id);
+        m_syncRep.setValue("x.org.iotivity.ns.state", (int)1);
+        m_syncRep.setValue("x.org.iotivity.ns.providerid", providerId);
         OC::OCPlatform::notifyAllObservers(m_syncHandle);
     }
-    void sendDismiss(const std::string & id)
+    void sendDismiss(const uint64_t & id)
     {
-        m_syncRep.setValue("ID", id);
-        m_syncRep.setValue("STATE", (int)1);
+        std::string providerId = "123456789012345678901234567890123456";
+        m_syncRep.setValue<int>("x.org.iotivity.ns.messageid", id);
+        m_syncRep.setValue("x.org.iotivity.ns.state", (int)2);
+        m_syncRep.setValue("x.org.iotivity.ns.providerid", providerId);
         OC::OCPlatform::notifyAllObservers(m_syncHandle);
     }
 
-    void setMessage(const std::string & id, const std::string & title, const std::string & content)
+    void setMessage(const uint64_t & id, const std::string & title, const std::string & content)
+    {
+        std::string providerId = "123456789012345678901234567890123456";
+        m_messageRep.setValue<int>("x.org.iotivity.ns.messageid", id);
+        m_messageRep.setValue<std::string>("x.org.iotivity.ns.title", title);
+        m_messageRep.setValue<std::string>("x.org.iotivity.ns.contenttext", content);
+        m_messageRep.setValue<std::string>("x.org.iotivity.ns.providerid", providerId);
+    }
+
+    void setTopics(const NS_TopicList & topics)
+    {
+        bool isChanged = false;
+        std::for_each (topics.begin(), topics.end(),
+            [this, & isChanged](const std::string & topic)
+            {
+                auto found = std::find(
+                        this->m_topicList.begin(), this->m_topicList.end(), topic);
+                if (found == this->m_topicList.end())
+                {
+                    this->m_topicList.push_back(topic);
+                    isChanged = true;
+                }
+            });
+
+        if (isChanged)
+        {
+            this->notifyMessage((uint64_t)messageType::NS_TOPIC, "", "");
+        }
+    }
+
+    NS_TopicList getTopics() const
     {
-        m_messageRep.setValue("ID", id);
-        m_messageRep.setValue("TITLE", title);
-        m_messageRep.setValue("CONTENTTEXT", content);
+        return m_topicList;
+    }
+
+    void updateTopicState(const NS_TopicStateList & allowedTopics)
+    {
+        std::for_each (allowedTopics.begin(), allowedTopics.end(),
+            [this](const NS_TopicState & allowedTopic)
+            {
+                auto found = this->m_allowedTopicList.find(allowedTopic.first);
+                if (found != this->m_allowedTopicList.end())
+                {
+                    found->second = allowedTopic.second;
+                }
+            });
+    }
+
+    NS_TopicStateList getTopicAllowState() const
+    {
+        return m_allowedTopicList;
     }
 
     void deleteNotificationResource()
@@ -230,6 +380,9 @@ public:
         OC::OCPlatform::unregisterResource(m_notificationHandle);
         OC::OCPlatform::unregisterResource(m_messageHandle);
         OC::OCPlatform::unregisterResource(m_syncHandle);
+        OC::OCPlatform::unregisterResource(m_topicHandle);
+        m_allowedTopicList.clear();
+        m_topicList.clear();
     }
 
     void createNotificationResource()
@@ -247,11 +400,28 @@ public:
         OC::OCPlatform::startPresence(30);
 
         std::string notificationUri = m_notificationUri;
-        std::string resourceTypeName = "oic.r.message.notification";
+        std::string resourceTypeName = "x.org.iotivity.notification.topic";
         std::string resourceInterface = OC::DEFAULT_INTERFACE;
 
         uint8_t resourceProperty = OC_OBSERVABLE;
-        std::string childUri = uri + m_messageUri;
+        std::string childUri = uri + m_topicUri;
+        try
+        {
+            OC::OCPlatform::registerResource(
+                    m_topicHandle, childUri,
+                    resourceTypeName, resourceInterface,
+                    std::bind(& NSProviderSimulator::entityHandler, this,
+                            std::placeholders::_1, requestType::NS_TOPIC),
+                            resourceProperty);
+        }
+        catch(std::exception & e)
+        {
+            std::cout << e.what() << std::endl;
+        }
+
+        //resourceProperty |= OC_OBSERVABLE;
+        resourceTypeName = "x.org.iotivity.notification.message";
+        childUri = uri + m_messageUri;
         try
         {
             OC::OCPlatform::registerResource(
@@ -260,12 +430,13 @@ public:
                     std::bind(& NSProviderSimulator::entityHandler, this,
                             std::placeholders::_1, requestType::NS_MESSAGE),
                             resourceProperty);
-        } catch (std::exception & e)
+        }
+        catch (std::exception & e)
         {
             std::cout << e.what() << std::endl;
         }
 
-        resourceTypeName = "oic.r.sync.notification";
+        resourceTypeName = "x.org.iotivity.notification.sync";
         childUri = uri + m_syncUri;
         try
         {
@@ -275,13 +446,14 @@ public:
                     std::bind(& NSProviderSimulator::entityHandler, this,
                             std::placeholders::_1, requestType::NS_SYNC),
                             resourceProperty);
-        } catch (std::exception & e)
+        }
+        catch (std::exception & e)
         {
             std::cout << e.what() << std::endl;
         }
 
-        resourceProperty = OC_DISCOVERABLE;
-        resourceTypeName = "oic.r.notification";
+        resourceProperty |= OC_DISCOVERABLE;
+        resourceTypeName = "x.org.iotivity.notification";
         try
         {
             OC::OCPlatform::registerResource(
@@ -290,7 +462,8 @@ public:
                     std::bind(& NSProviderSimulator::entityHandler, this,
                             std::placeholders::_1, requestType::NS_NOTIFICATION),
                             resourceProperty);
-        } catch (std::exception & e)
+        }
+        catch (std::exception & e)
         {
             std::cout << e.what() << std::endl;
         }