1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <gtest/gtest.h>
23 #include <HippoMocks/hippomocks.h>
25 #include "ResourceCacheManager.h"
26 #include "DataCache.h"
27 #include "RCSResourceAttributes.h"
28 #include "ResponseStatement.h"
29 #include "UnitTestHelper.h"
31 using namespace OIC::Service;
33 class DataCacheTest : public TestWithMock
36 typedef std::function <
37 void(const OIC::Service::HeaderOptions &, const OIC::Service::ResponseStatement &,
40 typedef std::function <
41 void(const OIC::Service::HeaderOptions &, const OIC::Service::ResponseStatement &, int,
42 int) > ObserveCallback;
44 std::shared_ptr<DataCache> cacheHandler;
45 PrimitiveResource::Ptr pResource;
52 TestWithMock::SetUp();
53 pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(),
54 [](PrimitiveResource *)
59 mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(false);
60 cacheHandler.reset(new DataCache());
61 cb = ([](std::shared_ptr<PrimitiveResource >, const RCSResourceAttributes &)->OCStackResult
67 virtual void TearDown()
71 TestWithMock::TearDown();
75 TEST_F(DataCacheTest, initializeDataCache_normalCase)
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);
83 cacheHandler->initializeDataCache(pResource);
86 TEST_F(DataCacheTest, initializeDataCache_normalCaseObservable)
89 mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
90 [](GetCallback callback)
92 OIC::Service::HeaderOptions hos;
94 OIC::Service::RCSResourceAttributes attr;
95 OIC::Service::ResponseStatement rep(attr);
96 callback(hos, rep, OC_STACK_OK);
100 mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(true);
101 mocks.OnCall(pResource.get(), PrimitiveResource::requestObserve).Do(
102 [](ObserveCallback callback)
104 OIC::Service::HeaderOptions hos;
105 OIC::Service::RCSResourceAttributes attr;
106 OIC::Service::ResponseStatement rep(attr);
108 callback(hos, rep, OC_STACK_OK, seq);
112 mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
114 cacheHandler->initializeDataCache(pResource);
117 TEST_F(DataCacheTest, initializeDataCache_normalCaseNonObservable)
120 mocks.OnCall(pResource.get(), PrimitiveResource::requestGet).Do(
121 [](GetCallback callback)
123 OIC::Service::HeaderOptions hos;
125 OIC::Service::RCSResourceAttributes attr;
126 OIC::Service::ResponseStatement rep(attr);
127 callback(hos, rep, OC_STACK_OK);
131 mocks.OnCall(pResource.get(), PrimitiveResource::isObservable).Return(false);
132 mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
134 cacheHandler->initializeDataCache(pResource);
139 TEST_F(DataCacheTest, initializeDataCache_normalCaseTimeOut)
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);
147 cacheHandler->initializeDataCache(pResource);
152 TEST_F(DataCacheTest, addSubscriber_normalCase)
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);
160 cacheHandler->initializeDataCache(pResource);
161 REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
162 long reportTime = 1l;
164 ASSERT_NE(cacheHandler->addSubscriber(cb, rf, reportTime), 0);
167 TEST_F(DataCacheTest, deleteSubsciber_normalCase)
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);
175 cacheHandler->initializeDataCache(pResource);
177 REPORT_FREQUENCY rf = REPORT_FREQUENCY::UPTODATE;
178 long reportTime = 1l;
180 id = cacheHandler->addSubscriber(cb, rf, reportTime);
182 ASSERT_NE(cacheHandler->deleteSubscriber(id), 0);
185 TEST_F(DataCacheTest, getCacheState_normalCase)
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);
193 cacheHandler->initializeDataCache(pResource);
195 ASSERT_EQ(cacheHandler->getCacheState(), CACHE_STATE::READY_YET);
198 TEST_F(DataCacheTest, getCachedData_normalCase)
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);
206 cacheHandler->initializeDataCache(pResource);
208 ASSERT_EQ(cacheHandler->getCachedData(), RCSResourceAttributes());
211 TEST_F(DataCacheTest, getPrimitiveResource_normalCase)
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);
219 cacheHandler->initializeDataCache(pResource);
221 ASSERT_EQ(cacheHandler->getPrimitiveResource(), pResource);
224 TEST_F(DataCacheTest, requestGet_normalCase)
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);
233 cacheHandler->initializeDataCache(pResource);
235 cacheHandler->requestGet();
238 TEST_F(DataCacheTest, isEmptySubscriber_normalCase)
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);
246 cacheHandler->initializeDataCache(pResource);
248 ASSERT_EQ(cacheHandler->isEmptySubscriber(), true);
251 TEST_F(DataCacheTest, requestGet_normalCasetest)
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)
260 OIC::Service::HeaderOptions hos;
261 OIC::Service::RCSResourceAttributes attr;
262 OIC::Service::ResponseStatement rep(attr);
263 callback(hos, rep, OC_STACK_OK);
266 mocks.OnCall(pResource.get(), PrimitiveResource::cancelObserve);
268 cacheHandler->initializeDataCache(pResource);
270 cacheHandler->requestGet();