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 resourceStateChanged(ResourceState) { }
47 void cacheUpdatedCallback(const RCSResourceAttributes&) {}
49 class RemoteResourceObjectTest: public TestWithMock
52 RCSResourceObject::Ptr server;
53 RCSRemoteResourceObject::Ptr object;
61 void Wait(int waitingTime = DEFAULT_WAITING_TIME_IN_MILLIS)
63 std::unique_lock< std::mutex > lock{ mutex };
64 cond.wait_for(lock, std::chrono::milliseconds{ waitingTime });
70 TestWithMock::SetUp();
74 WaitUntilDiscovered();
76 ASSERT_NE(object, nullptr);
81 TestWithMock::TearDown();
83 // This method is to make sure objects disposed.
84 WaitForPtrBeingUnique();
90 server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE).build();
91 server->setAttribute(ATTR_KEY, ATTR_VALUE);
94 void WaitUntilDiscovered()
96 for (int i=0; i<10 && !object; ++i)
98 const std::string uri = "/oic/res";
99 auto discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
100 RCSAddress::multicast(), uri, 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, QueryParamsForGetWillBePassedToBase)
173 virtual RCSGetResponse handle(const RCSRequest&, RCSResourceAttributes&) = 0;
174 virtual ~CustomHandler() {}
177 constexpr char PARAM_KEY[] { "aKey" };
178 constexpr char VALUE[] { "value" };
180 object->get(RCSQueryParams().setResourceInterface(RESOURCEINTERFACE).setResourceType(RESOURCETYPE).
181 put(PARAM_KEY, VALUE),
182 [](const HeaderOpts&, const RCSRepresentation&, int){});
184 auto mockHandler = mocks.Mock< CustomHandler >();
186 mocks.ExpectCall(mockHandler, CustomHandler::handle).
187 Match([](const RCSRequest& request, RCSResourceAttributes&)
189 return request.getInterface() == RESOURCEINTERFACE &&
190 request.getQueryParams().at(PARAM_KEY) == VALUE;
193 Do([this](const RCSRequest&, RCSResourceAttributes&)
196 return RCSGetResponse::defaultAction();
200 server->setGetRequestHandler(std::bind(&CustomHandler::handle, mockHandler,
201 std::placeholders::_1, std::placeholders::_2));
206 TEST_F(RemoteResourceObjectTest, MonitoringIsNotStartedByDefault)
208 ASSERT_FALSE(object->isMonitoring());
211 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfFunctionIsEmpty)
213 ASSERT_THROW(object->startMonitoring({ }), RCSInvalidParameterException);
216 TEST_F(RemoteResourceObjectTest, IsMonitoringReturnsTrueAfterStartMonitoring)
218 object->startMonitoring(resourceStateChanged);
220 ASSERT_TRUE(object->isMonitoring());
223 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfTryingToStartAgain)
225 object->startMonitoring(resourceStateChanged);
227 ASSERT_THROW(object->startMonitoring(resourceStateChanged), RCSBadRequestException);
230 TEST_F(RemoteResourceObjectTest, DefaultStateIsNone)
232 ASSERT_EQ(ResourceState::NONE, object->getState());
235 TEST_F(RemoteResourceObjectTest, CachingIsNotStartedByDefault)
237 ASSERT_FALSE(object->isCaching());
240 TEST_F(RemoteResourceObjectTest, IsCachingReturnsTrueAfterStartCaching)
242 object->startCaching(cacheUpdatedCallback);
244 ASSERT_TRUE(object->isCaching());
247 TEST_F(RemoteResourceObjectTest, StartCachingThrowsIfTryingToStartAgain)
249 object->startCaching(cacheUpdatedCallback);
251 ASSERT_THROW(object->startCaching(), RCSBadRequestException);
254 TEST_F(RemoteResourceObjectTest, DefaultCacheStateIsNone)
256 ASSERT_EQ(CacheState::NONE, object->getCacheState());
259 TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
261 object->startCaching();
263 ASSERT_EQ(CacheState::UNREADY, object->getCacheState());
266 TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
268 mocks.ExpectCallFunc(cacheUpdatedCallback).
269 Do([this](const RCSResourceAttributes&){ Proceed(); });
271 object->startCaching(cacheUpdatedCallback);
274 ASSERT_EQ(CacheState::READY, object->getCacheState());
277 TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
279 mocks.ExpectCallFunc(cacheUpdatedCallback).
280 Do([this](const RCSResourceAttributes&){ Proceed(); });
282 object->startCaching(cacheUpdatedCallback);
285 ASSERT_TRUE(object->isCachedAvailable());
288 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
290 mocks.OnCallFunc(cacheUpdatedCallback).
291 Do([this](const RCSResourceAttributes&){ Proceed(); });
292 object->startCaching(cacheUpdatedCallback);
295 mocks.ExpectCallFunc(cacheUpdatedCallback).
296 Do([this](const RCSResourceAttributes&){ Proceed(); });
298 server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
303 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdatedAttributes)
305 constexpr int newValue = ATTR_VALUE + 1;
307 mocks.OnCallFunc(cacheUpdatedCallback).
308 Do([this](const RCSResourceAttributes&){ Proceed(); });
309 object->startCaching(cacheUpdatedCallback);
312 mocks.ExpectCallFunc(cacheUpdatedCallback).
313 Match([this](const RCSResourceAttributes& attrs){
314 return attrs.at(ATTR_KEY) == newValue;
316 Do([this](const RCSResourceAttributes&){ Proceed(); });
318 server->setAttribute(ATTR_KEY, newValue);
323 TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
325 ASSERT_THROW(object->getCachedAttributes(), RCSBadRequestException);
328 TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
330 mocks.OnCallFunc(cacheUpdatedCallback).
331 Do([this](const RCSResourceAttributes&){ Proceed(); });
332 object->startCaching(cacheUpdatedCallback);
335 RCSResourceObject::LockGuard lock{ server };
337 ASSERT_EQ(object->getCachedAttributes(), server->getAttributes());
340 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
342 ASSERT_THROW(object->getCachedAttribute(ATTR_KEY), RCSBadRequestException);
345 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
347 mocks.OnCallFunc(cacheUpdatedCallback).
348 Do([this](const RCSResourceAttributes&){ Proceed(); });
349 object->startCaching(cacheUpdatedCallback);
352 ASSERT_THROW(object->getCachedAttribute(""), RCSInvalidKeyException);
355 TEST_F(RemoteResourceObjectTest, HasSameUriWithServer)
357 EXPECT_EQ(RESOURCEURI, object->getUri());
360 TEST_F(RemoteResourceObjectTest, HasSameTypeWithServer)
362 EXPECT_EQ(RESOURCETYPE, object->getTypes()[0]);
365 TEST_F(RemoteResourceObjectTest, HasSameInterfaceWithServer)
367 EXPECT_EQ(RESOURCEINTERFACE, object->getInterfaces()[0]);