Bug fixes unittest for ExpectCallNotifyOnConsumerByAcceptIsTrue.
[platform/upstream/iotivity.git] / service / notification / unittest / NSProviderTest.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 "NSProviderInterface.h"
30 #include "NSConsumerSimulator.h"
31 #include "NSCommon.h"
32
33 namespace
34 {
35     std::atomic_bool g_isStartedStack(false);
36
37     std::chrono::milliseconds g_waitForResponse(500);
38
39     std::condition_variable responseCon;
40     std::mutex mutexForCondition;
41
42     NSConsumerSimulator g_consumerSimul;
43     NSConsumer * g_consumer;
44 }
45
46 class TestWithMock: public testing::Test
47 {
48 public:
49     MockRepository mocks;
50
51 protected:
52     virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
53
54     virtual void TearDown() {
55         try
56         {
57             mocks.VerifyAll();
58         }
59         catch (...)
60         {
61             mocks.reset();
62             throw;
63         }
64     }
65 };
66
67 class NotificationProviderTest : public TestWithMock
68 {
69 public:
70     NotificationProviderTest() = default;
71     ~NotificationProviderTest() = default;
72
73     static void NSRequestedSubscribeCallbackEmpty(NSConsumer *)
74     {
75         std::cout << __func__ << std::endl;
76     }
77
78     static void NSSyncCallbackEmpty(NSSyncInfo *)
79     {
80         std::cout << __func__ << std::endl;
81     }
82
83     static void NSMessageCallbackFromConsumerEmpty(
84             const int &, const std::string &, const std::string &, const std::string &)
85     {
86         std::cout << __func__ << std::endl;
87     }
88
89     static void NSSyncCallbackFromConsumerEmpty(int, int)
90     {
91         std::cout << __func__ << std::endl;
92     }
93
94 protected:
95
96     void SetUp()
97     {
98         TestWithMock::SetUp();
99
100         if (g_isStartedStack == false)
101         {
102             OC::PlatformConfig cfg
103             {
104                 OC::ServiceType::InProc,
105                 OC::ModeType::Both,
106                 "0.0.0.0",
107                 0,
108                 OC::QualityOfService::HighQos
109             };
110             OC::OCPlatform::Configure(cfg);
111
112             try
113             {
114                 OC::OCPlatform::stopPresence();
115             }
116             catch (...)
117             {
118
119             }
120
121             g_isStartedStack = true;
122         }
123
124     }
125
126     void TearDown()
127     {
128         TestWithMock::TearDown();
129     }
130
131 };
132
133 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyTrue)
134 {
135     NSProviderConfig config;
136     config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
137     config.syncInfoCallback = NSSyncCallbackEmpty;
138     config.policy = true;
139     config.userInfo = NULL;
140
141     NSResult ret = NSStartProvider(config);
142
143     std::unique_lock< std::mutex > lock{ mutexForCondition };
144     responseCon.wait_for(lock, g_waitForResponse);
145
146     EXPECT_EQ(ret, NS_OK);
147 }
148
149 TEST_F(NotificationProviderTest, StopProviderPositive)
150 {
151     NSResult ret = NSStopProvider();
152
153     std::unique_lock< std::mutex > lock{ mutexForCondition };
154     responseCon.wait_for(lock, g_waitForResponse);
155
156     EXPECT_EQ(ret, NS_OK);
157 }
158
159 TEST_F(NotificationProviderTest, StartProviderPositiveWithNSPolicyFalse)
160 {
161     NSProviderConfig config;
162     config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
163     config.syncInfoCallback = NSSyncCallbackEmpty;
164     config.policy = false;
165     config.userInfo = NULL;
166
167     NSResult ret = NSStartProvider(config);
168
169     std::unique_lock< std::mutex > lock{ mutexForCondition };
170     responseCon.wait_for(lock, std::chrono::milliseconds(3000));
171     g_consumerSimul.findProvider();
172
173     responseCon.wait_for(lock, std::chrono::milliseconds(3000));
174     NSStopProvider();
175     EXPECT_EQ(ret, NS_OK);
176 }
177
178 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
179 {
180     mocks.ExpectCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
181             [](NSConsumer * consumer)
182             {
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();
187             });
188
189     NSProviderConfig config;
190     config.subRequestCallback = NSRequestedSubscribeCallbackEmpty;
191     config.syncInfoCallback = NSSyncCallbackEmpty;
192     config.policy = true;
193     config.userInfo = NULL;
194
195     NSStartProvider(config);
196
197     {
198         std::unique_lock< std::mutex > lock{ mutexForCondition };
199         responseCon.wait_for(lock, g_waitForResponse);
200     }
201
202     g_consumerSimul.setCallback(NSMessageCallbackFromConsumerEmpty,
203             NSSyncCallbackFromConsumerEmpty);
204     g_consumerSimul.findProvider();
205
206     std::unique_lock< std::mutex > lock{ mutexForCondition };
207     responseCon.wait_for(lock, std::chrono::milliseconds(1000));
208 }
209
210 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
211 {
212     bool expectTrue = true;
213     int msgID;
214
215     mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
216             [& expectTrue, &msgID](const int &id, const std::string&, const std::string&, const std::string&)
217             {
218                 if (id == msgID)
219                 {
220                     std::cout << "This function never call" << std::endl;
221                     expectTrue = false;
222                 }
223             });
224
225     NSAcceptSubscription(g_consumer, false);
226
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());
232
233     NSSendMessage(msg);
234     {
235         std::unique_lock< std::mutex > lock{ mutexForCondition };
236         responseCon.wait_for(lock, g_waitForResponse);
237     }
238
239     std::unique_lock< std::mutex > lock{ mutexForCondition };
240     responseCon.wait_for(lock, std::chrono::milliseconds(1000));
241
242     EXPECT_EQ(expectTrue, true);
243
244     NSAcceptSubscription(g_consumer, true);
245 }
246
247 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
248 {
249     int msgID;
250
251     mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
252             [&msgID](const int &id, const std::string&, const std::string&, const std::string&)
253             {
254                 std::cout << "id : " << id << std::endl;
255                 if (id == msgID)
256                 {
257                     std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
258                     responseCon.notify_all();
259                 }
260             });
261
262     NSAcceptSubscription(g_consumer, true);
263
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());
269     NSSendMessage(msg);
270
271     std::unique_lock< std::mutex > lock{ mutexForCondition };
272     responseCon.wait(lock);
273 }
274
275 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
276 {
277     int id;
278
279     mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
280             [& id](int & type, int &syncId)
281             {
282         std::cout << "NSSyncCallbackEmpty" << std::endl;
283                 if (syncId == id &&
284                         type == NS_SYNC_READ)
285                 {
286                     std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
287                     responseCon.notify_all();
288                 }
289             });
290
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());
296
297     NSProviderSendSyncInfo(msg->messageId, NS_SYNC_READ);
298     std::unique_lock< std::mutex > lock{ mutexForCondition };
299     responseCon.wait_for(lock, std::chrono::milliseconds(5000));
300 }
301
302 TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
303 {
304     int type = NS_SYNC_READ;
305     int id;
306     mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
307             [& id](NSSyncInfo * sync)
308             {
309                 std::cout << "NSSyncCallbackEmpty" << std::endl;
310                 if ((int)sync->messageId == id && sync->state == NS_SYNC_READ)
311                 {
312                     std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
313                     responseCon.notify_all();
314                 }
315             });
316
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());
322
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));
326 }
327
328 TEST_F(NotificationProviderTest, CancelObserves)
329 {
330     bool ret = g_consumerSimul.cancelObserves();
331
332     std::unique_lock< std::mutex > lock{ mutexForCondition };
333     responseCon.wait_for(lock, std::chrono::milliseconds(5000));
334
335     EXPECT_EQ(ret, true);
336 }