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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "UnitTestHelper.h"
27 #include "RCSDiscoveryManager.h"
28 #include "RCSRemoteResourceObject.h"
29 #include "RCSResourceAttributes.h"
30 #include "RCSAddress.h"
32 using namespace testing;
33 using namespace OIC::Service;
35 class ResourceContainerTestSimulator
36 : public std::enable_shared_from_this<ResourceContainerTestSimulator>
39 typedef std::shared_ptr<ResourceContainerTestSimulator> Ptr;
41 RCSResourceObject::Ptr server;
42 RCSRemoteResourceObject::Ptr remoteResource;
44 static constexpr int DEFAULT_WAITTIME = 3000;
47 std::mutex mutexForDiscovery;
49 std::string MULTICASTURI;
50 std::string RESOURCEURI;
51 std::string RESOURCETYPE;
52 std::string RESOURCEINTERFACE;
55 std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
58 ResourceContainerTestSimulator()
59 : server(nullptr), remoteResource(nullptr),
61 MULTICASTURI("/oic/res"),
62 RESOURCEURI("/a/TempHumSensor/Container"),
63 RESOURCETYPE("resource.container"),
64 RESOURCEINTERFACE("oic.if.baseline"),
65 ATTR_KEY("TestResourceContainer"),
69 ~ResourceContainerTestSimulator()
73 if (remoteResource != nullptr && remoteResource->isCaching())
75 remoteResource->stopCaching();
77 if (remoteResource != nullptr && remoteResource->isMonitoring())
79 remoteResource->stopMonitoring();
82 catch (RCSException &e)
84 std::cout << "exception : " << e.what() << std::endl;
89 void onDiscoveryResource_Impl(RCSRemoteResourceObject::Ptr resourceObject)
91 if (remoteResource != nullptr)
96 if (RESOURCEURI.compare(resourceObject->getUri()) != 0)
101 remoteResource = resourceObject;
102 mutexForDiscovery.unlock();
105 static void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject,
106 std::weak_ptr<ResourceContainerTestSimulator> rPtr)
108 std::shared_ptr<ResourceContainerTestSimulator> ptr = rPtr.lock();
111 ptr->onDiscoveryResource_Impl(resourceObject);
115 std::cout << "Aleady delete simulator\n";
118 void waitForDiscovery()
120 std::chrono::milliseconds interval(DEFAULT_WAITTIME);
123 if (mutexForDiscovery.try_lock())
125 mutexForDiscovery.unlock();
128 std::this_thread::sleep_for(interval);
133 void defaultRunSimulator()
140 void createResource()
142 server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE)
143 .setDiscoverable(true).setObservable(true).build();
144 server->setAttribute(ATTR_KEY, ATTR_VALUE);
147 void discoveryResource()
149 discoveryResource(RESOURCETYPE);
152 void discoveryResource(std::string &resourceType)
156 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
157 RCSAddress::multicast(), MULTICASTURI, resourceType,
158 std::bind(onDiscoveryResource, std::placeholders::_1,
159 std::weak_ptr<ResourceContainerTestSimulator>(shared_from_this())));
160 mutexForDiscovery.lock();
162 catch (RCSInvalidParameterException &e)
164 std::cout << "exception : " << e.what() << std::endl;
168 std::string getServerUri() const
173 RCSResourceObject::Ptr getResourceServer() const
178 RCSRemoteResourceObject::Ptr getRemoteResource() const
180 return remoteResource;
183 void ChangeAttributeValue()
185 std::chrono::milliseconds interval(DEFAULT_WAITTIME);
186 if (server != nullptr)
187 server->setAttribute(ATTR_KEY, ATTR_VALUE + 10);
188 std::this_thread::sleep_for(interval);
191 void ChangeResourceState()
193 std::chrono::milliseconds interval(DEFAULT_WAITTIME);
194 if (server != nullptr)
196 std::this_thread::sleep_for(interval);