Fix OCResourceResponseTest warning
[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(NewResourceUriTest, SetGetNewResourceUriValidUri)
35     {
36         OCResourceResponse response;
37         std::string uri = "/a/light";
38         EXPECT_NO_THROW(response.setNewResourceUri(uri));
39         EXPECT_EQ(uri, response.getNewResourceUri());
40     }
41
42     TEST(NewResourceUriTest, SetGetNewResourceUriEmpty)
43     {
44         OCResourceResponse response;
45         std::string uri = "/a/light";
46         EXPECT_NO_THROW(response.setNewResourceUri(uri));
47         EXPECT_NE("", response.getNewResourceUri());
48     }
49
50     TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsValidOption)
51     {
52         OCResourceResponse response;
53         const uint16_t API_VERSION = 2048;
54         const std::string FRIDGE_CLIENT_API_VERSION = "v.1.0";
55         HeaderOptions headerOptions;
56         HeaderOption::OCHeaderOption apiVersion(API_VERSION, FRIDGE_CLIENT_API_VERSION);
57         headerOptions.push_back(apiVersion);
58
59         EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
60         EXPECT_FALSE(headerOptions.empty());
61         EXPECT_EQ(apiVersion.getOptionID(),
62                 response.getHeaderOptions()[0].getOptionID());
63         EXPECT_EQ(apiVersion.getOptionData(),
64                 response.getHeaderOptions()[0].getOptionData());
65      }
66
67     TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsEmpty)
68     {
69         OCResourceResponse response;
70         HeaderOptions headerOptions;
71
72         EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
73         EXPECT_TRUE(headerOptions.empty());
74     }
75
76     TEST(RequestHandleTest, SetGetRequestHandleValidHandle)
77     {
78         const char query[] = "?rt=core.light";
79         const char address[] = "127.0.0.1";
80         OCResourceResponse response;
81         OCServerRequest request;
82
83         request.method = OC_REST_GET;
84         strncpy(request.query, query, sizeof(request.query));
85         request.devAddr.flags = OC_DEFAULT_FLAGS;
86         request.devAddr.adapter = OC_DEFAULT_ADAPTER;
87         strncpy(request.devAddr.addr, address, sizeof(request.devAddr.addr));
88         request.devAddr.port = 5364;
89         request.qos = OC_LOW_QOS;
90         request.coapID = 0;
91         request.delayedResNeeded = 0;
92
93         OCRequestHandle handle = static_cast<OCRequestHandle>(&request);
94         EXPECT_EQ(NULL, response.getRequestHandle());
95         EXPECT_NO_THROW(response.setRequestHandle(handle));
96         EXPECT_NE(static_cast<OCRequestHandle>(NULL), response.getRequestHandle());
97     }
98
99     TEST(RequestHandleTest, SetGetRequestHandleNullHandle)
100     {
101         OCResourceResponse response;
102         OCRequestHandle handle = nullptr;
103
104         EXPECT_EQ(NULL, response.getRequestHandle());
105         EXPECT_NO_THROW(response.setRequestHandle(handle));
106         EXPECT_EQ(NULL, response.getRequestHandle());
107     }
108
109 #ifdef _MSC_VER
110     TEST(ResourceHandleTest, DISABLED_SetGetResourceHandleValidHandle)
111 #else
112     TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
113 #endif
114     {
115         OCResourceResponse response;
116         OCResourceHandle resHandle;
117
118         std::string resourceURI = "/a/light2";
119         std::string resourceTypeName = "core.light";
120         std::string resourceInterface = DEFAULT_INTERFACE;
121         uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
122
123         EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(resHandle, resourceURI,
124                                          resourceTypeName, resourceInterface, nullptr,
125                                          resourceProperty));
126         EXPECT_EQ(NULL, response.getResourceHandle());
127         EXPECT_NO_THROW(response.setResourceHandle(resHandle));
128         EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
129     }
130
131     TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
132     {
133         OCResourceResponse response;
134         OCResourceHandle handle = nullptr;
135
136         EXPECT_EQ(NULL, response.getResourceHandle());
137         EXPECT_NO_THROW(response.setResourceHandle(handle));
138         EXPECT_EQ(NULL, response.getResourceHandle());
139     }
140
141     TEST(ResponseResultTest, SetGetResponseResultValidInput)
142     {
143         OCResourceResponse response;
144         OCEntityHandlerResult result = OC_EH_SLOW;
145         EXPECT_NO_THROW(response.setResponseResult(result));
146         EXPECT_EQ(result, response.getResponseResult());
147     }
148
149     TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
150     {
151         OCResourceResponse response;
152         OCRepresentation lightRepresentation;
153         const std::string LIGHT_RESOURCE_KEY = "light";
154         lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
155         EXPECT_TRUE(response.getResourceRepresentation().emptyData());
156         EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation));
157         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
158     }
159
160     TEST(ResourceRepresentation,
161             SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
162     {
163         OCResourceResponse response;
164         OCRepresentation lightRepresentation;
165
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             SetGetResourceRepresentationWithRepresentationAndInterface)
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                 LINK_INTERFACE));
184         EXPECT_FALSE(response.getResourceRepresentation().emptyData());
185     }
186 }