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"
28 #include <condition_variable>
31 using namespace OIC::Service;
34 constexpr char RESOURCEURI[]{ "/a/TemperatureSensor" };
35 constexpr char RESOURCETYPE[]{ "resource.type" };
36 constexpr char RESOURCEINTERFACE[]{ "oic.if.baseline" };
38 constexpr char ATTR_KEY[]{ "Temperature" };
39 constexpr int ATTR_VALUE{ 0 };
41 constexpr int DEFAULT_WAITING_TIME_IN_MILLIS = 3000;
43 void getRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
44 void setRemoteAttributesCallback(const RCSResourceAttributes&, int) {}
45 void resourceStateChanged(ResourceState) { }
46 void cacheUpdatedCallback(const RCSResourceAttributes&) {}
48 class RemoteResourceObjectTest: public TestWithMock
51 RCSResourceObject::Ptr server;
52 RCSRemoteResourceObject::Ptr object;
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();
73 WaitUntilDiscovered();
75 ASSERT_NE(object, nullptr);
80 TestWithMock::TearDown();
82 // This method is to make sure objects disposed.
83 WaitForPtrBeingUnique();
89 server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE).build();
90 server->setAttribute(ATTR_KEY, ATTR_VALUE);
93 void WaitUntilDiscovered()
95 for (int i=0; i<10 && !object; ++i)
97 const std::string uri = "/oic/res";
98 auto discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
99 RCSAddress::multicast(), uri, RESOURCETYPE,
100 std::bind(&RemoteResourceObjectTest::resourceDiscovered, this,
101 std::placeholders::_1));
106 void WaitForPtrBeingUnique()
108 while((object && !object.unique()) || (server && !server.unique()))
110 std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
114 void resourceDiscovered(RCSRemoteResourceObject::Ptr resourceObject)
116 object = resourceObject;
122 std::condition_variable cond;
126 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesDoesNotAllowEmptyFunction)
128 ASSERT_THROW(object->getRemoteAttributes({ }), RCSInvalidParameterException);
131 TEST_F(RemoteResourceObjectTest, GetRemoteAttributesGetsAttributesOfServer)
133 mocks.ExpectCallFunc(getRemoteAttributesCallback).Match(
134 [this](const RCSResourceAttributes& attrs, int)
136 RCSResourceObject::LockGuard lock{ server };
137 return attrs == server->getAttributes();
139 ).Do([this](const RCSResourceAttributes&, int){ Proceed(); });
141 object->getRemoteAttributes(getRemoteAttributesCallback);
146 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesDoesNotAllowEmptyFunction)
148 ASSERT_THROW(object->setRemoteAttributes({ }, { }), RCSInvalidParameterException);
151 TEST_F(RemoteResourceObjectTest, SetRemoteAttributesSetsAttributesOfServer)
153 constexpr int newValue = ATTR_VALUE + 1;
154 RCSResourceAttributes newAttrs;
155 newAttrs[ATTR_KEY] = newValue;
157 mocks.ExpectCallFunc(setRemoteAttributesCallback).
158 Do([this](const RCSResourceAttributes&, int){ Proceed(); });
160 object->setRemoteAttributes(newAttrs, setRemoteAttributesCallback);
163 ASSERT_EQ(newValue, server->getAttributeValue(ATTR_KEY));
166 TEST_F(RemoteResourceObjectTest, MonitoringIsNotStartedByDefault)
168 ASSERT_FALSE(object->isMonitoring());
171 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfFunctionIsEmpty)
173 ASSERT_THROW(object->startMonitoring({ }), RCSInvalidParameterException);
176 TEST_F(RemoteResourceObjectTest, IsMonitoringReturnsTrueAfterStartMonitoring)
178 object->startMonitoring(resourceStateChanged);
180 ASSERT_TRUE(object->isMonitoring());
183 TEST_F(RemoteResourceObjectTest, StartMonitoringThrowsIfTryingToStartAgain)
185 object->startMonitoring(resourceStateChanged);
187 ASSERT_THROW(object->startMonitoring(resourceStateChanged), RCSBadRequestException);
190 TEST_F(RemoteResourceObjectTest, DefaultStateIsNone)
192 ASSERT_EQ(ResourceState::NONE, object->getState());
195 TEST_F(RemoteResourceObjectTest, CachingIsNotStartedByDefault)
197 ASSERT_FALSE(object->isCaching());
200 TEST_F(RemoteResourceObjectTest, IsCachingReturnsTrueAfterStartCaching)
202 object->startCaching(cacheUpdatedCallback);
204 ASSERT_TRUE(object->isCaching());
207 TEST_F(RemoteResourceObjectTest, StartCachingThrowsIfTryingToStartAgain)
209 object->startCaching(cacheUpdatedCallback);
211 ASSERT_THROW(object->startCaching(), RCSBadRequestException);
214 TEST_F(RemoteResourceObjectTest, DefaultCacheStateIsNone)
216 ASSERT_EQ(CacheState::NONE, object->getCacheState());
219 TEST_F(RemoteResourceObjectTest, CacheStateIsUnreadyAfterStartCaching)
221 object->startCaching();
223 ASSERT_EQ(CacheState::UNREADY, object->getCacheState());
226 TEST_F(RemoteResourceObjectTest, CacheStateIsReadyAfterCacheUpdated)
228 mocks.ExpectCallFunc(cacheUpdatedCallback).
229 Do([this](const RCSResourceAttributes&){ Proceed(); });
231 object->startCaching(cacheUpdatedCallback);
234 ASSERT_EQ(CacheState::READY, object->getCacheState());
237 TEST_F(RemoteResourceObjectTest, IsCachedAvailableReturnsTrueWhenCacheIsReady)
239 mocks.ExpectCallFunc(cacheUpdatedCallback).
240 Do([this](const RCSResourceAttributes&){ Proceed(); });
242 object->startCaching(cacheUpdatedCallback);
245 ASSERT_TRUE(object->isCachedAvailable());
248 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWheneverCacheUpdated)
250 mocks.OnCallFunc(cacheUpdatedCallback).
251 Do([this](const RCSResourceAttributes&){ Proceed(); });
252 object->startCaching(cacheUpdatedCallback);
255 mocks.ExpectCallFunc(cacheUpdatedCallback).
256 Do([this](const RCSResourceAttributes&){ Proceed(); });
258 server->setAttribute(ATTR_KEY, ATTR_VALUE + 1);
263 TEST_F(RemoteResourceObjectTest, DISABLED_CacheUpdatedCallbackBeCalledWithUpdatedAttributes)
265 constexpr int newValue = ATTR_VALUE + 1;
267 mocks.OnCallFunc(cacheUpdatedCallback).
268 Do([this](const RCSResourceAttributes&){ Proceed(); });
269 object->startCaching(cacheUpdatedCallback);
272 mocks.ExpectCallFunc(cacheUpdatedCallback).
273 Match([this](const RCSResourceAttributes& attrs){
274 return attrs.at(ATTR_KEY) == newValue;
276 Do([this](const RCSResourceAttributes&){ Proceed(); });
278 server->setAttribute(ATTR_KEY, newValue);
283 TEST_F(RemoteResourceObjectTest, GetCachedAttributesThrowsIfCachingIsNotStarted)
285 ASSERT_THROW(object->getCachedAttributes(), RCSBadRequestException);
288 TEST_F(RemoteResourceObjectTest, CachedAttributesHasSameAttributesWithServer)
290 mocks.OnCallFunc(cacheUpdatedCallback).
291 Do([this](const RCSResourceAttributes&){ Proceed(); });
292 object->startCaching(cacheUpdatedCallback);
295 RCSResourceObject::LockGuard lock{ server };
297 ASSERT_EQ(object->getCachedAttributes(), server->getAttributes());
300 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfCachingIsNotStarted)
302 ASSERT_THROW(object->getCachedAttribute(ATTR_KEY), RCSBadRequestException);
305 TEST_F(RemoteResourceObjectTest, GetCachedAttributeThrowsIfKeyIsInvalid)
307 mocks.OnCallFunc(cacheUpdatedCallback).
308 Do([this](const RCSResourceAttributes&){ Proceed(); });
309 object->startCaching(cacheUpdatedCallback);
312 ASSERT_THROW(object->getCachedAttribute(""), RCSInvalidKeyException);
315 TEST_F(RemoteResourceObjectTest, HasSameUriWithServer)
317 EXPECT_EQ(RESOURCEURI, object->getUri());
320 TEST_F(RemoteResourceObjectTest, HasSameTypeWithServer)
322 EXPECT_EQ(RESOURCETYPE, object->getTypes()[0]);
325 TEST_F(RemoteResourceObjectTest, HasSameInterfaceWithServer)
327 EXPECT_EQ(RESOURCEINTERFACE, object->getInterfaces()[0]);