22f84213e940407e0bd07cc251737a35e06b3e6a
[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 "NSConsumerSimulator.h"
30
31 #include "NSCommon.h"
32 #include "NSProviderInterface.h"
33
34 namespace
35 {
36     std::atomic_bool g_isStartedStack(false);
37
38     std::chrono::milliseconds g_waitForResponse(500);
39
40     std::condition_variable responseCon;
41     std::mutex mutexForCondition;
42
43     NSConsumerSimulator g_consumerSimul;
44     NSConsumer * g_consumer;
45
46 }
47
48 class TestWithMock: public testing::Test
49 {
50 public:
51     MockRepository mocks;
52
53 protected:
54     virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
55
56     virtual void TearDown() {
57         try
58         {
59             mocks.VerifyAll();
60         }
61         catch (...)
62         {
63             mocks.reset();
64             throw;
65         }
66     }
67 };
68
69 class NotificationProviderTest : public TestWithMock
70 {
71 public:
72     NotificationProviderTest() = default;
73     ~NotificationProviderTest() = default;
74
75     static void NSRequestedSubscribeCallbackEmpty(NSConsumer *)
76     {
77         std::cout << __func__ << std::endl;
78     }
79
80     static void NSSyncCallbackEmpty(NSSync *)
81     {
82         std::cout << __func__ << std::endl;
83     }
84
85     static void NSMessageCallbackFromConsumerEmpty(
86             const std::string &, const std::string &, const std::string &)
87     {
88         std::cout << __func__ << std::endl;
89     }
90
91     static void NSSyncCallbackFromConsumerEmpty(int, const std::string &)
92     {
93         std::cout << __func__ << std::endl;
94     }
95
96 protected:
97
98     void SetUp()
99     {
100         TestWithMock::SetUp();
101
102         if (g_isStartedStack == false)
103         {
104             OC::PlatformConfig cfg
105             {
106                 OC::ServiceType::InProc,
107                 OC::ModeType::Both,
108                 "0.0.0.0",
109                 0,
110                 OC::QualityOfService::LowQos
111             };
112             OC::OCPlatform::Configure(cfg);
113
114             try
115             {
116                 OC::OCPlatform::stopPresence();
117             }
118             catch (...)
119             {
120
121             }
122
123             g_isStartedStack = true;
124         }
125
126     }
127
128     void TearDown()
129     {
130         TestWithMock::TearDown();
131     }
132
133 };
134
135 TEST_F(NotificationProviderTest, StartProviderPositive)
136 {
137     NSResult ret = NSStartProvider(NS_ACCEPTER_PROVIDER,
138             NSRequestedSubscribeCallbackEmpty,
139             NSSyncCallbackEmpty);
140
141     std::unique_lock< std::mutex > lock{ mutexForCondition };
142     responseCon.wait_for(lock, g_waitForResponse);
143
144     EXPECT_EQ(ret, NS_OK);
145 }
146
147 TEST_F(NotificationProviderTest, StopProviderPositive)
148 {
149     NSResult ret = NSStopProvider();
150
151     std::unique_lock< std::mutex > lock{ mutexForCondition };
152     responseCon.wait_for(lock, g_waitForResponse);
153
154     EXPECT_EQ(ret, NS_OK);
155 }
156
157 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
158 {
159     mocks.ExpectCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
160             [](NSConsumer * consumer)
161             {
162                 std::cout << "NSRequestedSubscribeCallback" << std::endl;
163                 g_consumer = consumer;
164                 responseCon.notify_all();
165             });
166
167     NSStartProvider(NS_ACCEPTER_PROVIDER,
168             NSRequestedSubscribeCallbackEmpty, NSSyncCallbackEmpty);
169
170     {
171         std::unique_lock< std::mutex > lock{ mutexForCondition };
172         responseCon.wait_for(lock, g_waitForResponse);
173     }
174
175     g_consumerSimul.setCallback(NSMessageCallbackFromConsumerEmpty,
176             NSSyncCallbackFromConsumerEmpty);
177     g_consumerSimul.findProvider();
178
179     std::unique_lock< std::mutex > lock{ mutexForCondition };
180     responseCon.wait_for(lock, std::chrono::milliseconds(1000));
181 }
182
183 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
184 {
185     bool expectTrue = true;
186
187     mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
188             [& expectTrue](const std::string &id, const std::string&, const std::string&)
189             {
190                 if (id == "NeverCallNotifyOnConsumerByAcceptIsFalse")
191                 {
192                     std::cout << "This function never call" << std::endl;
193                     expectTrue = false;
194                 }
195             });
196
197     NSAccept(g_consumer, false);
198
199     NSMessage * msg = new NSMessage();
200     msg->mId = strdup(std::string("NeverCallNotifyOnConsumerByAcceptIsFalse").c_str());
201     msg->mTitle = strdup(std::string("Title").c_str());
202     msg->mContentText = strdup(std::string("ContentText").c_str());
203     NSSendNotification(msg);
204     {
205         std::unique_lock< std::mutex > lock{ mutexForCondition };
206         responseCon.wait_for(lock, g_waitForResponse);
207     }
208
209     std::unique_lock< std::mutex > lock{ mutexForCondition };
210     responseCon.wait_for(lock, std::chrono::milliseconds(1000));
211
212     EXPECT_EQ(expectTrue, true);
213 }
214
215 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
216 {
217     mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
218             [](const std::string &id, const std::string&, const std::string&)
219             {
220                 if (id == "ExpectCallNotifyOnConsumerByAcceptIsTrue")
221                 {
222                     std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
223                     responseCon.notify_all();
224                 }
225             });
226
227     NSAccept(g_consumer, true);
228
229     NSMessage * msg = new NSMessage();
230     msg->mId = strdup(std::string("ExpectCallNotifyOnConsumerByAcceptIsTrue").c_str());
231     msg->mTitle = strdup(std::string("Title").c_str());
232     msg->mContentText = strdup(std::string("ContentText").c_str());
233     NSSendNotification(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, g_waitForResponse);
241 }
242
243 //TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
244 //{
245 //    int type = 0;
246 //    std::string id = "ExpectCallNotifyOnConsumerByAcceptIsTrue";
247 //    mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
248 //            [& id](int type, const std::string & syncId)
249 //            {
250 //        std::cout << "NSSyncCallbackEmpty" << std::endl;
251 //                if (syncId == id &&
252 //                        type == Notification_Read)
253 //                {
254 //                    std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
255 //                    responseCon.notify_all();
256 //                }
257 //            });
258 //
259 //    NSMessage * msg = new NSMessage();
260 //    msg->mId = strdup(std::string("ExpectCallNotifyOnConsumerByAcceptIsTrue").c_str());
261 //    msg->mTitle = strdup(std::string("Title").c_str());
262 //    msg->mContentText = strdup(std::string("ContentText").c_str());
263 //    NSProviderReadCheck(msg);
264 //    std::unique_lock< std::mutex > lock{ mutexForCondition };
265 //    responseCon.wait_for(lock, std::chrono::milliseconds(5000));
266 //}
267
268 //TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
269 //{
270 //    int type = 0;
271 //    std::string id("ExpectCallNotifyOnConsumerByAcceptIsTrue");
272 //    mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
273 //            [& id](NSSync * sync)
274 //            {
275 //                std::cout << "NSSyncCallbackEmpty" << std::endl;
276 //                if (sync->mMessageId == id && sync->mState == Notification_Read)
277 //                {
278 //                    std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
279 //                    responseCon.notify_all();
280 //                }
281 //            });
282 //
283 //    g_consumerSimul.syncToProvider(type, std::string(id));
284 //    std::unique_lock< std::mutex > lock{ mutexForCondition };
285 //    responseCon.wait_for(lock, std::chrono::milliseconds(5000));
286 //}