Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / resource-hosting / 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     const std::string HOSTING_RESOURCE_TYPE = "oic.r.resourcehosting";
34     const std::string TEST_ATT_KEY = "Temperature";
35
36     bool isStarted = false;
37     bool isFinished = false;
38
39     ResourceEncapsulationTestSimulator testObject;
40     RCSRemoteResourceObject::Ptr remoteObject;
41
42     HostingObject::Ptr instance;
43     RCSRemoteResourceObject::Ptr discoveredResource;
44
45     std::condition_variable responseCon;
46
47     void onDestroy() { }
48     void onDiscoveryResource(RCSRemoteResourceObject::Ptr){ }
49     void onUpdatedCache(const RCSResourceAttributes &) { }
50     void onSetAttributes(const RCSResourceAttributes &, int) { }
51
52     void setup()
53     {
54         if(!isStarted)
55         {
56             testObject.defaultRunSimulator();
57             remoteObject = testObject.getRemoteResource();
58
59             instance = HostingObject::createHostingObject(
60                         remoteObject, &onDestroy);
61
62             testObject.getResourceServer()->setAttribute(
63                     "Temperature", RCSResourceAttributes::Value((int)0));
64
65             isStarted = true;
66         }
67     }
68
69     void tearDown()
70     {
71         if(isFinished)
72         {
73             testObject.destroy();
74             instance.reset();
75             isStarted = false;
76         }
77     }
78 }
79
80 class HostingObjectTest : public TestWithMock
81 {
82 public:
83     std::mutex mutexForCondition;
84
85 protected:
86
87     void SetUp()
88     {
89         TestWithMock::SetUp();
90         setup();
91     }
92
93     void TearDown()
94     {
95         TestWithMock::TearDown();
96         tearDown();
97     }
98
99 public:
100     void waitForCondition(int waitingTime = 1000)
101     {
102         std::unique_lock< std::mutex > lock{ mutexForCondition };
103         responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
104     }
105
106     void notifyCondition()
107     {
108         responseCon.notify_all();
109     }
110
111 };
112
113 TEST_F(HostingObjectTest, startCachingAtInitialize)
114 {
115     EXPECT_TRUE(remoteObject->isCaching());
116 }
117
118 TEST_F(HostingObjectTest, startMonitoringAtInitialize)
119 {
120     ASSERT_TRUE(remoteObject->isMonitoring());
121 }
122
123 TEST_F(HostingObjectTest, getRemoteResourceisValid)
124 {
125     ASSERT_EQ(remoteObject->getUri(), instance->getRemoteResource()->getUri());
126 }
127
128 TEST_F(HostingObjectTest, createMirroredServer)
129 {
130     int waitForResponse = 1000;
131     std::string uri = "";
132
133     std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask = { };
134
135     waitForCondition(waitForResponse);
136     mocks.OnCallFunc(onDiscoveryResource).Do(
137             [this, &uri, &discoveryTask, &testObject, &discoveredResource]
138              (RCSRemoteResourceObject::Ptr ptr)
139             {
140                 if(ptr->getUri() == testObject.getHostedServerUri())
141                 {
142                     uri = ptr->getUri();
143                     discoveredResource = ptr;
144                     discoveryTask->cancel();
145                     notifyCondition();
146                 }
147             });
148
149     discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
150             RCSAddress::multicast(), "/oic/res", HOSTING_RESOURCE_TYPE, onDiscoveryResource);
151     waitForCondition(waitForResponse);
152
153     EXPECT_EQ(testObject.getHostedServerUri(), uri);
154 }
155
156 TEST_F(HostingObjectTest, UpdateCachedDataWhenChangedOriginResource)
157 {
158     int waitForResponse = 1000;
159     RCSResourceAttributes::Value result = { };
160
161     mocks.OnCallFunc(onUpdatedCache).Do(
162             [this, &result](const RCSResourceAttributes & att)
163             {
164                 result = att.at(TEST_ATT_KEY);
165                 notifyCondition();
166             });
167
168     discoveredResource->startCaching(onUpdatedCache);
169     std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse});
170
171     RCSResourceAttributes::Value settingValue = 10;
172     testObject.getResourceServer()->setAttribute(TEST_ATT_KEY, settingValue);
173     waitForCondition(waitForResponse);
174
175     isFinished = true;
176
177     EXPECT_EQ(result.toString(), settingValue.toString());
178
179 }
180
181 TEST_F(HostingObjectTest, SetDataToMirroredResource)
182 {
183     int waitForResponse = 1000;
184     RCSResourceAttributes::Value result = { };
185
186     mocks.ExpectCallFunc(onSetAttributes).Do(
187             [this, & result](const RCSResourceAttributes &att, int)
188             {
189                 result = att.at(TEST_ATT_KEY);
190                 notifyCondition();
191             });
192     RCSResourceAttributes setAttrs;
193     RCSResourceAttributes::Value settingValue = 20;
194     setAttrs[TEST_ATT_KEY] = settingValue;
195     discoveredResource->setRemoteAttributes(setAttrs, onSetAttributes);
196     waitForCondition(waitForResponse);
197
198     EXPECT_EQ(result.toString(), settingValue.toString());
199 }
200
201 TEST_F(HostingObjectTest, ExpectCallOnDestroyWhenStopHostingObject)
202 {
203     int waitForResponse = 1000;
204
205     mocks.ExpectCallFunc(onDestroy).Do(
206             [& responseCon]()
207             {
208                 responseCon.notify_all();
209             });
210
211     testObject.destroy();
212     instance.reset();
213     waitForCondition(waitForResponse);
214 }