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"
22 #include "RCSRemoteResourceObject.h"
23 #include "RCSDiscoveryManager.h"
24 #include "RCSResourceObject.h"
25 #include "PrimitiveResource.h"
26 #include <condition_variable>
30 using namespace OIC::Service;
33 constexpr char RESOURCEURI[]{ "/a/TemperatureSensor" };
34 constexpr char RESOURCETYPE[]{ "Resource.Hosting" };
35 constexpr char RESOURCEINTERFACE[]{ "oic.if.baseline" };
37 constexpr char ATTR_KEY[]{ "Temperature" };
38 constexpr int ATTR_VALUE{ 0 };
40 constexpr int DEFAULT_WAITING_TIME_IN_MILLIS = 3000;
42 void getRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
43 void setRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
44 void resourceStateChanged(ResourceState) { }
45 void cacheUpdatedCallback(const RCSResourceAttributes&) {}
47 class RemoteResourceObjectTest: public TestWithMock
50 RCSResourceObject::Ptr server;
51 RCSRemoteResourceObject::Ptr object;
52 std::shared_ptr< bool > finished;
60 void Wait(int waitingTime = DEFAULT_WAITING_TIME_IN_MILLIS)
62 std::unique_lock< std::mutex > lock{ mutex };
63 cond.wait_for(lock, std::chrono::milliseconds{ waitingTime });
69 TestWithMock::SetUp();
71 finished = std::make_shared< bool >(false);
75 WaitUntilDiscovered();
80 TestWithMock::TearDown();
82 // This method is to make sure objects disposed.
83 WaitForPtrBeingUnique();
91 server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE).build();
92 server->setAttribute(ATTR_KEY, ATTR_VALUE);
97 std::lock_guard<std::mutex> lock{ mutexForObject };
98 return object == nullptr;
101 void WaitUntilDiscovered()
103 while (checkObject())
105 const std::string uri = "/oic/res";
106 const std::string type = "Resource.Hosting";
107 auto discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
108 RCSAddress::multicast(), uri, type, std::bind(resourceDiscovered, this, finished,
109 std::placeholders::_1));
114 void WaitForPtrBeingUnique()
116 while((object && !object.unique()) || (server && !server.unique()))
118 std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
122 // This callback is to protect crash from crashes caused by delayed callbacks
123 static void resourceDiscovered(RemoteResourceObjectTest* test,
124 std::shared_ptr< bool > finished, RCSRemoteResourceObject::Ptr resourceObject)
126 if (*finished) return;
129 std::lock_guard< std::mutex > lock{ test->mutexForObject };
131 if (test->object) return;
133 test->object = resourceObject;
140 std::condition_variable cond;
142 std::mutex mutexForObject;
145 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesDoesNotAllowEmptyFunction)
147 ASSERT_THROW(object->getRemoteAttributes({ }), InvalidParameterException);
150 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesGetsAttributesOfServer)
152 mocks.ExpectCallFunc(getRemoteAttributesCallback).Match(
153 [this](const RCSResourceAttributes& attrs, int)
155 RCSResourceObject::LockGuard lock{ server };
156 return attrs == server->getAttributes();
158 ).Do([this](const RCSResourceAttributes&, int){ Proceed(); });
160 object->getRemoteAttributes(getRemoteAttributesCallback);
165 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesDoesNotAllowEmptyFunction)
167 ASSERT_THROW(object->setRemoteAttributes({ }, { }), InvalidParameterException);
170 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
172 constexpr int newValue = ATTR_VALUE + 1;
173 RCSResourceAttributes newAttrs;
174 newAttrs[ATTR_KEY] = newValue;
176 mocks.ExpectCallFunc(setRemoteAttributesCallback).
177 Do([this](const RCSResourceAttributes&, int){ Proceed(); });
179 object->setRemoteAttributes(newAttrs, setRemoteAttributesCallback);
182 ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
185 TEST_F(RemoteResourceObjectTest, MonitoringIsNotStartedByDefault)
187 ASSERT_FALSE(object->isMonitoring());
190 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfFunctionIsEmpty)
192 ASSERT_THROW(object->startMonitoring({ }), InvalidParameterException);
195 TEST_F(RemoteResourceObjectTest, IsMonitoringReturnsTrueAfterStartMonitoring)
197 object->startMonitoring(resourceStateChanged);
199 ASSERT_TRUE(object->isMonitoring());
202 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfTryingToStartAgain)
204 object->startMonitoring(resourceStateChanged);
206 ASSERT_THROW(object->startMonitoring(resourceStateChanged), BadRequestException);
209 TEST_F(RemoteResourceObjectTest, DefaultStateIsNone)
211 ASSERT_EQ(ResourceState::NONE, object->getState());
214 TEST_F(RemoteResourceObjectTest, CachingIsNotStartedByDefault)
216 ASSERT_FALSE(object->isCaching());
219 TEST_F(RemoteResourceObjectTest, IsCachingReturnsTrueAfterStartCaching)
221 object->startCaching(cacheUpdatedCallback);
223 ASSERT_TRUE(object->isCaching());
226 TEST_F(RemoteResourceObjectTest, StartCachingThrowsIfTryingToStartAgain)
228 object->startCaching(cacheUpdatedCallback);
230 ASSERT_THROW(object->startCaching(), BadRequestException);
233 TEST_F(RemoteResourceObjectTest, DefaultCacheStateIsNone)
235 ASSERT_EQ(CacheState::NONE, object->getCacheState());
238 TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
240 object->startCaching();
242 ASSERT_EQ(CacheState::UNREADY, object->getCacheState());
245 TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
247 mocks.ExpectCallFunc(cacheUpdatedCallback).
248 Do([this](const RCSResourceAttributes&){ Proceed(); });
250 object->startCaching(cacheUpdatedCallback);
253 ASSERT_EQ(CacheState::READY, object->getCacheState());
256 TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
258 mocks.ExpectCallFunc(cacheUpdatedCallback).
259 Do([this](const RCSResourceAttributes&){ Proceed(); });
261 object->startCaching(cacheUpdatedCallback);
264 ASSERT_TRUE(object->isCachedAvailable());
267 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
269 mocks.OnCallFunc(cacheUpdatedCallback).
270 Do([this](const RCSResourceAttributes&){ Proceed(); });
271 object->startCaching(cacheUpdatedCallback);
274 mocks.ExpectCallFunc(cacheUpdatedCallback).
275 Do([this](const RCSResourceAttributes&){ Proceed(); });
277 server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
282 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdatedAttributes)
284 constexpr int newValue = ATTR_VALUE + 1;
286 mocks.OnCallFunc(cacheUpdatedCallback).
287 Do([this](const RCSResourceAttributes&){ Proceed(); });
288 object->startCaching(cacheUpdatedCallback);
291 mocks.ExpectCallFunc(cacheUpdatedCallback).
292 Match([this](const RCSResourceAttributes& attrs){
293 return attrs.at(ATTR_KEY) == newValue;
295 Do([this](const RCSResourceAttributes&){ Proceed(); });
297 server->setAttribute(ATTR_KEY, newValue);
302 TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
304 ASSERT_THROW(object->getCachedAttributes(), BadRequestException);
307 TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
309 mocks.OnCallFunc(cacheUpdatedCallback).
310 Do([this](const RCSResourceAttributes&){ Proceed(); });
311 object->startCaching(cacheUpdatedCallback);
314 RCSResourceObject::LockGuard lock{ server };
316 ASSERT_EQ(object->getCachedAttributes(), server->getAttributes());
319 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
321 ASSERT_THROW(object->getCachedAttribute(ATTR_KEY), BadRequestException);
324 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
326 mocks.OnCallFunc(cacheUpdatedCallback).
327 Do([this](const RCSResourceAttributes&){ Proceed(); });
328 object->startCaching(cacheUpdatedCallback);
331 ASSERT_THROW(object->getCachedAttribute(""), InvalidKeyException);
334 TEST_F(RemoteResourceObjectTest, HasSameUriWithServer)
336 EXPECT_EQ(RESOURCEURI, object->getUri());
339 TEST_F(RemoteResourceObjectTest, HasSameTypeWithServer)
341 EXPECT_EQ(RESOURCETYPE, object->getTypes()[0]);
344 TEST_F(RemoteResourceObjectTest, HasSameInterfaceWithServer)
346 EXPECT_EQ(RESOURCEINTERFACE, object->getInterfaces()[0]);