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