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());
41 EXPECT_NO_THROW(response.setErrorCode(setCode));
42 EXPECT_EQ(setCode, response.getErrorCode());
44 EXPECT_NO_THROW(response.setErrorCode(setCode));
45 EXPECT_EQ(setCode, response.getErrorCode());
48 TEST(NewResourceUriTest, SetGetNewResourceUriValidUri)
50 OCResourceResponse response;
51 std::string uri = "/a/light";
52 EXPECT_NO_THROW(response.setNewResourceUri(uri));
53 EXPECT_EQ(uri, response.getNewResourceUri());
56 TEST(NewResourceUriTest, SetGetNewResourceUriEmpty)
58 OCResourceResponse response;
59 std::string uri = "/a/light";
60 EXPECT_NO_THROW(response.setNewResourceUri(uri));
61 EXPECT_NE("", response.getNewResourceUri());
64 TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsValidOption)
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);
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());
81 TEST(ResposeHeaderOptionsTest, SetGetHeaderOptionsEmpty)
83 OCResourceResponse response;
84 HeaderOptions headerOptions;
86 EXPECT_NO_THROW(response.setHeaderOptions(headerOptions));
87 EXPECT_TRUE(headerOptions.empty());
90 TEST(RequestHandleTest, SetGetRequestHandleValidHandle)
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;
104 request.delayedResNeeded = 0;
105 request.requestId = 0x1234;
107 OCRequestHandle handle = request.requestId;
108 EXPECT_EQ(0, response.getRequestHandle());
109 EXPECT_NO_THROW(response.setRequestHandle(handle));
110 EXPECT_NE(0, response.getRequestHandle());
113 TEST(RequestHandleTest, SetGetRequestHandleNullHandle)
115 OCResourceResponse response;
116 OCRequestHandle handle = 0;
118 EXPECT_EQ(NULL, response.getRequestHandle());
119 EXPECT_NO_THROW(response.setRequestHandle(handle));
120 EXPECT_EQ(NULL, response.getRequestHandle());
123 TEST(ResourceHandleTest, SetGetResourceHandleValidHandle)
125 OCResourceResponse response;
126 OCResourceHandle resHandle;
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;
133 EXPECT_EQ(OC_STACK_OK, OCPlatform::registerResource(resHandle, resourceURI,
134 resourceTypeName, resourceInterface, nullptr,
136 EXPECT_EQ(NULL, response.getResourceHandle());
137 EXPECT_NO_THROW(response.setResourceHandle(resHandle));
138 EXPECT_NE(static_cast<OCResourceHandle>(NULL), response.getResourceHandle());
141 TEST(ResourceHandleTest, SetGetResourceHandleNullHandle)
143 OCResourceResponse response;
144 OCResourceHandle handle = nullptr;
146 EXPECT_EQ(NULL, response.getResourceHandle());
147 EXPECT_NO_THROW(response.setResourceHandle(handle));
148 EXPECT_EQ(NULL, response.getResourceHandle());
151 TEST(ResponseResultTest, SetGetResponseResultValidInput)
153 OCResourceResponse response;
154 OCEntityHandlerResult result = OC_EH_SLOW;
155 EXPECT_NO_THROW(response.setResponseResult(result));
156 EXPECT_EQ(result, response.getResponseResult());
159 TEST(ResourceRepresentation, SetGetResourceRepresentationWithValidRepresentation)
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());
170 TEST(ResourceRepresentation,
171 SetGetResourceRepresentationWithRepresentationAndEmptyInterface)
173 OCResourceResponse response;
174 OCRepresentation lightRepresentation;
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());
183 TEST(ResourceRepresentation,
184 SetGetResourceRepresentationWithRepresentationAndInterface)
186 OCResourceResponse response;
187 OCRepresentation lightRepresentation;
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,
194 EXPECT_FALSE(response.getResourceRepresentation().emptyData());