Merge branch 'master' into notification-service
[platform/upstream/iotivity.git] / service / notification / unittest / NSConsumerTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
23 #include <atomic>
24 #include <functional>
25 #include <condition_variable>
26 #include <mutex>
27 #include <chrono>
28
29 #include "ocstack.h"
30
31 #include "NSCommon.h"
32 #include "NSConsumerInterface.h"
33
34 #include "NSProviderSimulator.h"
35
36 namespace
37 {
38     NSProviderSimulator g_providerSimul;
39     NSProvider * g_provider;
40
41     std::atomic_bool g_isStartedStack(false);
42
43     std::chrono::milliseconds g_waitForResponse(500);
44
45     std::condition_variable responseCon;
46     std::mutex mutexForCondition;
47
48 }
49
50 class TestWithMock: public testing::Test
51 {
52 public:
53     MockRepository mocks;
54
55 protected:
56     virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
57
58     virtual void TearDown() {
59         try
60         {
61             mocks.VerifyAll();
62         }
63         catch (...)
64         {
65             mocks.reset();
66             throw;
67         }
68     }
69 };
70
71 class NotificationConsumerTest : public TestWithMock
72 {
73 public:
74     NotificationConsumerTest() = default;
75     ~NotificationConsumerTest() = default;
76
77     static void NSProviderDiscoveredCallbackEmpty(NSProvider *)
78     {
79         std::cout << __func__ << std::endl;
80     }
81
82     static void NSNotificationReceivedCallbackEmpty(NSProvider *, NSMessage *) { }
83
84     static void NSSyncCallbackEmpty(NSProvider *, NSSync *) { }
85
86     static void foundResourceEmpty(std::shared_ptr< OC::OCResource >) { }
87
88 protected:
89
90     void SetUp()
91     {
92         TestWithMock::SetUp();
93
94         if (g_isStartedStack == false)
95         {
96             OC::PlatformConfig cfg
97             {
98                 OC::ServiceType::InProc,
99                 OC::ModeType::Both,
100                 "0.0.0.0",
101                 0,
102                 OC::QualityOfService::LowQos
103             };
104             OC::OCPlatform::Configure(cfg);
105
106             try
107             {
108                 OC::OCPlatform::stopPresence();
109             }
110             catch (...)
111             {
112
113             }
114
115             g_isStartedStack = true;
116         }
117
118     }
119
120     void TearDown()
121     {
122         TestWithMock::TearDown();
123     }
124
125 };
126
127 TEST_F(NotificationConsumerTest, StartConsumerPositive)
128 {
129     EXPECT_EQ(NS_OK,
130               NSStartConsumer(
131                     NSProviderDiscoveredCallbackEmpty,
132                     NSNotificationReceivedCallbackEmpty,
133                     NSSyncCallbackEmpty));
134 }
135
136 TEST_F(NotificationConsumerTest, StopConsumerPositive)
137 {
138     EXPECT_EQ(NSStopConsumer(), NS_OK);
139 }
140
141 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
142 {
143     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
144             [this, & responseCon](NSProvider *)
145             {
146                 std::cout << "Call Discovered" << std::endl;
147                 responseCon.notify_all();
148             });
149
150     NSStartConsumer(
151             NSProviderDiscoveredCallbackEmpty,
152             NSNotificationReceivedCallbackEmpty,
153             NSSyncCallbackEmpty);
154
155     g_providerSimul.setAccepter(1);
156     g_providerSimul.createNotificationResource();
157
158     std::unique_lock< std::mutex > lock{ mutexForCondition };
159     responseCon.wait_for(lock, g_waitForResponse);
160
161     NSStopConsumer();
162     g_providerSimul.deleteNotificationResource();
163 }
164
165 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
166 {
167     g_providerSimul.setAccepter(1);
168     g_providerSimul.createNotificationResource();
169     {
170         std::unique_lock< std::mutex > lock{ mutexForCondition };
171         responseCon.wait_for(lock, g_waitForResponse);
172     }
173
174     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
175             [this, & responseCon](NSProvider *)
176             {
177                 std::cout << "Call Discovered" << std::endl;
178                 responseCon.notify_all();
179             });
180
181     NSStartConsumer(
182             NSProviderDiscoveredCallbackEmpty,
183             NSNotificationReceivedCallbackEmpty,
184             NSSyncCallbackEmpty);
185
186     std::unique_lock< std::mutex > lock{ mutexForCondition };
187     responseCon.wait_for(lock, g_waitForResponse);
188
189 }
190
191 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
192 {
193     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty)
194             .Do(
195             [this, & responseCon](NSProvider * provider)
196             {
197                 std::cout << "Call Discovered" << std::endl;
198                 g_provider = provider;
199                 responseCon.notify_all();
200             });
201
202     NSRescanProvider();
203
204     std::unique_lock< std::mutex > lock{ mutexForCondition };
205     responseCon.wait_for(lock, g_waitForResponse);
206
207 //    NSStopConsumer();
208 }
209
210 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
211 {
212     NSResult ret = NSSubscribe(g_provider);
213     std::unique_lock< std::mutex > lock{ mutexForCondition };
214     responseCon.wait_for(lock, g_waitForResponse);
215
216     EXPECT_EQ(NS_OK, ret);
217 }
218
219 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
220 {
221     std::string id = "id";
222     std::string title = "title";
223     std::string msg = "msg";
224
225     mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
226             [](NSProvider *, NSMessage * message)
227             {
228                 std::cout << "Income Notification : " << message->mId << std::endl;
229                 NSDropNSObject(message);
230             });
231
232     g_providerSimul.notifyMessage(id, title, msg);
233
234     std::unique_lock< std::mutex > lock{ mutexForCondition };
235     responseCon.wait_for(lock, g_waitForResponse);
236
237 }
238
239 TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
240 {
241     NSResult ret = NSUnsubscribe(g_provider);
242     std::unique_lock< std::mutex > lock{ mutexForCondition };
243     responseCon.wait_for(lock, g_waitForResponse);
244
245     EXPECT_EQ(NS_OK, ret);
246 }
247
248 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
249 {
250     std::string id = "ExpectReceiveNotificationWithAccepterisProvider";
251     std::string title = "title";
252     std::string msg = "msg";
253
254     g_providerSimul.setAccepter(0);
255
256     NSRescanProvider();
257     {
258         std::unique_lock< std::mutex > lock{ mutexForCondition };
259         responseCon.wait_for(lock, g_waitForResponse);
260     }
261
262     mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
263             [](NSProvider *, NSMessage * message)
264             {
265                 std::cout << "Income Notification : " << message->mId << std::endl;
266                 NSDropNSObject(message);
267             });
268
269     g_providerSimul.notifyMessage(id, title, msg);
270
271     std::unique_lock< std::mutex > lock{ mutexForCondition };
272     responseCon.wait_for(lock, g_waitForResponse);
273
274 //    g_providerSimul.deleteNotificationResource();
275 //    NSStopConsumer();
276 }
277
278 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
279 {
280     std::string id = "ExpectCallbackReadCheckWhenProviderNotifySync";
281     std::string title = "title";
282     std::string msg = "msg";
283
284     NSSyncTypes type = Notification_Dismiss;
285
286     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
287             [](NSProvider *, NSMessage * message)
288             {
289                 std::cout << "Income Notification : " << message->mId << std::endl;
290                 NSDropNSObject(message);
291             });
292
293     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
294             [& type](NSProvider *, NSSync * sync)
295             {
296                 std::cout << "Income Notification : " << sync->mMessageId
297                         << ", State : " << sync->mState << std::endl;
298                 type = sync->mState;
299
300             });
301
302     g_providerSimul.notifyMessage(id, title, msg);
303     {
304         std::unique_lock< std::mutex > lock{ mutexForCondition };
305         responseCon.wait_for(lock, g_waitForResponse);
306     }
307
308     g_providerSimul.sendRead(id);
309     {
310         std::unique_lock< std::mutex > lock{ mutexForCondition };
311         responseCon.wait_for(lock, g_waitForResponse);
312     }
313
314 //    g_providerSimul.deleteNotificationResource();
315 //    NSStopConsumer();
316
317     EXPECT_EQ(Notification_Read, type);
318 }
319
320 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
321 {
322     std::string id = "ExpectCallbackDismissCheckWhenProviderNotifySync";
323     std::string title = "title";
324     std::string msg = "msg";
325
326     NSSyncTypes type = Notification_Read;
327
328     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
329             [](NSProvider *, NSMessage * message)
330             {
331                 std::cout << "Income Notification : " << message->mId << std::endl;
332                 NSDropNSObject(message);
333             });
334
335     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
336             [& type](NSProvider *, NSSync * sync)
337             {
338                 std::cout << "Income Notification : " << sync->mMessageId
339                         << ", State : " << sync->mState << std::endl;
340                 type = sync->mState;
341
342             });
343
344     g_providerSimul.notifyMessage(id, title, msg);
345     {
346         std::unique_lock< std::mutex > lock{ mutexForCondition };
347         responseCon.wait_for(lock, g_waitForResponse);
348     }
349
350     g_providerSimul.sendDismiss(id);
351     {
352         std::unique_lock< std::mutex > lock{ mutexForCondition };
353         responseCon.wait_for(lock, g_waitForResponse);
354     }
355
356 //    g_providerSimul.deleteNotificationResource();
357 //    NSStopConsumer();
358
359     EXPECT_EQ(Notification_Dismiss, type);
360 }
361
362 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
363 {
364     std::string id = "ExpectCallbackReadCheckWhenConsumerPostSync";
365     std::string title = "title";
366     std::string msg = "msg";
367
368     NSSyncTypes type = Notification_Dismiss;
369
370     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
371             [](NSProvider *, NSMessage * message)
372             {
373                 std::cout << "Income Notification : " << message->mId << std::endl;
374                 NSConsumerReadCheck(message);
375                 std::unique_lock< std::mutex > lock{ mutexForCondition };
376                 responseCon.wait_for(lock, g_waitForResponse);
377             });
378
379     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
380             [& type](NSProvider *, NSSync * sync)
381             {
382                 std::cout << "Income Notification : " << sync->mMessageId
383                         << ", State : " << sync->mState << std::endl;
384                 type = sync->mState;
385
386             });
387
388     g_providerSimul.notifyMessage(id, title, msg);
389     {
390         std::unique_lock< std::mutex > lock{ mutexForCondition };
391         responseCon.wait_for(lock, g_waitForResponse);
392     }
393
394 //    g_providerSimul.deleteNotificationResource();
395 //    NSStopConsumer();
396
397     EXPECT_EQ(Notification_Read, type);
398 }
399
400 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
401 {
402     std::string id = "ExpectCallbackDismissCheckWhenConsumerPostSync";
403     std::string title = "title";
404     std::string msg = "msg";
405
406     NSSyncTypes type = Notification_Read;
407
408     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
409             [](NSProvider *, NSMessage * message)
410             {
411                 std::cout << "Income Notification : " << message->mId << std::endl;
412                 NSConsumerDismissCheck(message);
413                 std::unique_lock< std::mutex > lock{ mutexForCondition };
414                 responseCon.wait_for(lock, g_waitForResponse);
415             });
416
417     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
418             [& type](NSProvider *, NSSync * sync)
419             {
420                 std::cout << "Income Notification : " << sync->mMessageId
421                         << ", State : " << sync->mState << std::endl;
422                 type = sync->mState;
423
424             });
425
426     g_providerSimul.notifyMessage(id, title, msg);
427     {
428         std::unique_lock< std::mutex > lock{ mutexForCondition };
429         responseCon.wait_for(lock, g_waitForResponse);
430     }
431
432     g_providerSimul.deleteNotificationResource();
433     NSStopConsumer();
434
435     EXPECT_EQ(Notification_Dismiss, type);
436 }