From 46319acf8dcac32150818dbe4b788b5e5e5028e9 Mon Sep 17 00:00:00 2001 From: Poovizhi Date: Wed, 8 Mar 2017 18:30:33 +0530 Subject: [PATCH] [IOT-1905] Fix for Unit test in Notification service in C++ layer Dependency of hippomocks is removed in notificaiton service Change-Id: I9705e2d92a4eec4addecb5a51893a7898d056867 Signed-off-by: Poovizhi Reviewed-on: https://gerrit.iotivity.org/gerrit/17779 Reviewed-by: Uze Choi Tested-by: Uze Choi --- .../cpp-wrapper/unittest/NSConsumerServiceTest.cpp | 279 ++++++------------- .../unittest/NSProviderServiceSimulator.h | 13 +- .../cpp-wrapper/unittest/NSProviderServiceTest.cpp | 298 ++++++++------------- .../notification/cpp-wrapper/unittest/SConscript | 8 +- 4 files changed, 217 insertions(+), 381 deletions(-) mode change 100755 => 100644 service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp mode change 100755 => 100644 service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h mode change 100755 => 100644 service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp diff --git a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp old mode 100755 new mode 100644 index caa1502..ca7a45b --- a/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSConsumerServiceTest.cpp @@ -19,7 +19,6 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include -#include #include #include #include @@ -54,13 +53,20 @@ namespace NS_SELECTION_PROVIDER = 1 }; + OIC::Service::NSProviderState g_expectedState = OIC::Service::NSProviderState::DENY; + OIC::Service::NSProviderState g_revState = OIC::Service::NSProviderState::DENY; + + uint64_t g_messageId = 0; + + OIC::Service::NSSyncInfo::NSSyncType g_expectedSyncType = + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; + OIC::Service::NSSyncInfo::NSSyncType g_syncType = + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; + } class TestWithMock: public testing::Test { - public: - MockRepository mocks; - protected: virtual ~TestWithMock() noexcept(noexcept(std::declval().~Test())) { @@ -69,15 +75,7 @@ class TestWithMock: public testing::Test virtual void TearDown() { - try - { - mocks.VerifyAll(); - } - catch (...) - { - mocks.reset(); - throw; - } + } }; @@ -87,29 +85,46 @@ class NotificationServiceConsumerTest : public TestWithMock NotificationServiceConsumerTest() = default; ~NotificationServiceConsumerTest() = default; - static void ProviderDiscoveredCallbackEmpty( std::shared_ptr ) + static void ProviderDiscoveredCallback( std::shared_ptr provider) { std::cout << __func__ << std::endl; + g_provider = provider; + responseCon.notify_all(); } - static void NotificationReceivedCallbackEmpty( OIC::Service::NSMessage ) + static void NotificationReceivedCallback( OIC::Service::NSMessage message) { - std::cout << __func__ << std::endl; + std::cout << "Income Notification : " << message.getMessageId() << std::endl; + g_messageId = message.getMessageId(); + responseCon.notify_all(); } - static void SyncCallbackEmpty(OIC::Service::NSSyncInfo) + static void SyncCallback(OIC::Service::NSSyncInfo sync) { std::cout << __func__ << std::endl; + g_syncType = sync.getState(); + if (g_expectedSyncType == sync.getState()) + { + std::cout << "[Expected]" << __func__ << std::endl; + responseCon.notify_all(); + } } - static void FoundResourceEmpty(std::shared_ptr< OC::OCResource >) + static void FoundResource(std::shared_ptr< OC::OCResource >) { std::cout << __func__ << std::endl; } - static void ProviderChangedCallbackEmpty( OIC::Service::NSProviderState ) + static void ProviderChangedCallback( OIC::Service::NSProviderState state) { std::cout << __func__ << std::endl; + + if (g_expectedState == state) + { + g_revState = state; + std::cout << "[Expected]" << __func__ << std::endl; + responseCon.notify_all(); + } } protected: @@ -153,7 +168,7 @@ class NotificationServiceConsumerTest : public TestWithMock TEST_F(NotificationServiceConsumerTest, StartConsumerPositive) { - OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty); + OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallback); } TEST_F(NotificationServiceConsumerTest, StopConsumerPositive) @@ -163,15 +178,7 @@ TEST_F(NotificationServiceConsumerTest, StopConsumerPositive) TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst) { - mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do( - [this]( std::shared_ptr provider) - { - std::cout << "Call Discovered" << std::endl; - std::cout << provider->getProviderId() << std::endl; - responseCon.notify_all(); - }); - - OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty); + OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallback); g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER); g_providerSimul.createNotificationResource(); @@ -192,16 +199,7 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStart responseCon.wait_for(lock, g_waitForResponse); } - mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do( - [this]( std::shared_ptr provider) - { - std::cout << "Call Discovered" << std::endl; - g_provider = provider; - std::cout << g_provider->getProviderId() << std::endl; - responseCon.notify_all(); - }); - - OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty); + OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallback); std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); @@ -213,14 +211,6 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenStart TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenRescan) { g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER); - mocks.OnCallFunc(ProviderDiscoveredCallbackEmpty).Do( - [this]( std::shared_ptr provider) - { - std::cout << "Call Discovered" << std::endl; - g_provider = provider; - std::cout << g_provider->getProviderId() << std::endl; - responseCon.notify_all(); - }); OIC::Service::NSConsumerService::getInstance()->rescanProvider(); @@ -230,28 +220,19 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithNonAccepterWhenResca TEST_F(NotificationServiceConsumerTest, ExpectSubscribeSuccess) { - OIC::Service::NSProviderState revState = OIC::Service::NSProviderState::DENY; - mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do( - [this, & revState](OIC::Service::NSProviderState state) - { - std::cout << "Income Accepted subscription : " << std::endl; - revState = state; - responseCon.notify_all(); - }); - ASSERT_NE(nullptr, g_provider) << "error: discovery failure"; + g_expectedState = OIC::Service::NSProviderState::ALLOW; - g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback) - ProviderChangedCallbackEmpty, - (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty, - (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty); + g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback)ProviderChangedCallback, + (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallback, + (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallback); if (!g_provider->isSubscribed()) { g_provider->subscribe(); } std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); - EXPECT_EQ(OIC::Service::NSProviderState::ALLOW, revState); + EXPECT_EQ(g_expectedState, g_revState); } TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification) @@ -260,18 +241,11 @@ TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotification) std::string title = "title"; std::string msg = "msg"; - mocks.ExpectCallFunc(NotificationReceivedCallbackEmpty).Do( - [this]( OIC::Service::NSMessage message) - { - std::cout << "Income Notification : " << message.getMessageId() << std::endl; - responseCon.notify_all(); - }); - g_providerSimul.notifyMessage(id, title, msg); std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); - + EXPECT_EQ(id, g_messageId); OIC::Service::NSConsumerService::getInstance()->stop(); } @@ -279,27 +253,22 @@ TEST_F(NotificationServiceConsumerTest, DiscoverProviderWithAccepterisProvider) { g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER); - mocks.ExpectCallFunc(ProviderDiscoveredCallbackEmpty).Do( - [this]( std::shared_ptr provider) - { - std::cout << "Call Discovered" << std::endl; - g_provider = provider; - g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback) - ProviderChangedCallbackEmpty, - (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallbackEmpty, - (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallbackEmpty); - if (!g_provider->isSubscribed()) - { - g_provider->subscribe(); - } - std::cout << g_provider->getProviderId() << std::endl; - responseCon.notify_all(); - }); + OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallback); - OIC::Service::NSConsumerService::getInstance()->start(ProviderDiscoveredCallbackEmpty); std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); + + ASSERT_NE(nullptr, g_provider) << "error: discovery failure"; + g_provider->setListener( (OIC::Service::NSProvider::ProviderStateCallback) + ProviderChangedCallback, + (OIC::Service::NSProvider::MessageReceivedCallback)NotificationReceivedCallback, + (OIC::Service::NSProvider::SyncInfoReceivedCallback)SyncCallback); + if (!g_provider->isSubscribed()) + { + g_provider->subscribe(); + } + } TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisProvider) @@ -307,22 +276,13 @@ TEST_F(NotificationServiceConsumerTest, ExpectReceiveNotificationWithAccepterisP uint64_t id = 11; std::string title = "title"; std::string msg = "msg"; - uint64_t revId = 1; - - mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do( - [this, & id, & revId](OIC::Service::NSMessage message) - { - std::cout << "Income Notification : " << message.getMessageId() << std::endl; - revId = message.getMessageId(); - responseCon.notify_all(); - }); g_providerSimul.notifyMessage(id, title, msg); std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); - EXPECT_EQ(id, revId); + EXPECT_EQ(id, g_messageId); } TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync) @@ -330,23 +290,8 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotif uint64_t id = 12; std::string title = "title"; std::string msg = "msg"; - OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; - - mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do( - [this]( OIC::Service::NSMessage message) - { - std::cout << "Income Notification : " << message.getMessageId() << std::endl; - }); - - mocks.OnCallFunc(SyncCallbackEmpty).Do( - [& type, this](OIC::Service::NSSyncInfo sync) - { - std::cout << "Income SyncInfo : " << sync.getMessageId() - << ", State : " << (int) sync.getState() << std::endl; - type = sync.getState(); - responseCon.notify_all(); - }); + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; g_providerSimul.notifyMessage(id, title, msg); { std::unique_lock< std::mutex > lock { mutexForCondition }; @@ -359,7 +304,8 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenProviderNotif responseCon.wait_for(lock, g_waitForResponse); } - EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type); + EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, g_syncType); + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; } TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync) @@ -367,22 +313,8 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNo uint64_t id = 13; std::string title = "title"; std::string msg = "msg"; - OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; - - mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do( - [this]( OIC::Service::NSMessage message) - { - std::cout << "Income Notification : " << message.getMessageId() << std::endl; - }); - - mocks.OnCallFunc(SyncCallbackEmpty).Do( - [& type, this](OIC::Service::NSSyncInfo sync) - { - std::cout << "Income Notification : " << sync.getMessageId() - << ", State : " << (int) sync.getState() << std::endl; - type = sync.getState(); - responseCon.notify_all(); - }); + g_syncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; g_providerSimul.notifyMessage(id, title, msg); { @@ -396,7 +328,8 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenProviderNo responseCon.wait_for(lock, g_waitForResponse); } - EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type); + EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, g_syncType); + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; } TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync) @@ -404,36 +337,24 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackReadCheckWhenConsumerPostS uint64_t id = 14; std::string title = "title"; std::string msg = "msg"; - OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; + g_syncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; ASSERT_NE(nullptr, g_provider) << "error: discovery failure"; - mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do( - [this]( OIC::Service::NSMessage message) - { - std::cout << "Income Notification : " << message.getMessageId() << std::endl; - g_provider->sendSyncInfo(message.getMessageId(), - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); - }); - - mocks.OnCallFunc(SyncCallbackEmpty).Do( - [& type, this](OIC::Service::NSSyncInfo sync) - { - std::cout << "Income Notification : " << sync.getMessageId() - << ", State : " << (int) sync.getState() << std::endl; - type = sync.getState(); - responseCon.notify_all(); - }); - g_providerSimul.notifyMessage(id, title, msg); { std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); } - EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, type); + g_provider->sendSyncInfo(g_messageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); + std::unique_lock< std::mutex > lock { mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ, g_syncType); + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; } TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync) @@ -441,36 +362,24 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackDismissCheckWhenConsumerPo uint64_t id = 15; std::string title = "title"; std::string msg = "msg"; - OIC::Service::NSSyncInfo::NSSyncType type = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; + g_syncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; ASSERT_NE(nullptr, g_provider) << "error: discovery failure"; - mocks.OnCallFunc(NotificationReceivedCallbackEmpty).Do( - [this]( OIC::Service::NSMessage message) - { - std::cout << "Income Notification : " << message.getMessageId() << std::endl; - g_provider->sendSyncInfo(message.getMessageId(), - OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); - }); - - mocks.OnCallFunc(SyncCallbackEmpty).Do( - [& type, this](OIC::Service::NSSyncInfo sync) - { - std::cout << "Income Notification : " << sync.getMessageId() - << ", State : " << (int) sync.getState() << std::endl; - type = sync.getState(); - responseCon.notify_all(); - }); - g_providerSimul.notifyMessage(id, title, msg); { std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); } - EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, type); + g_provider->sendSyncInfo(g_messageId, + OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED); + std::unique_lock< std::mutex > lock { mutexForCondition }; + responseCon.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED, g_syncType); + g_expectedSyncType = OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; } TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithValidProviderId) @@ -495,14 +404,7 @@ TEST_F(NotificationServiceConsumerTest, ExpectGetProviderSuccessWithInvalidProvi 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(); - }); + g_expectedState = OIC::Service::NSProviderState::TOPIC; NSProviderSimulator::NS_TopicList topics; topics.push_back("1"); @@ -510,11 +412,13 @@ TEST_F(NotificationServiceConsumerTest, ExpectCallbackTopicUpdated) topics.push_back("3"); g_providerSimul.setTopics(topics); - + std::chrono::milliseconds waitForTopicUdateComplete(1000); std::unique_lock< std::mutex > lock{ mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); - EXPECT_EQ(OIC::Service::NSProviderState::TOPIC, revState); + EXPECT_EQ(OIC::Service::NSProviderState::TOPIC, g_revState); + responseCon.wait_for(lock, waitForTopicUdateComplete); + g_expectedState = OIC::Service::NSProviderState::STOPPED; } TEST_F(NotificationServiceConsumerTest, ExpectEQTopicList) @@ -564,19 +468,12 @@ TEST_F(NotificationServiceConsumerTest, ExpectFailUpdateTopicOnConsumer) TEST_F(NotificationServiceConsumerTest, ExpectCallbackDeletedProvider) { - OIC::Service::NSProviderState type = OIC::Service::NSProviderState::ALLOW; - mocks.OnCallFunc(ProviderChangedCallbackEmpty).Do( - [& type, this](OIC::Service::NSProviderState state) - { - std::cout << "Income Changed Callback : " << std::endl; - type = state; - }); - + g_expectedState = OIC::Service::NSProviderState::STOPPED; g_providerSimul.deleteNotificationResource(); std::unique_lock< std::mutex > lock { mutexForCondition }; responseCon.wait_for(lock, g_waitForResponse); - EXPECT_EQ(type, OIC::Service::NSProviderState::STOPPED); + EXPECT_EQ(g_revState, OIC::Service::NSProviderState::STOPPED); OIC::Service::NSConsumerService::getInstance()->stop(); } diff --git a/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h b/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h old mode 100755 new mode 100644 index 01f9ff3..79b0b1a --- a/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h +++ b/service/notification/cpp-wrapper/unittest/NSProviderServiceSimulator.h @@ -23,6 +23,8 @@ #include #include +#include + #include "OCPlatform.h" #include "OCApi.h" @@ -79,7 +81,7 @@ class NSProviderSimulator NSProviderSimulator() : m_notificationHandle(), m_messageHandle(), m_syncHandle(), m_topicHandle(), m_syncRep(), m_messageRep(), m_accepter(0), - m_notificationUri(std::string("/notification")), + m_notificationUri(std::string("/notificationTest")), m_messageUri(std::string("/message")), m_syncUri(std::string("/sync")), m_topicUri(std::string("/topic")), @@ -182,7 +184,8 @@ class NSProviderSimulator m_syncRep = requests->getResourceRepresentation(); std::cout << "Receive POST for Sync" << std::endl; - std::cout << "provider Id : " << m_syncRep.getValueToString("x.org.iotivity.ns.providerid") << 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); @@ -324,9 +327,9 @@ class NSProviderSimulator { std::string providerId = "123456789012345678901234567890123456"; m_messageRep.setValue("x.org.iotivity.ns.messageid", id); - m_messageRep.setValue("x.org.iotivity.ns.title", title); - m_messageRep.setValue("x.org.iotivity.ns.contenttext", content); - m_messageRep.setValue("x.org.iotivity.ns.providerid", providerId); + m_messageRep.setValue("x.org.iotivity.ns.title", title); + m_messageRep.setValue("x.org.iotivity.ns.contenttext", content); + m_messageRep.setValue("x.org.iotivity.ns.providerid", providerId); } void setTopics(const NS_TopicList &topics) diff --git a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp old mode 100755 new mode 100644 index 7b6cd1c..dc474f5 --- a/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp +++ b/service/notification/cpp-wrapper/unittest/NSProviderServiceTest.cpp @@ -19,7 +19,6 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include -#include #include #include #include @@ -37,20 +36,29 @@ namespace { std::atomic_bool g_isStartedStack(false); - std::chrono::milliseconds g_waitForResponse(500); + std::chrono::milliseconds g_waitForResponse(1000); - std::condition_variable responseCon; - std::mutex mutexForCondition; + std::condition_variable responseProviderSub; + std::mutex responseProviderSubLock; + + std::condition_variable responseProviderSync; + std::mutex responseProviderSyncLock; + + std::condition_variable responseConsumerMessage; + std::mutex responseConsumerMessageLock; + + std::condition_variable responseConsumerSync; + std::mutex responseConsumerSyncLock; NSConsumerSimulator g_consumerSimul; std::shared_ptr g_consumer; + + int expectedMsgId; + int expectedSyncType = NS_SYNC_READ; } class TestWithMock: public testing::Test { - public: - MockRepository mocks; - protected: virtual ~TestWithMock() noexcept(noexcept(std::declval().~Test())) { @@ -58,15 +66,7 @@ class TestWithMock: public testing::Test virtual void TearDown() { - try - { - mocks.VerifyAll(); - } - catch (...) - { - mocks.reset(); - throw; - } + } }; @@ -76,25 +76,37 @@ class NotificationProviderServiceTest : public TestWithMock NotificationProviderServiceTest() = default; ~NotificationProviderServiceTest() = default; - static void ConsumerSubscribedCallbackEmpty(std::shared_ptr ) + static void ConsumerSubscribedCallback(std::shared_ptr consumer) { std::cout << __func__ << std::endl; + g_consumer = consumer; + + responseProviderSub.notify_all(); } - static void MessageSynchronizedCallbackEmpty(OIC::Service::NSSyncInfo) + static void MessageSynchronizedCallback(OIC::Service::NSSyncInfo sync) { std::cout << __func__ << std::endl; + expectedSyncType = (int) sync.getState(); + expectedMsgId = sync.getMessageId(); + responseProviderSync.notify_all(); } - static void MessageCallbackFromConsumerEmpty( - const int &, const std::string &, const std::string &, const std::string &) + static void MessageCallbackFromConsumer( + const int &id, const std::string &, const std::string &, const std::string &) { std::cout << __func__ << std::endl; + expectedMsgId = id; + std::cout << expectedMsgId << std::endl; + responseConsumerMessage.notify_all(); } - static void SyncCallbackFromConsumerEmpty(int, int) + static void SyncCallbackFromConsumer(const int type, const int syncId) { std::cout << __func__ << std::endl; + expectedSyncType = type; + expectedMsgId = syncId; + responseConsumerSync.notify_all(); } protected: @@ -139,9 +151,11 @@ class NotificationProviderServiceTest : public TestWithMock TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyTrue) { OIC::Service::NSProviderService::ProviderConfig config; - config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty; - config.m_syncInfoCb = MessageSynchronizedCallbackEmpty; + config.m_subscribeRequestCb = ConsumerSubscribedCallback; + config.m_syncInfoCb = MessageSynchronizedCallback; config.subControllability = true; + config.userInfo = "user1"; + config.resourceSecurity = false; OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->start(config); @@ -158,9 +172,10 @@ TEST_F(NotificationProviderServiceTest, StopProviderPositive) TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyFalse) { OIC::Service::NSProviderService::ProviderConfig config; - config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty; - config.m_syncInfoCb = MessageSynchronizedCallbackEmpty; + config.m_subscribeRequestCb = ConsumerSubscribedCallback; + config.m_syncInfoCb = MessageSynchronizedCallback; config.subControllability = false; + config.resourceSecurity = false; OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->start(config); @@ -172,52 +187,33 @@ TEST_F(NotificationProviderServiceTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider) { g_consumer = NULL; - mocks.ExpectCallFunc(ConsumerSubscribedCallbackEmpty).Do( - []( std::shared_ptr consumer) - { - std::cout << "ConsumerSubscribedCallbackEmpty" << std::endl; - g_consumer = consumer; - responseCon.notify_all(); - }); OIC::Service::NSProviderService::ProviderConfig config; - config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty; - config.m_syncInfoCb = MessageSynchronizedCallbackEmpty; + config.m_subscribeRequestCb = ConsumerSubscribedCallback; + config.m_syncInfoCb = MessageSynchronizedCallback; config.subControllability = true; + config.resourceSecurity = false; OIC::Service::NSProviderService::getInstance()->start(config); - { - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); - } - - g_consumerSimul.setCallback(MessageCallbackFromConsumerEmpty, - SyncCallbackFromConsumerEmpty); + g_consumerSimul.setCallback(MessageCallbackFromConsumer, + SyncCallbackFromConsumer); g_consumerSimul.findProvider(); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); + // maximum waiting time for subscription is 1.5 sec. + // usually maximum time is 1 sec. (g_waitForResponse = 1 sec.) + // but, in the secured case is need to more wait for processing. + std::chrono::milliseconds waitForSubscription(1500); + std::unique_lock< std::mutex > lock{ responseProviderSubLock }; + responseProviderSub.wait_for(lock, waitForSubscription); ASSERT_NE(nullptr, g_consumer) << "error: discovery failure"; } TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse) { - bool expectTrue = true; int msgID = 0; - mocks.OnCallFunc(MessageCallbackFromConsumerEmpty).Do( - [& expectTrue, &msgID](const int &id, const std::string &, const std::string &, - const std::string &) - { - if (id == msgID) - { - std::cout << "This function never call" << std::endl; - expectTrue = false; - } - responseCon.notify_all(); - }); ASSERT_NE(nullptr, g_consumer) << "error: discovery failure"; @@ -229,15 +225,12 @@ TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse msg.setSourceName(std::string("OCF")); OIC::Service::NSProviderService::getInstance()->sendMessage(msg); - { - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); - } + std::unique_lock< std::mutex > lock{ responseConsumerMessageLock }; + responseConsumerMessage.wait_for(lock, g_waitForResponse); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); + EXPECT_NE(expectedMsgId, msgID); + responseConsumerMessage.wait_for(lock, g_waitForResponse); - EXPECT_EQ(expectTrue, true); } TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue) @@ -246,16 +239,6 @@ TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue ASSERT_NE(nullptr, g_consumer) << "error: discovery failure"; - mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do( - [&msgID](const int &id, const std::string &, const std::string &, const std::string &) - { - if (id == msgID) - { - std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl; - responseCon.notify_all(); - } - }); - g_consumer->acceptSubscription(true); OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage(); @@ -265,26 +248,18 @@ TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue msg.setSourceName(std::string("OCF")); OIC::Service::NSProviderService::getInstance()->sendMessage(msg); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); + std::unique_lock< std::mutex > lock{ responseConsumerMessageLock }; + responseConsumerMessage.wait_for(lock, g_waitForResponse); + EXPECT_EQ(expectedMsgId, msgID); } TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer) { int id = 0; + int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; + expectedSyncType = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; - mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do( - [& id](int &type, int &syncId) - { - std::cout << "MessageSynchronizedCallbackEmpty" << std::endl; - if (syncId == id && - type == (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ) - { - std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl; - responseCon.notify_all(); - } - }); OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage(); id = (int)msg.getMessageId(); @@ -294,25 +269,19 @@ TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer) OIC::Service::NSProviderService::getInstance()->sendSyncInfo(msg.getMessageId(), OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); + std::unique_lock< std::mutex > lock{ responseConsumerSyncLock }; + responseConsumerSync.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(expectedMsgId, id); + EXPECT_EQ(expectedSyncType, type); } TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer) { int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ; + expectedSyncType = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_DELETED; + int id = 0; - mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do( - [& id](OIC::Service::NSSyncInfo sync) - { - std::cout << "MessageSynchronizedCallbackEmpty" << std::endl; - if ((int)sync.getMessageId() == id - && sync.getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ) - { - std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl; - responseCon.notify_all(); - } - }); OIC::Service::NSMessage msg = OIC::Service::NSProviderService::getInstance()->createMessage(); id = (int)msg.getMessageId(); @@ -320,8 +289,11 @@ TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer) msg.setContentText(std::string("ContentText")); msg.setSourceName(std::string("OCF")); g_consumerSimul.syncToProvider(type, id, msg.getProviderId()); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait(lock); + std::unique_lock< std::mutex > lock{ responseProviderSyncLock }; + responseProviderSync.wait_for(lock, g_waitForResponse); + + EXPECT_EQ(expectedMsgId, id); + EXPECT_EQ(expectedSyncType, type); } TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopics) @@ -330,36 +302,28 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualAddedTopicsAndRegisteredTopic std::string str2("TEST2"); OIC::Service::NSProviderService::getInstance()->registerTopic(str1); OIC::Service::NSProviderService::getInstance()->registerTopic(str2); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); + bool isSame = false; - auto topicList = - OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList(); - if (!topicList) + auto topicList = OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList(); + + std::string compString[10]; + int i = 0; + for (auto itr : topicList->getTopicsList()) { - printf("topic is NULL\n"); - isSame = false; + compString[i] = itr.getTopicName(); i++; } - else + std::cout << compString[0] << std::endl; + std::cout << compString[1] << std::endl; + if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0) { - std::string compString[10]; - int i = 0; - for (auto itr : topicList->getTopicsList()) - { - compString[i] = itr.getTopicName(); i++; - } - std::cout << compString[0] << std::endl; - std::cout << compString[1] << std::endl; - if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0) - { - isSame = true; - } + isSame = true; } + EXPECT_EQ(isSame, true); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - responseCon.wait_for(lock, g_waitForResponse); + } TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegisteredTopics) @@ -369,31 +333,25 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnregisteredTopicsAndRegister OIC::Service::NSProviderService::getInstance()->registerTopic(str1); OIC::Service::NSProviderService::getInstance()->registerTopic(str2); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); bool isSame = false; auto topicList = OIC::Service::NSProviderService::getInstance()->getRegisteredTopicList(); - if (!topicList) + + std::string compStr[10]; + int i = 0; + for (auto itr : topicList->getTopicsList()) { - printf("topic is NULL\n"); - isSame = false; + compStr[i] = itr.getTopicName(); i++; } - else + std::cout << compStr << std::endl; + if (str1.compare(compStr[0]) == 0) { - auto topic = topicList->getTopicsList(); - auto it = topic.begin(); - std::string compStr = (*it).getTopicName() ; - std::cout << compStr << std::endl; - if (str1.compare(compStr) == 0 ) - { - isSame = true; - } + isSame = true; } + EXPECT_EQ(isSame, true); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); - responseCon.wait_for(lock, g_waitForResponse); } TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsumerTopics) @@ -407,40 +365,31 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualSetConsumerTopicsAndGetConsum g_consumer->setTopic(str1); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); bool isSame = false; auto topicList = g_consumer->getConsumerTopicList(); - if (!topicList) + std::string compString[10]; + int i = 0, state[10] = {0}; + for (auto itr : topicList->getTopicsList()) { - printf("topic is NULL\n"); - isSame = false; + compString[i] = itr.getTopicName(); + state[i++] = (int) itr.getState(); } - else + std::cout << compString[0] << std::endl; + std::cout << compString[1] << std::endl; + if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0 + && state[0] == 1 && state[1] == 0) { - std::string compString[10]; - int i = 0, state[10] = {0}; - for (auto itr : topicList->getTopicsList()) - { - compString[i] = itr.getTopicName(); - state[i++] = (int) itr.getState(); - } - std::cout << compString[0] << std::endl; - std::cout << compString[1] << std::endl; - if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0 - && state[0] == 1 && state[1] == 0) - { - isSame = true; - } + isSame = true; } + EXPECT_EQ(isSame, true); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - responseCon.wait_for(lock, g_waitForResponse); + } TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics) @@ -456,40 +405,28 @@ TEST_F(NotificationProviderServiceTest, ExpectEqualUnSetConsumerTopicsAndGetCons g_consumer->setTopic(str2); g_consumer->unsetTopic(str1); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); - bool isSame = false; auto topicList = g_consumer->getConsumerTopicList(); - if (!topicList) + std::string compString[10]; + int i = 0, state[10] = {0}; + for (auto itr : topicList->getTopicsList()) { - printf("topic is NULL\n"); - isSame = false; + compString[i] = itr.getTopicName(); + state[i++] = (int) itr.getState(); } - else + std::cout << compString[0] << std::endl; + std::cout << compString[1] << std::endl; + if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0 + && state[0] == 0 && state[1] == 1) { - std::string compString[10]; - int i = 0, state[10] = {0}; - for (auto itr : topicList->getTopicsList()) - { - compString[i] = itr.getTopicName(); - state[i++] = (int) itr.getState(); - } - std::cout << compString[0] << std::endl; - std::cout << compString[1] << std::endl; - if (str1.compare(compString[0]) == 0 && str2.compare(compString[1]) == 0 - && state[0] == 0 && state[1] == 1) - { - isSame = true; - } + isSame = true; } EXPECT_EQ(isSame, true); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str1); OIC::Service::NSProviderService::getInstance()->unregisterTopic(str2); - responseCon.wait_for(lock, g_waitForResponse); } @@ -497,8 +434,7 @@ TEST_F(NotificationProviderServiceTest, CancelObserves) { bool ret = g_consumerSimul.cancelObserves(); - std::unique_lock< std::mutex > lock { mutexForCondition }; - responseCon.wait_for(lock, g_waitForResponse); + std::this_thread::sleep_for(g_waitForResponse); EXPECT_EQ(ret, true); } diff --git a/service/notification/cpp-wrapper/unittest/SConscript b/service/notification/cpp-wrapper/unittest/SConscript index ddef6cd..2a43f72 100644 --- a/service/notification/cpp-wrapper/unittest/SConscript +++ b/service/notification/cpp-wrapper/unittest/SConscript @@ -58,8 +58,8 @@ notification_wrapper_test_env.AppendUnique(LIBS = [ if env.get('WITH_TCP') == True: notification_wrapper_test_env.AppendUnique(CPPDEFINES = ['WITH_TCP']) - if env.get('SECURED') == '1': - notification_wrapper_test_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509', 'mbedcrypto']) +if env.get('SECURED') == '1': + notification_wrapper_test_env.AppendUnique(LIBS = ['mbedtls', 'mbedx509', 'mbedcrypto']) if target_os not in ['windows', 'winrt']: notification_wrapper_test_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-fmessage-length=0', '-std=c++0x']) @@ -106,8 +106,8 @@ env.AppendTarget('notification_provider_wrapper_test') if env.get('TEST') == '1': if env.get('SECURED') != '1': -# TODO: fix this test on linux and remove this comment line - if target_os in []: +# TODO: fix this test for MLK and remove this comment line + if target_os in ['linux'] and env.get('SECURED') != '1': from tools.scons.RunTest import * run_test(notification_consumer_wrapper_test_env, '', -- 2.7.4