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 = CT_DEFAULT;
53 std::vector<std::string> types = {"intel.rpost"};
54 std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
56 auto ret = OCPlatform::constructResourceObject(host, uri,
57 connectivityType, false, types, ifaces);
61 ADD_FAILURE() << "ConstructResourceObject result was null";
62 throw std::runtime_error("ConstructResourceObject result was null");
69 TEST(ConstructResourceTest, ConstructResourceObject)
71 EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string("")));
74 TEST(ConstructResourceTest, ConstructResourceObjectWithoutPortNumber)
76 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2", "/resource"));
79 TEST(ResourceGetTest, DISABLED_ResourceGetForValidUri)
81 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
82 EXPECT_TRUE(resource != NULL);
83 EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
87 TEST(ResourceGetTest, DISABLED_ResourceGetForBadUri)
89 OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
90 EXPECT_TRUE(resource != NULL);
91 EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
94 TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos)
96 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
97 EXPECT_TRUE(resource != NULL);
98 EXPECT_EQ(OC_STACK_OK,
99 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos));
102 TEST(ResourceGetTest, DISABLED_ResourceGetWithLowQos)
104 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
105 EXPECT_TRUE(resource != NULL);
106 EXPECT_EQ(OC_STACK_OK,
107 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos));
110 TEST(ResourceGetTest, DISABLED_ResourceGetWithMidQos)
112 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
113 EXPECT_TRUE(resource != NULL);
114 EXPECT_EQ(OC_STACK_OK,
115 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos));
118 TEST(ResourceGetTest, DISABLED_ResourceGetWithNaQos)
120 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
121 EXPECT_TRUE(resource != NULL);
122 EXPECT_EQ(OC_STACK_OK,
123 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
126 TEST(ResourceGetTest, DISABLED_ResourceGetWithRTIFNaQos)
128 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
129 EXPECT_TRUE(resource != NULL);
130 EXPECT_EQ(OC_STACK_OK,
131 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
132 QualityOfService::NaQos));
135 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceType)
137 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
138 EXPECT_TRUE(resource != NULL);
140 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
141 QualityOfService::NaQos));
145 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceInterface)
147 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
148 EXPECT_TRUE(resource != NULL);
150 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
153 TEST(ResourceGetTest, DISABLED_ResourceGetWithTypeAndInterface)
155 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
156 EXPECT_TRUE(resource != NULL);
157 EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
161 TEST(ResourcePostTest, DISABLED_ResourcePostValidConfiguration)
163 OCRepresentation rep;
164 QueryParamsMap query = {};
165 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
166 EXPECT_TRUE(resource != NULL);
167 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
170 TEST(ResourcePostTest, DISABLED_ResourcePostWithNaQos)
172 OCRepresentation rep;
173 QueryParamsMap query = {};
174 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
175 EXPECT_TRUE(resource != NULL);
176 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
179 TEST(ResourcePostTest, DISABLED_ResourcePostWithMidQos)
181 OCRepresentation rep;
182 QueryParamsMap query = {};
183 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
184 EXPECT_TRUE(resource != NULL);
185 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
188 TEST(ResourcePostTest, DISABLED_ResourcePostWithLowQos)
190 OCRepresentation rep;
191 QueryParamsMap query = {};
192 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
193 EXPECT_TRUE(resource != NULL);
194 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
197 TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos)
199 OCRepresentation rep;
200 QueryParamsMap query = {};
201 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
202 EXPECT_TRUE(resource != NULL);
203 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos));
207 TEST(ResourcePutTest, DISABLED_ResourcePutForValid)
209 OCRepresentation rep;
210 QueryParamsMap query = {};
211 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
212 EXPECT_TRUE(resource != NULL);
213 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut));
216 TEST(ResourcePutTest, DISABLED_ResourcePutWithNaQos)
218 OCRepresentation rep;
219 QueryParamsMap query = {};
220 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
221 EXPECT_TRUE(resource != NULL);
222 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
225 TEST(ResourcePutTest, DISABLED_ResourcePutWithLowQos)
227 OCRepresentation rep;
228 QueryParamsMap query = {};
229 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
230 EXPECT_TRUE(resource != NULL);
231 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
234 TEST(ResourcePutTest, DISABLED_ResourcePutWithMidQos)
236 OCRepresentation rep;
237 QueryParamsMap query = {};
238 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
239 EXPECT_TRUE(resource != NULL);
240 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
243 TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos)
245 OCRepresentation rep;
246 QueryParamsMap query = {};
247 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
248 EXPECT_TRUE(resource != NULL);
249 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
252 TEST(ResourcePutTest, DISABLED_ResourcePutWithTypeAndInterface)
254 OCRepresentation rep;
255 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
256 EXPECT_TRUE(resource != NULL);
257 EXPECT_EQ(OC_STACK_OK,
258 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
261 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullType)
263 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
264 EXPECT_TRUE(resource != NULL);
265 OCRepresentation rep;
267 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
268 HeaderOptions headerOptions;
269 onGetPut(headerOptions, rep, OC_STACK_OK);
272 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullInterface)
274 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
275 EXPECT_TRUE(resource != NULL);
276 OCRepresentation rep;
277 EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
281 TEST(ResourceObserveTest, DISABLED_ResourceObserveValidUri)
283 OCResource::Ptr resource =
284 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
285 EXPECT_TRUE(resource != NULL);
286 QueryParamsMap query = {};
287 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
290 TEST(ResourceObserveTest, DISABLED_ResourceObserveLoQos)
292 QueryParamsMap query = {};
293 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
294 EXPECT_TRUE(resource != NULL);
295 EXPECT_EQ(OC_STACK_OK,
296 resource->observe(ObserveType::ObserveAll, query, &onObserve,
297 QualityOfService::LowQos));
300 TEST(ResourceObserveTest, DISABLED_ResourceObserveNaQos)
302 QueryParamsMap query = {};
303 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
304 EXPECT_TRUE(resource != NULL);
305 EXPECT_EQ(OC_STACK_OK,
306 resource->observe(ObserveType::ObserveAll, query, &onObserve,
307 QualityOfService::NaQos));
310 TEST(ResourceObserveTest, DISABLED_ResourceObserveMidQos)
312 QueryParamsMap query = {};
313 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
314 EXPECT_TRUE(resource != NULL);
315 EXPECT_EQ(OC_STACK_OK,
316 resource->observe(ObserveType::ObserveAll, query, &onObserve,
317 QualityOfService::MidQos));
320 TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos)
322 QueryParamsMap query = {};
323 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
324 EXPECT_TRUE(resource != NULL);
325 EXPECT_EQ(OC_STACK_OK,
326 resource->observe(ObserveType::ObserveAll, query, &onObserve,
327 QualityOfService::HighQos));
330 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveValidUri)
332 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
333 EXPECT_TRUE(resource != NULL);
334 QueryParamsMap query = {};
335 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
336 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
339 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithNaQos)
341 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
342 EXPECT_TRUE(resource != NULL);
343 QueryParamsMap query = {};
344 EXPECT_EQ(OC_STACK_OK,
345 resource->observe(ObserveType::ObserveAll, query, &onObserve,
346 QualityOfService::NaQos));
347 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
350 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithLowQos)
352 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
353 EXPECT_TRUE(resource != NULL);
354 QueryParamsMap query = {};
355 EXPECT_EQ(OC_STACK_OK,
356 resource->observe(ObserveType::ObserveAll, query, &onObserve,
357 QualityOfService::LowQos));
358 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
361 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithMidQos)
363 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
364 EXPECT_TRUE(resource != NULL);
365 QueryParamsMap query = {};
366 EXPECT_EQ(OC_STACK_OK,
367 resource->observe(ObserveType::ObserveAll, query, &onObserve,
368 QualityOfService::MidQos));
369 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
372 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos)
374 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
375 EXPECT_TRUE(resource != NULL);
376 QueryParamsMap query = {};
377 EXPECT_EQ(OC_STACK_OK,
378 resource->observe(ObserveType::ObserveAll, query, &onObserve,
379 QualityOfService::HighQos));
380 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
384 TEST(DeleteResourceTest, DISABLED_DeleteResourceValidUri)
386 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
387 EXPECT_TRUE(resource != NULL);
388 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
391 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithLowQos)
393 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
394 EXPECT_TRUE(resource != NULL);
395 EXPECT_EQ(OC_STACK_OK,
396 resource->deleteResource(&deleteResponse, QualityOfService::LowQos));
399 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithMidQos)
401 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
402 EXPECT_TRUE(resource != NULL);
403 EXPECT_EQ(OC_STACK_OK,
404 resource->deleteResource(&deleteResponse, QualityOfService::MidQos));
407 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithHighQos)
409 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
410 EXPECT_TRUE(resource != NULL);
411 EXPECT_EQ(OC_STACK_OK,
412 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
414 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithNaQos)
416 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
417 EXPECT_TRUE(resource != NULL);
418 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
421 //GetResourceInterfaces Test
422 TEST(GetResourceInterfacesTest, GetResourceInterfaces)
424 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
425 EXPECT_TRUE(resource != NULL);
426 EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
429 //GetResourceTypes Test
430 TEST(GetResourceTypesTest, GetResourceTypes)
432 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
433 EXPECT_TRUE(resource != NULL);
434 EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
440 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
441 EXPECT_TRUE(resource != NULL);
442 EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
448 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
449 EXPECT_TRUE(resource != NULL);
450 EXPECT_TRUE(resource->uri() == "/resource");
453 //ConnectivityType Test
454 TEST(ConnectivityTypeTest, ConnectivityType)
456 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
457 EXPECT_TRUE(resource != NULL);
458 EXPECT_TRUE(resource->connectivityType() == CT_DEFAULT);
462 TEST(IsObservableTest, isObservable)
464 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
465 EXPECT_TRUE(resource != NULL);
466 EXPECT_FALSE(resource->isObservable());
472 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
473 EXPECT_TRUE(resource != NULL);
474 EXPECT_TRUE(resource->sid() == "");
477 //UniqueIdentifier Test
478 TEST(UniqueIdentifierTest, uniqueIdentifier)
480 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
481 EXPECT_TRUE(resource != NULL);
482 std::ostringstream ss;
483 ss << resource->uniqueIdentifier();
484 EXPECT_TRUE(ss.str() == "/resource");
487 // HeaderOptions Test
488 TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
490 const uint16_t API_VERSION = 2048;
491 const uint16_t TOKEN = 3000;
492 HeaderOptions headerOptions;
493 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
494 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
495 headerOptions.push_back(apiVersion);
496 headerOptions.push_back(clientToken);
498 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
499 EXPECT_TRUE(resource != NULL);
500 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
501 EXPECT_NO_THROW(resource->unsetHeaderOptions());
504 TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
506 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
507 EXPECT_TRUE(resource != NULL);
508 HeaderOptions headerOptions;
509 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
510 EXPECT_NO_THROW(resource->unsetHeaderOptions());