Fixed memory issues related to AddClientCB
[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
26 namespace OCResourceTest
27 {
28     using namespace OC;
29     // Callbacks
30
31     void onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
32                         const int& eCode, const int& sequenceNumber)
33     {
34     }
35
36     void onGetPut(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
37     {
38     }
39
40     void foundResource(std::shared_ptr<OCResource> resource)
41     {
42
43     }
44
45     //Helper method
46     OCResource::Ptr ConstructResourceObject(std::string host, std::string uri)
47     {
48         OCConnectivityType connectivityType = OC_WIFI;
49         std::vector<std::string> types = {"intel.rpost"};
50         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
51
52         return OCPlatform::constructResourceObject(host, uri,
53                 connectivityType, false, types, ifaces);
54     }
55
56     //ConstructResourceTest
57     TEST(ConstructResourceTest, ConstructResourceObject)
58     {
59         EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string("")));
60     }
61
62     TEST(ResourceGetTest, ResourceGetForValidUri)
63     {
64         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
65         if(resource)
66         {
67             QueryParamsMap test;
68             EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
69         }
70     }
71
72     TEST(ResourceGetTest, ResourceGetForBadUri)
73     {
74         OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
75         if(resource)
76         {
77             QueryParamsMap test;
78             EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
79         }
80     }
81
82     TEST(ResourcePutTest, ResourcePutForValid)
83     {
84         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
85         if(resource)
86         {
87             QueryParamsMap test;
88             OCRepresentation rep;
89             EXPECT_EQ(OC_STACK_OK, resource->put(rep, test, &onGetPut));
90         }
91     }
92
93     TEST(ResourcePostTest, ResourcePostValidConfiguration)
94     {
95         PlatformConfig cfg;
96         OCPlatform::Configure(cfg);
97
98         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
99         if(resource)
100         {
101             OCRepresentation rep;
102             QueryParamsMap test;
103             EXPECT_EQ(OC_STACK_OK, resource->post(rep, test, &onGetPut));
104         }
105     }
106
107     TEST(ResourceObserveTest, ResourceObserveValidUri)
108     {
109         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
110         if(resource)
111         {
112             QueryParamsMap test;
113             OCRepresentation rep;
114             EXPECT_EQ(OC_STACK_OK,resource->observe(ObserveType::ObserveAll, test, &onObserve));
115         }
116     }
117
118 }
119