1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
25 #include <condition_variable>
29 #include "NSProviderService.h"
30 #include "NSConsumerServiceSimulator.h"
32 #include "NSSyncInfo.h"
33 #include "NSMessage.h"
34 #include "NSMediaContents.h"
38 std::atomic_bool g_isStartedStack(false);
40 std::chrono::milliseconds g_waitForResponse(3000);
42 std::condition_variable responseCon;
43 std::mutex mutexForCondition;
45 NSConsumerSimulator g_consumerSimul;
46 OIC::Service::NSConsumer * g_consumer;
49 class TestWithMock: public testing::Test
55 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
57 virtual void TearDown() {
70 class NotificationProviderServiceTest : public TestWithMock
73 NotificationProviderServiceTest() = default;
74 ~NotificationProviderServiceTest() = default;
76 static void ConsumerSubscribedCallbackEmpty(OIC::Service::NSConsumer *)
78 std::cout << __func__ << std::endl;
81 static void MessageSynchronizedCallbackEmpty(OIC::Service::NSSyncInfo *)
83 std::cout << __func__ << std::endl;
86 static void MessageCallbackFromConsumerEmpty(
87 const int &, const std::string &, const std::string &, const std::string &)
89 std::cout << __func__ << std::endl;
92 static void SyncCallbackFromConsumerEmpty(int, int)
94 std::cout << __func__ << std::endl;
101 TestWithMock::SetUp();
103 if (g_isStartedStack == false)
105 OC::PlatformConfig cfg
107 OC::ServiceType::InProc,
111 OC::QualityOfService::HighQos
113 OC::OCPlatform::Configure(cfg);
117 OC::OCPlatform::stopPresence();
124 g_isStartedStack = true;
131 TestWithMock::TearDown();
136 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyTrue)
138 OIC::Service::NSProviderService::ProviderConfig config;
139 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
140 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
141 config.policy = true;
143 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->Start(config);
145 std::unique_lock< std::mutex > lock{ mutexForCondition };
146 responseCon.wait_for(lock, g_waitForResponse);
148 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
151 TEST_F(NotificationProviderServiceTest, StopProviderPositive)
153 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->Stop();
155 std::unique_lock< std::mutex > lock{ mutexForCondition };
156 responseCon.wait_for(lock, g_waitForResponse);
158 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
161 TEST_F(NotificationProviderServiceTest, StartProviderPositiveWithPolicyFalse)
163 OIC::Service::NSProviderService::ProviderConfig config;
164 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
165 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
166 config.policy = false;
168 OIC::Service::NSResult ret = OIC::Service::NSProviderService::getInstance()->Start(config);
170 std::unique_lock< std::mutex > lock{ mutexForCondition };
171 responseCon.wait_for(lock, std::chrono::milliseconds(3000));
172 g_consumerSimul.findProvider();
174 responseCon.wait_for(lock, std::chrono::milliseconds(3000));
175 OIC::Service::NSProviderService::getInstance()->Stop();
176 EXPECT_EQ(ret, OIC::Service::NSResult::OK);
179 TEST_F(NotificationProviderServiceTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
181 mocks.ExpectCallFunc(ConsumerSubscribedCallbackEmpty).Do(
182 []( OIC::Service::NSConsumer * consumer)
184 std::cout << "ConsumerSubscribedCallbackEmpty" << std::endl;
185 g_consumer = new OIC::Service::NSConsumer(consumer->getConsumerId());
186 responseCon.notify_all();
189 OIC::Service::NSProviderService::ProviderConfig config;
190 config.m_subscribeRequestCb = ConsumerSubscribedCallbackEmpty;
191 config.m_syncInfoCb = MessageSynchronizedCallbackEmpty;
192 config.policy = true;
194 OIC::Service::NSProviderService::getInstance()->Start(config);
197 std::unique_lock< std::mutex > lock{ mutexForCondition };
198 responseCon.wait_for(lock, g_waitForResponse);
201 g_consumerSimul.setCallback(MessageCallbackFromConsumerEmpty,
202 SyncCallbackFromConsumerEmpty);
203 g_consumerSimul.findProvider();
205 std::unique_lock< std::mutex > lock{ mutexForCondition };
206 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
209 TEST_F(NotificationProviderServiceTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
211 bool expectTrue = true;
214 mocks.OnCallFunc(MessageCallbackFromConsumerEmpty).Do(
215 [& expectTrue, &msgID](const int &id, const std::string&, const std::string&, const std::string&)
219 std::cout << "This function never call" << std::endl;
224 g_consumer->acceptSubscription(g_consumer, false);
226 OIC::Service::NSMessage * msg = OIC::Service::NSProviderService::getInstance()->CreateMessage();
227 msgID = (int)msg->getMessageId();
228 msg->setTitle(std::string("Title"));
229 msg->setContentText(std::string("ContentText"));
230 msg->setSourceName(std::string("OCF"));
232 OIC::Service::NSProviderService::getInstance()->SendMessage(msg);
234 std::unique_lock< std::mutex > lock{ mutexForCondition };
235 responseCon.wait_for(lock, g_waitForResponse);
238 std::unique_lock< std::mutex > lock{ mutexForCondition };
239 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
241 EXPECT_EQ(expectTrue, true);
244 TEST_F(NotificationProviderServiceTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
248 mocks.ExpectCallFunc(MessageCallbackFromConsumerEmpty).Do(
249 [&msgID](const int &id, const std::string&, const std::string&, const std::string&)
253 std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
254 responseCon.notify_all();
258 g_consumer->acceptSubscription(g_consumer, true);
260 OIC::Service::NSMessage * msg = OIC::Service::NSProviderService::getInstance()->CreateMessage();
261 msgID = (int)msg->getMessageId();
262 msg->setTitle(std::string("Title"));
263 msg->setContentText(std::string("ContentText"));
264 msg->setSourceName(std::string("OCF"));
266 OIC::Service::NSProviderService::getInstance()->SendMessage(msg);
268 std::unique_lock< std::mutex > lock{ mutexForCondition };
269 responseCon.wait_for(lock, g_waitForResponse);
272 std::unique_lock< std::mutex > lock{ mutexForCondition };
273 responseCon.wait_for(lock, g_waitForResponse);
276 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadToConsumer)
280 mocks.ExpectCallFunc(SyncCallbackFromConsumerEmpty).Do(
281 [& id](int & type, int &syncId)
283 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
285 type == (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
287 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
288 responseCon.notify_all();
292 OIC::Service::NSMessage * msg = OIC::Service::NSProviderService::getInstance()->CreateMessage();
293 id = (int)msg->getMessageId();
294 msg->setTitle(std::string("Title"));
295 msg->setContentText(std::string("ContentText"));
296 msg->setSourceName(std::string("OCF"));
298 OIC::Service::NSProviderService::getInstance()->SendSyncInfo(msg->getMessageId(), OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ);
299 std::unique_lock< std::mutex > lock{ mutexForCondition };
300 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
303 TEST_F(NotificationProviderServiceTest, ExpectCallbackSyncOnReadFromConsumer)
305 int type = (int)OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ;
307 mocks.ExpectCallFunc(MessageSynchronizedCallbackEmpty).Do(
308 [& id](OIC::Service::NSSyncInfo * sync)
310 std::cout << "MessageSynchronizedCallbackEmpty" << std::endl;
311 if ((int)sync->getMessageId() == id && sync->getState() == OIC::Service::NSSyncInfo::NSSyncType::NS_SYNC_READ)
313 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
314 responseCon.notify_all();
318 OIC::Service::NSMessage * msg = OIC::Service::NSProviderService::getInstance()->CreateMessage();
319 id = (int)msg->getMessageId();
320 msg->setTitle(std::string("Title"));
321 msg->setContentText(std::string("ContentText"));
322 msg->setSourceName(std::string("OCF"));
324 g_consumerSimul.syncToProvider(type, id, msg->getProviderId());
325 std::unique_lock< std::mutex > lock{ mutexForCondition };
326 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
329 TEST_F(NotificationProviderServiceTest, CancelObserves)
331 bool ret = g_consumerSimul.cancelObserves();
333 std::unique_lock< std::mutex > lock{ mutexForCondition };
334 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
336 EXPECT_EQ(ret, true);