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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "ResourceEncapsulationTestSimulator.h"
24 #include "HostingObject.h"
26 #include "RCSDiscoveryManager.h"
28 using namespace testing;
29 using namespace OIC::Service;
33 const std::string HOSTING_RESOURCE_TYPE = "oic.r.resourcehosting";
34 const std::string TEST_ATT_KEY = "Temperature";
36 bool isStarted = false;
37 bool isFinished = false;
39 ResourceEncapsulationTestSimulator testObject;
40 RCSRemoteResourceObject::Ptr remoteObject;
42 HostingObject::Ptr instance;
43 RCSRemoteResourceObject::Ptr discoveredResource;
45 std::condition_variable responseCon;
48 void onDiscoveryResource(RCSRemoteResourceObject::Ptr){ }
49 void onUpdatedCache(const RCSResourceAttributes &) { }
50 void onSetAttributes(const RCSResourceAttributes &, int) { }
56 testObject.defaultRunSimulator();
57 remoteObject = testObject.getRemoteResource();
59 instance = HostingObject::createHostingObject(
60 remoteObject, &onDestroy);
62 testObject.getResourceServer()->setAttribute(
63 "Temperature", RCSResourceAttributes::Value((int)0));
80 class HostingObjectTest : public TestWithMock
83 std::mutex mutexForCondition;
89 TestWithMock::SetUp();
95 TestWithMock::TearDown();
100 void waitForCondition(int waitingTime = 1000)
102 std::unique_lock< std::mutex > lock{ mutexForCondition };
103 responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
106 void notifyCondition()
108 responseCon.notify_all();
113 TEST_F(HostingObjectTest, startCachingAtInitialize)
115 EXPECT_TRUE(remoteObject->isCaching());
118 TEST_F(HostingObjectTest, startMonitoringAtInitialize)
120 ASSERT_TRUE(remoteObject->isMonitoring());
123 TEST_F(HostingObjectTest, getRemoteResourceisValid)
125 ASSERT_EQ(remoteObject->getUri(), instance->getRemoteResource()->getUri());
128 TEST_F(HostingObjectTest, createMirroredServer)
130 int waitForResponse = 1000;
131 std::string uri = "";
133 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask = { };
135 waitForCondition(waitForResponse);
136 mocks.OnCallFunc(onDiscoveryResource).Do(
137 [this, &uri, &discoveryTask, &testObject, &discoveredResource]
138 (RCSRemoteResourceObject::Ptr ptr)
140 if(ptr->getUri() == testObject.getHostedServerUri())
143 discoveredResource = ptr;
144 discoveryTask->cancel();
149 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
150 RCSAddress::multicast(), "/oic/res", HOSTING_RESOURCE_TYPE, onDiscoveryResource);
151 waitForCondition(waitForResponse);
153 EXPECT_EQ(testObject.getHostedServerUri(), uri);
156 TEST_F(HostingObjectTest, UpdateCachedDataWhenChangedOriginResource)
158 int waitForResponse = 1000;
159 RCSResourceAttributes::Value result = { };
161 mocks.OnCallFunc(onUpdatedCache).Do(
162 [this, &result](const RCSResourceAttributes & att)
164 result = att.at(TEST_ATT_KEY);
168 discoveredResource->startCaching(onUpdatedCache);
169 std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
171 RCSResourceAttributes::Value settingValue = 10;
172 testObject.getResourceServer()->setAttribute(TEST_ATT_KEY, settingValue);
173 waitForCondition(waitForResponse);
177 EXPECT_EQ(result.toString(), settingValue.toString());
181 TEST_F(HostingObjectTest, SetDataToMirroredResource)
183 int waitForResponse = 1000;
184 RCSResourceAttributes::Value result = { };
186 mocks.ExpectCallFunc(onSetAttributes).Do(
187 [this, & result](const RCSResourceAttributes &att, int)
189 result = att.at(TEST_ATT_KEY);
192 RCSResourceAttributes setAttrs;
193 RCSResourceAttributes::Value settingValue = 20;
194 setAttrs[TEST_ATT_KEY] = settingValue;
195 discoveredResource->setRemoteAttributes(setAttrs, onSetAttributes);
196 waitForCondition(waitForResponse);
198 EXPECT_EQ(result.toString(), settingValue.toString());
201 TEST_F(HostingObjectTest, ExpectCallOnDestroyWhenStopHostingObject)
203 int waitForResponse = 1000;
205 mocks.ExpectCallFunc(onDestroy).Do(
208 responseCon.notify_all();
211 testObject.destroy();
213 waitForCondition(waitForResponse);