1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <OCPlatform.h>
23 #include <gtest/gtest.h>
24 #include <OCResourceResponse.h>
25 #include <ocserverrequest.h>
26 #include <InProcServerWrapper.h>
28 namespace PH = std::placeholders;
29 namespace OCResourceResponseTest
34 TEST(ErrorCodeTest, SetGetErrorCodeValidCode)
36 OCResourceResponse response;
38 EXPECT_NO_THROW(response.setErrorCode(setCode));
39 EXPECT_EQ(setCode, response.getErrorCode());
42 TEST(NewResourceUriTest, SetGetNewResourceUriValidUri)
44 OCResourceResponse response;
45 std::string uri = "/a/light";
46 EXPECT_NO_THROW(response.setNewResourceUri(uri));
47 EXPECT_EQ(uri, response.getNewResourceUri());
50 TEST(NewResourceUriTest, SetGetNewResourceUriEmpty)
52 OCResourceResponse response;
53 std::string uri = "/a/light";
54 EXPECT_NO_THROW(response.setNewResourceUri(uri));
55 EXPECT_NE("", response.getNewResourceUri());
58 TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsValidOption)
60 OCResourceResponse response;
61 const uint16_t API_VERSION = 2048;
62 const std::string FRIDGE_CLIENT_API_VERSION = "v.1.0";
63 HeaderOptions headerOptions;
64 HeaderOption::OCHeaderOption apiVersion(API_VERSION, FRIDGE_CLIENT_API_VERSION);
65 headerOptions.push_back(apiVersion);
67 EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
68 EXPECT_FALSE(headerOptions.empty());
69 EXPECT_EQ(apiVersion.getOptionID(),
70 response.getHeaderOptions()[0].getOptionID());
71 EXPECT_EQ(apiVersion.getOptionData(),
72 response.getHeaderOptions()[0].getOptionData());
75 TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsEmpty)
77 OCResourceResponse response;
78 HeaderOptions headerOptions;
80 EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
81 EXPECT_TRUE(headerOptions.empty());
84 TEST(RequestHandleTest, SetGetRequestHandleValidHandle)
86 char query[] = "?rt=core.light";
87 char address[] = "127.0.0.1";
88 OCResourceResponse response;
89 OCServerRequest request;
90 request.method = OC_REST_GET;
91 strncpy(request.query, query, sizeof(query));
92 request.connectivityType = CA_IPV4;
93 strncpy(request.addressInfo.IP.ipAddress, address, sizeof(query));
94 request.addressInfo.IP.port = 5364;
95 request.qos = OC_LOW_QOS;
97 request.delayedResNeeded = 0;
100 OCRequestHandle handle = static_cast<OCRequestHandle>(&request);
101 EXPECT_EQ(NULL, response.getRequestHandle());
102 EXPECT_NO_THROW(response.setRequestHandle(handle));
103 EXPECT_NE(static_cast<OCRequestHandle>(NULL), response.getRequestHandle());
106 TEST(RequestHandleTest, SetGetRequestHandleNullHandle)
108 OCResourceResponse response;
109 OCRequestHandle handle = nullptr;
111 EXPECT_EQ(NULL, response.getRequestHandle());
112 EXPECT_NO_THROW(response.setRequestHandle(handle));
113 EXPECT_EQ(NULL, response.getRequestHandle());
116 TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
118 OCResourceResponse response;
119 OCResourceHandle resHandle;
121 std::string resourceURI = "/a/light1";
122 std::string resourceTypeName = "core.light";
123 std::string resourceInterface = DEFAULT_INTERFACE;
124 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
126 EXPECT_EQ(OC_STACK_OK, OCCreateResource(&resHandle, resourceURI.c_str(),
127 resourceTypeName.c_str(), resourceInterface.c_str(), nullptr,
129 EXPECT_EQ(NULL, response.getResourceHandle());
130 EXPECT_NO_THROW(response.setResourceHandle(resHandle));
131 EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
134 TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
136 OCResourceResponse response;
137 OCResourceHandle handle = nullptr;
139 EXPECT_EQ(NULL, response.getResourceHandle());
140 EXPECT_NO_THROW(response.setResourceHandle(handle));
141 EXPECT_EQ(NULL, response.getResourceHandle());
144 TEST(ResponseResultTest, SetGetResponseResultValidInput)
146 OCResourceResponse response;
147 OCEntityHandlerResult result = OC_EH_SLOW;
148 EXPECT_NO_THROW(response.setResponseResult(result));
149 EXPECT_EQ(result, response.getResponseResult());
152 TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
154 OCResourceResponse response;
155 OCRepresentation lightRepresentation;
156 const std::string LIGHT_RESOURCE_KEY = "light";
157 lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
158 EXPECT_TRUE(response.getResourceRepresentation().emptyData());
159 EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation));
160 EXPECT_FALSE(response.getResourceRepresentation().emptyData());
163 TEST(ResourceRepresentation,
164 SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
166 OCResourceResponse response;
167 OCRepresentation lightRepresentation;
169 const std::string LIGHT_RESOURCE_KEY = "light";
170 lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
171 EXPECT_TRUE(response.getResourceRepresentation().emptyData());
172 EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation, ""));
173 EXPECT_FALSE(response.getResourceRepresentation().emptyData());
176 TEST(ResourceRepresentation,
177 SetGetResourceRepresentationWithRepresentationAndInterface)
179 OCResourceResponse response;
180 OCRepresentation lightRepresentation;
182 const std::string LIGHT_RESOURCE_KEY = "light";
183 lightRepresentation.setValue(LIGHT_RESOURCE_KEY, 0);
184 EXPECT_TRUE(response.getResourceRepresentation().emptyData());
185 EXPECT_NO_THROW(response.setResourceRepresentation(lightRepresentation,
187 EXPECT_FALSE(response.getResourceRepresentation().emptyData());