Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / resource-hosting / unittest / ResourceHostingUnitTest.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
25 #include "ResourceHosting.h"
26
27 using namespace testing;
28 using namespace OIC::Service;
29
30 namespace
31 {
32     bool isStarted = false;
33     bool isFinished = false;
34
35     ResourceEncapsulationTestSimulator testObject;
36
37     void onDiscoveryResource(RCSRemoteResourceObject::Ptr) { }
38 }
39
40 class ResourceHostingTest : public TestWithMock
41 {
42 public:
43     std::mutex mutexForCondition;
44     std::condition_variable responseCon;
45     std::unique_ptr<RCSDiscoveryManager::DiscoveryTask> discoveryTask;
46
47 protected:
48
49     void SetUp()
50     {
51         TestWithMock::SetUp();
52         if(!isStarted)
53         {
54             testObject.createResource("1");
55
56             isStarted = true;
57         }
58     }
59
60     void TearDown()
61     {
62         TestWithMock::TearDown();
63
64         if(isFinished)
65         {
66             testObject.destroy();
67             isStarted = false;
68         }
69     }
70
71 public:
72     void waitForCondition(int waitingTime = 1000)
73     {
74         std::unique_lock< std::mutex > lock{ mutexForCondition };
75         responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
76     }
77
78     void notifyCondition()
79     {
80         responseCon.notify_all();
81     }
82
83 };
84
85 TEST(ResourceHostingSTATICMethodTest, getInstanceAllwaysSameReturnInstance)
86 {
87     EXPECT_EQ(ResourceHosting::getInstance(), ResourceHosting::getInstance());
88 }
89
90 TEST_F(ResourceHostingTest, startHosting)
91 {
92     try
93     {
94         ResourceHosting::getInstance()->startHosting();
95     } catch (...)
96     {
97         FAIL() << "Non-Expected Exception";
98     }
99 }
100
101 TEST_F(ResourceHostingTest, HostingFoundBeforeMakeOriginServer)
102 {
103     std::string uri = "";
104     testObject.getResourceServer()->setAttribute(
105             "Temperature", RCSResourceAttributes::Value((int)0));
106     waitForCondition();
107
108     mocks.OnCallFunc(onDiscoveryResource).Do(
109             [this, &uri, &testObject](RCSRemoteResourceObject::Ptr ptr)
110             {
111                 if(ptr->getUri() == testObject.getHostedServerUri())
112                 {
113                     uri = ptr->getUri();
114                     discoveryTask->cancel();
115                     notifyCondition();
116                 }
117             });
118
119     discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(
120             RCSAddress::multicast(), "oic.r.resourcehosting", onDiscoveryResource);
121     waitForCondition();
122
123     std::string mirroredUri = { testObject.getHostedServerUri() };
124
125     ASSERT_EQ(mirroredUri, uri);
126 }
127
128
129 TEST_F(ResourceHostingTest, stopHosting)
130 {
131     ResourceHosting::getInstance()->stopHosting();
132     isFinished = true;
133 }