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"
25 #include "ResourceHosting.h"
27 using namespace testing;
28 using namespace OIC::Service;
32 bool isStarted = false;
33 bool isFinished = false;
35 ResourceEncapsulationTestSimulator testObject;
37 void onDiscoveryResource(RCSRemoteResourceObject::Ptr) { }
40 class ResourceHostingTest : public TestWithMock
43 std::mutex mutexForCondition;
44 std::condition_variable responseCon;
45 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
51 TestWithMock::SetUp();
54 testObject.createResource("1");
62 TestWithMock::TearDown();
72 void waitForCondition(int waitingTime = 1000)
74 std::unique_lock< std::mutex > lock{ mutexForCondition };
75 responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
78 void notifyCondition()
80 responseCon.notify_all();
85 TEST(ResourceHostingSTATICMethodTest, getInstanceAllwaysSameReturnInstance)
87 EXPECT_EQ(ResourceHosting::getInstance(), ResourceHosting::getInstance());
90 TEST_F(ResourceHostingTest, startHosting)
94 ResourceHosting::getInstance()->startHosting();
97 FAIL() << "Non-Expected Exception";
101 TEST_F(ResourceHostingTest, HostingFoundBeforeMakeOriginServer)
103 std::string uri = "";
104 testObject.getResourceServer()->setAttribute(
105 "Temperature", RCSResourceAttributes::Value((int)0));
108 mocks.OnCallFunc(onDiscoveryResource).Do(
109 [this, &uri, &testObject](RCSRemoteResourceObject::Ptr ptr)
111 if(ptr->getUri() == testObject.getHostedServerUri())
114 discoveryTask->cancel();
119 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
120 RCSAddress::multicast(), "oic.r.resourcehosting", onDiscoveryResource);
123 std::string mirroredUri = { testObject.getHostedServerUri() };
125 ASSERT_EQ(mirroredUri, uri);
129 TEST_F(ResourceHostingTest, stopHosting)
131 ResourceHosting::getInstance()->stopHosting();