1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
24 #include <PrimitiveResourceImpl.h>
25 #include <AssertUtils.h>
27 #include <OCResource.h>
28 #include <OCPlatform.h>
30 using namespace testing;
31 using namespace OIC::Service;
33 const std::string KEY{ "key" };
38 virtual ~FakeOCResource() {};
40 virtual OCStackResult get(const OC::QueryParamsMap&, OC::GetCallback) = 0;
42 virtual OCStackResult put(
43 const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PutCallback) = 0;
45 virtual OCStackResult observe(
46 OC::ObserveType, const OC::QueryParamsMap&, OC::ObserveCallback) = 0;
48 virtual OCStackResult cancelObserve() = 0;
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;
56 virtual bool isObservable() const = 0;
59 class PrimitiveResourceTest: public Test
63 PrimitiveResource::Ptr resource;
64 FakeOCResource* fakeResource;
68 fakeResource = mocks.Mock< FakeOCResource >();
70 resource.reset(new PrimitiveResourceImpl< FakeOCResource >{
71 std::shared_ptr< FakeOCResource >(fakeResource, [](FakeOCResource*) {}) });
75 TEST_F(PrimitiveResourceTest, RequestGetInvokesOCResourceGet)
77 mocks.ExpectCall(fakeResource, FakeOCResource::get).Return(OC_STACK_OK);
79 resource->requestGet(PrimitiveResource::GetCallback());
82 TEST_F(PrimitiveResourceTest, RequestGetThrowsOCResourceGetReturnsNotOK)
84 mocks.OnCall(fakeResource, FakeOCResource::get).Return(OC_STACK_ERROR);
86 ASSERT_THROW(resource->requestGet(PrimitiveResource::GetCallback()), PlatformException);
89 TEST_F(PrimitiveResourceTest, RequestSetInvokesOCResourcePut)
91 mocks.ExpectCall(fakeResource, FakeOCResource::put).Return(OC_STACK_OK);
93 resource->requestSet(ResourceAttributes(), PrimitiveResource::SetCallback());
96 TEST_F(PrimitiveResourceTest, RequestSetThrowsOCResourcePutReturnsNotOK)
98 mocks.OnCall(fakeResource, FakeOCResource::put).Return(OC_STACK_ERROR);
100 ASSERT_THROW(resource->requestSet(ResourceAttributes(), PrimitiveResource::SetCallback()),
104 TEST_F(PrimitiveResourceTest, RequestSetPassResourceAttributesToOCResourcePut)
106 constexpr int value{ -200 };
108 ResourceAttributes attrs;
110 mocks.ExpectCall(fakeResource, FakeOCResource::put).Match(
111 [](const OC::OCRepresentation& ocRep, const OC::QueryParamsMap&, OC::PutCallback)
113 return ocRep.getValue<int>(KEY) == value;
115 ).Return(OC_STACK_OK);
119 resource->requestSet(attrs, PrimitiveResource::SetCallback());
122 TEST_F(PrimitiveResourceTest, RequestObserveInvokesOCResourceObserve)
124 mocks.ExpectCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_OK);
126 resource->requestObserve(PrimitiveResource::ObserveCallback());
129 TEST_F(PrimitiveResourceTest, RequestObserveThrowsOCResourceObserveReturnsNotOK)
131 mocks.OnCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_ERROR);
133 ASSERT_THROW(resource->requestObserve(PrimitiveResource::ObserveCallback()), PlatformException);
136 TEST_F(PrimitiveResourceTest, DelegteGettersToOCResource)
138 const std::string host{ "host_test_" };
139 const std::string uri{ "uri/test/" };
141 mocks.OnCall(fakeResource, FakeOCResource::isObservable).Return(false);
142 mocks.OnCall(fakeResource, FakeOCResource::host).Return(host);
143 mocks.OnCall(fakeResource, FakeOCResource::uri).Return(uri);
145 ASSERT_FALSE(resource->isObservable());
146 ASSERT_EQ(host, resource->getHost());
147 ASSERT_EQ(uri, resource->getUri());
151 TEST_F(PrimitiveResourceTest, ResponseStatementHasSameValuesWithOCRepresentationReceived)
153 constexpr int errorCode{ 202 };
154 constexpr int value{ 1999 };
156 mocks.OnCall(fakeResource, FakeOCResource::get).Do(
157 [](const OC::QueryParamsMap&, OC::GetCallback cb)
159 OC::OCRepresentation ocRep;
162 cb(OC::HeaderOptions(), ocRep, errorCode);
165 ).Return(OC_STACK_OK);
167 resource->requestGet(
168 [&](const HeaderOptions&, const ResponseStatement& response, int e)
170 ASSERT_EQ(e, errorCode);
171 ASSERT_EQ(response.getAttributes().at(KEY).get<int>(), value);
177 class DiscoverResourceTest: public Test
180 MockRepository mocks;
182 typedef OCStackResult (*FindResource)(const std::string&, const std::string&,
183 OCConnectivityType, OC::FindCallback);
185 static void discovered(std::shared_ptr< PrimitiveResource >)
191 TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
193 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).Do(
194 [](const std::string&, const std::string&, OCConnectivityType,
195 OC::FindCallback callback) -> OCStackResult
200 ).Return(OC_STACK_OK);
202 mocks.ExpectCallFunc(discovered);
204 discoverResource("", "", OCConnectivityType{ }, discovered);
207 TEST_F(DiscoverResourceTest, ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
209 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).
210 Return(OC_STACK_ERROR);
212 EXPECT_THROW(discoverResource("", "", OCConnectivityType{ }, discovered), PlatformException);