X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fnotification%2Funittest%2FNSConsumerSimulator.h;h=161a9ee91addece6beac7a93d7d18b94d69c2e94;hb=7f00f942c39b7bc27c7eeecf213a239c3fe4173c;hp=f1177ba09a4c97f4c8b6c7533cc956af6577a069;hpb=8afbcc232fc00c4bfe8b13cf85a9c7111a74f4dc;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/notification/unittest/NSConsumerSimulator.h b/service/notification/unittest/NSConsumerSimulator.h index f1177ba..161a9ee 100644 --- a/service/notification/unittest/NSConsumerSimulator.h +++ b/service/notification/unittest/NSConsumerSimulator.h @@ -29,16 +29,20 @@ class NSConsumerSimulator { private: - std::function m_messageFunc; - std::function m_syncFunc; + std::function m_messageFunc; + std::function m_syncFunc; std::shared_ptr m_syncResource; + std::shared_ptr m_msgResource; + std::shared_ptr 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 & messageFunc, - const std::function & syncFunc) + bool cancelObserves() + { + if(!msgResourceCancelObserve(OC::QualityOfService::HighQos) && + !syncResourceCancelObserve(OC::QualityOfService::HighQos)) + return true; + return false; + } + + void setCallback(std::function messageFunc, + const std::function & 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 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 resource) { - std::cout << __func__ << " " << rep.getHost() << " result : " << eCode << std::endl; + OC::QueryParamsMap map; + map.insert(std::pair(std::string("x.org.iotivity.ns.consumerid"), + std::string("123456789012345678901234567890123456"))); - std::shared_ptr msgResource - = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/message", - resource->connectivityType(), false, resource->getResourceTypes(), - resource->getResourceInterfaces()); + try + { + std::vector 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(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 ) { - 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("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("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("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("STATE")), - std::string(rep.getValueToString("ID"))); + m_syncFunc(int(rep.getValue("x.org.iotivity.ns.state")), int(rep.getValue("x.org.iotivity.ns.messageid"))); } } + + void onTopicGet(const OC::HeaderOptions &/*headerOption*/, + const OC::OCRepresentation & rep , const int /*eCode*/, + std::shared_ptr /*resource*/) + { + + if(!isTopicPost) + { + isTopicPost = true; + OC::OCRepresentation postRep; + + std::vector topicArr = + rep.getValue>("x.org.iotivity.ns.topiclist"); + + std::vector postTopicArr; + + for(std::vector::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("x.org.iotivity.ns.topicstate")); + + postTopicArr.push_back(topic); + +// std::cout << "tName : " << tName << std::endl; +// std::cout << "tState : " << tState << std::endl; + } + + postRep.setValue> + ("x.org.iotivity.ns.topiclist", postTopicArr); + + OC::QueryParamsMap map; + map.insert(std::pair(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); + } };