Add C++ Unit tests
[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 uri)
47     {
48         std::vector<std::string> types = {"intel.rpost"};
49         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
50         return OCPlatform::constructResourceObject(std::string(""), uri,
51                                 false, types, ifaces);
52     }
53
54     //ConstructResourceTest
55     TEST(ConstructResourceTest, ConstructResourceObject)
56     {
57         EXPECT_ANY_THROW(ConstructResourceObject(std::string("")));
58     }
59
60     //ResourceGetTest
61     TEST(ResourceGetTest, ResourceGetForInvalidUri)
62     {
63         OCResource::Ptr resource = ConstructResourceObject("192.168.1.2:5000");
64         if(resource)
65         {
66             QueryParamsMap test;
67             EXPECT_ANY_THROW(resource->get(test, &onGetPut));
68         }
69     }
70
71     TEST(ResourceGetTest, ResourceGetForValidUri)
72     {
73         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000");
74         if(resource)
75         {
76             QueryParamsMap test;
77             EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
78         }
79     }
80
81     //ResourcePutTest
82     TEST(ResourcePutTest, ResourcePutForInvalidUri)
83     {
84         OCResource::Ptr resource = ConstructResourceObject("192.168.1.2:5000");
85         if(resource)
86         {
87             OCRepresentation rep;
88             QueryParamsMap test;
89             EXPECT_ANY_THROW(resource->put(rep, test, &onGetPut));
90         }
91     }
92
93     TEST(ResourcePutTest, ResourcePutForValid)
94     {
95         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000");
96         if(resource)
97         {
98             QueryParamsMap test;
99             OCRepresentation rep;
100             EXPECT_EQ(OC_STACK_OK, resource->put(rep, test, &onGetPut));
101         }
102     }
103
104     //ResourcePostTest
105     TEST(ResourcePostTest, ResourcePostForInvalidUri)
106     {
107         OCResource::Ptr resource = ConstructResourceObject("192.168.1.2:5000");
108         if(resource)
109         {
110             OCRepresentation rep;
111             QueryParamsMap test;
112             EXPECT_ANY_THROW(resource->post(rep, test, &onGetPut));
113         }
114     }
115
116     TEST(ResourcePostTest, ResourcePostValidConfiguration)
117     {
118         PlatformConfig cfg;
119         OCPlatform::Configure(cfg);
120
121         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000");
122         if(resource)
123         {
124             OCRepresentation rep;
125             QueryParamsMap test;
126             EXPECT_EQ(OC_STACK_OK, resource->post(rep, test, &onGetPut));
127         }
128     }
129
130     //ResourceObserveTest
131     TEST(ResourceObserveTest, ResourceObserveInValidUri)
132      {
133         OCResource::Ptr resource = ConstructResourceObject("192.168.1.2:5000");
134         if(resource)
135         {
136             QueryParamsMap test;
137             EXPECT_ANY_THROW(resource->observe(ObserveType::ObserveAll, test, &onObserve));
138         }
139     }
140
141     TEST(ResourceObserveTest, ResourceObserveValidUri)
142     {
143         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000");
144         if(resource)
145         {
146             QueryParamsMap test;
147             OCRepresentation rep;
148             EXPECT_EQ(OC_STACK_OK,resource->observe(ObserveType::ObserveAll, test, &onObserve));
149         }
150     }
151
152 }