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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "UnitTestHelper.h"
23 #include "RCSRemoteResourceObject.h"
24 #include "RCSDiscoveryManager.h"
25 #include "RCSResourceObject.h"
26 #include "RCSAddress.h"
27 #include "RCSRequest.h"
29 #include <condition_variable>
32 using namespace OIC::Service;
35 constexpr char RESOURCEURI[]{ "/a/TemperatureSensor" };
36 constexpr char RESOURCETYPE[]{ "resource.type" };
37 constexpr char RESOURCEINTERFACE[]{ "oic.if.baseline" };
39 constexpr char ATTR_KEY[]{ "Temperature" };
40 constexpr int ATTR_VALUE{ 0 };
42 constexpr int DEFAULT_WAITING_TIME_IN_MILLIS = 3000;
44 void getRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
45 void setRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
46 void setRemoteRepresentationCallback(const HeaderOpts&, const RCSRepresentation&, int) {}
47 void resourceStateChanged(ResourceState) { }
48 void cacheUpdatedCallback(const RCSResourceAttributes&) {}
50 class RemoteResourceObjectTest: public TestWithMock
53 RCSResourceObject::Ptr server;
54 RCSRemoteResourceObject::Ptr object;
62 void Wait(int waitingTime = DEFAULT_WAITING_TIME_IN_MILLIS)
64 std::unique_lock< std::mutex > lock{ mutex };
65 cond.wait_for(lock, std::chrono::milliseconds{ waitingTime });
71 TestWithMock::SetUp();
75 WaitUntilDiscovered();
77 ASSERT_NE(object, nullptr);
82 TestWithMock::TearDown();
84 // This method is to make sure objects disposed.
85 WaitForPtrBeingUnique();
91 server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE).build();
92 server->setAttribute(ATTR_KEY, ATTR_VALUE);
95 void WaitUntilDiscovered()
97 for (int i=0; i<10 && !object; ++i)
99 auto discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
100 RCSAddress::multicast(), RESOURCETYPE,
101 std::bind(&RemoteResourceObjectTest::resourceDiscovered, this,
102 std::placeholders::_1));
104 discoveryTask->cancel();
108 void WaitForPtrBeingUnique()
110 while((object && !object.unique()) || (server && !server.unique()))
112 std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
116 void resourceDiscovered(RCSRemoteResourceObject::Ptr resourceObject)
118 object = resourceObject;
124 std::condition_variable cond;
128 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesDoesNotAllowEmptyFunction)
130 ASSERT_THROW(object->getRemoteAttributes({ }), RCSInvalidParameterException);
133 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesGetsAttributesOfServer)
135 mocks.ExpectCallFunc(getRemoteAttributesCallback).Match(
136 [this](const RCSResourceAttributes& attrs, int)
138 RCSResourceObject::LockGuard lock{ server };
139 return attrs == server->getAttributes();
141 ).Do([this](const RCSResourceAttributes&, int){ Proceed(); });
143 object->getRemoteAttributes(getRemoteAttributesCallback);
148 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesDoesNotAllowEmptyFunction)
150 ASSERT_THROW(object->setRemoteAttributes({ }, { }), RCSInvalidParameterException);
153 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
155 constexpr int newValue = ATTR_VALUE + 1;
156 RCSResourceAttributes newAttrs;
157 newAttrs[ATTR_KEY] = newValue;
159 mocks.ExpectCallFunc(setRemoteAttributesCallback).
160 Do([this](const RCSResourceAttributes&, int){ Proceed(); });
162 object->setRemoteAttributes(newAttrs, setRemoteAttributesCallback);
165 ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
168 TEST_F(RemoteResourceObjectTest, SetRemoteRepresentationDoesNotAllowEmptyFunction)
170 RCSQueryParams queryParams;
171 RCSRepresentation rcsRep;
172 ASSERT_THROW(object->set(queryParams, rcsRep, {}), RCSInvalidParameterException);
175 TEST_F(RemoteResourceObjectTest, SetRemoteRepresentationSetsRepresentationOfServer)
177 RCSRepresentation rcsRep;
178 RCSQueryParams queryParams;
179 constexpr int newValue = ATTR_VALUE + 1;
180 RCSResourceAttributes newAttrs;
181 newAttrs[ATTR_KEY] = newValue;
183 rcsRep.setAttributes(newAttrs);
185 mocks.ExpectCallFunc(setRemoteRepresentationCallback).
186 Do([this](const HeaderOpts&, const RCSRepresentation&, int){ Proceed(); });
188 object->set(queryParams, rcsRep, setRemoteRepresentationCallback);
191 ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
194 TEST_F(RemoteResourceObjectTest, QueryParamsForGetWillBePassedToBase)
199 virtual RCSGetResponse handle(const RCSRequest&, RCSResourceAttributes&) = 0;
200 virtual ~CustomHandler() {}
203 constexpr char PARAM_KEY[] { "aKey" };
204 constexpr char VALUE[] { "value" };
206 object->get(RCSQueryParams().setResourceInterface(RESOURCEINTERFACE).setResourceType(RESOURCETYPE).
207 put(PARAM_KEY, VALUE),
208 [](const HeaderOpts&, const RCSRepresentation&, int){});
210 auto mockHandler = mocks.Mock< CustomHandler >();
212 mocks.ExpectCall(mockHandler, CustomHandler::handle).
213 Match([](const RCSRequest& request, RCSResourceAttributes&)
215 return request.getInterface() == RESOURCEINTERFACE &&
216 request.getQueryParams().at(PARAM_KEY) == VALUE;
219 Do([this](const RCSRequest&, RCSResourceAttributes&)
222 return RCSGetResponse::defaultAction();
226 server->setGetRequestHandler(std::bind(&CustomHandler::handle, mockHandler,
227 std::placeholders::_1, std::placeholders::_2));
232 TEST_F(RemoteResourceObjectTest, MonitoringIsNotStartedByDefault)
234 ASSERT_FALSE(object->isMonitoring());
237 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfFunctionIsEmpty)
239 ASSERT_THROW(object->startMonitoring({ }), RCSInvalidParameterException);
242 TEST_F(RemoteResourceObjectTest, IsMonitoringReturnsTrueAfterStartMonitoring)
244 object->startMonitoring(resourceStateChanged);
246 ASSERT_TRUE(object->isMonitoring());
249 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfTryingToStartAgain)
251 object->startMonitoring(resourceStateChanged);
253 ASSERT_THROW(object->startMonitoring(resourceStateChanged), RCSBadRequestException);
256 TEST_F(RemoteResourceObjectTest, DefaultStateIsNone)
258 ASSERT_EQ(ResourceState::NONE, object->getState());
261 TEST_F(RemoteResourceObjectTest, CachingIsNotStartedByDefault)
263 ASSERT_FALSE(object->isCaching());
266 TEST_F(RemoteResourceObjectTest, IsCachingReturnsTrueAfterStartCaching)
268 object->startCaching(cacheUpdatedCallback);
270 ASSERT_TRUE(object->isCaching());
273 TEST_F(RemoteResourceObjectTest, StartCachingThrowsIfTryingToStartAgain)
275 object->startCaching(cacheUpdatedCallback);
277 ASSERT_THROW(object->startCaching(), RCSBadRequestException);
280 TEST_F(RemoteResourceObjectTest, DefaultCacheStateIsNone)
282 ASSERT_EQ(CacheState::NONE, object->getCacheState());
285 TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
287 object->startCaching();
289 ASSERT_EQ(CacheState::UNREADY, object->getCacheState());
292 TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
294 mocks.ExpectCallFunc(cacheUpdatedCallback).
295 Do([this](const RCSResourceAttributes&){ Proceed(); });
297 object->startCaching(cacheUpdatedCallback);
300 ASSERT_EQ(CacheState::READY, object->getCacheState());
303 TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
305 mocks.ExpectCallFunc(cacheUpdatedCallback).
306 Do([this](const RCSResourceAttributes&){ Proceed(); });
308 object->startCaching(cacheUpdatedCallback);
311 ASSERT_TRUE(object->isCachedAvailable());
314 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
316 mocks.OnCallFunc(cacheUpdatedCallback).
317 Do([this](const RCSResourceAttributes&){ Proceed(); });
318 object->startCaching(cacheUpdatedCallback);
321 mocks.ExpectCallFunc(cacheUpdatedCallback).
322 Do([this](const RCSResourceAttributes&){ Proceed(); });
324 server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
329 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdatedAttributes)
331 constexpr int newValue = ATTR_VALUE + 1;
333 mocks.OnCallFunc(cacheUpdatedCallback).
334 Do([this](const RCSResourceAttributes&){ Proceed(); });
335 object->startCaching(cacheUpdatedCallback);
338 mocks.ExpectCallFunc(cacheUpdatedCallback).
339 Match([this](const RCSResourceAttributes& attrs){
340 return attrs.at(ATTR_KEY) == newValue;
342 Do([this](const RCSResourceAttributes&){ Proceed(); });
344 server->setAttribute(ATTR_KEY, newValue);
349 TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
351 ASSERT_THROW(object->getCachedAttributes(), RCSBadRequestException);
354 TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
356 mocks.OnCallFunc(cacheUpdatedCallback).
357 Do([this](const RCSResourceAttributes&){ Proceed(); });
358 object->startCaching(cacheUpdatedCallback);
361 RCSResourceObject::LockGuard lock{ server };
363 ASSERT_EQ(object->getCachedAttributes(), server->getAttributes());
366 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
368 ASSERT_THROW(object->getCachedAttribute(ATTR_KEY), RCSBadRequestException);
371 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
373 mocks.OnCallFunc(cacheUpdatedCallback).
374 Do([this](const RCSResourceAttributes&){ Proceed(); });
375 object->startCaching(cacheUpdatedCallback);
378 ASSERT_THROW(object->getCachedAttribute(""), RCSInvalidKeyException);
381 TEST_F(RemoteResourceObjectTest, HasSameUriWithServer)
383 EXPECT_EQ(RESOURCEURI, object->getUri());
386 TEST_F(RemoteResourceObjectTest, HasSameTypeWithServer)
388 EXPECT_EQ(RESOURCETYPE, object->getTypes()[0]);
391 TEST_F(RemoteResourceObjectTest, HasSameInterfaceWithServer)
393 EXPECT_EQ(RESOURCEINTERFACE, object->getInterfaces()[0]);