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(ResourceGetTest, DISABLED_ResourceGetForValidUri)
91 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
92 EXPECT_TRUE(resource != NULL);
93 EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
97 TEST(ResourceGetTest, DISABLED_ResourceGetForBadUri)
99 OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
100 EXPECT_TRUE(resource != NULL);
101 EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
104 TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos)
106 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
107 EXPECT_TRUE(resource != NULL);
108 EXPECT_EQ(OC_STACK_OK,
109 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos));
112 TEST(ResourceGetTest, DISABLED_ResourceGetWithLowQos)
114 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
115 EXPECT_TRUE(resource != NULL);
116 EXPECT_EQ(OC_STACK_OK,
117 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos));
120 TEST(ResourceGetTest, DISABLED_ResourceGetWithMidQos)
122 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
123 EXPECT_TRUE(resource != NULL);
124 EXPECT_EQ(OC_STACK_OK,
125 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos));
128 TEST(ResourceGetTest, DISABLED_ResourceGetWithNaQos)
130 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
131 EXPECT_TRUE(resource != NULL);
132 EXPECT_EQ(OC_STACK_OK,
133 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
136 TEST(ResourceGetTest, DISABLED_ResourceGetWithRTIFNaQos)
138 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
139 EXPECT_TRUE(resource != NULL);
140 EXPECT_EQ(OC_STACK_OK,
141 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
142 QualityOfService::NaQos));
145 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceType)
147 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
148 EXPECT_TRUE(resource != NULL);
150 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
151 QualityOfService::NaQos));
155 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceInterface)
157 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
158 EXPECT_TRUE(resource != NULL);
160 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
163 TEST(ResourceGetTest, DISABLED_ResourceGetWithTypeAndInterface)
165 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
166 EXPECT_TRUE(resource != NULL);
167 EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
171 TEST(ResourcePostTest, DISABLED_ResourcePostValidConfiguration)
173 OCRepresentation rep;
174 QueryParamsMap query = {};
175 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
176 EXPECT_TRUE(resource != NULL);
177 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
180 TEST(ResourcePostTest, DISABLED_ResourcePostWithNaQos)
182 OCRepresentation rep;
183 QueryParamsMap query = {};
184 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
185 EXPECT_TRUE(resource != NULL);
186 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
189 TEST(ResourcePostTest, DISABLED_ResourcePostWithMidQos)
191 OCRepresentation rep;
192 QueryParamsMap query = {};
193 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
194 EXPECT_TRUE(resource != NULL);
195 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
198 TEST(ResourcePostTest, DISABLED_ResourcePostWithLowQos)
200 OCRepresentation rep;
201 QueryParamsMap query = {};
202 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
203 EXPECT_TRUE(resource != NULL);
204 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
207 TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos)
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->post(rep, query, &onGetPut, QualityOfService::HighQos));
217 TEST(ResourcePutTest, DISABLED_ResourcePutForValid)
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->put(rep, query, &onGetPut));
226 TEST(ResourcePutTest, DISABLED_ResourcePutWithNaQos)
228 OCRepresentation rep;
229 QueryParamsMap query = {};
230 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
231 EXPECT_TRUE(resource != NULL);
232 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
235 TEST(ResourcePutTest, DISABLED_ResourcePutWithLowQos)
237 OCRepresentation rep;
238 QueryParamsMap query = {};
239 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
240 EXPECT_TRUE(resource != NULL);
241 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
244 TEST(ResourcePutTest, DISABLED_ResourcePutWithMidQos)
246 OCRepresentation rep;
247 QueryParamsMap query = {};
248 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
249 EXPECT_TRUE(resource != NULL);
250 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
253 TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos)
255 OCRepresentation rep;
256 QueryParamsMap query = {};
257 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
258 EXPECT_TRUE(resource != NULL);
259 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
262 TEST(ResourcePutTest, DISABLED_ResourcePutWithTypeAndInterface)
264 OCRepresentation rep;
265 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
266 EXPECT_TRUE(resource != NULL);
267 EXPECT_EQ(OC_STACK_OK,
268 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
271 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullType)
273 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
274 EXPECT_TRUE(resource != NULL);
275 OCRepresentation rep;
277 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
278 HeaderOptions headerOptions;
279 onGetPut(headerOptions, rep, OC_STACK_OK);
282 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullInterface)
284 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
285 EXPECT_TRUE(resource != NULL);
286 OCRepresentation rep;
287 EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
291 TEST(ResourceObserveTest, DISABLED_ResourceObserveValidUri)
293 OCResource::Ptr resource =
294 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
295 EXPECT_TRUE(resource != NULL);
296 QueryParamsMap query = {};
297 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
300 TEST(ResourceObserveTest, DISABLED_ResourceObserveLoQos)
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::LowQos));
310 TEST(ResourceObserveTest, DISABLED_ResourceObserveNaQos)
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::NaQos));
320 TEST(ResourceObserveTest, DISABLED_ResourceObserveMidQos)
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::MidQos));
330 TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos)
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::HighQos));
340 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveValidUri)
342 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
343 EXPECT_TRUE(resource != NULL);
344 QueryParamsMap query = {};
345 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
346 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
349 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithNaQos)
351 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
352 EXPECT_TRUE(resource != NULL);
353 QueryParamsMap query = {};
354 EXPECT_EQ(OC_STACK_OK,
355 resource->observe(ObserveType::ObserveAll, query, &onObserve,
356 QualityOfService::NaQos));
357 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
360 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithLowQos)
362 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
363 EXPECT_TRUE(resource != NULL);
364 QueryParamsMap query = {};
365 EXPECT_EQ(OC_STACK_OK,
366 resource->observe(ObserveType::ObserveAll, query, &onObserve,
367 QualityOfService::LowQos));
368 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
371 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithMidQos)
373 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
374 EXPECT_TRUE(resource != NULL);
375 QueryParamsMap query = {};
376 EXPECT_EQ(OC_STACK_OK,
377 resource->observe(ObserveType::ObserveAll, query, &onObserve,
378 QualityOfService::MidQos));
379 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
382 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos)
384 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
385 EXPECT_TRUE(resource != NULL);
386 QueryParamsMap query = {};
387 EXPECT_EQ(OC_STACK_OK,
388 resource->observe(ObserveType::ObserveAll, query, &onObserve,
389 QualityOfService::HighQos));
390 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
394 TEST(DeleteResourceTest, DISABLED_DeleteResourceValidUri)
396 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
397 EXPECT_TRUE(resource != NULL);
398 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
401 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithLowQos)
403 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
404 EXPECT_TRUE(resource != NULL);
405 EXPECT_EQ(OC_STACK_OK,
406 resource->deleteResource(&deleteResponse, QualityOfService::LowQos));
409 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithMidQos)
411 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
412 EXPECT_TRUE(resource != NULL);
413 EXPECT_EQ(OC_STACK_OK,
414 resource->deleteResource(&deleteResponse, QualityOfService::MidQos));
417 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithHighQos)
419 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
420 EXPECT_TRUE(resource != NULL);
421 EXPECT_EQ(OC_STACK_OK,
422 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
424 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithNaQos)
426 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
427 EXPECT_TRUE(resource != NULL);
428 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
431 //GetResourceInterfaces Test
432 TEST(GetResourceInterfacesTest, GetResourceInterfaces)
434 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
435 EXPECT_TRUE(resource != NULL);
436 EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
439 //GetResourceTypes Test
440 TEST(GetResourceTypesTest, GetResourceTypes)
442 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
443 EXPECT_TRUE(resource != NULL);
444 EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
450 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
451 EXPECT_TRUE(resource != NULL);
452 EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
458 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
459 EXPECT_TRUE(resource != NULL);
460 EXPECT_TRUE(resource->uri() == "/resource");
463 //ConnectivityType Test
464 TEST(ConnectivityTypeTest, ConnectivityType)
466 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
467 EXPECT_TRUE(resource != NULL);
468 EXPECT_TRUE(resource->connectivityType() == CT_DEFAULT);
472 TEST(IsObservableTest, isObservable)
474 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
475 EXPECT_TRUE(resource != NULL);
476 EXPECT_FALSE(resource->isObservable());
482 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
483 EXPECT_TRUE(resource != NULL);
484 EXPECT_TRUE(resource->sid() == "");
487 //UniqueIdentifier Test
488 TEST(UniqueIdentifierTest, uniqueIdentifier)
490 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
491 EXPECT_TRUE(resource != NULL);
492 std::ostringstream ss;
493 ss << resource->uniqueIdentifier();
494 EXPECT_TRUE(ss.str() == "/resource");
497 // HeaderOptions Test
498 TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
500 const uint16_t API_VERSION = 2048;
501 const uint16_t TOKEN = 3000;
502 HeaderOptions headerOptions;
503 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
504 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
505 headerOptions.push_back(apiVersion);
506 headerOptions.push_back(clientToken);
508 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
509 EXPECT_TRUE(resource != NULL);
510 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
511 EXPECT_NO_THROW(resource->unsetHeaderOptions());
514 TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
516 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
517 EXPECT_TRUE(resource != NULL);
518 HeaderOptions headerOptions;
519 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
520 EXPECT_NO_THROW(resource->unsetHeaderOptions());