From 56190b5008c5f5534f4fa7ae8fc330ad0500706e Mon Sep 17 00:00:00 2001 From: "nikhil.a" Date: Fri, 7 Oct 2016 16:15:22 +0530 Subject: [PATCH] Update and fix of UnitTest for cpp-wrapper 1)Updated new unittest for consumer and provider 2)Fix for removing segmentation fault Change-Id: If06e9606c405a3fa2dace726a5c9320399b06e66 Signed-off-by: nikhil.a Reviewed-on: https://gerrit.iotivity.org/gerrit/12943 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- service/notification/cpp-wrapper/SConscript | 3 + .../unittest/NSConsumerServiceSimulator.h | 45 +++--- .../cpp-wrapper/unittest/NSConsumerServiceTest.cpp | 90 +++++++++-- .../unittest/NSProviderServiceSimulator.h | 168 +++++++++++++++++++-- .../cpp-wrapper/unittest/NSProviderServiceTest.cpp | 12 +- 5 files changed, 267 insertions(+), 51 deletions(-) diff --git a/service/notification/cpp-wrapper/SConscript b/service/notification/cpp-wrapper/SConscript index fa553bd..7dc5d41 100755 --- a/service/notification/cpp-wrapper/SConscript +++ b/service/notification/cpp-wrapper/SConscript @@ -31,3 +31,6 @@ SConscript('consumer/SConscript') if target_os == 'linux': SConscript('examples/linux/SConscript') +# Go to build c++ wrapper unittest +if target_os == 'linux': + SConscript('unittest/SConscript') diff --git a/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h b/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h index cec3045..64bdac9 100644 --- a/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h +++ b/service/notification/cpp-wrapper/unittest/NSConsumerServiceSimulator.h @@ -35,6 +35,7 @@ private: std::shared_ptr m_syncResource; std::shared_ptr m_msgResource; + std::shared_ptr m_topicResource; public: NSConsumerSimulator() @@ -60,7 +61,6 @@ public: { if (m_syncResource == nullptr) { - std::cout << "m_syncResource is null" << std::endl; return; } @@ -90,39 +90,32 @@ 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; 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); + 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("consumerId"), std::string("123456789012345678901234567890123456"))); + map.insert(std::pair(std::string("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 rts{"oic.wk.notification"}; @@ -153,31 +146,41 @@ 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 ) { - std::cout << __func__ << " " << rep.getHost() << " result : " << eCode; - std::cout << " uri : " << rep.getUri() << std::endl; if (rep.getUri() == "/notification/message" && rep.hasAttribute("messageId") && rep.getValue("messageId") != 1) { - std::cout << "ID : " << rep.getValue("messageId") << std::endl; - std::cout << "TITLE : " << rep.getValueToString("title") << std::endl; - std::cout << "CONTENT : " << rep.getValueToString("contentText") << std::endl; m_messageFunc(int(rep.getValue("messageId")), std::string(rep.getValueToString("title")), std::string(rep.getValueToString("contentText")), std::string(rep.getValueToString("source"))); + if(rep.getValue("messageId") == 3) + { + m_topicResource->get(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("state")), int(rep.getValue("messageId"))); } } + void onTopicGet(const OC::HeaderOptions &/*headerOption*/, + const OC::OCRepresentation & /*rep*/ , const int /*eCode*/, + std::shared_ptr /*resource*/) + { + } OCStackResult msgResourceCancelObserve(OC::QualityOfService qos) { diff --git a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp index 87cb5b2..3f1deaf 100644 --- a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp @@ -461,6 +461,83 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPo EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type); } +TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId) +{ + OIC::Service::NSProvider * provider = + OIC::Service::NSConsumerService::getInstance()->getProvider(g_provider->getProviderId()); + int ret = strcmp(provider->getProviderId().c_str(), g_provider->getProviderId().c_str()); + EXPECT_EQ(0, ret); +} + +TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId) +{ + OIC::Service::NSProvider * provider = + OIC::Service::NSConsumerService::getInstance()->getProvider("123456789012345678901234567890123457"); + EXPECT_EQ(provider, (void*)NULL); +} + +TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated) +{ + OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::STOPPED; + mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do( + [this, & revState](OIC::Service::NSProviderState state) + { + std::cout << "Income Changed Callback : " << (int)state << std::endl; + revState = state; + responseCon.notify_all(); + }); + + NSProviderSimulator::NS_TopicList topics; + topics.push_back("1"); + topics.push_back("2"); + topics.push_back("3"); + + g_providerSimul.setTopics(topics); + + std::unique_lock< std::mutex > lock{ mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(OIC::Service::NSProviderState::TOPIC, revState); +} + +TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList) +{ + bool isSame = true; + + NSProviderSimulator::NS_TopicList topics; + topics.push_back("1"); + topics.push_back("2"); + topics.push_back("3"); + + std::list retTopic = g_provider->getTopicList()->getTopicsList(); + auto it1=retTopic.begin(); + auto it2=topics.begin(); + while( it1 != retTopic.end() || it2 != topics.end() ) + { + if((*it1)->getTopicName() != *it2) + { + isSame = false; break; + } + it1++;it2++; + } + + + EXPECT_EQ(true, isSame); +} + +TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer) +{ + OIC::Service::NSTopicsList * retTopic = g_provider->getTopicList(); + for (auto it : retTopic->getTopicsList()) + { + it->setState(OIC::Service::NSTopic::NSTopicState::SUBSCRIBED); + } + OIC::Service::NSResult ret = g_provider->updateTopicList(retTopic); + + EXPECT_EQ(OIC::Service::NSResult::ERROR, ret); +} + + TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider) { OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW; @@ -469,7 +546,6 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider) { std::cout << "Income Changed Callback : " << std::endl; type = state; - responseCon.notify_all(); }); g_providerSimul.deleteNotificationResource(); @@ -480,15 +556,3 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider) EXPECT_EQ(type, OIC::Service::NSProviderState::STOPPED); OIC::Service::NSConsumerService::getInstance()->stop(); } - -//TO add when unsubscibe API is added back -//TEST_F(NotificationServiceConsumerTest, ExpectUnsubscribeSuccess) -//{ -// g_provider->unsubscribe(); -// std::unique_lock< std::mutex > lock{ mutexForCondition }; -// responseCon.wait_for(lock, g_waitForResponse); -// -// g_providerSimul.deleteNotificationResource(); -// OIC::Service::NSConsumerService::getInstance()->stop(); -// -//} diff --git a/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h b/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h index d87303b..2245567 100644 --- a/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h +++ b/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h @@ -22,6 +22,7 @@ #define _NS_PROVIDER_SERVICE_SIMULATOR_H_ #include +#include #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 NS_TopicState; + typedef std::map NS_TopicStateList; + + typedef std::list 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,16 +69,22 @@ 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_topicUri(std::string("/topic")), + m_topicList(), + m_allowedTopicList(), m_syncObservers() { @@ -97,10 +122,12 @@ private: { std::string msgUri = m_notificationUri + m_messageUri; std::string syncUri = m_notificationUri + m_syncUri; + std::string topicUri = m_notificationUri + m_topicUri; std::string providerId = "123456789012345678901234567890123456"; - rep.setValue("subControllability", (bool) m_accepter); + rep.setValue("subControllability", m_accepter); rep.setValue("messageUri", msgUri); rep.setValue("syncUri", syncUri); + rep.setValue("topicUri", topicUri); rep.setValue("providerId", providerId); } else if (type == requestType::NS_SYNC) @@ -111,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(topic), TopicAllowState::DENY)); + } + ); + } + + std::vector topicArr; + + std::for_each (m_allowedTopicList.begin(), m_allowedTopicList.end(), + [& topicArr](const NS_TopicState & topicState) + { + OC::OCRepresentation topic; + topic.setValue("topicName", topicState.first); + topic.setValue("topicState", (int) topicState.second); + topicArr.push_back(topic); + } + ); + + rep.setValue> + ("topicList", topicArr); + } else { return NULL; @@ -120,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 << "provider Id : " << m_syncRep.getValueToString("providerId") << 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("providerId") << std::endl; + std::cout << "Sync State : " << m_syncRep.getValueToString("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>("topicList"); + + std::for_each (receivePayload.begin(), receivePayload.end(), + [this](const OC::OCRepresentation & rep) + { + auto tmp = m_allowedTopicList.find(rep.getValueToString("topicName")); + if (tmp != m_allowedTopicList.end()) + { + tmp->second = (TopicAllowState) rep.getValue("topicState"); + } + } + ); + } } } @@ -159,7 +235,7 @@ private: { OC::OCRepresentation rep; std::string providerId = "123456789012345678901234567890123456"; - rep.setValue("messageId", (int)1); + rep.setValue("messageId", (int)messageType::NS_ALLOW); rep.setValue("providerId", providerId); auto response = std::make_shared(); @@ -255,11 +331,58 @@ public: m_messageRep.setValue("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 + { + 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() { 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() @@ -277,11 +400,28 @@ public: OC::OCPlatform::startPresence(30); std::string notificationUri = m_notificationUri; - std::string resourceTypeName = "oic.wk.notification.message"; + std::string resourceTypeName = "oic.wk.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 = "oic.wk.notification.message"; + childUri = uri + m_messageUri; try { OC::OCPlatform::registerResource( @@ -312,7 +452,7 @@ public: std::cout << e.what() << std::endl; } - resourceProperty = OC_DISCOVERABLE; + resourceProperty |= OC_DISCOVERABLE; resourceTypeName = "oic.wk.notification"; try { diff --git a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp index b5e76af..a9acd13 100644 --- a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp @@ -258,10 +258,8 @@ TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue msg->setTitle(std::string("Title")); msg->setContentText(std::string("ContentText")); msg->setSourceName(std::string("OCF")); - OIC::Service::NSProviderService::getInstance()->registerTopic("OCF_TOPIC1"); - g_consumer->setTopic("OCF_TOPIC1"); + OIC::Service::NSProviderService::getInstance()->sendMessage(msg); - OIC::Service::NSProviderService::getInstance()->unregisterTopic("OCF_TOPIC1"); std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); } @@ -481,6 +479,14 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons responseCon.wait_for(lock, g_waitForResponse); } +TEST_F(NotificationProviderServiceTest, ExpectFailSendMessage) +{ + OIC::Service::NSResult result = OIC::Service::NSResult::OK; + result = OIC::Service::NSProviderService::getInstance()->sendMessage(nullptr); + + EXPECT_EQ(result, OIC::Service::NSResult::ERROR); +} + TEST_F(NotificationProviderServiceTest, CancelObserves) { bool ret = g_consumerSimul.cancelObserves(); -- 2.7.4