b7a8d5673714009d201c84c21d721669b75b568e
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceCache / unittests / DataCacheTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 <iostream>
22 #include <gtest/gtest.h>
23 #include <HippoMocks/hippomocks.h>
24
25 #include "ResourceCacheManager.h"
26 #include "DataCache.h"
27 #include "RCSResourceAttributes.h"
28 #include "ResponseStatement.h"
29 #include "UnitTestHelper.h"
30
31 using namespace OIC::Service;
32
33 class DataCacheTest : public TestWithMock
34 {
35     public:
36         typedef std::function <
37         void(const OIC::Service::HeaderOptions &, const OIC::Service::ResponseStatement &,
38              int) > GetCallback;
39
40         typedef std::function <
41         void(const OIC::Service::HeaderOptions &, const OIC::Service::ResponseStatement &, int,
42              int) > ObserveCallback;
43     public:
44         std::shared_ptr<DataCache> cacheHandler;
45         PrimitiveResource::Ptr pResource;
46         CacheCB cb;
47         CacheID id;
48
49     protected:
50         virtual void SetUp()
51         {
52             TestWithMock::SetUp();
53             pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(),
54                                                [](PrimitiveResource *)
55                                                {
56
57                                                });
58
59             mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(false);
60             cacheHandler.reset(new DataCache());
61             cb = ([](std::shared_ptr<PrimitiveResource >, const RCSResourceAttributes &)->OCStackResult
62                     {
63                         return OC_STACK_OK;
64                     });
65         }
66
67         virtual void TearDown()
68         {
69             cacheHandler.reset();
70             pResource.reset();
71             TestWithMock::TearDown();
72         }
73 };
74
75 TEST_F(DataCacheTest, initializeDataCache_normalCase)
76 {
77
78     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
79     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
80     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
81     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
82
83     cacheHandler->initializeDataCache(pResource);
84 }
85
86 TEST_F(DataCacheTest, initializeDataCache_normalCaseObservable)
87 {
88
89     mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
90         [](GetCallback callback)
91     {
92         OIC::Service::HeaderOptions hos;
93
94         OIC::Service::RCSResourceAttributes attr;
95         OIC::Service::ResponseStatement rep(attr);
96         callback(hos, rep, OC_STACK_OK);
97         return;
98     }
99     );
100     mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
101     mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve).Do(
102         [](ObserveCallback callback)
103     {
104         OIC::Service::HeaderOptions hos;
105         OIC::Service::RCSResourceAttributes attr;
106         OIC::Service::ResponseStatement rep(attr);
107         int seq = 0;
108         callback(hos, rep, OC_STACK_OK, seq);
109         return;
110     }
111     );
112     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
113
114     cacheHandler->initializeDataCache(pResource);
115 }
116
117 TEST_F(DataCacheTest, initializeDataCache_normalCaseNonObservable)
118 {
119
120     mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
121         [](GetCallback callback)
122     {
123         OIC::Service::HeaderOptions hos;
124
125         OIC::Service::RCSResourceAttributes attr;
126         OIC::Service::ResponseStatement rep(attr);
127         callback(hos, rep, OC_STACK_OK);
128         return;
129     }
130     );
131     mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(false);
132     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
133
134     cacheHandler->initializeDataCache(pResource);
135
136     sleep(3);
137 }
138
139 TEST_F(DataCacheTest, initializeDataCache_normalCaseTimeOut)
140 {
141
142     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
143     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
144     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
145     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
146
147     cacheHandler->initializeDataCache(pResource);
148
149     sleep(3);
150 }
151
152 TEST_F(DataCacheTest, addSubscriber_normalCase)
153 {
154
155     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
156     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
157     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
158     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
159
160     cacheHandler->initializeDataCache(pResource);
161     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
162     long reportTime = 1l;
163
164     ASSERT_NE(cacheHandler->addSubscriber(cb, rf, reportTime), 0);
165 }
166
167 TEST_F(DataCacheTest, deleteSubsciber_normalCase)
168 {
169
170     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
171     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
172     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
173     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
174
175     cacheHandler->initializeDataCache(pResource);
176
177     REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
178     long reportTime = 1l;
179
180     id = cacheHandler->addSubscriber(cb, rf, reportTime);
181
182     ASSERT_NE(cacheHandler->deleteSubscriber(id), 0);
183 }
184
185 TEST_F(DataCacheTest, getCacheState_normalCase)
186 {
187
188     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
189     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
190     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
191     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
192
193     cacheHandler->initializeDataCache(pResource);
194
195     ASSERT_EQ(cacheHandler->getCacheState(), CACHE_STATE::READY_YET);
196 }
197
198 TEST_F(DataCacheTest, getCachedData_normalCase)
199 {
200
201     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
202     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
203     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
204     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
205
206     cacheHandler->initializeDataCache(pResource);
207
208     ASSERT_EQ(cacheHandler->getCachedData(), RCSResourceAttributes());
209 }
210
211 TEST_F(DataCacheTest, getPrimitiveResource_normalCase)
212 {
213
214     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
215     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
216     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
217     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
218
219     cacheHandler->initializeDataCache(pResource);
220
221     ASSERT_EQ(cacheHandler->getPrimitiveResource(), pResource);
222 }
223
224 TEST_F(DataCacheTest, requestGet_normalCase)
225 {
226
227     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
228     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
229     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
230     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
231     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
232
233     cacheHandler->initializeDataCache(pResource);
234
235     cacheHandler->requestGet();
236 }
237
238 TEST_F(DataCacheTest, isEmptySubscriber_normalCase)
239 {
240
241     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
242     mocks.ExpectCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
243     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
244     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
245
246     cacheHandler->initializeDataCache(pResource);
247
248     ASSERT_EQ(cacheHandler->isEmptySubscriber(), true);
249 }
250
251 TEST_F(DataCacheTest, requestGet_normalCasetest)
252 {
253
254     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestGet);
255     mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
256     mocks.ExpectCall(pResource.get(), PrimitiveResource::requestObserve);
257     mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
258         [](GetCallback callback)
259     {
260         OIC::Service::HeaderOptions hos;
261         OIC::Service::RCSResourceAttributes attr;
262         OIC::Service::ResponseStatement rep(attr);
263         callback(hos, rep, OC_STACK_OK);
264         return;
265     });
266     mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
267
268     cacheHandler->initializeDataCache(pResource);
269
270     cacheHandler->requestGet();
271 }