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