Catching OIC::Service::ResourceCacheManager::InvalidParameterException
[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     private:
45         std::mutex mutexForDiscovery;
46
47         std::string MULTICASTURI;
48         std::string RESOURCEURI;
49         std::string RESOURCETYPE;
50         std::string RESOURCEINTERFACE;
51         std::string ATTR_KEY;
52         int ATTR_VALUE;
53         std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
54
55     public:
56         ResourceContainerTestSimulator()
57             : server(nullptr), remoteResource(nullptr),
58               mutexForDiscovery(),
59               MULTICASTURI("/oic/res"),
60               RESOURCEURI("/a/TempHumSensor/Container"),
61               RESOURCETYPE("resource.container"),
62               RESOURCEINTERFACE("oic.if.baseline"),
63               ATTR_KEY("Temperature"),
64               ATTR_VALUE(0)
65         { }
66
67         ~ResourceContainerTestSimulator()
68         {
69             try
70             {
71                 if (remoteResource != nullptr && remoteResource->isCaching())
72                 {
73                     remoteResource->stopCaching();
74                 }
75                 if (remoteResource != nullptr && remoteResource->isMonitoring())
76                 {
77                     remoteResource->stopMonitoring();
78                 }
79             }
80             catch (RCSException &e)
81             {
82                 std::cout << "exception : " << e.what() << std::endl;
83             }
84         }
85
86     private:
87         void onDiscoveryResource_Impl(RCSRemoteResourceObject::Ptr resourceObject)
88         {
89             if (remoteResource != nullptr)
90             {
91                 return;
92             }
93
94             if (RESOURCEURI.compare(resourceObject->getUri()) != 0)
95             {
96                 return;
97             }
98
99             remoteResource = resourceObject;
100             mutexForDiscovery.unlock();
101         }
102
103         static void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject,
104                                         std::weak_ptr<ResourceContainerTestSimulator> rPtr)
105         {
106             std::shared_ptr<ResourceContainerTestSimulator> ptr = rPtr.lock();
107             if (ptr != nullptr)
108             {
109                 ptr->onDiscoveryResource_Impl(resourceObject);
110             }
111             else
112             {
113                 std::cout << "Aleady delete simulator\n";
114             }
115         }
116         void waitForDiscovery()
117         {
118             std::chrono::milliseconds interval(100);
119             while (true)
120             {
121                 if (mutexForDiscovery.try_lock())
122                 {
123                     mutexForDiscovery.unlock();
124                     return;
125                 }
126                 std::this_thread::sleep_for(interval);
127             }
128         }
129
130     public:
131         void defaultRunSimulator()
132         {
133             createResource();
134             discoveryResource();
135             waitForDiscovery();
136         }
137
138         void createResource()
139         {
140             server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE)
141                      .setDiscoverable(true).setObservable(true).build();
142             server->setAttribute(ATTR_KEY, ATTR_VALUE);
143         }
144
145         void discoveryResource()
146         {
147             discoveryResource(RESOURCETYPE);
148         }
149
150         void discoveryResource(std::string &resourceType)
151         {
152             try
153             {
154                 discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
155                                     RCSAddress::multicast(), MULTICASTURI, resourceType,
156                                     std::bind(onDiscoveryResource, std::placeholders::_1,
157                                               std::weak_ptr<ResourceContainerTestSimulator>(shared_from_this())));
158                 mutexForDiscovery.lock();
159             }
160             catch (RCSInvalidParameterException &e)
161             {
162                 std::cout << "exception : " << e.what() << std::endl;
163             }
164         }
165
166         std::string getServerUri() const
167         {
168             return RESOURCEURI;
169         }
170
171         RCSResourceObject::Ptr getResourceServer() const
172         {
173             return server;
174         }
175
176         RCSRemoteResourceObject::Ptr getRemoteResource() const
177         {
178             return remoteResource;
179         }
180
181         void ChangeAttributeValue()
182         {
183             std::chrono::milliseconds interval(100);
184             if (server != nullptr)
185                 server->setAttribute(ATTR_KEY, ATTR_VALUE + 10);
186             std::this_thread::sleep_for(interval);
187         }
188
189         void ChangeResourceState()
190         {
191             std::chrono::milliseconds interval(400);
192             if (server != nullptr)
193                 server = nullptr;
194             std::this_thread::sleep_for(interval);
195         }
196
197 };