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