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(const std::string&, const std::string&,
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;
57 virtual OCConnectivityType connectivityType() const = 0;
59 virtual bool isObservable() const = 0;
62 class PrimitiveResourceTest: public TestWithMock
65 PrimitiveResource::Ptr resource;
66 FakeOCResource* fakeResource;
70 TestWithMock::SetUp();
72 fakeResource = mocks.Mock< FakeOCResource >();
74 resource.reset(new PrimitiveResourceImpl< FakeOCResource >{
75 std::shared_ptr< FakeOCResource >(fakeResource, [](FakeOCResource*) {}) });
79 TEST_F(PrimitiveResourceTest, RequestGetInvokesOCResourceGet)
81 mocks.ExpectCall(fakeResource, FakeOCResource::get).Return(OC_STACK_OK);
83 resource->requestGet(PrimitiveResource::GetCallback());
86 TEST_F(PrimitiveResourceTest, RequestGetThrowsOCResourceGetReturnsNotOK)
88 mocks.OnCall(fakeResource, FakeOCResource::get).Return(OC_STACK_ERROR);
90 ASSERT_THROW(resource->requestGet(PrimitiveResource::GetCallback()), RCSPlatformException);
93 TEST_F(PrimitiveResourceTest, RequestSetInvokesOCResourcePost)
95 mocks.ExpectCall(fakeResource, FakeOCResource::post).Return(OC_STACK_OK);
97 resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback());
100 TEST_F(PrimitiveResourceTest, RequestSetThrowsOCResourcePostReturnsNotOK)
102 mocks.OnCall(fakeResource, FakeOCResource::post).Return(OC_STACK_ERROR);
104 ASSERT_THROW(resource->requestSet(RCSResourceAttributes{ }, PrimitiveResource::SetCallback()),
105 RCSPlatformException);
108 TEST_F(PrimitiveResourceTest, RequestSetPassResourceAttributesToOCResourcePost)
110 constexpr int value{ -200 };
112 RCSResourceAttributes attrs;
114 mocks.ExpectCall(fakeResource, FakeOCResource::post).Match(
115 [](const std::string&, const std::string&, const OC::OCRepresentation& ocRep,
116 const OC::QueryParamsMap&, OC::PutCallback)
118 return ocRep.getValue<int>(KEY) == value;
120 ).Return(OC_STACK_OK);
124 resource->requestSet(attrs, PrimitiveResource::SetCallback());
127 TEST_F(PrimitiveResourceTest, RequestObserveInvokesOCResourceObserve)
129 mocks.ExpectCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_OK);
131 resource->requestObserve(PrimitiveResource::ObserveCallback());
134 TEST_F(PrimitiveResourceTest, RequestObserveThrowsOCResourceObserveReturnsNotOK)
136 mocks.OnCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_ERROR);
138 ASSERT_THROW(resource->requestObserve(PrimitiveResource::ObserveCallback()), RCSPlatformException);
141 TEST_F(PrimitiveResourceTest, DelegteGettersToOCResource)
143 const std::string host{ "host_test_" };
144 const std::string uri{ "uri/test/" };
146 mocks.OnCall(fakeResource, FakeOCResource::isObservable).Return(false);
147 mocks.OnCall(fakeResource, FakeOCResource::host).Return(host);
148 mocks.OnCall(fakeResource, FakeOCResource::uri).Return(uri);
150 ASSERT_FALSE(resource->isObservable());
151 ASSERT_EQ(host, resource->getHost());
152 ASSERT_EQ(uri, resource->getUri());
156 TEST_F(PrimitiveResourceTest, ResponseStatementHasSameValuesWithOCRepresentationReceived)
158 constexpr int errorCode{ 202 };
159 constexpr int value{ 1999 };
161 mocks.OnCall(fakeResource, FakeOCResource::get).Do(
162 [](const std::string&, const std::string&, const OC::QueryParamsMap&, OC::GetCallback cb)
164 OC::OCRepresentation ocRep;
167 cb(OC::HeaderOptions(), ocRep, errorCode);
170 ).Return(OC_STACK_OK);
172 resource->requestGet(
173 [&](const HeaderOptions&, const ResponseStatement& response, int e)
175 ASSERT_EQ(e, errorCode);
176 ASSERT_EQ(response.getAttributes().at(KEY).get<int>(), value);
182 class DiscoverResourceTest: public TestWithMock
185 typedef OCStackResult (*FindResource)(const std::string&, const std::string&,
186 OCConnectivityType, OC::FindCallback);
189 static void discovered(std::shared_ptr< PrimitiveResource >) {}
192 TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
194 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).Do(
195 [](const std::string&, const std::string&, OCConnectivityType,
196 OC::FindCallback callback) -> OCStackResult
201 ).Return(OC_STACK_OK);
203 mocks.ExpectCallFunc(discovered);
205 discoverResource("", "", OCConnectivityType{ }, discovered);
208 TEST_F(DiscoverResourceTest, ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
210 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).
211 Return(OC_STACK_ERROR);
213 EXPECT_THROW(discoverResource("", "", OCConnectivityType{ }, discovered), RCSPlatformException);