1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
24 #include <RCSResponse.h>
25 #include <ResourceObject.h>
27 #include <RequestHandler.h>
28 #include <ResourceAttributesConverter.h>
30 #include <OCPlatform.h>
34 using namespace testing;
36 using namespace OIC::Service;
39 typedef OCStackResult (*registerResourceSig)(OCResourceHandle&,
46 static constexpr char KEY[] = "key";
49 void EXPECT_RESPONSE(shared_ptr< OCResourceResponse > ocResponse,
50 const OCEntityHandlerResult& result, int errorCode, const ResourceAttributes& attrs)
52 EXPECT_EQ(ocResponse->getResponseResult(), result);
53 EXPECT_EQ(ocResponse->getErrorCode(), errorCode);
54 EXPECT_EQ(ResourceAttributesConverter::fromOCRepresentation(
55 ocResponse->getResourceRepresentation()), attrs);
59 class RCSResponseTest: public Test
65 template< typename T >
66 shared_ptr< OCResourceResponse > buildResponse(const T& response)
68 ResourceObject::Ptr server =
69 ResourceObject::Builder("a/test", "", "").build();
71 return response.getHandler()->buildResponse(*server);
77 mocks.OnCallFuncOverload(static_cast< registerResourceSig >(OCPlatform::registerResource))
80 mocks.OnCallFunc(OCPlatform::unregisterResource).Return(OC_STACK_OK);
84 TEST_F(RCSResponseTest, GetDefaultActionHasEmptyAttrs)
86 EXPECT_RESPONSE(buildResponse(RCSGetResponse::defaultAction()),
87 RequestHandler::DEFAULT_RESULT, RequestHandler::DEFAULT_ERROR_CODE,
88 ResourceAttributes());
91 TEST_F(RCSResponseTest, GetResponseHasResultsPassedCodes)
93 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
94 constexpr int errorCode{ -10 };
96 EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(result, errorCode)),
97 result, errorCode, ResourceAttributes());
100 TEST_F(RCSResponseTest, GetResponseHasAttrsAndResultsPassedCodes)
102 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
103 constexpr int errorCode{ -10 };
105 ResourceAttributes attrs;
108 EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(attrs, result, errorCode)),
109 result, errorCode, attrs);
112 TEST_F(RCSResponseTest, GetResponseCanMoveAttrs)
114 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
115 constexpr int errorCode{ -10 };
117 ResourceAttributes attrs;
120 ResourceAttributes attrsClone;
121 attrsClone[KEY] = 100;
124 buildResponse(RCSGetResponse::create(std::move(attrs), result, errorCode)),
125 result, errorCode, attrsClone);
127 EXPECT_TRUE(attrs.empty());
130 TEST_F(RCSResponseTest, SetDefaultActionHasEmptyAttrs)
132 EXPECT_RESPONSE(buildResponse(RCSSetResponse::defaultAction()),
133 RequestHandler::DEFAULT_RESULT, RequestHandler::DEFAULT_ERROR_CODE,
134 ResourceAttributes());
137 TEST_F(RCSResponseTest, SetResponseHasResultsPassedCodes)
139 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
140 constexpr int errorCode{ -10 };
142 EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(result, errorCode)),
143 result, errorCode, ResourceAttributes());
146 TEST_F(RCSResponseTest, SetResponseHasAttrsAndResultsPassedCodes)
148 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
149 constexpr int errorCode{ -10 };
151 ResourceAttributes attrs;
154 EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(attrs, result, errorCode)),
155 result, errorCode, attrs);
158 TEST_F(RCSResponseTest, SetResponseCanMoveAttrs)
160 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
161 constexpr int errorCode{ -10 };
163 ResourceAttributes attrs;
166 ResourceAttributes attrsClone;
167 attrsClone[KEY] = 100;
170 buildResponse(RCSSetResponse::create(std::move(attrs), result, errorCode)),
171 result, errorCode, attrsClone);
173 EXPECT_TRUE(attrs.empty());
177 TEST_F(RCSResponseTest, DefaultSetResponseHasDefaultMethod)
179 EXPECT_EQ(RCSSetResponse::AcceptanceMethod::DEFAULT,
180 RCSSetResponse::defaultAction().getAcceptanceMethod());
183 TEST_F(RCSResponseTest, AcceptSetResponseHasAcceptMethod)
185 EXPECT_EQ(RCSSetResponse::AcceptanceMethod::ACCEPT,
186 RCSSetResponse::accept().getAcceptanceMethod());
189 TEST_F(RCSResponseTest, IgnoreSetResponseHasIgnoreMethod)
191 EXPECT_EQ(RCSSetResponse::AcceptanceMethod::IGNORE,
192 RCSSetResponse::ignore().getAcceptanceMethod());
195 TEST_F(RCSResponseTest, SetResponseHasMethodSetBySetter)
197 RCSSetResponse::AcceptanceMethod method = RCSSetResponse::AcceptanceMethod::ACCEPT;
198 RCSSetResponse response =
199 RCSSetResponse::defaultAction().setAcceptanceMethod(method);
201 EXPECT_EQ(method, response.getAcceptanceMethod());