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(ResourceGetTest, DISABLED_ResourceGetForValidUri)
76 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
77 EXPECT_TRUE(resource != NULL);
78 EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
82 TEST(ResourceGetTest, DISABLED_ResourceGetForBadUri)
84 OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
85 EXPECT_TRUE(resource != NULL);
86 EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
89 TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos)
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::HighQos));
97 TEST(ResourceGetTest, DISABLED_ResourceGetWithLowQos)
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::LowQos));
105 TEST(ResourceGetTest, DISABLED_ResourceGetWithMidQos)
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::MidQos));
113 TEST(ResourceGetTest, DISABLED_ResourceGetWithNaQos)
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(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
121 TEST(ResourceGetTest, DISABLED_ResourceGetWithRTIFNaQos)
123 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
124 EXPECT_TRUE(resource != NULL);
125 EXPECT_EQ(OC_STACK_OK,
126 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
127 QualityOfService::NaQos));
130 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceType)
132 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
133 EXPECT_TRUE(resource != NULL);
135 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
136 QualityOfService::NaQos));
140 TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceInterface)
142 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
143 EXPECT_TRUE(resource != NULL);
145 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
148 TEST(ResourceGetTest, DISABLED_ResourceGetWithTypeAndInterface)
150 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
151 EXPECT_TRUE(resource != NULL);
152 EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
156 TEST(ResourcePostTest, DISABLED_ResourcePostValidConfiguration)
158 OCRepresentation rep;
159 QueryParamsMap query = {};
160 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
161 EXPECT_TRUE(resource != NULL);
162 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
165 TEST(ResourcePostTest, DISABLED_ResourcePostWithNaQos)
167 OCRepresentation rep;
168 QueryParamsMap query = {};
169 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
170 EXPECT_TRUE(resource != NULL);
171 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
174 TEST(ResourcePostTest, DISABLED_ResourcePostWithMidQos)
176 OCRepresentation rep;
177 QueryParamsMap query = {};
178 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
179 EXPECT_TRUE(resource != NULL);
180 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
183 TEST(ResourcePostTest, DISABLED_ResourcePostWithLowQos)
185 OCRepresentation rep;
186 QueryParamsMap query = {};
187 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
188 EXPECT_TRUE(resource != NULL);
189 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
192 TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos)
194 OCRepresentation rep;
195 QueryParamsMap query = {};
196 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
197 EXPECT_TRUE(resource != NULL);
198 EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos));
202 TEST(ResourcePutTest, DISABLED_ResourcePutForValid)
204 OCRepresentation rep;
205 QueryParamsMap query = {};
206 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
207 EXPECT_TRUE(resource != NULL);
208 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut));
211 TEST(ResourcePutTest, DISABLED_ResourcePutWithNaQos)
213 OCRepresentation rep;
214 QueryParamsMap query = {};
215 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
216 EXPECT_TRUE(resource != NULL);
217 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
220 TEST(ResourcePutTest, DISABLED_ResourcePutWithLowQos)
222 OCRepresentation rep;
223 QueryParamsMap query = {};
224 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
225 EXPECT_TRUE(resource != NULL);
226 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
229 TEST(ResourcePutTest, DISABLED_ResourcePutWithMidQos)
231 OCRepresentation rep;
232 QueryParamsMap query = {};
233 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
234 EXPECT_TRUE(resource != NULL);
235 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
238 TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos)
240 OCRepresentation rep;
241 QueryParamsMap query = {};
242 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
243 EXPECT_TRUE(resource != NULL);
244 EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
247 TEST(ResourcePutTest, DISABLED_ResourcePutWithTypeAndInterface)
249 OCRepresentation rep;
250 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
251 EXPECT_TRUE(resource != NULL);
252 EXPECT_EQ(OC_STACK_OK,
253 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
256 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullType)
258 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
259 EXPECT_TRUE(resource != NULL);
260 OCRepresentation rep;
262 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
263 HeaderOptions headerOptions;
264 onGetPut(headerOptions, rep, OC_STACK_OK);
267 TEST(ResourcePutTest, DISABLED_ResourcePutWithNullInterface)
269 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
270 EXPECT_TRUE(resource != NULL);
271 OCRepresentation rep;
272 EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
276 TEST(ResourceObserveTest, DISABLED_ResourceObserveValidUri)
278 OCResource::Ptr resource =
279 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
280 EXPECT_TRUE(resource != NULL);
281 QueryParamsMap query = {};
282 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
285 TEST(ResourceObserveTest, DISABLED_ResourceObserveLoQos)
287 QueryParamsMap query = {};
288 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
289 EXPECT_TRUE(resource != NULL);
290 EXPECT_EQ(OC_STACK_OK,
291 resource->observe(ObserveType::ObserveAll, query, &onObserve,
292 QualityOfService::LowQos));
295 TEST(ResourceObserveTest, DISABLED_ResourceObserveNaQos)
297 QueryParamsMap query = {};
298 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
299 EXPECT_TRUE(resource != NULL);
300 EXPECT_EQ(OC_STACK_OK,
301 resource->observe(ObserveType::ObserveAll, query, &onObserve,
302 QualityOfService::NaQos));
305 TEST(ResourceObserveTest, DISABLED_ResourceObserveMidQos)
307 QueryParamsMap query = {};
308 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
309 EXPECT_TRUE(resource != NULL);
310 EXPECT_EQ(OC_STACK_OK,
311 resource->observe(ObserveType::ObserveAll, query, &onObserve,
312 QualityOfService::MidQos));
315 TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos)
317 QueryParamsMap query = {};
318 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
319 EXPECT_TRUE(resource != NULL);
320 EXPECT_EQ(OC_STACK_OK,
321 resource->observe(ObserveType::ObserveAll, query, &onObserve,
322 QualityOfService::HighQos));
325 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveValidUri)
327 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
328 EXPECT_TRUE(resource != NULL);
329 QueryParamsMap query = {};
330 EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
331 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
334 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithNaQos)
336 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
337 EXPECT_TRUE(resource != NULL);
338 QueryParamsMap query = {};
339 EXPECT_EQ(OC_STACK_OK,
340 resource->observe(ObserveType::ObserveAll, query, &onObserve,
341 QualityOfService::NaQos));
342 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
345 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithLowQos)
347 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
348 EXPECT_TRUE(resource != NULL);
349 QueryParamsMap query = {};
350 EXPECT_EQ(OC_STACK_OK,
351 resource->observe(ObserveType::ObserveAll, query, &onObserve,
352 QualityOfService::LowQos));
353 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
356 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithMidQos)
358 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
359 EXPECT_TRUE(resource != NULL);
360 QueryParamsMap query = {};
361 EXPECT_EQ(OC_STACK_OK,
362 resource->observe(ObserveType::ObserveAll, query, &onObserve,
363 QualityOfService::MidQos));
364 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
367 TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos)
369 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
370 EXPECT_TRUE(resource != NULL);
371 QueryParamsMap query = {};
372 EXPECT_EQ(OC_STACK_OK,
373 resource->observe(ObserveType::ObserveAll, query, &onObserve,
374 QualityOfService::HighQos));
375 EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
379 TEST(DeleteResourceTest, DISABLED_DeleteResourceValidUri)
381 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
382 EXPECT_TRUE(resource != NULL);
383 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
386 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithLowQos)
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::LowQos));
394 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithMidQos)
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::MidQos));
402 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithHighQos)
404 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
405 EXPECT_TRUE(resource != NULL);
406 EXPECT_EQ(OC_STACK_OK,
407 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
409 TEST(DeleteResourceTest, DISABLED_DeleteResourceWithNaQos)
411 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
412 EXPECT_TRUE(resource != NULL);
413 EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
416 //GetResourceInterfaces Test
417 TEST(GetResourceInterfacesTest, GetResourceInterfaces)
419 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
420 EXPECT_TRUE(resource != NULL);
421 EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
424 //GetResourceTypes Test
425 TEST(GetResourceTypesTest, GetResourceTypes)
427 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
428 EXPECT_TRUE(resource != NULL);
429 EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
435 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
436 EXPECT_TRUE(resource != NULL);
437 EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
443 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
444 EXPECT_TRUE(resource != NULL);
445 EXPECT_TRUE(resource->uri() == "/resource");
448 //ConnectivityType Test
449 TEST(ConnectivityTypeTest, ConnectivityType)
451 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
452 EXPECT_TRUE(resource != NULL);
453 EXPECT_TRUE(resource->connectivityType() == CT_DEFAULT);
457 TEST(IsObservableTest, isObservable)
459 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
460 EXPECT_TRUE(resource != NULL);
461 EXPECT_FALSE(resource->isObservable());
467 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
468 EXPECT_TRUE(resource != NULL);
469 EXPECT_TRUE(resource->sid() == "");
472 //UniqueIdentifier Test
473 TEST(UniqueIdentifierTest, uniqueIdentifier)
475 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
476 EXPECT_TRUE(resource != NULL);
477 std::ostringstream ss;
478 ss << resource->uniqueIdentifier();
479 EXPECT_TRUE(ss.str() == "/resource");
482 // HeaderOptions Test
483 TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
485 const uint16_t API_VERSION = 2048;
486 const uint16_t TOKEN = 3000;
487 HeaderOptions headerOptions;
488 HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
489 HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
490 headerOptions.push_back(apiVersion);
491 headerOptions.push_back(clientToken);
493 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
494 EXPECT_TRUE(resource != NULL);
495 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
496 EXPECT_NO_THROW(resource->unsetHeaderOptions());
499 TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
501 OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
502 EXPECT_TRUE(resource != NULL);
503 HeaderOptions headerOptions;
504 EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
505 EXPECT_NO_THROW(resource->unsetHeaderOptions());