Updated unit test of consumer.
[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
59     }
60
61     virtual void TearDown()
62     {
63         try
64         {
65             mocks.VerifyAll();
66         }
67         catch (...)
68         {
69             mocks.reset();
70             throw;
71         }
72     }
73 };
74
75 class NotificationConsumerTest : public TestWithMock
76 {
77 public:
78     NotificationConsumerTest() = default;
79     ~NotificationConsumerTest() = default;
80
81     static void NSProviderDiscoveredCallbackEmpty(NSProvider *)
82     {
83         std::cout << __func__ << std::endl;
84     }
85
86     static void NSNotificationReceivedCallbackEmpty(NSMessage *)
87     {
88         std::cout << __func__ << std::endl;
89     }
90
91     static void NSSyncCallbackEmpty(NSSyncInfo *)
92     {
93         std::cout << __func__ << std::endl;
94     }
95
96     static void foundResourceEmpty(std::shared_ptr< OC::OCResource >)
97     {
98         std::cout << __func__ << std::endl;
99     }
100
101     static void SubscriptionAcceptedCallback(NSProvider *)
102     {
103         std::cout << __func__ << std::endl;
104     }
105
106 protected:
107
108     void SetUp()
109     {
110         TestWithMock::SetUp();
111
112         if (g_isStartedStack == false)
113         {
114             OC::PlatformConfig cfg
115             {
116                 OC::ServiceType::InProc,
117                 OC::ModeType::Both,
118                 "0.0.0.0",
119                 0,
120                 OC::QualityOfService::LowQos
121             };
122             OC::OCPlatform::Configure(cfg);
123
124             try
125             {
126                 OC::OCPlatform::stopPresence();
127             }
128             catch (...)
129             {
130
131             }
132
133             g_isStartedStack = true;
134         }
135
136     }
137
138     void TearDown()
139     {
140         TestWithMock::TearDown();
141     }
142
143 };
144
145 TEST_F(NotificationConsumerTest, StartConsumerPositive)
146 {
147     NSConsumerConfig cfg;
148     cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
149     cfg.acceptedCb = SubscriptionAcceptedCallback;
150     cfg.messageCb = NSNotificationReceivedCallbackEmpty;
151     cfg.syncInfoCb = NSSyncCallbackEmpty;
152     EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
153 }
154
155 TEST_F(NotificationConsumerTest, StopConsumerPositive)
156 {
157     EXPECT_EQ(NSStopConsumer(), NS_OK);
158 }
159
160 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
161 {
162     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
163             [this](NSProvider *)
164             {
165                 std::cout << "Call Discovered" << std::endl;
166                 responseCon.notify_all();
167             });
168
169     NSConsumerConfig cfg;
170     cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
171     cfg.acceptedCb = SubscriptionAcceptedCallback;
172     cfg.messageCb = NSNotificationReceivedCallbackEmpty;
173     cfg.syncInfoCb = NSSyncCallbackEmpty;
174     NSStartConsumer(cfg);
175
176     g_providerSimul.setAccepter(1);
177     g_providerSimul.createNotificationResource();
178
179     std::unique_lock< std::mutex > lock{ mutexForCondition };
180     responseCon.wait_for(lock, g_waitForResponse);
181
182     NSStopConsumer();
183     g_providerSimul.deleteNotificationResource();
184 }
185
186 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
187 {
188     g_providerSimul.setAccepter(1);
189     g_providerSimul.createNotificationResource();
190     {
191         std::unique_lock< std::mutex > lock{ mutexForCondition };
192         responseCon.wait_for(lock, g_waitForResponse);
193     }
194
195     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
196             [this](NSProvider * provider)
197             {
198                 std::cout << "Call Discovered" << std::endl;
199                 g_provider = provider;
200                 responseCon.notify_all();
201             });
202
203     NSConsumerConfig cfg;
204     cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
205     cfg.acceptedCb = SubscriptionAcceptedCallback;
206     cfg.messageCb = NSNotificationReceivedCallbackEmpty;
207     cfg.syncInfoCb = NSSyncCallbackEmpty;
208     NSStartConsumer(cfg);
209
210     std::unique_lock< std::mutex > lock{ mutexForCondition };
211     responseCon.wait_for(lock, g_waitForResponse);
212
213 }
214
215 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
216 {
217     g_providerSimul.setAccepter(1);
218     mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
219             [this](NSProvider * provider)
220             {
221                 std::cout << "Call Discovered" << std::endl;
222                 g_provider = provider;
223                 std::cout << g_provider->providerId << std::endl;
224                 responseCon.notify_all();
225             });
226
227     NSRescanProvider();
228
229     std::unique_lock< std::mutex > lock{ mutexForCondition };
230     responseCon.wait_for(lock, g_waitForResponse);
231
232 //    NSStopConsumer();
233 }
234
235 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
236 {
237     mocks.ExpectCallFunc(SubscriptionAcceptedCallback).Do(
238             [](NSProvider * )
239             {
240                 std::cout << "Income Accepted subscription : " << std::endl;
241             });
242
243     NSResult ret = NSSubscribe(g_provider);
244     std::unique_lock< std::mutex > lock{ mutexForCondition };
245     responseCon.wait_for(lock, g_waitForResponse);
246
247     EXPECT_EQ(NS_OK, ret);
248 }
249
250 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
251 {
252     uint64_t id = 10;
253     std::string title = "title";
254     std::string msg = "msg";
255
256     mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
257             [](NSMessage * message)
258             {
259                 std::cout << "Income Notification : " << message->messageId << std::endl;
260             });
261
262     g_providerSimul.notifyMessage(id, title, msg);
263
264     std::unique_lock< std::mutex > lock{ mutexForCondition };
265     responseCon.wait_for(lock, g_waitForResponse);
266
267     NSStopConsumer();
268 }
269
270 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
271 {
272     uint64_t id = 11;
273     std::string title = "title";
274     std::string msg = "msg";
275
276     g_providerSimul.setAccepter(0);
277
278     NSConsumerConfig cfg;
279     cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
280     cfg.acceptedCb = SubscriptionAcceptedCallback;
281     cfg.messageCb = NSNotificationReceivedCallbackEmpty;
282     cfg.syncInfoCb = NSSyncCallbackEmpty;
283     NSStartConsumer(cfg);
284     {
285         std::unique_lock< std::mutex > lock{ mutexForCondition };
286         responseCon.wait_for(lock, g_waitForResponse);
287     }
288
289     mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
290             [](NSMessage * message)
291             {
292                 std::cout << "Income Notification : " << message->messageId << std::endl;
293             });
294
295     g_providerSimul.notifyMessage(id, title, msg);
296
297     std::unique_lock< std::mutex > lock{ mutexForCondition };
298     responseCon.wait_for(lock, g_waitForResponse);
299
300 //    g_providerSimul.deleteNotificationResource();
301 //    NSStopConsumer();
302 }
303
304 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
305 {
306     uint64_t id = 12;
307     std::string title = "title";
308     std::string msg = "msg";
309
310     NSSyncType type = NS_SYNC_DELETED;
311
312     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
313             [](NSMessage * message)
314             {
315                 std::cout << "Income Notification : " << message->messageId << std::endl;
316             });
317
318     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
319             [& type](NSSyncInfo * sync)
320             {
321                 std::cout << "Income SyncInfo : " << sync->messageId
322                         << ", State : " << sync->state << std::endl;
323                 type = sync->state;
324
325             });
326
327     g_providerSimul.notifyMessage(id, title, msg);
328     {
329         std::unique_lock< std::mutex > lock{ mutexForCondition };
330         responseCon.wait_for(lock, g_waitForResponse);
331     }
332
333     g_providerSimul.sendRead(id);
334     {
335         std::unique_lock< std::mutex > lock{ mutexForCondition };
336         responseCon.wait_for(lock, g_waitForResponse);
337     }
338
339 //    g_providerSimul.deleteNotificationResource();
340 //    NSStopConsumer();
341
342     EXPECT_EQ(NS_SYNC_READ, type);
343 }
344
345 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
346 {
347     uint64_t id = 13;
348     std::string title = "title";
349     std::string msg = "msg";
350
351     NSSyncType type = NS_SYNC_READ;
352
353     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
354             [](NSMessage * message)
355             {
356                 std::cout << "Income Notification : " << message->messageId << std::endl;
357             });
358
359     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
360             [& type](NSSyncInfo * sync)
361             {
362                 std::cout << "Income Notification : " << sync->messageId
363                         << ", State : " << sync->state << std::endl;
364                 type = sync->state;
365
366             });
367
368     g_providerSimul.notifyMessage(id, title, msg);
369     {
370         std::unique_lock< std::mutex > lock{ mutexForCondition };
371         responseCon.wait_for(lock, g_waitForResponse);
372     }
373
374     g_providerSimul.sendDismiss(id);
375     {
376         std::unique_lock< std::mutex > lock{ mutexForCondition };
377         responseCon.wait_for(lock, g_waitForResponse);
378     }
379
380 //    g_providerSimul.deleteNotificationResource();
381 //    NSStopConsumer();
382
383     EXPECT_EQ(NS_SYNC_DELETED, type);
384 }
385
386 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
387 {
388     uint64_t id = 14;
389     std::string title = "title";
390     std::string msg = "msg";
391
392     NSSyncType type = NS_SYNC_DELETED;
393
394     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
395             [](NSMessage * message)
396             {
397                 std::cout << "Income Notification : " << message->messageId << std::endl;
398                 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
399                 std::unique_lock< std::mutex > lock{ mutexForCondition };
400                 responseCon.wait_for(lock, g_waitForResponse);
401             });
402
403     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
404             [& type](NSSyncInfo * sync)
405             {
406                 std::cout << "Income Notification : " << sync->messageId
407                         << ", State : " << sync->state << std::endl;
408                 type = sync->state;
409
410             });
411
412     g_providerSimul.notifyMessage(id, title, msg);
413     {
414         std::unique_lock< std::mutex > lock{ mutexForCondition };
415         responseCon.wait_for(lock, g_waitForResponse);
416     }
417
418 //    g_providerSimul.deleteNotificationResource();
419 //    NSStopConsumer();
420
421     EXPECT_EQ(NS_SYNC_READ, type);
422 }
423
424 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
425 {
426     uint64_t id = 15;
427     std::string title = "title";
428     std::string msg = "msg";
429
430     NSSyncType type = NS_SYNC_READ;
431
432     mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
433             [](NSMessage * message)
434             {
435                 std::cout << "Income Notification : " << message->messageId << std::endl;
436                 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
437                 std::unique_lock< std::mutex > lock{ mutexForCondition };
438                 responseCon.wait_for(lock, g_waitForResponse);
439             });
440
441     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
442             [& type](NSSyncInfo * sync)
443             {
444                 std::cout << "Income Notification : " << sync->messageId
445                         << ", State : " << sync->state << std::endl;
446                 type = sync->state;
447
448             });
449
450     g_providerSimul.notifyMessage(id, title, msg);
451     {
452         std::unique_lock< std::mutex > lock{ mutexForCondition };
453         responseCon.wait_for(lock, g_waitForResponse);
454     }
455
456     EXPECT_EQ(NS_SYNC_DELETED, type);
457 }
458
459 TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
460 {
461     NSResult ret = NSUnsubscribe(g_provider);
462     std::unique_lock< std::mutex > lock{ mutexForCondition };
463     responseCon.wait_for(lock, g_waitForResponse);
464
465     g_providerSimul.deleteNotificationResource();
466     NSStopConsumer();
467
468     EXPECT_EQ(NS_OK, ret);
469 }