Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / resource-hosting / src / unittest / HostingObjectUnitTest.cpp
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
23 #include "ResourceEncapsulationTestSimulator.h"
24 #include "HostingObject.h"
25
26 #include "RCSDiscoveryManager.h"
27
28 using namespace testing;
29 using namespace OIC::Service;
30
31 namespace
32 {
33     bool isDeleted = false;
34     void onDestroy(std::weak_ptr<HostingObject> rPtr)
35     {
36         HostingObject::Ptr ptr = rPtr.lock();
37         if(ptr) ptr.reset();
38         isDeleted = true;
39     }
40     void onDiscoveryResource(RCSRemoteResourceObject::Ptr){ }
41
42     void onUpdatedCache(const RCSResourceAttributes &) { }
43 }
44
45 class HostingObjectTest : public TestWithMock
46 {
47 public:
48     ResourceEncapsulationTestSimulator::Ptr testObject;
49     RCSRemoteResourceObject::Ptr remoteObject;
50
51     std::mutex mutexForCondition;
52     std::condition_variable responseCon;
53
54 protected:
55
56     void SetUp()
57     {
58         TestWithMock::SetUp();
59
60         testObject = std::make_shared<ResourceEncapsulationTestSimulator>();
61         testObject->defaultRunSimulator();
62         remoteObject = testObject->getRemoteResource();
63
64         isDeleted = false;
65     }
66
67     void TearDown()
68     {
69         TestWithMock::TearDown();
70
71         if(remoteObject.use_count() > 0)
72         {
73             if(remoteObject->isCaching())
74             {
75                 remoteObject->stopCaching();
76             }
77             if(remoteObject->isMonitoring())
78             {
79                 remoteObject->stopMonitoring();
80             }
81         }
82         testObject->destroy();
83     }
84
85 public:
86     void waitForCondition(int waitingTime = 1000)
87     {
88         std::unique_lock< std::mutex > lock{ mutexForCondition };
89         responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
90     }
91
92     void notifyCondition()
93     {
94         responseCon.notify_all();
95     }
96
97 };
98
99 TEST_F(HostingObjectTest, startCachingAtInitialize)
100 {
101     HostingObject::Ptr instance = std::make_shared<HostingObject>();
102     instance->initializeHostingObject(
103             remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
104
105     EXPECT_TRUE(remoteObject->isCaching());
106 }
107
108 TEST_F(HostingObjectTest, startMonitoringAtInitialize)
109 {
110     HostingObject::Ptr instance = std::make_shared<HostingObject>();
111     instance->initializeHostingObject(
112             remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
113
114     ASSERT_TRUE(remoteObject->isMonitoring());
115 }
116
117 TEST_F(HostingObjectTest, getRemoteResourceisValid)
118 {
119     HostingObject::Ptr instance = std::make_shared<HostingObject>();
120     instance->initializeHostingObject(
121             remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
122
123     ASSERT_EQ(remoteObject->getUri(), instance->getRemoteResource()->getUri());
124 }
125
126 TEST_F(HostingObjectTest, createMirroredServer)
127 {
128     int waitForResponse = 1000;
129     std::string uri = "";
130
131     HostingObject::Ptr instance = std::make_shared<HostingObject>();
132     instance->initializeHostingObject(
133             remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
134     std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
135
136     std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask = { };
137
138     mocks.OnCallFunc(onDiscoveryResource).Do(
139             [this, &uri, &discoveryTask](RCSRemoteResourceObject::Ptr ptr)
140             {
141                 if(ptr->getUri() == testObject->getHostedServerUri())
142                 {
143                     uri = ptr->getUri();
144                     discoveryTask->cancel();
145                     notifyCondition();
146                 }
147             });
148
149     discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
150             RCSAddress::multicast(), "resource.hosting", onDiscoveryResource);
151     waitForCondition(waitForResponse);
152
153     EXPECT_EQ(testObject->getHostedServerUri(), uri);
154 }
155
156 TEST_F(HostingObjectTest, UpdateCachedDataWhenChangedOriginResource)
157 {
158     int waitForResponse = 1000;
159     HostingObject::Ptr instance = std::make_shared<HostingObject>();
160     instance->initializeHostingObject(
161             remoteObject, std::bind(onDestroy, std::weak_ptr<HostingObject>(instance)));
162     std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
163
164     std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask = { };
165     RCSRemoteResourceObject::Ptr discoveredResource = { };
166
167     mocks.OnCallFunc(onDiscoveryResource).Do(
168             [this, &discoveredResource, &discoveryTask](RCSRemoteResourceObject::Ptr ptr)
169             {
170                 if(ptr->getUri() == testObject->getHostedServerUri())
171                 {
172                     discoveredResource = ptr;
173                     discoveryTask->cancel();
174                     notifyCondition();
175                 }
176             });
177
178     discoveryTask =  RCSDiscoveryManager::getInstance()->discoverResourceByType(
179             RCSAddress::multicast(), "resource.hosting", onDiscoveryResource);
180     waitForCondition(waitForResponse);
181
182     RCSResourceAttributes::Value result = { };
183     mocks.OnCallFunc(onUpdatedCache).Do(
184             [this, &result](const RCSResourceAttributes & att)
185             {
186                 result = att.at("Temperature");
187                 notifyCondition();
188             });
189
190     discoveredResource->startCaching(onUpdatedCache);
191     std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
192
193     RCSResourceAttributes::Value settingValue = 10;
194     testObject->getResourceServer()->setAttribute("Temperature", settingValue);
195     waitForCondition(waitForResponse);
196
197     EXPECT_EQ(result.toString(), settingValue.toString());
198
199 }