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 OC::QueryParamsMap&, OC::GetCallback) = 0;
40 virtual OCStackResult put(
41 const OC::OCRepresentation&, const OC::QueryParamsMap&, OC::PutCallback) = 0;
43 virtual OCStackResult observe(
44 OC::ObserveType, const OC::QueryParamsMap&, OC::ObserveCallback) = 0;
46 virtual OCStackResult cancelObserve() = 0;
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;
54 virtual bool isObservable() const = 0;
57 class PrimitiveResourceTest: public TestWithMock
60 PrimitiveResource::Ptr resource;
61 FakeOCResource* fakeResource;
65 TestWithMock::SetUp();
67 fakeResource = mocks.Mock< FakeOCResource >();
69 resource.reset(new PrimitiveResourceImpl< FakeOCResource >{
70 std::shared_ptr< FakeOCResource >(fakeResource, [](FakeOCResource*) {}) });
74 TEST_F(PrimitiveResourceTest, RequestGetInvokesOCResourceGet)
76 mocks.ExpectCall(fakeResource, FakeOCResource::get).Return(OC_STACK_OK);
78 resource->requestGet(PrimitiveResource::GetCallback());
81 TEST_F(PrimitiveResourceTest, RequestGetThrowsOCResourceGetReturnsNotOK)
83 mocks.OnCall(fakeResource, FakeOCResource::get).Return(OC_STACK_ERROR);
85 ASSERT_THROW(resource->requestGet(PrimitiveResource::GetCallback()), PlatformException);
88 TEST_F(PrimitiveResourceTest, RequestSetInvokesOCResourcePut)
90 mocks.ExpectCall(fakeResource, FakeOCResource::put).Return(OC_STACK_OK);
92 resource->requestSet(ResourceAttributes(), PrimitiveResource::SetCallback());
95 TEST_F(PrimitiveResourceTest, RequestSetThrowsOCResourcePutReturnsNotOK)
97 mocks.OnCall(fakeResource, FakeOCResource::put).Return(OC_STACK_ERROR);
99 ASSERT_THROW(resource->requestSet(ResourceAttributes(), PrimitiveResource::SetCallback()),
103 TEST_F(PrimitiveResourceTest, RequestSetPassResourceAttributesToOCResourcePut)
105 constexpr int value{ -200 };
107 ResourceAttributes attrs;
109 mocks.ExpectCall(fakeResource, FakeOCResource::put).Match(
110 [](const OC::OCRepresentation& ocRep, const OC::QueryParamsMap&, OC::PutCallback)
112 return ocRep.getValue<int>(KEY) == value;
114 ).Return(OC_STACK_OK);
118 resource->requestSet(attrs, PrimitiveResource::SetCallback());
121 TEST_F(PrimitiveResourceTest, RequestObserveInvokesOCResourceObserve)
123 mocks.ExpectCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_OK);
125 resource->requestObserve(PrimitiveResource::ObserveCallback());
128 TEST_F(PrimitiveResourceTest, RequestObserveThrowsOCResourceObserveReturnsNotOK)
130 mocks.OnCall(fakeResource, FakeOCResource::observe).Return(OC_STACK_ERROR);
132 ASSERT_THROW(resource->requestObserve(PrimitiveResource::ObserveCallback()), PlatformException);
135 TEST_F(PrimitiveResourceTest, DelegteGettersToOCResource)
137 const std::string host{ "host_test_" };
138 const std::string uri{ "uri/test/" };
140 mocks.OnCall(fakeResource, FakeOCResource::isObservable).Return(false);
141 mocks.OnCall(fakeResource, FakeOCResource::host).Return(host);
142 mocks.OnCall(fakeResource, FakeOCResource::uri).Return(uri);
144 ASSERT_FALSE(resource->isObservable());
145 ASSERT_EQ(host, resource->getHost());
146 ASSERT_EQ(uri, resource->getUri());
150 TEST_F(PrimitiveResourceTest, ResponseStatementHasSameValuesWithOCRepresentationReceived)
152 constexpr int errorCode{ 202 };
153 constexpr int value{ 1999 };
155 mocks.OnCall(fakeResource, FakeOCResource::get).Do(
156 [](const OC::QueryParamsMap&, OC::GetCallback cb)
158 OC::OCRepresentation ocRep;
161 cb(OC::HeaderOptions(), ocRep, errorCode);
164 ).Return(OC_STACK_OK);
166 resource->requestGet(
167 [&](const HeaderOptions&, const ResponseStatement& response, int e)
169 ASSERT_EQ(e, errorCode);
170 ASSERT_EQ(response.getAttributes().at(KEY).get<int>(), value);
176 class DiscoverResourceTest: public TestWithMock
179 typedef OCStackResult (*FindResource)(const std::string&, const std::string&,
180 OCConnectivityType, OC::FindCallback);
183 static void discovered(std::shared_ptr< PrimitiveResource >) {}
186 TEST_F(DiscoverResourceTest, CallbackIsInvokedWhenResourceIsDiscovered)
188 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).Do(
189 [](const std::string&, const std::string&, OCConnectivityType,
190 OC::FindCallback callback) -> OCStackResult
195 ).Return(OC_STACK_OK);
197 mocks.ExpectCallFunc(discovered);
199 discoverResource("", "", OCConnectivityType{ }, discovered);
202 TEST_F(DiscoverResourceTest, ThrowsdWhenOCPlatformFindResourceReturnsNotOK)
204 mocks.ExpectCallFuncOverload(static_cast<FindResource>(OC::OCPlatform::findResource)).
205 Return(OC_STACK_ERROR);
207 EXPECT_THROW(discoverResource("", "", OCConnectivityType{ }, discovered), PlatformException);