Commit 46319acf authored by Poovizhi's avatar Poovizhi Committed by Uze Choi

[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: default avatarPoovizhi <poovizhi.a@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/17779Reviewed-by: default avatarUze Choi <uzchoi@samsung.com>
Tested-by: default avatarUze Choi <uzchoi@samsung.com>
parent 02c32aaf
This diff is collapsed.
......@@ -23,6 +23,8 @@
#include <iostream>
#include <memory>
#include <list>
#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<int>("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<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)
......
......@@ -19,7 +19,6 @@
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <gtest/gtest.h>
#include <HippoMocks/hippomocks.h>
#include <atomic>
#include <functional>
#include <condition_variable>
......@@ -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<OIC::Service::NSConsumer> 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>().~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<OIC::Service::NSConsumer> )
static void ConsumerSubscribedCallback(std::shared_ptr<OIC::Service::NSConsumer> 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<OIC::Service::NSConsumer> 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);
}
......@@ -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,
'',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment