replace : iotivity -> iotivity-sec
[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(const std::string&, const std::string&,
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     virtual OCConnectivityType connectivityType() const = 0;
58
59     virtual bool isObservable() const = 0;
60 };
61
62 class PrimitiveResourceTest: public TestWithMock
63 {
64 public:
65     PrimitiveResource::Ptr resource;
66     FakeOCResource* fakeResource;
67
68 protected:
69     void SetUp() {
70         TestWithMock::SetUp();
71
72         fakeResource = mocks.Mock< FakeOCResource >();
73
74         resource.reset(new PrimitiveResourceImpl< FakeOCResource >{
75             std::shared_ptr< FakeOCResource >(fakeResource, [](FakeOCResource*) {}) });
76     }
77 };
78
79 TEST_F(PrimitiveResourceTest, RequestGetInvokesOCResourceGet)
80 {
81     mocks.ExpectCall(fakeResource, FakeOCResource::get).Return(OC_STACK_OK);
82
83     resource->requestGet(PrimitiveResource::GetCallback());
84 }
85
86 TEST_F(PrimitiveResourceTest, RequestGetThrowsOCResourceGetReturnsNotOK)
87 {
88     mocks.OnCall(fakeResource, FakeOCResource::get).Return(OC_STACK_ERROR);
89
90     ASSERT_THROW(resource->requestGet(PrimitiveResource::GetCallback()), RCSPlatformException);
91 }
92
93 TEST_F(PrimitiveResourceTest, RequestSetInvokesOCResourcePost)
94 {
95     mocks.ExpectCall(fakeResource, FakeOCResource::post).Return(OC_STACK_OK);
96
97     resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback());
98 }
99
100 TEST_F(PrimitiveResourceTest, RequestSetThrowsOCResourcePostReturnsNotOK)
101 {
102     mocks.OnCall(fakeResource, FakeOCResource::post).Return(OC_STACK_ERROR);
103
104     ASSERT_THROW(resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback()),
105             RCSPlatformException);
106 }
107
108 TEST_F(PrimitiveResourceTest, RequestSetPassResourceAttributesToOCResourcePost)
109 {
110     constexpr int value{ -200 };
111
112     RCSResourceAttributes attrs;
113
114     mocks.ExpectCall(fakeResource, FakeOCResource::post).Match(
115             [](const std::string&, const std::string&, const OC::OCRepresentation& ocRep,
116                     const OC::QueryParamsMap&, OC::PutCallback)
117             {
118                 return ocRep.getValue<int>(KEY) == value;
119             }
120         ).Return(OC_STACK_OK);
121
122     attrs[KEY] = value;
123
124     resource->requestSet(attrs, PrimitiveResource::SetCallback());
125 }
126
127 TEST_F(PrimitiveResourceTest, RequestObserveInvokesOCResourceObserve)
128 {
129     mocks.ExpectCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_OK);
130
131     resource->requestObserve(PrimitiveResource::ObserveCallback());
132 }
133
134 TEST_F(PrimitiveResourceTest, RequestObserveThrowsOCResourceObserveReturnsNotOK)
135 {
136     mocks.OnCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_ERROR);
137
138     ASSERT_THROW(resource->requestObserve(PrimitiveResource::ObserveCallback()), RCSPlatformException);
139 }
140
141 TEST_F(PrimitiveResourceTest, DelegteGettersToOCResource)
142 {
143     const std::string host{ "host_test_" };
144     const std::string uri{ "uri/test/" };
145
146     mocks.OnCall(fakeResource, FakeOCResource::isObservable).Return(false);
147     mocks.OnCall(fakeResource, FakeOCResource::host).Return(host);
148     mocks.OnCall(fakeResource, FakeOCResource::uri).Return(uri);
149
150     ASSERT_FALSE(resource->isObservable());
151     ASSERT_EQ(host, resource->getHost());
152     ASSERT_EQ(uri, resource->getUri());
153 }
154
155
156 TEST_F(PrimitiveResourceTest, ResponseStatementHasSameValuesWithOCRepresentationReceived)
157 {
158     constexpr int errorCode{ 202 };
159     constexpr int value{ 1999 };
160
161     mocks.OnCall(fakeResource, FakeOCResource::get).Do(
162             [](const std::string&, const std::string&, const OC::QueryParamsMap&, OC::GetCallback cb)
163             {
164                 OC::OCRepresentation ocRep;
165                 ocRep[KEY] = value;
166
167                 cb(OC::HeaderOptions(), ocRep, errorCode);
168                 return OC_STACK_OK;
169             }
170         ).Return(OC_STACK_OK);
171
172     resource->requestGet(
173             [&](const HeaderOptions&, const ResponseStatement& response, int e)
174             {
175                 ASSERT_EQ(e, errorCode);
176                 ASSERT_EQ(response.getAttributes().at(KEY).get<int>(), value);
177             }
178         );
179 }
180
181
182 class DiscoverResourceTest: public TestWithMock
183 {
184 public:
185     typedef OCStackResult (*FindResource)(const std::string&, const std::string&,
186             OCConnectivityType, OC::FindCallback);
187
188 public:
189     static void discovered(std::shared_ptr< PrimitiveResource >) {}
190 };
191
192 TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
193 {
194     mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).Do(
195             [](const std::string&, const std::string&, OCConnectivityType,
196                     OC::FindCallback callback) -> OCStackResult
197             {
198                 callback(nullptr);
199                 return OC_STACK_OK;
200             }
201         ).Return(OC_STACK_OK);
202
203     mocks.ExpectCallFunc(discovered);
204
205     discoverResource("", "", OCConnectivityType{ }, discovered);
206 }
207
208 TEST_F(DiscoverResourceTest, ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
209 {
210     mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).
211             Return(OC_STACK_ERROR);
212
213     EXPECT_THROW(discoverResource("", "", OCConnectivityType{ }, discovered), RCSPlatformException);
214 }
215