Revert "[CONPRO-1337] Disabled Presence Feature"
[platform/upstream/iotivity.git] / resource / unittests / OCResourceResponseTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 #include <OCPlatform.h>
22 #include <OCApi.h>
23 #include <gtest/gtest.h>
24 #include <OCResourceResponse.h>
25 #include <ocserverrequest.h>
26 #include <InProcServerWrapper.h>
27
28 namespace PH = std::placeholders;
29 namespace OCResourceResponseTest
30 {
31     using namespace OC;
32     using namespace std;
33
34     TEST(ErrorCodeTest, SetGetErrorCodeValidCode)
35     {
36         OCResourceResponse response;
37         int setCode = 200;
38         EXPECT_NO_THROW(response.setErrorCode(setCode));
39         EXPECT_EQ(setCode, response.getErrorCode());
40         setCode = 500;
41         EXPECT_NO_THROW(response.setErrorCode(setCode));
42         EXPECT_EQ(setCode, response.getErrorCode());
43         setCode = 400;
44         EXPECT_NO_THROW(response.setErrorCode(setCode));
45         EXPECT_EQ(setCode, response.getErrorCode());
46     }
47
48     TEST(NewResourceUriTest, SetGetNewResourceUriValidUri)
49     {
50         OCResourceResponse response;
51         std::string uri = "/a/light";
52         EXPECT_NO_THROW(response.setNewResourceUri(uri));
53         EXPECT_EQ(uri, response.getNewResourceUri());
54     }
55
56     TEST(NewResourceUriTest, SetGetNewResourceUriEmpty)
57     {
58         OCResourceResponse response;
59         std::string uri = "/a/light";
60         EXPECT_NO_THROW(response.setNewResourceUri(uri));
61         EXPECT_NE("", response.getNewResourceUri());
62     }
63
64     TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsValidOption)
65     {
66         OCResourceResponse response;
67         const uint16_t API_VERSION = 2048;
68         const std::string FRIDGE_CLIENT_API_VERSION = "v.1.0";
69         HeaderOptions headerOptions;
70         HeaderOption::OCHeaderOption apiVersion(API_VERSION, FRIDGE_CLIENT_API_VERSION);
71         headerOptions.push_back(apiVersion);
72
73         EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
74         EXPECT_FALSE(headerOptions.empty());
75         EXPECT_EQ(apiVersion.getOptionID(),
76                 response.getHeaderOptions()[0].getOptionID());
77         EXPECT_EQ(apiVersion.getOptionData(),
78                 response.getHeaderOptions()[0].getOptionData());
79      }
80
81     TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsEmpty)
82     {
83         OCResourceResponse response;
84         HeaderOptions headerOptions;
85
86         EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
87         EXPECT_TRUE(headerOptions.empty());
88     }
89
90     TEST(RequestHandleTest, SetGetRequestHandleValidHandle)
91     {
92         char query[] = "?rt=core.light";
93         char address[] = "127.0.0.1";
94         OCResourceResponse response;
95         OCServerRequest request;
96         request.method = OC_REST_GET;
97         strncpy(request.query, query, sizeof(query));
98         request.devAddr.flags = OC_DEFAULT_FLAGS;
99         request.devAddr.adapter = OC_DEFAULT_ADAPTER;
100         strncpy(request.devAddr.addr, address, sizeof(query));
101         request.devAddr.port = 5364;
102         request.qos = OC_LOW_QOS;
103         request.coapID = 0;
104         request.delayedResNeeded = 0;
105         request.requestId = 0x1234;
106
107         OCRequestHandle handle = request.requestId;
108         EXPECT_EQ(0, response.getRequestHandle());
109         EXPECT_NO_THROW(response.setRequestHandle(handle));
110         EXPECT_NE(0, response.getRequestHandle());
111     }
112
113     TEST(RequestHandleTest, SetGetRequestHandleNullHandle)
114     {
115         OCResourceResponse response;
116         OCRequestHandle handle = 0;
117
118         EXPECT_EQ(NULL, response.getRequestHandle());
119         EXPECT_NO_THROW(response.setRequestHandle(handle));
120         EXPECT_EQ(NULL, response.getRequestHandle());
121     }
122
123     TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
124     {
125         OCResourceResponse response;
126         OCResourceHandle resHandle;
127
128         std::string resourceURI = "/a/light2";
129         std::string resourceTypeName = "core.light";
130         std::string resourceInterface = DEFAULT_INTERFACE;
131         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
132
133         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(resHandle, resourceURI,
134                                          resourceTypeName, resourceInterface, nullptr,
135                                          resourceProperty));
136         EXPECT_EQ(NULL, response.getResourceHandle());
137         EXPECT_NO_THROW(response.setResourceHandle(resHandle));
138         EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
139     }
140
141     TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
142     {
143         OCResourceResponse response;
144         OCResourceHandle handle = nullptr;
145
146         EXPECT_EQ(NULL, response.getResourceHandle());
147         EXPECT_NO_THROW(response.setResourceHandle(handle));
148         EXPECT_EQ(NULL, response.getResourceHandle());
149     }
150
151     TEST(ResponseResultTest, SetGetResponseResultValidInput)
152     {
153         OCResourceResponse response;
154         OCEntityHandlerResult result = OC_EH_SLOW;
155         EXPECT_NO_THROW(response.setResponseResult(result));
156         EXPECT_EQ(result, response.getResponseResult());
157     }
158
159     TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
160     {
161         OCResourceResponse response;
162         OCRepresentation lightRepresentation;
163         const std::string LIGHT_RESOURCE_KEY = "light";
164         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
165         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
166         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation));
167         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
168     }
169
170     TEST(ResourceRepresentation,
171             SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
172     {
173         OCResourceResponse response;
174         OCRepresentation lightRepresentation;
175
176         const std::string LIGHT_RESOURCE_KEY = "light";
177         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
178         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
179         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation, ""));
180         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
181     }
182
183     TEST(ResourceRepresentation,
184             SetGetResourceRepresentationWithRepresentationAndInterface)
185     {
186         OCResourceResponse response;
187         OCRepresentation lightRepresentation;
188
189         const std::string LIGHT_RESOURCE_KEY = "light";
190         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
191         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
192         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation,
193                 LINK_INTERFACE));
194         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
195     }
196 }