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