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