6a4b7ff6965cdd81629fbce3d0710a1998746715
[platform/upstream/iotivity.git] / service / resource-manipulation / src / common / primitiveResource / unittests / PresenceSubscriberTest.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 <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
23
24 #include <PresenceSubscriber.h>
25 #include <RCSException.h>
26
27 #include <OCPlatform.h>
28
29 using namespace testing;
30 using namespace OIC::Service;
31
32 typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
33         const std::string&, OCConnectivityType, SubscribeCallback);
34 typedef OCStackResult (*subscribePresenceSig2)(OC::OCPlatform::OCPresenceHandle&,
35         const std::string&, const std::string&, OCConnectivityType, SubscribeCallback);
36
37 const std::string HOST{ "host" };
38 const OCConnectivityType CONTYPE{ };
39
40 class PresenceSubscriberNonMemberTest: public Test
41 {
42 public:
43     OCDoHandle handle;
44     MockRepository mocks;
45
46 };
47
48 TEST_F(PresenceSubscriberNonMemberTest, OCPlatformSubscribePresenceWillBeCalled)
49 {
50     mocks.ExpectCallFuncOverload(
51             static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence))
52                         .With(_, HOST,CONTYPE, _).Return(OC_STACK_OK);
53
54     subscribePresence(handle, HOST, CONTYPE, SubscribeCallback());
55 }
56
57 TEST_F(PresenceSubscriberNonMemberTest, SubscribePresenceThrowsIfResultIsNotOK)
58 {
59     mocks.ExpectCallFuncOverload(
60             static_cast< subscribePresenceSig1>(OC::OCPlatform::subscribePresence))
61                     .Return(OC_STACK_ERROR);
62
63     ASSERT_THROW(subscribePresence(handle, "", CONTYPE, SubscribeCallback()), PlatformException);
64 }
65
66 TEST_F(PresenceSubscriberNonMemberTest, OCPlatformUnsubscribePresenceWillBeCalled)
67 {
68     mocks.ExpectCallFuncOverload(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
69
70     unsubscribePresence(handle);
71 }
72
73 TEST_F(PresenceSubscriberNonMemberTest, UnsubscribePresenceThrowIfResultIsNotOK)
74 {
75     mocks.ExpectCallFuncOverload(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_ERROR);
76
77     ASSERT_THROW(unsubscribePresence(handle), PlatformException);
78 }
79
80
81
82 class PresenceSubscriberTest: public Test
83 {
84 public:
85     MockRepository mocks;
86
87 protected:
88     void SetUp() {
89         mocks.OnCallFuncOverload(
90                 static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Do(
91
92             [](OC::OCPlatform::OCPresenceHandle& handle, const std::string&,
93                     OCConnectivityType, SubscribeCallback) -> OCStackResult
94             {
95                 handle = reinterpret_cast<OC::OCPlatform::OCPresenceHandle>(1);
96                 return OC_STACK_OK;
97             }
98         );
99
100         mocks.OnCallFunc(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
101     }
102 };
103
104 TEST_F(PresenceSubscriberTest, IsNotSubscribingWhenCreatedWithDefaultConstructor)
105 {
106     PresenceSubscriber subscriber;
107     ASSERT_FALSE(subscriber.isSubscribing());
108 }
109
110 TEST_F(PresenceSubscriberTest, ConstructorCallOCPlatformSubscribe)
111 {
112     mocks.ExpectCallFuncOverload(
113             static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence))
114                      .With(_, HOST, CONTYPE, _).Return(OC_STACK_OK);
115
116     PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
117 }
118
119 TEST_F(PresenceSubscriberTest, ConstructorWithResourceTypeCallOCPlatformSubscribe)
120 {
121     const std::string resType { "resType" };
122
123     mocks.ExpectCallFuncOverload(
124             static_cast< subscribePresenceSig2 >(OC::OCPlatform::subscribePresence))
125                      .With(_, HOST, resType, CONTYPE, _).Return(OC_STACK_OK);
126
127     PresenceSubscriber subscriber{ HOST, resType, CONTYPE, SubscribeCallback() };
128 }
129
130 TEST_F(PresenceSubscriberTest, ConstructorThrowsIfResultIsNotOK)
131 {
132     mocks.ExpectCallFuncOverload(
133             static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence))
134                     .Return(OC_STACK_ERROR);
135
136     ASSERT_THROW(PresenceSubscriber(HOST, CONTYPE, SubscribeCallback()), PlatformException);
137 }
138
139 TEST_F(PresenceSubscriberTest, IsSubscribingIfConstructedWithoutException)
140 {
141     PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
142
143     ASSERT_TRUE(subscriber.isSubscribing());
144 }
145
146 TEST_F(PresenceSubscriberTest, IsSubscribingOfMovedSubscriberReturnsFalse)
147 {
148     PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
149
150     PresenceSubscriber newSubscriber{ std::move(subscriber) };
151
152     ASSERT_FALSE(subscriber.isSubscribing());
153 }
154
155 TEST_F(PresenceSubscriberTest, IsSubscribingOfMovedSubscriberWithAssignmentReturnsFalse)
156 {
157     PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
158
159     PresenceSubscriber newSubscriber;
160
161     newSubscriber = std::move(subscriber);
162
163     ASSERT_FALSE(subscriber.isSubscribing());
164 }
165
166 TEST_F(PresenceSubscriberTest, UnsubscribeWillBeCalledWhenSubscriberIsDestoryed)
167 {
168     mocks.ExpectCallFunc(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
169     {
170         PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
171     }
172 }