Added RCSRepresentation class and get function with params in PrimitiveResource.
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / primitiveResource / unittests / PrimitiveResourceTest.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 "PrimitiveResourceImpl.h"
24 #include "AssertUtils.h"
25
26 #include "OCResource.h"
27 #include "OCPlatform.h"
28
29 using namespace OIC::Service;
30
31 const std::string KEY{ "key" };
32
33 class FakeOCResource
34 {
35 public:
36     virtual ~FakeOCResource() {};
37
38     virtual OCStackResult get(const std::string&, const std::string&,
39             const OC::QueryParamsMap&, OC::GetCallback) = 0;
40
41     virtual OCStackResult put(
42             const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PutCallback) = 0;
43
44     virtual OCStackResult post(
45             const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PostCallback) = 0;
46
47     virtual OCStackResult observe(
48             OC::ObserveType, const OC::QueryParamsMap&, OC::ObserveCallback) = 0;
49
50     virtual OCStackResult cancelObserve() = 0;
51
52     virtual std::string sid() const = 0;
53     virtual std::string uri() const = 0;
54     virtual std::string host() const = 0;
55     virtual std::vector<std::string> getResourceTypes() const = 0;
56     virtual std::vector<std::string> getResourceInterfaces() const = 0;
57
58     virtual bool isObservable() const = 0;
59 };
60
61 class PrimitiveResourceTest: public TestWithMock
62 {
63 public:
64     PrimitiveResource::Ptr resource;
65     FakeOCResource* fakeResource;
66
67 protected:
68     void SetUp() {
69         TestWithMock::SetUp();
70
71         fakeResource = mocks.Mock< FakeOCResource >();
72
73         resource.reset(new PrimitiveResourceImpl< FakeOCResource >{
74             std::shared_ptr< FakeOCResource >(fakeResource, [](FakeOCResource*) {}) });
75     }
76 };
77
78 TEST_F(PrimitiveResourceTest, RequestGetInvokesOCResourceGet)
79 {
80     mocks.ExpectCall(fakeResource, FakeOCResource::get).Return(OC_STACK_OK);
81
82     resource->requestGet(PrimitiveResource::GetCallback());
83 }
84
85 TEST_F(PrimitiveResourceTest, RequestGetThrowsOCResourceGetReturnsNotOK)
86 {
87     mocks.OnCall(fakeResource, FakeOCResource::get).Return(OC_STACK_ERROR);
88
89     ASSERT_THROW(resource->requestGet(PrimitiveResource::GetCallback()), RCSPlatformException);
90 }
91
92 TEST_F(PrimitiveResourceTest, RequestSetInvokesOCResourcePost)
93 {
94     mocks.ExpectCall(fakeResource, FakeOCResource::post).Return(OC_STACK_OK);
95
96     resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback());
97 }
98
99 TEST_F(PrimitiveResourceTest, RequestSetThrowsOCResourcePostReturnsNotOK)
100 {
101     mocks.OnCall(fakeResource, FakeOCResource::post).Return(OC_STACK_ERROR);
102
103     ASSERT_THROW(resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback()),
104             RCSPlatformException);
105 }
106
107 TEST_F(PrimitiveResourceTest, RequestSetPassResourceAttributesToOCResourcePost)
108 {
109     constexpr int value{ -200 };
110
111     RCSResourceAttributes attrs;
112
113     mocks.ExpectCall(fakeResource, FakeOCResource::post).Match(
114             [](const OC::OCRepresentation& ocRep, const OC::QueryParamsMap&, OC::PutCallback)
115             {
116                 return ocRep.getValue<int>(KEY) == value;
117             }
118         ).Return(OC_STACK_OK);
119
120     attrs[KEY] = value;
121
122     resource->requestSet(attrs, PrimitiveResource::SetCallback());
123 }
124
125 TEST_F(PrimitiveResourceTest, RequestObserveInvokesOCResourceObserve)
126 {
127     mocks.ExpectCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_OK);
128
129     resource->requestObserve(PrimitiveResource::ObserveCallback());
130 }
131
132 TEST_F(PrimitiveResourceTest, RequestObserveThrowsOCResourceObserveReturnsNotOK)
133 {
134     mocks.OnCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_ERROR);
135
136     ASSERT_THROW(resource->requestObserve(PrimitiveResource::ObserveCallback()), RCSPlatformException);
137 }
138
139 TEST_F(PrimitiveResourceTest, DelegteGettersToOCResource)
140 {
141     const std::string host{ "host_test_" };
142     const std::string uri{ "uri/test/" };
143
144     mocks.OnCall(fakeResource, FakeOCResource::isObservable).Return(false);
145     mocks.OnCall(fakeResource, FakeOCResource::host).Return(host);
146     mocks.OnCall(fakeResource, FakeOCResource::uri).Return(uri);
147
148     ASSERT_FALSE(resource->isObservable());
149     ASSERT_EQ(host, resource->getHost());
150     ASSERT_EQ(uri, resource->getUri());
151 }
152
153
154 TEST_F(PrimitiveResourceTest, ResponseStatementHasSameValuesWithOCRepresentationReceived)
155 {
156     constexpr int errorCode{ 202 };
157     constexpr int value{ 1999 };
158
159     mocks.OnCall(fakeResource, FakeOCResource::get).Do(
160             [](const std::string&, const std::string&, const OC::QueryParamsMap&, OC::GetCallback cb)
161             {
162                 OC::OCRepresentation ocRep;
163                 ocRep[KEY] = value;
164
165                 cb(OC::HeaderOptions(), ocRep, errorCode);
166                 return OC_STACK_OK;
167             }
168         ).Return(OC_STACK_OK);
169
170     resource->requestGet(
171             [&](const HeaderOptions&, const ResponseStatement& response, int e)
172             {
173                 ASSERT_EQ(e, errorCode);
174                 ASSERT_EQ(response.getAttributes().at(KEY).get<int>(), value);
175             }
176         );
177 }
178
179
180 class DiscoverResourceTest: public TestWithMock
181 {
182 public:
183     typedef OCStackResult (*FindResource)(const std::string&, const std::string&,
184             OCConnectivityType, OC::FindCallback);
185
186 public:
187     static void discovered(std::shared_ptr< PrimitiveResource >) {}
188 };
189
190 TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
191 {
192     mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).Do(
193             [](const std::string&, const std::string&, OCConnectivityType,
194                     OC::FindCallback callback) -> OCStackResult
195             {
196                 callback(nullptr);
197                 return OC_STACK_OK;
198             }
199         ).Return(OC_STACK_OK);
200
201     mocks.ExpectCallFunc(discovered);
202
203     discoverResource("", "", OCConnectivityType{ }, discovered);
204 }
205
206 TEST_F(DiscoverResourceTest, ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
207 {
208     mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).
209             Return(OC_STACK_ERROR);
210
211     EXPECT_THROW(discoverResource("", "", OCConnectivityType{ }, discovered), RCSPlatformException);
212 }
213