Add C++ Unit tests
[platform/upstream/iotivity.git] / resource / unittests / OCPlatformTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 <OCPlatform.h>
22 #include <OCApi.h>
23 #include <gtest/gtest.h>
24
25 namespace OCPlatformTest
26 {
27     using namespace OC;
28
29     // Callbacks
30     OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
31     {
32         return OC_EH_OK;
33     }
34
35     const OCResourceHandle HANDLE_ZERO = 0;
36
37     PlatformConfig cfg;
38
39     void foundResource(std::shared_ptr<OCResource> resource)
40     {
41     }
42
43     std::string resourceURI;
44     std::string resourceTypeName = "core.res";
45     std::string resourceInterface = DEFAULT_INTERFACE;
46     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
47     OCResourceHandle resourceHandle;
48
49     //Helper methods
50     OCResourceHandle RegisterResource(std::string uri, std::string type, std::string iface)
51     {
52         EXPECT_EQ(OC_STACK_OK,OCPlatform::registerResource(
53                                         resourceHandle, uri, type,
54                                         iface, entityHandler, resourceProperty));
55         return resourceHandle;
56     }
57
58     OCResourceHandle RegisterResource(std::string uri, std::string type)
59     {
60         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
61                                         resourceHandle, uri, type,
62                                         resourceInterface, entityHandler, resourceProperty));
63         return resourceHandle;
64     }
65
66     OCResourceHandle RegisterResource(std::string uri)
67     {
68         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
69                                         resourceHandle, uri, resourceTypeName,
70                                         resourceInterface, entityHandler, resourceProperty));
71         return resourceHandle;
72     }
73
74     //RegisterResourceTest
75     TEST(RegisterResourceTest, RegisterSingleResource)
76     {
77         std::string uri = "/a/res2";
78         EXPECT_NE(HANDLE_ZERO, RegisterResource(uri));
79     }
80
81     TEST(RegisterResourceTest, RegisterMultipleResources)
82     {
83         std::string uri = "/a/multi";
84         //Good enough for 5 resources.
85         for(int i=0; i< 5; i++)
86         {
87             uri +=std::to_string(i);
88             EXPECT_NE(HANDLE_ZERO, RegisterResource(uri));
89         }
90     }
91
92     TEST(RegisterResourceTest, ReregisterResource)
93     {
94         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light5"),
95             std::string("core.light"));
96         EXPECT_EQ(OC_STACK_OK, OC::OCPlatform::unregisterResource(resourceHandle));
97
98         EXPECT_NE(HANDLE_ZERO, RegisterResource(std::string("/a/light5"),
99             std::string("core.light")));
100
101     }
102
103     TEST(RegisterResourceTest, RegisterEmptyResource)
104     {
105         // We should not allow empty URI.
106         std::string emptyStr = "";
107         EXPECT_ANY_THROW(OCPlatform::registerResource(resourceHandle, emptyStr, emptyStr,
108                                         emptyStr, entityHandler, resourceProperty));
109     }
110
111     TEST(RegisterResourceTest, RegisterZeroResourceProperty)
112     {
113         std::string uri = "/a/light6";
114         std::string type = "core.light";
115         uint8_t resourceProperty = 0;
116         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
117                 resourceHandle, uri, type,
118                 resourceInterface, entityHandler, resourceProperty));
119     }
120
121     //UnregisterTest
122     TEST(UnregisterTest, UnregisterZeroHandleValue)
123     {
124         EXPECT_ANY_THROW(OC::OCPlatform::unregisterResource(HANDLE_ZERO));
125     }
126
127     //UnbindResourcesTest
128     TEST(UnbindResourcesTest, UnbindResources)
129     {
130         OCResourceHandle resourceHome = RegisterResource(std::string("a/home"),
131             std::string("core.home"));
132         OCResourceHandle resourceKitchen = RegisterResource(std::string("a/kitchen"),
133             std::string("core.kitchen"), LINK_INTERFACE);
134         OCResourceHandle resourceRoom = RegisterResource(std::string("a/office"),
135             std::string("core.office"), LINK_INTERFACE);
136
137         std::vector<OCResourceHandle> rList;
138         rList.push_back(resourceKitchen);
139         rList.push_back(resourceRoom);
140         EXPECT_EQ(OC_STACK_OK, OCPlatform::bindResources(resourceHome, rList));
141         EXPECT_EQ(OC_STACK_OK, OCPlatform::unbindResources(resourceHome, rList));
142     }
143
144     TEST(UnbindResourcesTest, UnbindResourcesWithZero)
145     {
146         OCResourceHandle resourceHandle1 = 0;
147         OCResourceHandle resourceHandle2 = 0;
148         OCResourceHandle resourceHandle3 = 0;
149
150         std::vector<OCResourceHandle> rList;
151
152         rList.push_back(resourceHandle2);
153         rList.push_back(resourceHandle3);
154
155         EXPECT_ANY_THROW(OCPlatform::unbindResources(resourceHandle1, rList));
156     }
157
158     //BindInterfaceToResourceTest
159     TEST(BindInterfaceToResourceTest, BindResourceInterface)
160     {
161         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light"),
162             std::string("core.light"));
163         OCStackResult result = OC::OCPlatform::bindInterfaceToResource(resourceHandle,
164             BATCH_INTERFACE);
165         EXPECT_EQ(OC_STACK_OK, result);
166     }
167
168     TEST(BindInterfaceToResourceTest, BindZeroResourceInterface)
169     {
170         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light1"),
171             std::string("core.light"));
172         EXPECT_ANY_THROW(OC::OCPlatform::bindInterfaceToResource(resourceHandle, 0));
173     }
174
175     //BindTypeToResourceTest
176     TEST(BindTypeToResourceTest, BindResourceType)
177     {
178         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light3"),
179             std::string("core.light"));
180         OCStackResult result = OC::OCPlatform::bindTypeToResource(resourceHandle,
181             "core.brightlight");
182         EXPECT_EQ(OC_STACK_OK, result);
183     }
184
185     TEST(BindTypeToResourceTest, BindZeroResourceType)
186     {
187         OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light4"),
188             std::string("core.light"));
189         EXPECT_ANY_THROW(OC::OCPlatform::bindTypeToResource(resourceHandle, 0));
190     }
191
192     //UnbindResourceTest
193     TEST(UnbindResourceTest, BindAndUnbindResource)
194     {
195         OCResourceHandle resourceHandle1 = RegisterResource(std::string("a/unres"),
196             std::string("core.unres"));
197         OCResourceHandle resourceHandle2 = RegisterResource(std::string("a/unres2"),
198             std::string("core.unres"), LINK_INTERFACE);
199
200         EXPECT_EQ(OC_STACK_OK, OCPlatform::bindResource(resourceHandle1, resourceHandle2));
201         EXPECT_EQ(OC_STACK_OK, OCPlatform::unbindResource(resourceHandle1, resourceHandle2));
202     }
203
204     //PresenceTest
205     TEST(PresenceTest, StartAndStopPresence)
206     {
207         EXPECT_EQ(OC_STACK_OK, OCPlatform::startPresence(30));
208         EXPECT_NE(HANDLE_ZERO, RegisterResource( std::string("/a/Presence"),
209             std::string("core.Presence")));
210         EXPECT_EQ(OC_STACK_OK, OCPlatform::stopPresence());
211     }
212
213     TEST(OCPlatformTest, UnbindZeroRsourceHandleValue)
214     {
215         EXPECT_ANY_THROW(OCPlatform::unbindResource(HANDLE_ZERO, HANDLE_ZERO));
216     }
217
218     //NotifyAllObserverTest
219     TEST(NotifyAllObserverTest, NotifyAllObservers)
220     {
221         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs"),
222             std::string("core.obs"));
223         EXPECT_EQ(OC_STACK_NO_OBSERVERS, OCPlatform::notifyAllObservers(resourceHome));
224     }
225
226     TEST(NotifyAllObserverTest, NotifyListOfObservers)
227     {
228         OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs2"),
229             std::string("core.obs"));
230
231         std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
232         ObservationIds interestedObservers;
233         EXPECT_ANY_THROW(OCPlatform::notifyListOfObservers(resourceHome,
234             interestedObservers, resourceResponse));
235     }
236
237     //DeviceEntityHandlerTest
238     TEST(DeviceEntityHandlerTest, SetDefaultDeviceEntityHandler)
239     {
240         EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(entityHandler));
241         EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(NULL));
242     }
243 }