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(ConstructResourceTest, ConstructResourceObjectInvalidHost)
81 EXPECT_ANY_THROW(ConstructResourceObject("192.168.1.2:5000", "/resource"));
84 TEST(ConstructResourceTest, ConstructResourceObjectInvalidHost2)
86 EXPECT_ANY_THROW(ConstructResourceObject("coap://:5000", "/resource"));
89 TEST(ConstructResourceTest, ConstructResourceObjectInvalidUri)
91 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2:5000", "/"));
94 TEST(ConstructResourceTest, ConstructResourceObjectInvalidUri2)
96 EXPECT_ANY_THROW(ConstructResourceObject("coap://192.168.1.2:5000", "resource"));
99 TEST(ResourceGetTest, DISABLED_ResourceGetForValidUri)
101 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
102 EXPECT_TRUE(resource != NULL);
103 EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
107 TEST(ResourceGetTest, DISABLED_ResourceGetForBadUri)
109 OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
110 EXPECT_TRUE(resource != NULL);
111 EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
114 TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos)
116 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
117 EXPECT_TRUE(resource != NULL);
118 EXPECT_EQ(OC_STACK_OK,
119 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos));
122 TEST(ResourceGetTest, DISABLED_ResourceGetWithLowQos)
124 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
125 EXPECT_TRUE(resource != NULL);
126 EXPECT_EQ(OC_STACK_OK,
127 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos));
130 TEST(ResourceGetTest, DISABLED_ResourceGetWithMidQos)
132 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
133 EXPECT_TRUE(resource != NULL);
134 EXPECT_EQ(OC_STACK_OK,
135 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos));
138 TEST(ResourceGetTest, DISABLED_ResourceGetWithNaQos)
140 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
141 EXPECT_TRUE(resource != NULL);
142 EXPECT_EQ(OC_STACK_OK,
143 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
146 TEST(ResourceGetTest, DISABLED_ResourceGetWithRTIFNaQos)
148 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
149 EXPECT_TRUE(resource != NULL);
150 EXPECT_EQ(OC_STACK_OK,
151 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
152 QualityOfService::NaQos));
155 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceType)
157 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
158 EXPECT_TRUE(resource != NULL);
160 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
161 QualityOfService::NaQos));
165 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceInterface)
167 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
168 EXPECT_TRUE(resource != NULL);
170 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
173 TEST(ResourceGetTest, DISABLED_ResourceGetWithTypeAndInterface)
175 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
176 EXPECT_TRUE(resource != NULL);
177 EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
181 TEST(ResourcePostTest, DISABLED_ResourcePostValidConfiguration)
183 OCRepresentation rep;
184 QueryParamsMap query = {};
185 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
186 EXPECT_TRUE(resource != NULL);
187 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
190 TEST(ResourcePostTest, DISABLED_ResourcePostWithNaQos)
192 OCRepresentation rep;
193 QueryParamsMap query = {};
194 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
195 EXPECT_TRUE(resource != NULL);
196 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
199 TEST(ResourcePostTest, DISABLED_ResourcePostWithMidQos)
201 OCRepresentation rep;
202 QueryParamsMap query = {};
203 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
204 EXPECT_TRUE(resource != NULL);
205 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
208 TEST(ResourcePostTest, DISABLED_ResourcePostWithLowQos)
210 OCRepresentation rep;
211 QueryParamsMap query = {};
212 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
213 EXPECT_TRUE(resource != NULL);
214 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
217 TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos)
219 OCRepresentation rep;
220 QueryParamsMap query = {};
221 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
222 EXPECT_TRUE(resource != NULL);
223 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos));
227 TEST(ResourcePutTest, DISABLED_ResourcePutForValid)
229 OCRepresentation rep;
230 QueryParamsMap query = {};
231 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
232 EXPECT_TRUE(resource != NULL);
233 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut));
236 TEST(ResourcePutTest, DISABLED_ResourcePutWithNaQos)
238 OCRepresentation rep;
239 QueryParamsMap query = {};
240 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
241 EXPECT_TRUE(resource != NULL);
242 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
245 TEST(ResourcePutTest, DISABLED_ResourcePutWithLowQos)
247 OCRepresentation rep;
248 QueryParamsMap query = {};
249 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
250 EXPECT_TRUE(resource != NULL);
251 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
254 TEST(ResourcePutTest, DISABLED_ResourcePutWithMidQos)
256 OCRepresentation rep;
257 QueryParamsMap query = {};
258 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
259 EXPECT_TRUE(resource != NULL);
260 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
263 TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos)
265 OCRepresentation rep;
266 QueryParamsMap query = {};
267 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
268 EXPECT_TRUE(resource != NULL);
269 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
272 TEST(ResourcePutTest, DISABLED_ResourcePutWithTypeAndInterface)
274 OCRepresentation rep;
275 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
276 EXPECT_TRUE(resource != NULL);
277 EXPECT_EQ(OC_STACK_OK,
278 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
281 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullType)
283 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
284 EXPECT_TRUE(resource != NULL);
285 OCRepresentation rep;
287 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
288 HeaderOptions headerOptions;
289 onGetPut(headerOptions, rep, OC_STACK_OK);
292 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullInterface)
294 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
295 EXPECT_TRUE(resource != NULL);
296 OCRepresentation rep;
297 EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
301 TEST(ResourceObserveTest, DISABLED_ResourceObserveValidUri)
303 OCResource::Ptr resource =
304 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
305 EXPECT_TRUE(resource != NULL);
306 QueryParamsMap query = {};
307 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
310 TEST(ResourceObserveTest, DISABLED_ResourceObserveLoQos)
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::LowQos));
320 TEST(ResourceObserveTest, DISABLED_ResourceObserveNaQos)
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::NaQos));
330 TEST(ResourceObserveTest, DISABLED_ResourceObserveMidQos)
332 QueryParamsMap query = {};
333 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
334 EXPECT_TRUE(resource != NULL);
335 EXPECT_EQ(OC_STACK_OK,
336 resource->observe(ObserveType::ObserveAll, query, &onObserve,
337 QualityOfService::MidQos));
340 TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos)
342 QueryParamsMap query = {};
343 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
344 EXPECT_TRUE(resource != NULL);
345 EXPECT_EQ(OC_STACK_OK,
346 resource->observe(ObserveType::ObserveAll, query, &onObserve,
347 QualityOfService::HighQos));
350 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveValidUri)
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, resource->observe(ObserveType::ObserveAll, query, &onObserve));
356 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
359 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithNaQos)
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::NaQos));
367 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
370 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithLowQos)
372 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
373 EXPECT_TRUE(resource != NULL);
374 QueryParamsMap query = {};
375 EXPECT_EQ(OC_STACK_OK,
376 resource->observe(ObserveType::ObserveAll, query, &onObserve,
377 QualityOfService::LowQos));
378 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
381 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithMidQos)
383 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
384 EXPECT_TRUE(resource != NULL);
385 QueryParamsMap query = {};
386 EXPECT_EQ(OC_STACK_OK,
387 resource->observe(ObserveType::ObserveAll, query, &onObserve,
388 QualityOfService::MidQos));
389 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
392 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos)
394 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
395 EXPECT_TRUE(resource != NULL);
396 QueryParamsMap query = {};
397 EXPECT_EQ(OC_STACK_OK,
398 resource->observe(ObserveType::ObserveAll, query, &onObserve,
399 QualityOfService::HighQos));
400 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
404 TEST(DeleteResourceTest, DISABLED_DeleteResourceValidUri)
406 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
407 EXPECT_TRUE(resource != NULL);
408 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
411 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithLowQos)
413 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
414 EXPECT_TRUE(resource != NULL);
415 EXPECT_EQ(OC_STACK_OK,
416 resource->deleteResource(&deleteResponse, QualityOfService::LowQos));
419 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithMidQos)
421 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
422 EXPECT_TRUE(resource != NULL);
423 EXPECT_EQ(OC_STACK_OK,
424 resource->deleteResource(&deleteResponse, QualityOfService::MidQos));
427 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithHighQos)
429 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
430 EXPECT_TRUE(resource != NULL);
431 EXPECT_EQ(OC_STACK_OK,
432 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
434 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithNaQos)
436 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
437 EXPECT_TRUE(resource != NULL);
438 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
441 //GetResourceInterfaces Test
442 TEST(GetResourceInterfacesTest, GetResourceInterfaces)
444 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
445 EXPECT_TRUE(resource != NULL);
446 EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
449 //GetResourceTypes Test
450 TEST(GetResourceTypesTest, GetResourceTypes)
452 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
453 EXPECT_TRUE(resource != NULL);
454 EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
460 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
461 EXPECT_TRUE(resource != NULL);
462 EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
468 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
469 EXPECT_TRUE(resource != NULL);
470 EXPECT_TRUE(resource->uri() == "/resource");
473 //ConnectivityType Test
474 TEST(ConnectivityTypeTest, ConnectivityType)
476 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
477 EXPECT_TRUE(resource != NULL);
478 EXPECT_TRUE(resource->connectivityType() == CT_DEFAULT);
482 TEST(IsObservableTest, isObservable)
484 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
485 EXPECT_TRUE(resource != NULL);
486 EXPECT_FALSE(resource->isObservable());
492 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
493 EXPECT_TRUE(resource != NULL);
494 EXPECT_TRUE(resource->sid() == "");
497 //UniqueIdentifier Test
498 TEST(UniqueIdentifierTest, uniqueIdentifier)
500 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
501 EXPECT_TRUE(resource != NULL);
502 std::ostringstream ss;
503 ss << resource->uniqueIdentifier();
504 EXPECT_TRUE(ss.str() == "/resource");
507 // HeaderOptions Test
508 TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
510 const uint16_t API_VERSION = 2048;
511 const uint16_t TOKEN = 3000;
512 HeaderOptions headerOptions;
513 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
514 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
515 headerOptions.push_back(apiVersion);
516 headerOptions.push_back(clientToken);
518 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
519 EXPECT_TRUE(resource != NULL);
520 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
521 EXPECT_NO_THROW(resource->unsetHeaderOptions());
524 TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
526 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
527 EXPECT_TRUE(resource != NULL);
528 HeaderOptions headerOptions;
529 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
530 EXPECT_NO_THROW(resource->unsetHeaderOptions());