1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <OCPlatform.h>
23 #include <gtest/gtest.h>
25 namespace OCPlatformTest
30 OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
35 const OCResourceHandle HANDLE_ZERO = 0;
39 void foundResource(std::shared_ptr<OCResource> resource)
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;
50 OCResourceHandle RegisterResource(std::string uri, std::string type, std::string iface)
52 EXPECT_EQ(OC_STACK_OK,OCPlatform::registerResource(
53 resourceHandle, uri, type,
54 iface, entityHandler, resourceProperty));
55 return resourceHandle;
58 OCResourceHandle RegisterResource(std::string uri, std::string type)
60 EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
61 resourceHandle, uri, type,
62 resourceInterface, entityHandler, resourceProperty));
63 return resourceHandle;
66 OCResourceHandle RegisterResource(std::string uri)
68 EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(
69 resourceHandle, uri, resourceTypeName,
70 resourceInterface, entityHandler, resourceProperty));
71 return resourceHandle;
74 //RegisterResourceTest
75 TEST(RegisterResourceTest, RegisterSingleResource)
77 std::string uri = "/a/res2";
78 EXPECT_NE(HANDLE_ZERO, RegisterResource(uri));
81 TEST(RegisterResourceTest, RegisterMultipleResources)
83 std::string uri = "/a/multi";
84 //Good enough for 5 resources.
85 for(int i=0; i< 5; i++)
87 uri +=std::to_string(i);
88 EXPECT_NE(HANDLE_ZERO, RegisterResource(uri));
92 TEST(RegisterResourceTest, ReregisterResource)
94 OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light5"),
95 std::string("core.light"));
96 EXPECT_EQ(OC_STACK_OK, OC::OCPlatform::unregisterResource(resourceHandle));
98 EXPECT_NE(HANDLE_ZERO, RegisterResource(std::string("/a/light5"),
99 std::string("core.light")));
103 TEST(RegisterResourceTest, RegisterEmptyResource)
105 // We should not allow empty URI.
106 std::string emptyStr = "";
107 EXPECT_ANY_THROW(OCPlatform::registerResource(resourceHandle, emptyStr, emptyStr,
108 emptyStr, entityHandler, resourceProperty));
111 TEST(RegisterResourceTest, RegisterZeroResourceProperty)
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));
122 TEST(UnregisterTest, UnregisterZeroHandleValue)
124 EXPECT_ANY_THROW(OC::OCPlatform::unregisterResource(HANDLE_ZERO));
127 //UnbindResourcesTest
128 TEST(UnbindResourcesTest, UnbindResources)
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);
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));
144 TEST(UnbindResourcesTest, UnbindResourcesWithZero)
146 OCResourceHandle resourceHandle1 = 0;
147 OCResourceHandle resourceHandle2 = 0;
148 OCResourceHandle resourceHandle3 = 0;
150 std::vector<OCResourceHandle> rList;
152 rList.push_back(resourceHandle2);
153 rList.push_back(resourceHandle3);
155 EXPECT_ANY_THROW(OCPlatform::unbindResources(resourceHandle1, rList));
158 //BindInterfaceToResourceTest
159 TEST(BindInterfaceToResourceTest, BindResourceInterface)
161 OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light"),
162 std::string("core.light"));
163 OCStackResult result = OC::OCPlatform::bindInterfaceToResource(resourceHandle,
165 EXPECT_EQ(OC_STACK_OK, result);
168 TEST(BindInterfaceToResourceTest, BindZeroResourceInterface)
170 OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light1"),
171 std::string("core.light"));
172 EXPECT_ANY_THROW(OC::OCPlatform::bindInterfaceToResource(resourceHandle, 0));
175 //BindTypeToResourceTest
176 TEST(BindTypeToResourceTest, BindResourceType)
178 OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light3"),
179 std::string("core.light"));
180 OCStackResult result = OC::OCPlatform::bindTypeToResource(resourceHandle,
182 EXPECT_EQ(OC_STACK_OK, result);
185 TEST(BindTypeToResourceTest, BindZeroResourceType)
187 OCResourceHandle resourceHandle = RegisterResource(std::string("/a/light4"),
188 std::string("core.light"));
189 EXPECT_ANY_THROW(OC::OCPlatform::bindTypeToResource(resourceHandle, 0));
193 TEST(UnbindResourceTest, BindAndUnbindResource)
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);
200 EXPECT_EQ(OC_STACK_OK, OCPlatform::bindResource(resourceHandle1, resourceHandle2));
201 EXPECT_EQ(OC_STACK_OK, OCPlatform::unbindResource(resourceHandle1, resourceHandle2));
205 TEST(PresenceTest, StartAndStopPresence)
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());
213 TEST(OCPlatformTest, UnbindZeroRsourceHandleValue)
215 EXPECT_ANY_THROW(OCPlatform::unbindResource(HANDLE_ZERO, HANDLE_ZERO));
218 //NotifyAllObserverTest
219 TEST(NotifyAllObserverTest, NotifyAllObservers)
221 OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs"),
222 std::string("core.obs"));
223 EXPECT_EQ(OC_STACK_NO_OBSERVERS, OCPlatform::notifyAllObservers(resourceHome));
226 TEST(NotifyAllObserverTest, NotifyListOfObservers)
228 OCResourceHandle resourceHome = RegisterResource(std::string("/a/obs2"),
229 std::string("core.obs"));
231 std::shared_ptr<OCResourceResponse> resourceResponse(new OCResourceResponse());
232 ObservationIds interestedObservers;
233 EXPECT_ANY_THROW(OCPlatform::notifyListOfObservers(resourceHome,
234 interestedObservers, resourceResponse));
237 //DeviceEntityHandlerTest
238 TEST(DeviceEntityHandlerTest, SetDefaultDeviceEntityHandler)
240 EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(entityHandler));
241 EXPECT_EQ(OC_STACK_OK, OCPlatform::setDefaultDeviceEntityHandler(NULL));