RD Device Presence features in base layer
[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
106         OCRequestHandle handle = static_cast<OCRequestHandle>(&request);
107         EXPECT_EQ(NULL, response.getRequestHandle());
108         EXPECT_NO_THROW(response.setRequestHandle(handle));
109         EXPECT_NE(static_cast<OCRequestHandle>(NULL), response.getRequestHandle());
110     }
111
112     TEST(RequestHandleTest, SetGetRequestHandleNullHandle)
113     {
114         OCResourceResponse response;
115         OCRequestHandle handle = nullptr;
116
117         EXPECT_EQ(NULL, response.getRequestHandle());
118         EXPECT_NO_THROW(response.setRequestHandle(handle));
119         EXPECT_EQ(NULL, response.getRequestHandle());
120     }
121
122     TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
123     {
124         OCResourceResponse response;
125         OCResourceHandle resHandle;
126
127         std::string resourceURI = "/a/light2";
128         std::string resourceTypeName = "core.light";
129         std::string resourceInterface = DEFAULT_INTERFACE;
130         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
131
132         EXPECT_EQ(OC_STACK_OK, OCCreateResource(&resHandle, resourceTypeName.c_str(),
133                 resourceInterface.c_str(), resourceURI.c_str(), nullptr, nullptr,
134                 resourceProperty));
135         EXPECT_EQ(NULL, response.getResourceHandle());
136         EXPECT_NO_THROW(response.setResourceHandle(resHandle));
137         EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
138     }
139
140     TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
141     {
142         OCResourceResponse response;
143         OCResourceHandle handle = nullptr;
144
145         EXPECT_EQ(NULL, response.getResourceHandle());
146         EXPECT_NO_THROW(response.setResourceHandle(handle));
147         EXPECT_EQ(NULL, response.getResourceHandle());
148     }
149
150     TEST(ResponseResultTest, SetGetResponseResultValidInput)
151     {
152         OCResourceResponse response;
153         OCEntityHandlerResult result = OC_EH_SLOW;
154         EXPECT_NO_THROW(response.setResponseResult(result));
155         EXPECT_EQ(result, response.getResponseResult());
156     }
157
158     TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
159     {
160         OCResourceResponse response;
161         OCRepresentation lightRepresentation;
162         const std::string LIGHT_RESOURCE_KEY = "light";
163         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
164         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
165         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation));
166         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
167     }
168
169     TEST(ResourceRepresentation,
170             SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
171     {
172         OCResourceResponse response;
173         OCRepresentation lightRepresentation;
174
175         const std::string LIGHT_RESOURCE_KEY = "light";
176         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
177         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
178         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation, ""));
179         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
180     }
181
182     TEST(ResourceRepresentation,
183             SetGetResourceRepresentationWithRepresentationAndInterface)
184     {
185         OCResourceResponse response;
186         OCRepresentation lightRepresentation;
187
188         const std::string LIGHT_RESOURCE_KEY = "light";
189         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
190         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
191         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation,
192                 LINK_INTERFACE));
193         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
194     }
195 }