666c9ae13afe26f9dacee23b14b64ec0de2559a0
[platform/upstream/iotivity.git] / service / resource-manipulation / src / serverBuilder / unittests / RCSResponseTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
23
24 #include <RCSResponse.h>
25 #include <ResourceObject.h>
26
27 #include <RequestHandler.h>
28 #include <ResourceAttributesConverter.h>
29
30 #include <OCPlatform.h>
31
32 using namespace std;
33
34 using namespace testing;
35
36 using namespace OIC::Service;
37 using namespace OC;
38
39 typedef OCStackResult (*registerResourceSig)(OCResourceHandle&,
40                        string&,
41                        const string&,
42                        const string&,
43                        EntityHandler,
44                        uint8_t );
45
46 static constexpr char KEY[] = "key";
47
48
49 void EXPECT_RESPONSE(shared_ptr< OCResourceResponse > ocResponse,
50         const OCEntityHandlerResult& result, int errorCode, const ResourceAttributes& attrs)
51 {
52     EXPECT_EQ(ocResponse->getResponseResult(), result);
53     EXPECT_EQ(ocResponse->getErrorCode(), errorCode);
54     EXPECT_EQ(ResourceAttributesConverter::fromOCRepresentation(
55                     ocResponse->getResourceRepresentation()), attrs);
56 }
57
58
59 class RCSResponseTest: public Test
60 {
61 public:
62     MockRepository mocks;
63
64 public:
65     template< typename T >
66     shared_ptr< OCResourceResponse > buildResponse(const T& response)
67     {
68         ResourceObject::Ptr server =
69                 ResourceObject::Builder("a/test", "", "").build();
70
71         return response.getHandler()->buildResponse(*server);
72     }
73
74 protected:
75     void SetUp()
76     {
77         mocks.OnCallFuncOverload(static_cast< registerResourceSig >(OCPlatform::registerResource))
78                 .Return(OC_STACK_OK);
79
80         mocks.OnCallFunc(OCPlatform::unregisterResource).Return(OC_STACK_OK);
81     }
82 };
83
84 TEST_F(RCSResponseTest, GetDefaultActionHasEmptyAttrs)
85 {
86     EXPECT_RESPONSE(buildResponse(RCSGetResponse::defaultAction()),
87             RequestHandler::DEFAULT_RESULT, RequestHandler::DEFAULT_ERROR_CODE,
88             ResourceAttributes());
89 }
90
91 TEST_F(RCSResponseTest, GetResponseHasResultsPassedCodes)
92 {
93     constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
94     constexpr int errorCode{ -10 };
95
96     EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(result, errorCode)),
97             result, errorCode, ResourceAttributes());
98 }
99
100 TEST_F(RCSResponseTest, GetResponseHasAttrsAndResultsPassedCodes)
101 {
102     constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
103     constexpr int errorCode{ -10 };
104
105     ResourceAttributes attrs;
106     attrs[KEY] = 100;
107
108     EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(attrs, result, errorCode)),
109             result, errorCode, attrs);
110 }
111
112 TEST_F(RCSResponseTest, GetResponseCanMoveAttrs)
113 {
114     constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
115     constexpr int errorCode{ -10 };
116
117     ResourceAttributes attrs;
118     attrs[KEY] = 100;
119
120     ResourceAttributes attrsClone;
121     attrsClone[KEY] = 100;
122
123     EXPECT_RESPONSE(
124             buildResponse(RCSGetResponse::create(std::move(attrs), result, errorCode)),
125             result, errorCode, attrsClone);
126
127     EXPECT_TRUE(attrs.empty());
128 }
129
130 TEST_F(RCSResponseTest, SetDefaultActionHasEmptyAttrs)
131 {
132     EXPECT_RESPONSE(buildResponse(RCSSetResponse::defaultAction()),
133             RequestHandler::DEFAULT_RESULT, RequestHandler::DEFAULT_ERROR_CODE,
134             ResourceAttributes());
135 }
136
137 TEST_F(RCSResponseTest, SetResponseHasResultsPassedCodes)
138 {
139     constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
140     constexpr int errorCode{ -10 };
141
142     EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(result, errorCode)),
143             result, errorCode, ResourceAttributes());
144 }
145
146 TEST_F(RCSResponseTest, SetResponseHasAttrsAndResultsPassedCodes)
147 {
148     constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
149     constexpr int errorCode{ -10 };
150
151     ResourceAttributes attrs;
152     attrs[KEY] = 100;
153
154     EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(attrs, result, errorCode)),
155             result, errorCode, attrs);
156 }
157
158 TEST_F(RCSResponseTest, SetResponseCanMoveAttrs)
159 {
160     constexpr OCEntityHandlerResult result{ OC_EH_ERROR };
161     constexpr int errorCode{ -10 };
162
163     ResourceAttributes attrs;
164     attrs[KEY] = 100;
165
166     ResourceAttributes attrsClone;
167     attrsClone[KEY] = 100;
168
169     EXPECT_RESPONSE(
170             buildResponse(RCSSetResponse::create(std::move(attrs), result, errorCode)),
171             result, errorCode, attrsClone);
172
173     EXPECT_TRUE(attrs.empty());
174 }
175
176
177 TEST_F(RCSResponseTest, DefaultSetResponseHasDefaultMethod)
178 {
179     EXPECT_EQ(RCSSetResponse::AcceptanceMethod::DEFAULT,
180             RCSSetResponse::defaultAction().getAcceptanceMethod());
181 }
182
183 TEST_F(RCSResponseTest, AcceptSetResponseHasAcceptMethod)
184 {
185     EXPECT_EQ(RCSSetResponse::AcceptanceMethod::ACCEPT,
186             RCSSetResponse::accept().getAcceptanceMethod());
187 }
188
189 TEST_F(RCSResponseTest, IgnoreSetResponseHasIgnoreMethod)
190 {
191     EXPECT_EQ(RCSSetResponse::AcceptanceMethod::IGNORE,
192             RCSSetResponse::ignore().getAcceptanceMethod());
193 }
194
195 TEST_F(RCSResponseTest, SetResponseHasMethodSetBySetter)
196 {
197     RCSSetResponse::AcceptanceMethod method = RCSSetResponse::AcceptanceMethod::ACCEPT;
198     RCSSetResponse response =
199             RCSSetResponse::defaultAction().setAcceptanceMethod(method);
200
201     EXPECT_EQ(method, response.getAcceptanceMethod());
202 }