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 "NSProviderInterface.h"
30 #include "NSConsumerSimulator.h"
35 std::atomic_bool g_isStartedStack(false);
37 std::chrono::milliseconds g_waitForResponse(500);
39 std::condition_variable responseCon;
40 std::mutex mutexForCondition;
42 NSConsumerSimulator g_consumerSimul;
43 NSConsumer * g_consumer;
46 class TestWithMock: public testing::Test
52 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
54 virtual void TearDown() {
67 class NotificationProviderTest : public TestWithMock
70 NotificationProviderTest() = default;
71 ~NotificationProviderTest() = default;
73 static void NSRequestedSubscribeCallbackEmpty(NSConsumer *)
75 std::cout << __func__ << std::endl;
78 static void NSSyncCallbackEmpty(NSSyncInfo *)
80 std::cout << __func__ << std::endl;
83 static void NSMessageCallbackFromConsumerEmpty(
84 const int &, const std::string &, const std::string &, const std::string &)
86 std::cout << __func__ << std::endl;
89 static void NSSyncCallbackFromConsumerEmpty(int, int)
91 std::cout << __func__ << std::endl;
98 TestWithMock::SetUp();
100 if (g_isStartedStack == false)
102 OC::PlatformConfig cfg
104 OC::ServiceType::InProc,
108 OC::QualityOfService::HighQos
110 OC::OCPlatform::Configure(cfg);
114 OC::OCPlatform::stopPresence();
121 g_isStartedStack = true;
128 TestWithMock::TearDown();
133 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyTrue)
135 NSProviderConfig config;
136 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
137 config.syncInfoCallback = NSSyncCallbackEmpty;
138 config.policy = true;
139 config.userInfo = NULL;
141 NSResult ret = NSStartProvider(config);
143 std::unique_lock< std::mutex > lock{ mutexForCondition };
144 responseCon.wait_for(lock, g_waitForResponse);
146 EXPECT_EQ(ret, NS_OK);
149 TEST_F(NotificationProviderTest, StopProviderPositive)
151 NSResult ret = NSStopProvider();
153 std::unique_lock< std::mutex > lock{ mutexForCondition };
154 responseCon.wait_for(lock, g_waitForResponse);
156 EXPECT_EQ(ret, NS_OK);
159 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyFalse)
161 NSProviderConfig config;
162 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
163 config.syncInfoCallback = NSSyncCallbackEmpty;
164 config.policy = false;
165 config.userInfo = NULL;
167 NSResult ret = NSStartProvider(config);
169 std::unique_lock< std::mutex > lock{ mutexForCondition };
170 responseCon.wait_for(lock, std::chrono::milliseconds(3000));
171 g_consumerSimul.findProvider();
173 responseCon.wait_for(lock, std::chrono::milliseconds(3000));
175 EXPECT_EQ(ret, NS_OK);
178 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
180 mocks.ExpectCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
181 [](NSConsumer * consumer)
183 std::cout << "NSRequestedSubscribeCallback" << std::endl;
184 g_consumer = (NSConsumer *)malloc(sizeof(NSConsumer));
185 strncpy(g_consumer->consumerId , consumer->consumerId, 37);
186 responseCon.notify_all();
189 NSProviderConfig config;
190 config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
191 config.syncInfoCallback = NSSyncCallbackEmpty;
192 config.policy = true;
193 config.userInfo = NULL;
195 NSStartProvider(config);
198 std::unique_lock< std::mutex > lock{ mutexForCondition };
199 responseCon.wait_for(lock, g_waitForResponse);
202 g_consumerSimul.setCallback(NSMessageCallbackFromConsumerEmpty,
203 NSSyncCallbackFromConsumerEmpty);
204 g_consumerSimul.findProvider();
206 std::unique_lock< std::mutex > lock{ mutexForCondition };
207 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
210 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
212 bool expectTrue = true;
215 mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
216 [& expectTrue, &msgID](const int &id, const std::string&, const std::string&, const std::string&)
220 std::cout << "This function never call" << std::endl;
225 NSAcceptSubscription(g_consumer, false);
227 NSMessage * msg = NSCreateMessage();
228 msgID = (int)msg->messageId;
229 msg->title = strdup(std::string("Title").c_str());
230 msg->contentText = strdup(std::string("ContentText").c_str());
231 msg->sourceName = strdup(std::string("OCF").c_str());
235 std::unique_lock< std::mutex > lock{ mutexForCondition };
236 responseCon.wait_for(lock, g_waitForResponse);
239 std::unique_lock< std::mutex > lock{ mutexForCondition };
240 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
242 EXPECT_EQ(expectTrue, true);
244 NSAcceptSubscription(g_consumer, true);
247 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
251 mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
252 [&msgID](const int &id, const std::string&, const std::string&, const std::string&)
254 std::cout << "id : " << id << std::endl;
257 std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
258 responseCon.notify_all();
262 NSAcceptSubscription(g_consumer, true);
264 NSMessage * msg = NSCreateMessage();
265 msgID = (int)msg->messageId;
266 msg->title = strdup(std::string("Title").c_str());
267 msg->contentText = strdup(std::string("ContentText").c_str());
268 msg->sourceName = strdup(std::string("OCF").c_str());
271 std::unique_lock< std::mutex > lock{ mutexForCondition };
272 responseCon.wait(lock);
275 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
279 mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
280 [& id](int & type, int &syncId)
282 std::cout << "NSSyncCallbackEmpty" << std::endl;
284 type == NS_SYNC_READ)
286 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
287 responseCon.notify_all();
291 NSMessage * msg = NSCreateMessage();
292 id = (int)msg->messageId;
293 msg->title = strdup(std::string("Title").c_str());
294 msg->contentText = strdup(std::string("ContentText").c_str());
295 msg->sourceName = strdup(std::string("OCF").c_str());
297 NSProviderSendSyncInfo(msg->messageId, NS_SYNC_READ);
298 std::unique_lock< std::mutex > lock{ mutexForCondition };
299 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
302 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
304 int type = NS_SYNC_READ;
306 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
307 [& id](NSSyncInfo * sync)
309 std::cout << "NSSyncCallbackEmpty" << std::endl;
310 if ((int)sync->messageId == id && sync->state == NS_SYNC_READ)
312 std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
313 responseCon.notify_all();
317 NSMessage * msg = NSCreateMessage();
318 id = (int)msg->messageId;
319 msg->title = strdup(std::string("Title").c_str());
320 msg->contentText = strdup(std::string("ContentText").c_str());
321 msg->sourceName = strdup(std::string("OCF").c_str());
323 g_consumerSimul.syncToProvider(type, id, msg->providerId);
324 std::unique_lock< std::mutex > lock{ mutexForCondition };
325 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
328 TEST_F(NotificationProviderTest, CancelObserves)
330 bool ret = g_consumerSimul.cancelObserves();
332 std::unique_lock< std::mutex > lock{ mutexForCondition };
333 responseCon.wait_for(lock, std::chrono::milliseconds(5000));
335 EXPECT_EQ(ret, true);