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 "UnitTestHelper.h"
23 #include "PrimitiveResourceImpl.h"
24 #include "AssertUtils.h"
26 #include "OCResource.h"
27 #include "OCPlatform.h"
29 using namespace OIC::Service;
31 const std::string KEY{ "key" };
36 virtual ~FakeOCResource() {};
38 virtual OCStackResult get(const std::string&, const std::string&,
39 const OC::QueryParamsMap&, OC::GetCallback) = 0;
41 virtual OCStackResult put(
42 const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PutCallback) = 0;
44 virtual OCStackResult post(
45 const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PostCallback) = 0;
47 virtual OCStackResult observe(
48 OC::ObserveType, const OC::QueryParamsMap&, OC::ObserveCallback) = 0;
50 virtual OCStackResult cancelObserve() = 0;
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;
58 virtual bool isObservable() const = 0;
61 class PrimitiveResourceTest: public TestWithMock
64 PrimitiveResource::Ptr resource;
65 FakeOCResource* fakeResource;
69 TestWithMock::SetUp();
71 fakeResource = mocks.Mock< FakeOCResource >();
73 resource.reset(new PrimitiveResourceImpl< FakeOCResource >{
74 std::shared_ptr< FakeOCResource >(fakeResource, [](FakeOCResource*) {}) });
78 TEST_F(PrimitiveResourceTest, RequestGetInvokesOCResourceGet)
80 mocks.ExpectCall(fakeResource, FakeOCResource::get).Return(OC_STACK_OK);
82 resource->requestGet(PrimitiveResource::GetCallback());
85 TEST_F(PrimitiveResourceTest, RequestGetThrowsOCResourceGetReturnsNotOK)
87 mocks.OnCall(fakeResource, FakeOCResource::get).Return(OC_STACK_ERROR);
89 ASSERT_THROW(resource->requestGet(PrimitiveResource::GetCallback()), RCSPlatformException);
92 TEST_F(PrimitiveResourceTest, RequestSetInvokesOCResourcePost)
94 mocks.ExpectCall(fakeResource, FakeOCResource::post).Return(OC_STACK_OK);
96 resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback());
99 TEST_F(PrimitiveResourceTest, RequestSetThrowsOCResourcePostReturnsNotOK)
101 mocks.OnCall(fakeResource, FakeOCResource::post).Return(OC_STACK_ERROR);
103 ASSERT_THROW(resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback()),
104 RCSPlatformException);
107 TEST_F(PrimitiveResourceTest, RequestSetPassResourceAttributesToOCResourcePost)
109 constexpr int value{ -200 };
111 RCSResourceAttributes attrs;
113 mocks.ExpectCall(fakeResource, FakeOCResource::post).Match(
114 [](const OC::OCRepresentation& ocRep, const OC::QueryParamsMap&, OC::PutCallback)
116 return ocRep.getValue<int>(KEY) == value;
118 ).Return(OC_STACK_OK);
122 resource->requestSet(attrs, PrimitiveResource::SetCallback());
125 TEST_F(PrimitiveResourceTest, RequestObserveInvokesOCResourceObserve)
127 mocks.ExpectCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_OK);
129 resource->requestObserve(PrimitiveResource::ObserveCallback());
132 TEST_F(PrimitiveResourceTest, RequestObserveThrowsOCResourceObserveReturnsNotOK)
134 mocks.OnCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_ERROR);
136 ASSERT_THROW(resource->requestObserve(PrimitiveResource::ObserveCallback()), RCSPlatformException);
139 TEST_F(PrimitiveResourceTest, DelegteGettersToOCResource)
141 const std::string host{ "host_test_" };
142 const std::string uri{ "uri/test/" };
144 mocks.OnCall(fakeResource, FakeOCResource::isObservable).Return(false);
145 mocks.OnCall(fakeResource, FakeOCResource::host).Return(host);
146 mocks.OnCall(fakeResource, FakeOCResource::uri).Return(uri);
148 ASSERT_FALSE(resource->isObservable());
149 ASSERT_EQ(host, resource->getHost());
150 ASSERT_EQ(uri, resource->getUri());
154 TEST_F(PrimitiveResourceTest, ResponseStatementHasSameValuesWithOCRepresentationReceived)
156 constexpr int errorCode{ 202 };
157 constexpr int value{ 1999 };
159 mocks.OnCall(fakeResource, FakeOCResource::get).Do(
160 [](const std::string&, const std::string&, const OC::QueryParamsMap&, OC::GetCallback cb)
162 OC::OCRepresentation ocRep;
165 cb(OC::HeaderOptions(), ocRep, errorCode);
168 ).Return(OC_STACK_OK);
170 resource->requestGet(
171 [&](const HeaderOptions&, const ResponseStatement& response, int e)
173 ASSERT_EQ(e, errorCode);
174 ASSERT_EQ(response.getAttributes().at(KEY).get<int>(), value);
180 class DiscoverResourceTest: public TestWithMock
183 typedef OCStackResult (*FindResource)(const std::string&, const std::string&,
184 OCConnectivityType, OC::FindCallback);
187 static void discovered(std::shared_ptr< PrimitiveResource >) {}
190 TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
192 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).Do(
193 [](const std::string&, const std::string&, OCConnectivityType,
194 OC::FindCallback callback) -> OCStackResult
199 ).Return(OC_STACK_OK);
201 mocks.ExpectCallFunc(discovered);
203 discoverResource("", "", OCConnectivityType{ }, discovered);
206 TEST_F(DiscoverResourceTest, ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
208 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).
209 Return(OC_STACK_ERROR);
211 EXPECT_THROW(discoverResource("", "", OCConnectivityType{ }, discovered), RCSPlatformException);