Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceBroker / unittest / DevicePresenceUnitTest.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 #include <unistd.h>
21
22 #include "gtest/gtest.h"
23 #include "HippoMocks/hippomocks.h"
24
25 #include "BrokerTypes.h"
26 #include "PrimitiveResource.h"
27 #include "ResponseStatement.h"
28 #include "OCPlatform.h"
29 #include "DevicePresence.h"
30 #include "ResourcePresence.h"
31 #include "UnitTestHelper.h"
32
33 using namespace testing;
34 using namespace OIC::Service;
35 using namespace OC;
36
37 typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
38         const std::string&, OCConnectivityType, SubscribeCallback);
39
40 class DevicePresenceTest : public TestWithMock
41 {
42 public:
43     typedef std::function<void(OCStackResult,const unsigned int, const std::string&)> subscribeCallback;
44     DevicePresence * instance;
45     PrimitiveResource::Ptr pResource;
46     BrokerCB cb;
47     BrokerID id;
48
49 protected:
50
51     void SetUp()
52     {
53         TestWithMock::SetUp();
54         instance = (DevicePresence*)new DevicePresence();
55         pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
56         cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
57         id = 0;
58     }
59
60     void TearDown()
61     {
62         TestWithMock::TearDown();
63         pResource.reset();
64         id = 0;
65         cb = nullptr;
66     }
67
68     void MockingFunc()
69     {
70         mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string());
71         mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
72     }
73 };
74 TEST_F(DevicePresenceTest,timeoutCB_TimeOverWhenIsSubscribe)
75 {
76    MockingFunc();
77    instance->initializeDevicePresence(pResource);
78    std::cout<<"wait while done timeout device presence\n";
79    sleep((BROKER_DEVICE_PRESENCE_TIMEROUT/1000)+1);
80    ASSERT_EQ(DEVICE_STATE::LOST_SIGNAL,instance->getDeviceState());
81 }
82
83 TEST_F(DevicePresenceTest,SubscribeCB_NormalHandlingIfMessageOC_STACK_OK)
84 {
85    mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string());
86    mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Do(
87             [](OC::OCPlatform::OCPresenceHandle&,
88                     const std::string&, OCConnectivityType, SubscribeCallback callback)->OCStackResult{
89
90         callback(OC_STACK_OK,0,std::string());
91         return OC_STACK_OK;
92
93     }).Return(OC_STACK_OK);
94    instance->initializeDevicePresence(pResource);
95    ASSERT_NE(DEVICE_STATE::LOST_SIGNAL,instance->getDeviceState());
96 }
97
98 TEST_F(DevicePresenceTest,initializeDevicePresence_NormalHandlingIfNormalResource)
99 {
100
101     MockingFunc();
102
103     ASSERT_NO_THROW(instance->initializeDevicePresence(pResource));
104
105 }
106
107 TEST_F(DevicePresenceTest,initializeDevicePresence_ErrorHandlingIfAbnormalResource)
108 {
109
110     MockingFunc();
111     mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_ERROR);
112
113     ASSERT_THROW(instance->initializeDevicePresence(pResource),PlatformException);
114
115 }
116
117 TEST_F(DevicePresenceTest,addPresenceResource_NormalHandlingIfNormalResource)
118 {
119
120     ResourcePresence * resource = (ResourcePresence *)new ResourcePresence();
121     instance->addPresenceResource(resource);
122
123     ASSERT_FALSE(instance->isEmptyResourcePresence());
124
125 }
126
127 TEST_F(DevicePresenceTest,isEmptyResourcePresence_NormalHandling)
128 {
129
130     MockingFunc();
131
132     ASSERT_TRUE(instance->isEmptyResourcePresence());
133
134 }
135
136 TEST_F(DevicePresenceTest,getAddress_NormalHandling)
137 {
138
139     MockingFunc();
140
141     instance->initializeDevicePresence(pResource);
142     instance->getAddress();
143
144 }
145
146 TEST_F(DevicePresenceTest,NormalHandlingWhenReceivedCallbackMessage)
147 {
148
149     MockingFunc();
150
151 }