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 <UnitTestHelper.h>
23 #include <RCSResponse.h>
24 #include <ResourceObject.h>
26 #include <RequestHandler.h>
27 #include <ResourceAttributesConverter.h>
29 #include <OCPlatform.h>
33 using namespace OIC::Service;
36 typedef OCStackResult (*registerResourceSig)(OCResourceHandle&,
43 static constexpr char KEY[] = "key";
46 void EXPECT_RESPONSE(shared_ptr< OCResourceResponse > ocResponse,
47 const OCEntityHandlerResult& result, int errorCode, const ResourceAttributes& attrs)
49 EXPECT_EQ(ocResponse->getResponseResult(), result);
50 EXPECT_EQ(ocResponse->getErrorCode(), errorCode);
51 EXPECT_EQ(ResourceAttributesConverter::fromOCRepresentation(
52 ocResponse->getResourceRepresentation()), attrs);
56 class RCSResponseTest: public TestWithMock
59 template< typename T >
60 shared_ptr< OCResourceResponse > buildResponse(const T& response)
62 ResourceObject::Ptr server =
63 ResourceObject::Builder("a/test", "", "").build();
65 return response.getHandler()->buildResponse(*server);
71 TestWithMock::SetUp();
73 mocks.OnCallFuncOverload(static_cast< registerResourceSig >(OCPlatform::registerResource))
76 mocks.OnCallFunc(OCPlatform::unregisterResource).Return(OC_STACK_OK);
80 TEST_F(RCSResponseTest, GetDefaultActionHasEmptyAttrs)
82 EXPECT_RESPONSE(buildResponse(RCSGetResponse::defaultAction()),
83 RequestHandler::DEFAULT_RESULT, RequestHandler::DEFAULT_ERROR_CODE,
84 ResourceAttributes());
87 TEST_F(RCSResponseTest, GetResponseHasResultsPassedCodes)
89 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
90 constexpr int errorCode{ -10 };
92 EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(result, errorCode)),
93 result, errorCode, ResourceAttributes());
96 TEST_F(RCSResponseTest, GetResponseHasAttrsAndResultsPassedCodes)
98 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
99 constexpr int errorCode{ -10 };
101 ResourceAttributes attrs;
104 EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(attrs, result, errorCode)),
105 result, errorCode, attrs);
108 TEST_F(RCSResponseTest, GetResponseCanMoveAttrs)
110 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
111 constexpr int errorCode{ -10 };
113 ResourceAttributes attrs;
116 ResourceAttributes attrsClone;
117 attrsClone[KEY] = 100;
120 buildResponse(RCSGetResponse::create(std::move(attrs), result, errorCode)),
121 result, errorCode, attrsClone);
123 EXPECT_TRUE(attrs.empty());
126 TEST_F(RCSResponseTest, SetDefaultActionHasEmptyAttrs)
128 EXPECT_RESPONSE(buildResponse(RCSSetResponse::defaultAction()),
129 RequestHandler::DEFAULT_RESULT, RequestHandler::DEFAULT_ERROR_CODE,
130 ResourceAttributes());
133 TEST_F(RCSResponseTest, SetResponseHasResultsPassedCodes)
135 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
136 constexpr int errorCode{ -10 };
138 EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(result, errorCode)),
139 result, errorCode, ResourceAttributes());
142 TEST_F(RCSResponseTest, SetResponseHasAttrsAndResultsPassedCodes)
144 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
145 constexpr int errorCode{ -10 };
147 ResourceAttributes attrs;
150 EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(attrs, result, errorCode)),
151 result, errorCode, attrs);
154 TEST_F(RCSResponseTest, SetResponseCanMoveAttrs)
156 constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
157 constexpr int errorCode{ -10 };
159 ResourceAttributes attrs;
162 ResourceAttributes attrsClone;
163 attrsClone[KEY] = 100;
166 buildResponse(RCSSetResponse::create(std::move(attrs), result, errorCode)),
167 result, errorCode, attrsClone);
169 EXPECT_TRUE(attrs.empty());
173 TEST_F(RCSResponseTest, DefaultSetResponseHasDefaultMethod)
175 EXPECT_EQ(RCSSetResponse::AcceptanceMethod::DEFAULT,
176 RCSSetResponse::defaultAction().getAcceptanceMethod());
179 TEST_F(RCSResponseTest, AcceptSetResponseHasAcceptMethod)
181 EXPECT_EQ(RCSSetResponse::AcceptanceMethod::ACCEPT,
182 RCSSetResponse::accept().getAcceptanceMethod());
185 TEST_F(RCSResponseTest, IgnoreSetResponseHasIgnoreMethod)
187 EXPECT_EQ(RCSSetResponse::AcceptanceMethod::IGNORE,
188 RCSSetResponse::ignore().getAcceptanceMethod());
191 TEST_F(RCSResponseTest, SetResponseHasMethodSetBySetter)
193 RCSSetResponse::AcceptanceMethod method = RCSSetResponse::AcceptanceMethod::ACCEPT;
194 RCSSetResponse response =
195 RCSSetResponse::defaultAction().setAcceptanceMethod(method);
197 EXPECT_EQ(method, response.getAcceptanceMethod());