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