Merge branch 'master' into resource-manipulation
[platform/upstream/iotivity.git] / resource / unittests / OCResourceTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #include <OCPlatform.h>
23 #include <OCApi.h>
24 #include <gtest/gtest.h>
25 #include <string>
26 #include <map>
27
28 namespace OCResourceTest
29 {
30     using namespace OC;
31     // Callbacks
32     void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
33     {
34     }
35
36     void onGetPut(const HeaderOptions&, const OCRepresentation& , const int eCode)
37     {
38         EXPECT_EQ(eCode, OC_STACK_OK);
39     }
40
41     void foundResource(std::shared_ptr<OCResource> )
42     {
43     }
44
45     void deleteResponse(const HeaderOptions&, const int)
46     {
47     }
48
49     //Helper method
50     OCResource::Ptr ConstructResourceObject(std::string host, std::string uri)
51     {
52         OCConnectivityType connectivityType = CT_DEFAULT;
53         std::vector<std::string> types = {"intel.rpost"};
54         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
55
56         auto ret = OCPlatform::constructResourceObject(host, uri,
57                 connectivityType, false, types, ifaces);
58
59         if(!ret)
60         {
61             ADD_FAILURE() << "ConstructResourceObject result was null";
62             throw std::runtime_error("ConstructResourceObject result was null");
63         }
64
65         return ret;
66     }
67
68      //Get Test
69     TEST(ConstructResourceTest, ConstructResourceObject)
70     {
71         EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string("")));
72     }
73
74     TEST(ResourceGetTest, DISABLED_ResourceGetForValidUri)
75     {
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));
79
80     }
81
82     TEST(ResourceGetTest, DISABLED_ResourceGetForBadUri)
83     {
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);
87     }
88
89     TEST(ResourceGetTest, DISABLED_ResourceGetWithHighQos)
90     {
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));
95     }
96
97     TEST(ResourceGetTest, DISABLED_ResourceGetWithLowQos)
98     {
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));
103     }
104
105     TEST(ResourceGetTest, DISABLED_ResourceGetWithMidQos)
106     {
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));
111     }
112
113     TEST(ResourceGetTest, DISABLED_ResourceGetWithNaQos)
114     {
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));
119     }
120
121     TEST(ResourceGetTest, DISABLED_ResourceGetWithRTIFNaQos)
122     {
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));
128     }
129
130     TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceType)
131     {
132         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
133         EXPECT_TRUE(resource != NULL);
134         EXPECT_ANY_THROW(
135                 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
136                         QualityOfService::NaQos));
137     }
138
139
140     TEST(ResourceGetTest, DISABLED_ResourceGetWithNullResourceInterface)
141     {
142         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
143         EXPECT_TRUE(resource != NULL);
144         EXPECT_ANY_THROW(
145                 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
146     }
147
148     TEST(ResourceGetTest, DISABLED_ResourceGetWithTypeAndInterface)
149     {
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));
153     }
154
155     //Post Test
156     TEST(ResourcePostTest, DISABLED_ResourcePostValidConfiguration)
157     {
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));
163     }
164
165     TEST(ResourcePostTest, DISABLED_ResourcePostWithNaQos)
166     {
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));
172     }
173
174     TEST(ResourcePostTest, DISABLED_ResourcePostWithMidQos)
175     {
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));
181     }
182
183     TEST(ResourcePostTest, DISABLED_ResourcePostWithLowQos)
184     {
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));
190     }
191
192     TEST(ResourcePostTest, DISABLED_ResourcePostWithHighQos)
193     {
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));
199     }
200
201     //Put Test
202     TEST(ResourcePutTest, DISABLED_ResourcePutForValid)
203     {
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));
209     }
210
211     TEST(ResourcePutTest, DISABLED_ResourcePutWithNaQos)
212     {
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));
218     }
219
220     TEST(ResourcePutTest, DISABLED_ResourcePutWithLowQos)
221     {
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));
227     }
228
229     TEST(ResourcePutTest, DISABLED_ResourcePutWithMidQos)
230     {
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));
236     }
237
238     TEST(ResourcePutTest, DISABLED_ResourcePutWithHighQos)
239     {
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));
245     }
246
247     TEST(ResourcePutTest, DISABLED_ResourcePutWithTypeAndInterface)
248     {
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));
254     }
255
256     TEST(ResourcePutTest, DISABLED_ResourcePutWithNullType)
257     {
258         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
259         EXPECT_TRUE(resource != NULL);
260         OCRepresentation rep;
261         EXPECT_ANY_THROW(
262                 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
263         HeaderOptions headerOptions;
264         onGetPut(headerOptions, rep, OC_STACK_OK);
265     }
266
267     TEST(ResourcePutTest, DISABLED_ResourcePutWithNullInterface)
268     {
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));
273     }
274
275     // Observer Test
276     TEST(ResourceObserveTest, DISABLED_ResourceObserveValidUri)
277     {
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));
283     }
284
285     TEST(ResourceObserveTest, DISABLED_ResourceObserveLoQos)
286     {
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));
293     }
294
295     TEST(ResourceObserveTest, DISABLED_ResourceObserveNaQos)
296     {
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));
303     }
304
305     TEST(ResourceObserveTest, DISABLED_ResourceObserveMidQos)
306     {
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));
313     }
314
315     TEST(ResourceObserveTest, DISABLED_ResourceObserveHighQos)
316     {
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));
323     }
324
325     TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveValidUri)
326     {
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());
332     }
333
334     TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithNaQos)
335     {
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));
343     }
344
345     TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithLowQos)
346     {
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));
354     }
355
356     TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithMidQos)
357     {
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));
365     }
366
367     TEST(ResourceObserveTest, DISABLED_ResourceCancelObserveWithHighQos)
368     {
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));
376     }
377
378     //DeleteResource
379     TEST(DeleteResourceTest, DISABLED_DeleteResourceValidUri)
380     {
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));
384     }
385
386     TEST(DeleteResourceTest, DISABLED_DeleteResourceWithLowQos)
387     {
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));
392     }
393
394     TEST(DeleteResourceTest, DISABLED_DeleteResourceWithMidQos)
395     {
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));
400     }
401
402     TEST(DeleteResourceTest, DISABLED_DeleteResourceWithHighQos)
403     {
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));
408     }
409     TEST(DeleteResourceTest, DISABLED_DeleteResourceWithNaQos)
410     {
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));
414     }
415
416     //GetResourceInterfaces Test
417     TEST(GetResourceInterfacesTest, GetResourceInterfaces)
418     {
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);
422     }
423
424     //GetResourceTypes Test
425     TEST(GetResourceTypesTest, GetResourceTypes)
426     {
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");
430     }
431
432     // Host Test
433     TEST(HostTest, Host)
434     {
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");
438     }
439
440     //Uri Test
441     TEST(UriTest, Uri)
442     {
443         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
444         EXPECT_TRUE(resource != NULL);
445         EXPECT_TRUE(resource->uri() == "/resource");
446     }
447
448     //ConnectivityType Test
449     TEST(ConnectivityTypeTest, ConnectivityType)
450     {
451         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
452         EXPECT_TRUE(resource != NULL);
453         EXPECT_TRUE(resource->connectivityType() == CT_DEFAULT);
454     }
455
456     //IsObservable Test
457     TEST(IsObservableTest, isObservable)
458     {
459         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
460         EXPECT_TRUE(resource != NULL);
461         EXPECT_FALSE(resource->isObservable());
462     }
463
464     //SID Test
465     TEST(SidTest, sid)
466     {
467         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
468         EXPECT_TRUE(resource != NULL);
469         EXPECT_TRUE(resource->sid() == "");
470     }
471
472     //UniqueIdentifier Test
473     TEST(UniqueIdentifierTest, uniqueIdentifier)
474     {
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");
480     }
481
482     // HeaderOptions Test
483     TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
484     {
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);
492
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());
497     }
498
499     TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
500     {
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());
506     }
507 }
508