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));
107 void WaitForPtrBeingUnique()
109 while((object && !object.unique()) || (server && !server.unique()))
111 std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
115 void resourceDiscovered(RCSRemoteResourceObject::Ptr resourceObject)
117 object = resourceObject;
123 std::condition_variable cond;
127 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesDoesNotAllowEmptyFunction)
129 ASSERT_THROW(object->getRemoteAttributes({ }), RCSInvalidParameterException);
132 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesGetsAttributesOfServer)
134 mocks.ExpectCallFunc(getRemoteAttributesCallback).Match(
135 [this](const RCSResourceAttributes& attrs, int)
137 RCSResourceObject::LockGuard lock{ server };
138 return attrs == server->getAttributes();
140 ).Do([this](const RCSResourceAttributes&, int){ Proceed(); });
142 object->getRemoteAttributes(getRemoteAttributesCallback);
147 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesDoesNotAllowEmptyFunction)
149 ASSERT_THROW(object->setRemoteAttributes({ }, { }), RCSInvalidParameterException);
152 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
154 constexpr int newValue = ATTR_VALUE + 1;
155 RCSResourceAttributes newAttrs;
156 newAttrs[ATTR_KEY] = newValue;
158 mocks.ExpectCallFunc(setRemoteAttributesCallback).
159 Do([this](const RCSResourceAttributes&, int){ Proceed(); });
161 object->setRemoteAttributes(newAttrs, setRemoteAttributesCallback);
164 ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
167 TEST_F(RemoteResourceObjectTest, QueryParamsForGetWillBePassedToBase)
172 virtual RCSGetResponse handle(const RCSRequest&, RCSResourceAttributes&) = 0;
173 virtual ~CustomHandler() {}
176 constexpr char PARAM_KEY[] { "aKey" };
177 constexpr char VALUE[] { "value" };
179 object->get(RCSQueryParams().setResourceInterface(RESOURCEINTERFACE).setResuorceType(RESOURCETYPE).
180 put(PARAM_KEY, VALUE),
181 [](const HeaderOpts&, const RCSRepresentation&, int){});
183 auto mockHandler = mocks.Mock< CustomHandler >();
185 mocks.ExpectCall(mockHandler, CustomHandler::handle).
186 Match([](const RCSRequest& request, RCSResourceAttributes&)
188 return request.getInterface() == RESOURCEINTERFACE &&
189 request.getQueryParams().at(PARAM_KEY) == VALUE;
192 Do([this](const RCSRequest&, RCSResourceAttributes&)
195 return RCSGetResponse::defaultAction();
199 server->setGetRequestHandler(std::bind(&CustomHandler::handle, mockHandler,
200 std::placeholders::_1, std::placeholders::_2));
205 TEST_F(RemoteResourceObjectTest, MonitoringIsNotStartedByDefault)
207 ASSERT_FALSE(object->isMonitoring());
210 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfFunctionIsEmpty)
212 ASSERT_THROW(object->startMonitoring({ }), RCSInvalidParameterException);
215 TEST_F(RemoteResourceObjectTest, IsMonitoringReturnsTrueAfterStartMonitoring)
217 object->startMonitoring(resourceStateChanged);
219 ASSERT_TRUE(object->isMonitoring());
222 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfTryingToStartAgain)
224 object->startMonitoring(resourceStateChanged);
226 ASSERT_THROW(object->startMonitoring(resourceStateChanged), RCSBadRequestException);
229 TEST_F(RemoteResourceObjectTest, DefaultStateIsNone)
231 ASSERT_EQ(ResourceState::NONE, object->getState());
234 TEST_F(RemoteResourceObjectTest, CachingIsNotStartedByDefault)
236 ASSERT_FALSE(object->isCaching());
239 TEST_F(RemoteResourceObjectTest, IsCachingReturnsTrueAfterStartCaching)
241 object->startCaching(cacheUpdatedCallback);
243 ASSERT_TRUE(object->isCaching());
246 TEST_F(RemoteResourceObjectTest, StartCachingThrowsIfTryingToStartAgain)
248 object->startCaching(cacheUpdatedCallback);
250 ASSERT_THROW(object->startCaching(), RCSBadRequestException);
253 TEST_F(RemoteResourceObjectTest, DefaultCacheStateIsNone)
255 ASSERT_EQ(CacheState::NONE, object->getCacheState());
258 TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
260 object->startCaching();
262 ASSERT_EQ(CacheState::UNREADY, object->getCacheState());
265 TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
267 mocks.ExpectCallFunc(cacheUpdatedCallback).
268 Do([this](const RCSResourceAttributes&){ Proceed(); });
270 object->startCaching(cacheUpdatedCallback);
273 ASSERT_EQ(CacheState::READY, object->getCacheState());
276 TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
278 mocks.ExpectCallFunc(cacheUpdatedCallback).
279 Do([this](const RCSResourceAttributes&){ Proceed(); });
281 object->startCaching(cacheUpdatedCallback);
284 ASSERT_TRUE(object->isCachedAvailable());
287 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
289 mocks.OnCallFunc(cacheUpdatedCallback).
290 Do([this](const RCSResourceAttributes&){ Proceed(); });
291 object->startCaching(cacheUpdatedCallback);
294 mocks.ExpectCallFunc(cacheUpdatedCallback).
295 Do([this](const RCSResourceAttributes&){ Proceed(); });
297 server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
302 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdatedAttributes)
304 constexpr int newValue = ATTR_VALUE + 1;
306 mocks.OnCallFunc(cacheUpdatedCallback).
307 Do([this](const RCSResourceAttributes&){ Proceed(); });
308 object->startCaching(cacheUpdatedCallback);
311 mocks.ExpectCallFunc(cacheUpdatedCallback).
312 Match([this](const RCSResourceAttributes& attrs){
313 return attrs.at(ATTR_KEY) == newValue;
315 Do([this](const RCSResourceAttributes&){ Proceed(); });
317 server->setAttribute(ATTR_KEY, newValue);
322 TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
324 ASSERT_THROW(object->getCachedAttributes(), RCSBadRequestException);
327 TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
329 mocks.OnCallFunc(cacheUpdatedCallback).
330 Do([this](const RCSResourceAttributes&){ Proceed(); });
331 object->startCaching(cacheUpdatedCallback);
334 RCSResourceObject::LockGuard lock{ server };
336 ASSERT_EQ(object->getCachedAttributes(), server->getAttributes());
339 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
341 ASSERT_THROW(object->getCachedAttribute(ATTR_KEY), RCSBadRequestException);
344 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
346 mocks.OnCallFunc(cacheUpdatedCallback).
347 Do([this](const RCSResourceAttributes&){ Proceed(); });
348 object->startCaching(cacheUpdatedCallback);
351 ASSERT_THROW(object->getCachedAttribute(""), RCSInvalidKeyException);
354 TEST_F(RemoteResourceObjectTest, HasSameUriWithServer)
356 EXPECT_EQ(RESOURCEURI, object->getUri());
359 TEST_F(RemoteResourceObjectTest, HasSameTypeWithServer)
361 EXPECT_EQ(RESOURCETYPE, object->getTypes()[0]);
364 TEST_F(RemoteResourceObjectTest, HasSameInterfaceWithServer)
366 EXPECT_EQ(RESOURCEINTERFACE, object->getInterfaces()[0]);