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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <OCPlatform.h>
24 #include <gtest/gtest.h>
28 namespace OCResourceTest
32 void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
36 void onGetPut(const HeaderOptions&, const OCRepresentation& , const int eCode)
38 EXPECT_EQ(eCode, OC_STACK_OK);
41 void foundResource(std::shared_ptr<OCResource> )
45 void deleteResponse(const HeaderOptions&, const int)
50 OCResource::Ptr ConstructResourceObject(std::string host, std::string uri)
52 OCConnectivityType connectivityType = OC_WIFI;
53 std::vector<std::string> types = {"intel.rpost"};
54 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
56 return OCPlatform::constructResourceObject(host, uri,
57 connectivityType, false, types, ifaces);
61 TEST(ConstructResourceTest, ConstructResourceObject)
63 EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string("")));
66 TEST(ResourceGetTest, ResourceGetForValidUri)
68 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
69 EXPECT_TRUE(resource != NULL);
70 EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
74 TEST(ResourceGetTest, ResourceGetForBadUri)
76 OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
77 EXPECT_TRUE(resource != NULL);
78 EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
81 TEST(ResourceGetTest, ResourceGetWithHighQos)
83 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
84 EXPECT_TRUE(resource != NULL);
85 EXPECT_EQ(OC_STACK_OK,
86 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos));
89 TEST(ResourceGetTest, ResourceGetWithLowQos)
91 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
92 EXPECT_TRUE(resource != NULL);
93 EXPECT_EQ(OC_STACK_OK,
94 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos));
97 TEST(ResourceGetTest, ResourceGetWithMidQos)
99 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
100 EXPECT_TRUE(resource != NULL);
101 EXPECT_EQ(OC_STACK_OK,
102 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos));
105 TEST(ResourceGetTest, ResourceGetWithNaQos)
107 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
108 EXPECT_TRUE(resource != NULL);
109 EXPECT_EQ(OC_STACK_OK,
110 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
113 TEST(ResourceGetTest, ResourceGetWithRTIFNaQos)
115 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
116 EXPECT_TRUE(resource != NULL);
117 EXPECT_EQ(OC_STACK_OK,
118 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
119 QualityOfService::NaQos));
122 TEST(ResourceGetTest, ResourceGetWithNullResourceType)
124 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
125 EXPECT_TRUE(resource != NULL);
127 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
128 QualityOfService::NaQos));
132 TEST(ResourceGetTest, ResourceGetWithNullResourceInterface)
134 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
135 EXPECT_TRUE(resource != NULL);
137 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
140 TEST(ResourceGetTest, ResourceGetWithTypeAndInterface)
142 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
143 EXPECT_TRUE(resource != NULL);
144 EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
148 TEST(ResourcePostTest, ResourcePostValidConfiguration)
150 OCRepresentation rep;
151 QueryParamsMap query = {};
152 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
153 EXPECT_TRUE(resource != NULL);
154 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
157 TEST(ResourcePostTest, ResourcePostWithNaQos)
159 OCRepresentation rep;
160 QueryParamsMap query = {};
161 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
162 EXPECT_TRUE(resource != NULL);
163 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
166 TEST(ResourcePostTest, ResourcePostWithMidQos)
168 OCRepresentation rep;
169 QueryParamsMap query = {};
170 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
171 EXPECT_TRUE(resource != NULL);
172 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
175 TEST(ResourcePostTest, ResourcePostWithLowQos)
177 OCRepresentation rep;
178 QueryParamsMap query = {};
179 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
180 EXPECT_TRUE(resource != NULL);
181 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
184 TEST(ResourcePostTest, ResourcePostWithHighQos)
186 OCRepresentation rep;
187 QueryParamsMap query = {};
188 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
189 EXPECT_TRUE(resource != NULL);
190 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos));
194 TEST(ResourcePutTest, ResourcePutForValid)
196 OCRepresentation rep;
197 QueryParamsMap query = {};
198 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
199 EXPECT_TRUE(resource != NULL);
200 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut));
203 TEST(ResourcePutTest, ResourcePutWithNaQos)
205 OCRepresentation rep;
206 QueryParamsMap query = {};
207 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
208 EXPECT_TRUE(resource != NULL);
209 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
212 TEST(ResourcePutTest, ResourcePutWithLowQos)
214 OCRepresentation rep;
215 QueryParamsMap query = {};
216 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
217 EXPECT_TRUE(resource != NULL);
218 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
221 TEST(ResourcePutTest, ResourcePutWithMidQos)
223 OCRepresentation rep;
224 QueryParamsMap query = {};
225 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
226 EXPECT_TRUE(resource != NULL);
227 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
230 TEST(ResourcePutTest, ResourcePutWithHighQos)
232 OCRepresentation rep;
233 QueryParamsMap query = {};
234 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
235 EXPECT_TRUE(resource != NULL);
236 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
239 TEST(ResourcePutTest, ResourcePutWithTypeAndInterface)
241 OCRepresentation rep;
242 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
243 EXPECT_TRUE(resource != NULL);
244 EXPECT_EQ(OC_STACK_OK,
245 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
248 TEST(ResourcePutTest, ResourcePutWithNullType)
250 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
251 EXPECT_TRUE(resource != NULL);
252 OCRepresentation rep;
254 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
255 HeaderOptions headerOptions = {};
256 onGetPut(headerOptions, rep, OC_STACK_OK);
259 TEST(ResourcePutTest, ResourcePutWithNullInterface)
261 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
262 EXPECT_TRUE(resource != NULL);
263 OCRepresentation rep;
264 EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
268 TEST(ResourceObserveTest, ResourceObserveValidUri)
270 OCResource::Ptr resource =
271 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
272 EXPECT_TRUE(resource != NULL);
273 QueryParamsMap query = {};
274 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
277 TEST(ResourceObserveTest, ResourceObserveLoQos)
279 QueryParamsMap query = {};
280 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
281 EXPECT_TRUE(resource != NULL);
282 EXPECT_EQ(OC_STACK_OK,
283 resource->observe(ObserveType::ObserveAll, query, &onObserve,
284 QualityOfService::LowQos));
287 TEST(ResourceObserveTest, ResourceObserveNaQos)
289 QueryParamsMap query = {};
290 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
291 EXPECT_TRUE(resource != NULL);
292 EXPECT_EQ(OC_STACK_OK,
293 resource->observe(ObserveType::ObserveAll, query, &onObserve,
294 QualityOfService::NaQos));
297 TEST(ResourceObserveTest, ResourceObserveMidQos)
299 QueryParamsMap query = {};
300 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
301 EXPECT_TRUE(resource != NULL);
302 EXPECT_EQ(OC_STACK_OK,
303 resource->observe(ObserveType::ObserveAll, query, &onObserve,
304 QualityOfService::MidQos));
307 TEST(ResourceObserveTest, ResourceObserveHighQos)
309 QueryParamsMap query = {};
310 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
311 EXPECT_TRUE(resource != NULL);
312 EXPECT_EQ(OC_STACK_OK,
313 resource->observe(ObserveType::ObserveAll, query, &onObserve,
314 QualityOfService::HighQos));
317 TEST(ResourceObserveTest, ResourceCancelObserveValidUri)
319 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
320 EXPECT_TRUE(resource != NULL);
321 QueryParamsMap query = {};
322 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
323 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
326 TEST(ResourceObserveTest, ResourceCancelObserveWithNaQos)
328 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
329 EXPECT_TRUE(resource != NULL);
330 QueryParamsMap query = {};
331 EXPECT_EQ(OC_STACK_OK,
332 resource->observe(ObserveType::ObserveAll, query, &onObserve,
333 QualityOfService::NaQos));
334 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
337 TEST(ResourceObserveTest, ResourceCancelObserveWithLowQos)
339 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
340 EXPECT_TRUE(resource != NULL);
341 QueryParamsMap query = {};
342 EXPECT_EQ(OC_STACK_OK,
343 resource->observe(ObserveType::ObserveAll, query, &onObserve,
344 QualityOfService::LowQos));
345 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
348 TEST(ResourceObserveTest, ResourceCancelObserveWithMidQos)
350 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
351 EXPECT_TRUE(resource != NULL);
352 QueryParamsMap query = {};
353 EXPECT_EQ(OC_STACK_OK,
354 resource->observe(ObserveType::ObserveAll, query, &onObserve,
355 QualityOfService::MidQos));
356 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
359 TEST(ResourceObserveTest, ResourceCancelObserveWithHighQos)
361 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
362 EXPECT_TRUE(resource != NULL);
363 QueryParamsMap query = {};
364 EXPECT_EQ(OC_STACK_OK,
365 resource->observe(ObserveType::ObserveAll, query, &onObserve,
366 QualityOfService::HighQos));
367 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
371 TEST(DeleteResourceTest, DeleteResourceValidUri)
373 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
374 EXPECT_TRUE(resource != NULL);
375 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
378 TEST(DeleteResourceTest, DeleteResourceWithLowQos)
380 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
381 EXPECT_TRUE(resource != NULL);
382 EXPECT_EQ(OC_STACK_OK,
383 resource->deleteResource(&deleteResponse, QualityOfService::LowQos));
386 TEST(DeleteResourceTest, DeleteResourceWithMidQos)
388 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
389 EXPECT_TRUE(resource != NULL);
390 EXPECT_EQ(OC_STACK_OK,
391 resource->deleteResource(&deleteResponse, QualityOfService::MidQos));
394 TEST(DeleteResourceTest, DeleteResourceWithHighQos)
396 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
397 EXPECT_TRUE(resource != NULL);
398 EXPECT_EQ(OC_STACK_OK,
399 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
401 TEST(DeleteResourceTest, DeleteResourceWithNaQos)
403 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
404 EXPECT_TRUE(resource != NULL);
405 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
408 //GetResourceInterfaces Test
409 TEST(GetResourceInterfacesTest, GetResourceInterfaces)
411 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
412 EXPECT_TRUE(resource != NULL);
413 EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
416 //GetResourceTypes Test
417 TEST(GetResourceTypesTest, GetResourceTypes)
419 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
420 EXPECT_TRUE(resource != NULL);
421 EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
427 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
428 EXPECT_TRUE(resource != NULL);
429 EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
435 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
436 EXPECT_TRUE(resource != NULL);
437 EXPECT_TRUE(resource->uri() == "/resource");
440 //ConnectivityType Test
441 TEST(ConnectivityTypeTest, ConnectivityType)
443 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
444 EXPECT_TRUE(resource != NULL);
445 EXPECT_TRUE(resource->connectivityType() == OC_WIFI);
449 TEST(IsObservableTest, isObservable)
451 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
452 EXPECT_TRUE(resource != NULL);
453 EXPECT_FALSE(resource->isObservable());
459 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
460 EXPECT_TRUE(resource != NULL);
461 EXPECT_TRUE(resource->sid() == "0");
464 //UniqueIdentifier Test
465 TEST(UniqueIdentifierTest, uniqueIdentifier)
467 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
468 EXPECT_TRUE(resource != NULL);
469 std::ostringstream ss;
470 ss << resource->uniqueIdentifier();
471 EXPECT_TRUE(ss.str() == "0/resource");
474 // HeaderOptions Test
475 TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
477 const uint16_t API_VERSION = 2048;
478 const uint16_t TOKEN = 3000;
479 HeaderOptions headerOptions;
480 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
481 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
482 headerOptions.push_back(apiVersion);
483 headerOptions.push_back(clientToken);
485 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
486 EXPECT_TRUE(resource != NULL);
487 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
488 EXPECT_NO_THROW(resource->unsetHeaderOptions());
491 TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
493 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
494 EXPECT_TRUE(resource != NULL);
495 HeaderOptions headerOptions = {};
496 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
497 EXPECT_NO_THROW(resource->unsetHeaderOptions());