Merge remote-tracking branch 'origin/master' into notification-service
[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 #ifdef _MSC_VER
123     TEST(ResourceHandleTest, DISABLED_SetGetResourceHandleValidHandle)
124 #else
125     TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
126 #endif
127     {
128         OCResourceResponse response;
129         OCResourceHandle resHandle;
130
131         std::string resourceURI = "/a/light2";
132         std::string resourceTypeName = "core.light";
133         std::string resourceInterface = DEFAULT_INTERFACE;
134         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
135
136         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(resHandle, resourceURI,
137                                          resourceTypeName, resourceInterface, nullptr,
138                                          resourceProperty));
139         EXPECT_EQ(NULL, response.getResourceHandle());
140         EXPECT_NO_THROW(response.setResourceHandle(resHandle));
141         EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
142     }
143
144     TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
145     {
146         OCResourceResponse response;
147         OCResourceHandle handle = nullptr;
148
149         EXPECT_EQ(NULL, response.getResourceHandle());
150         EXPECT_NO_THROW(response.setResourceHandle(handle));
151         EXPECT_EQ(NULL, response.getResourceHandle());
152     }
153
154     TEST(ResponseResultTest, SetGetResponseResultValidInput)
155     {
156         OCResourceResponse response;
157         OCEntityHandlerResult result = OC_EH_SLOW;
158         EXPECT_NO_THROW(response.setResponseResult(result));
159         EXPECT_EQ(result, response.getResponseResult());
160     }
161
162     TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
163     {
164         OCResourceResponse response;
165         OCRepresentation lightRepresentation;
166         const std::string LIGHT_RESOURCE_KEY = "light";
167         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
168         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
169         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation));
170         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
171     }
172
173     TEST(ResourceRepresentation,
174             SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
175     {
176         OCResourceResponse response;
177         OCRepresentation lightRepresentation;
178
179         const std::string LIGHT_RESOURCE_KEY = "light";
180         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
181         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
182         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation, ""));
183         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
184     }
185
186     TEST(ResourceRepresentation,
187             SetGetResourceRepresentationWithRepresentationAndInterface)
188     {
189         OCResourceResponse response;
190         OCRepresentation lightRepresentation;
191
192         const std::string LIGHT_RESOURCE_KEY = "light";
193         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
194         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
195         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation,
196                 LINK_INTERFACE));
197         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
198     }
199 }