NS_SELECTION_PROVIDER = 1
};
+ NSConsumerConfig cfg;
+
+ NSProviderSimulator::NS_TopicStateList g_topicStateList;
}
class TestWithMock: public testing::Test
NotificationConsumerTest() = default;
~NotificationConsumerTest() = default;
- static void NSProviderDiscoveredCallbackEmpty(NSProvider *)
- {
- std::cout << __func__ << std::endl;
- }
-
static void NSNotificationReceivedCallbackEmpty(NSMessage *)
{
std::cout << __func__ << std::endl;
std::cout << __func__ << std::endl;
}
- static void NSProviderChangedCallback(NSProvider *, NSResponse)
+ static void NSProviderChangedCallback(NSProvider *, NSProviderState)
{
std::cout << __func__ << std::endl;
}
if (g_isStartedStack == false)
{
- OC::PlatformConfig cfg
+ OC::PlatformConfig occfg
{
OC::ServiceType::InProc,
OC::ModeType::Both,
0,
OC::QualityOfService::LowQos
};
- OC::OCPlatform::Configure(cfg);
+ OC::OCPlatform::Configure(occfg);
try
{
}
g_isStartedStack = true;
+
+ cfg.changedCb = NSProviderChangedCallback;
+ cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+ cfg.syncInfoCb = NSSyncCallbackEmpty;
}
}
};
+TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetChangedCB)
+{
+ cfg.changedCb = NULL;
+ cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+ cfg.syncInfoCb = NSSyncCallbackEmpty;
+
+ EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
+ std::unique_lock< std::mutex > lock{ mutexForCondition };
+ responseCon.wait_for(lock, g_waitForResponse);
+}
+
+TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetNotiReceiveCB)
+{
+ cfg.changedCb = NSProviderChangedCallback;
+ cfg.messageCb = NULL;
+ cfg.syncInfoCb = NSSyncCallbackEmpty;
+
+ EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
+ std::unique_lock< std::mutex > lock{ mutexForCondition };
+ responseCon.wait_for(lock, g_waitForResponse);
+}
+
+TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetSyncCB)
+{
+ cfg.changedCb = NSProviderChangedCallback;
+ cfg.messageCb = NSNotificationReceivedCallbackEmpty;
+ cfg.syncInfoCb = NULL;
+
+ EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
+ std::unique_lock< std::mutex > lock{ mutexForCondition };
+ responseCon.wait_for(lock, g_waitForResponse);
+}
+
TEST_F(NotificationConsumerTest, StartConsumerPositive)
{
- NSConsumerConfig cfg;
- cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
cfg.changedCb = NSProviderChangedCallback;
cfg.messageCb = NSNotificationReceivedCallbackEmpty;
cfg.syncInfoCb = NSSyncCallbackEmpty;
+
EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
+ std::unique_lock< std::mutex > lock{ mutexForCondition };
+ responseCon.wait_for(lock, g_waitForResponse);
}
TEST_F(NotificationConsumerTest, StopConsumerPositive)
EXPECT_EQ(NSStopConsumer(), NS_OK);
}
+TEST_F(NotificationConsumerTest, StopConsumerNegative)
+{
+ EXPECT_EQ(NSStopConsumer(), NS_ERROR);
+}
+
TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
{
- mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
- [this](NSProvider *)
+ NSProviderState revState = NS_STOPPED;
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [this, & revState](NSProvider *, NSProviderState state)
{
std::cout << "Call Discovered" << std::endl;
+ revState = state;
responseCon.notify_all();
});
- NSConsumerConfig cfg;
- cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
- cfg.changedCb = NSProviderChangedCallback;
- cfg.messageCb = NSNotificationReceivedCallbackEmpty;
- cfg.syncInfoCb = NSSyncCallbackEmpty;
NSStartConsumer(cfg);
g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
NSStopConsumer();
g_providerSimul.deleteNotificationResource();
+
+ EXPECT_EQ(NS_DISCOVERED, revState);
}
TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
responseCon.wait_for(lock, g_waitForResponse);
}
- mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
- [this](NSProvider * provider)
+ NSProviderState revState = NS_STOPPED;
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [this, & revState](NSProvider * provider, NSProviderState state)
{
std::cout << "Call Discovered" << std::endl;
+
g_provider = provider;
+ revState = state;
responseCon.notify_all();
});
- NSConsumerConfig cfg;
- cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
- cfg.changedCb = NSProviderChangedCallback;
- cfg.messageCb = NSNotificationReceivedCallbackEmpty;
- cfg.syncInfoCb = NSSyncCallbackEmpty;
NSStartConsumer(cfg);
std::unique_lock< std::mutex > lock{ mutexForCondition };
responseCon.wait_for(lock, g_waitForResponse);
+ EXPECT_EQ(NS_DISCOVERED, revState);
+
+ ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
}
TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
{
g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
- mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
- [this](NSProvider * provider)
+ NSProviderState revState = NS_STOPPED;
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [this, &revState](NSProvider * provider, NSProviderState state)
{
std::cout << "Call Discovered" << std::endl;
+ revState = state;
g_provider = provider;
std::cout << g_provider->providerId << std::endl;
responseCon.notify_all();
std::unique_lock< std::mutex > lock{ mutexForCondition };
responseCon.wait_for(lock, g_waitForResponse);
-// NSStopConsumer();
+ EXPECT_EQ(NS_DISCOVERED, revState);
}
TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
{
-// mocks.ExpectCallFunc(NSSubscriptionAcceptedCallback).Do(
-// [](NSProvider * )
-// {
-// std::cout << "Income Accepted subscription : " << std::endl;
-// });
+ NSProviderState revState = NS_DENY;
+
+ ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
- NSResult ret = NSSubscribe(g_provider);
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [this, & revState](NSProvider * , NSProviderState state)
+ {
+ std::cout << "Income Changed Callback : " << state << std::endl;
+ revState = state;
+ responseCon.notify_all();
+ });
+
+ NSResult ret = NSSubscribe(g_provider->providerId);
std::unique_lock< std::mutex > lock{ mutexForCondition };
responseCon.wait_for(lock, g_waitForResponse);
+ EXPECT_EQ(NS_ALLOW, revState);
EXPECT_EQ(NS_OK, ret);
}
uint64_t id = 10;
std::string title = "title";
std::string msg = "msg";
+ uint64_t revId = 0;
- mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
- [](NSMessage * message)
+ mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
+ [this, & revId](NSMessage * message)
{
std::cout << "Income Notification : " << message->messageId << std::endl;
+ revId = message->messageId;
+ 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);
NSStopConsumer();
+ g_providerSimul.deleteNotificationResource();
}
-TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
+TEST_F(NotificationConsumerTest, ExpectReceiveSubAllowWithAccepterisProvider)
{
- uint64_t id = 11;
- std::string title = "title";
- std::string msg = "msg";
-
g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
-
- NSConsumerConfig cfg;
- cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
- cfg.changedCb = NSProviderChangedCallback;
- cfg.messageCb = NSNotificationReceivedCallbackEmpty;
- cfg.syncInfoCb = NSSyncCallbackEmpty;
- NSStartConsumer(cfg);
+ NSProviderState revState = NS_DENY;
+ g_providerSimul.createNotificationResource();
{
std::unique_lock< std::mutex > lock{ mutexForCondition };
responseCon.wait_for(lock, g_waitForResponse);
}
- mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
- [](NSMessage * message)
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [this, & revState](NSProvider * provider, NSProviderState state)
+ {
+ std::cout << "Income Changed Callback : " << state << std::endl;
+ revState = state;
+ g_provider = provider;
+ responseCon.notify_all();
+ });
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [this, & revState](NSProvider *, NSProviderState state)
+ {
+ std::cout << "Income Changed Callback : " << state << std::endl;
+ revState = state;
+ responseCon.notify_all();
+ });
+
+ NSStartConsumer(cfg);
+ std::unique_lock< std::mutex > lock{ mutexForCondition };
+ responseCon.wait_for(lock, g_waitForResponse);
+
+ EXPECT_EQ(NS_ALLOW, revState);
+}
+
+TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
+{
+ uint64_t id = 11;
+ std::string title = "title";
+ std::string msg = "msg";
+ uint64_t revId = 1;
+
+ mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
+ [this, & id, & revId](NSMessage * message)
{
std::cout << "Income Notification : " << message->messageId << std::endl;
+ revId = message->messageId;
+ responseCon.notify_all();
});
g_providerSimul.notifyMessage(id, title, msg);
std::unique_lock< std::mutex > lock{ mutexForCondition };
responseCon.wait_for(lock, g_waitForResponse);
-// g_providerSimul.deleteNotificationResource();
-// NSStopConsumer();
+ EXPECT_EQ(id, revId);
}
TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
uint64_t id = 12;
std::string title = "title";
std::string msg = "msg";
-
NSSyncType type = NS_SYNC_DELETED;
mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
- [](NSMessage * message)
+ [this](NSMessage * message)
{
std::cout << "Income Notification : " << message->messageId << std::endl;
+ responseCon.notify_all();
});
- mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
- [& type](NSSyncInfo * sync)
+ mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+ [& type, this](NSSyncInfo * sync)
{
std::cout << "Income SyncInfo : " << sync->messageId
<< ", State : " << sync->state << std::endl;
type = sync->state;
-
+ responseCon.notify_all();
});
g_providerSimul.notifyMessage(id, title, msg);
responseCon.wait_for(lock, g_waitForResponse);
}
-// g_providerSimul.deleteNotificationResource();
-// NSStopConsumer();
-
EXPECT_EQ(NS_SYNC_READ, type);
}
uint64_t id = 13;
std::string title = "title";
std::string msg = "msg";
-
NSSyncType type = NS_SYNC_READ;
mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
- [](NSMessage * message)
+ [this](NSMessage * message)
{
std::cout << "Income Notification : " << message->messageId << std::endl;
+ responseCon.notify_all();
});
- mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
- [& type](NSSyncInfo * sync)
+ mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+ [& type, this](NSSyncInfo * sync)
{
std::cout << "Income Notification : " << sync->messageId
<< ", State : " << sync->state << std::endl;
type = sync->state;
-
+ responseCon.notify_all();
});
g_providerSimul.notifyMessage(id, title, msg);
responseCon.wait_for(lock, g_waitForResponse);
}
-// g_providerSimul.deleteNotificationResource();
-// NSStopConsumer();
-
EXPECT_EQ(NS_SYNC_DELETED, type);
}
uint64_t id = 14;
std::string title = "title";
std::string msg = "msg";
-
NSSyncType type = NS_SYNC_DELETED;
mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
- [](NSMessage * message)
+ [this](NSMessage * message)
{
std::cout << "Income Notification : " << message->messageId << std::endl;
NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
responseCon.wait_for(lock, g_waitForResponse);
});
- mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
- [& type](NSSyncInfo * sync)
+ mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+ [& type, this](NSSyncInfo * sync)
{
std::cout << "Income Notification : " << sync->messageId
<< ", State : " << sync->state << std::endl;
type = sync->state;
-
+ responseCon.notify_all();
});
g_providerSimul.notifyMessage(id, title, msg);
responseCon.wait_for(lock, g_waitForResponse);
}
-// g_providerSimul.deleteNotificationResource();
-// NSStopConsumer();
-
EXPECT_EQ(NS_SYNC_READ, type);
}
uint64_t id = 15;
std::string title = "title";
std::string msg = "msg";
-
- NSSyncType type = NS_SYNC_READ;
+ NSSyncType state = NS_SYNC_READ;
mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
- [](NSMessage * message)
+ [this](NSMessage * message)
{
std::cout << "Income Notification : " << message->messageId << std::endl;
NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
responseCon.wait_for(lock, g_waitForResponse);
});
- mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
- [& type](NSSyncInfo * sync)
+ mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
+ [& state, this](NSSyncInfo * sync)
{
std::cout << "Income Notification : " << sync->messageId
<< ", State : " << sync->state << std::endl;
- type = sync->state;
-
+ state = sync->state;
+ responseCon.notify_all();
});
g_providerSimul.notifyMessage(id, title, msg);
responseCon.wait_for(lock, g_waitForResponse);
}
- EXPECT_EQ(NS_SYNC_DELETED, type);
+ EXPECT_EQ(NS_SYNC_DELETED, state);
+}
+
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
+{
+ ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+ NSProvider * provider = NSConsumerGetProvider(g_provider->providerId);
+ int ret = strcmp(provider->providerId, g_provider->providerId);
+ EXPECT_EQ(0, ret);
+ free(provider);
+}
+
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
+{
+ NSProvider * provider = NSConsumerGetProvider("123456789012345678901234567890123457");
+ EXPECT_EQ(provider, (void*)NULL);
}
-TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithNULL)
{
- NSResult ret = NSUnsubscribe(g_provider);
+ NSProvider * provider = NSConsumerGetProvider(NULL);
+ EXPECT_EQ(provider, (void*)NULL);
+}
+
+TEST_F(NotificationConsumerTest, ExpectGetTopicListIsNULL)
+{
+ ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+ NSTopicLL * currentTopics = NSConsumerGetTopicList(g_provider->providerId);
+ EXPECT_EQ(NULL, currentTopics);
+}
+
+TEST_F(NotificationConsumerTest, ExpectCallbackTopicUpdated)
+{
+ NSProviderState revState = NS_STOPPED;
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [this, & revState](NSProvider * , NSProviderState state)
+ {
+ std::cout << "Income Changed Callback : " << 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(NS_TOPIC, revState);
+}
+
+TEST_F(NotificationConsumerTest, ExpectEQTopicList)
+{
+ bool isSame = false;
+
+ NSProviderSimulator::NS_TopicList topics;
+ topics.push_back("1");
+ topics.push_back("2");
+ topics.push_back("3");
+
+ ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+ NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
+ std::for_each (topics.begin(), topics.end(),
+ [this, & retTopic, & isSame](const std::string & str)
+ {
+ isSame = (str == std::string(retTopic->topicName));
+ retTopic = retTopic->next;
+ });
+
+ EXPECT_EQ(true, isSame);
+}
+
+TEST_F(NotificationConsumerTest, ExpectFailUpdateTopicOnConsumer)
+{
+ ASSERT_NE(nullptr, g_provider) << "error: discovery failure";
+
+ NSTopicLL * retTopic = NSConsumerGetTopicList(g_provider->providerId);
+ for (; retTopic; retTopic = retTopic->next)
+ {
+ retTopic->state = NS_TOPIC_SUBSCRIBED;
+ }
+ NSResult ret = NSConsumerUpdateTopicList(g_provider->providerId, retTopic);
+
+ EXPECT_EQ(NS_ERROR, ret);
+}
+
+TEST_F(NotificationConsumerTest, ExpectCallbackDeletedProvider)
+{
+ NSProviderState type = NS_ALLOW;
+ mocks.OnCallFunc(NSProviderChangedCallback).Do(
+ [& type, this](NSProvider * , NSProviderState state)
+ {
+ std::cout << "Income Changed Callback : " << state << std::endl;
+ type = state;
+ responseCon.notify_all();
+ });
+
g_providerSimul.deleteNotificationResource();
- NSStopConsumer();
- EXPECT_EQ(NS_OK, ret);
+ std::unique_lock< std::mutex > lock{ mutexForCondition };
+ responseCon.wait_for(lock, std::chrono::milliseconds(2000));
+
+ EXPECT_EQ(type, NS_STOPPED);
+ NSStopConsumer();
}