fix resource-container unit test
[platform/upstream/iotivity.git] / service / resource-container / unittests / ResourceContainerTestSimulator.h
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <memory>
22 #include <mutex>
23 #include <atomic>
24
25 #include "UnitTestHelper.h"
26
27 #include "RCSDiscoveryManager.h"
28 #include "RCSRemoteResourceObject.h"
29 #include "RCSResourceAttributes.h"
30 #include "RCSAddress.h"
31
32 using namespace testing;
33 using namespace OIC::Service;
34
35 class ResourceContainerTestSimulator
36     : public std::enable_shared_from_this<ResourceContainerTestSimulator>
37 {
38     public:
39         typedef std::shared_ptr<ResourceContainerTestSimulator> Ptr;
40
41         RCSResourceObject::Ptr server;
42         RCSRemoteResourceObject::Ptr remoteResource;
43
44         static constexpr int DEFAULT_WAITTIME = 3000;
45
46     private:
47         std::mutex mutexForDiscovery;
48
49         std::string MULTICASTURI;
50         std::string RESOURCEURI;
51         std::string RESOURCETYPE;
52         std::string RESOURCEINTERFACE;
53         std::string ATTR_KEY;
54         int ATTR_VALUE;
55         std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
56
57     public:
58         ResourceContainerTestSimulator()
59             : server(nullptr), remoteResource(nullptr),
60               mutexForDiscovery(),
61               MULTICASTURI("/oic/res"),
62               RESOURCEURI("/a/TempHumSensor/Container"),
63               RESOURCETYPE("resource.container"),
64               RESOURCEINTERFACE("oic.if.baseline"),
65               ATTR_KEY("TestResourceContainer"),
66               ATTR_VALUE(0)
67         { }
68
69         ~ResourceContainerTestSimulator()
70         {
71             try
72             {
73                 if (remoteResource != nullptr && remoteResource->isCaching())
74                 {
75                     remoteResource->stopCaching();
76                 }
77                 if (remoteResource != nullptr && remoteResource->isMonitoring())
78                 {
79                     remoteResource->stopMonitoring();
80                 }
81             }
82             catch (RCSException &e)
83             {
84                 std::cout << "exception : " << e.what() << std::endl;
85             }
86         }
87
88     private:
89         void onDiscoveryResource_Impl(RCSRemoteResourceObject::Ptr resourceObject)
90         {
91             if (remoteResource != nullptr)
92             {
93                 return;
94             }
95
96             if (RESOURCEURI.compare(resourceObject->getUri()) != 0)
97             {
98                 return;
99             }
100
101             remoteResource = resourceObject;
102             mutexForDiscovery.unlock();
103         }
104
105         static void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject,
106                                         std::weak_ptr<ResourceContainerTestSimulator> rPtr)
107         {
108             std::shared_ptr<ResourceContainerTestSimulator> ptr = rPtr.lock();
109             if (ptr != nullptr)
110             {
111                 ptr->onDiscoveryResource_Impl(resourceObject);
112             }
113             else
114             {
115                 std::cout << "Aleady delete simulator\n";
116             }
117         }
118         void waitForDiscovery()
119         {
120             std::chrono::milliseconds interval(DEFAULT_WAITTIME);
121             while (true)
122             {
123                 if (mutexForDiscovery.try_lock())
124                 {
125                     mutexForDiscovery.unlock();
126                     return;
127                 }
128                 std::this_thread::sleep_for(interval);
129             }
130         }
131
132     public:
133         void defaultRunSimulator()
134         {
135             createResource();
136             discoveryResource();
137             waitForDiscovery();
138         }
139
140         void createResource()
141         {
142             server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE)
143                      .setDiscoverable(true).setObservable(true).build();
144             server->setAttribute(ATTR_KEY, ATTR_VALUE);
145         }
146
147         void discoveryResource()
148         {
149             discoveryResource(RESOURCETYPE);
150         }
151
152         void discoveryResource(std::string &resourceType)
153         {
154             try
155             {
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();
161             }
162             catch (RCSInvalidParameterException &e)
163             {
164                 std::cout << "exception : " << e.what() << std::endl;
165             }
166         }
167
168         std::string getServerUri() const
169         {
170             return RESOURCEURI;
171         }
172
173         RCSResourceObject::Ptr getResourceServer() const
174         {
175             return server;
176         }
177
178         RCSRemoteResourceObject::Ptr getRemoteResource() const
179         {
180             return remoteResource;
181         }
182
183         void ChangeAttributeValue()
184         {
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);
189         }
190
191         void ChangeResourceState()
192         {
193             std::chrono::milliseconds interval(DEFAULT_WAITTIME);
194             if (server != nullptr)
195                 server = nullptr;
196             std::this_thread::sleep_for(interval);
197         }
198
199 };