2 #include <gtest/gtest.h>
4 #include "ResourceClient.h"
5 #include "RCSResourceObject.h"
6 #include "OCPlatform.h"
8 #define RESOURCEURI "/a/TemperatureSensor"
9 #define RESOURCETYPE "Resource.Hosting"
10 #define RESOURCEINTERFACE "oic.if.baseline"
12 using namespace OIC::Service;
15 bool cbresult = false;
16 std::string uri = "/oic/res?rt=Resource.Hosting";
17 std::shared_ptr<RemoteResourceObject> object;
18 DiscoveryManager *manager = DiscoveryManager::getInstance();
19 ResourceState receivedResourceState;
20 ResourceAttributes receivedResourceAttributes;
21 RCSResourceObject::Ptr server;
25 server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE,
26 RESOURCEINTERFACE).setDiscoverable(true).setObservable(true).build();
27 std::cout << "Resource created successfully " << std::endl;
28 server->setAttribute("Temperature", 0);
31 OC::ServiceType::InProc, ModeType::Client, "0.0.0.0", 0, OC::QualityOfService::LowQos
33 OCPlatform::Configure(config);
36 void destroyResource()
42 void onRemoteAttrReceived(const ResourceAttributes &attributes)
45 receivedResourceAttributes = attributes;
48 void onResourceDiscoveredCallback(std::shared_ptr<RemoteResourceObject> receivedObject)
50 std::cout << "Resource discovered" << std::endl;
51 object = receivedObject;
52 std::cout << object->getUri() << std::endl;
53 std::cout << "Resource discovered exited" << std::endl;
54 EXPECT_TRUE(object != NULL);
57 void onResourceStateChanged(ResourceState state)
59 receivedResourceState = state;
62 void onCacheUpdated(const ResourceAttributes attribute)
64 receivedResourceAttributes = attribute;
67 void OnRemoteAttributesSetCallback(const ResourceAttributes &attributes)
72 //Send valid call to discover resource
73 TEST(ResourceClientTest, testDiscoverResourcePass)
76 DiscoveryManager *instance = DiscoveryManager::getInstance();
79 instance->discoverResource("", uri, CT_DEFAULT, &onResourceDiscoveredCallback);
81 EXPECT_TRUE(object != NULL);
85 //Get remote attributes when callback is valid
86 TEST(ResourceClientTest, testGetRemoteAttributesPass)
91 object->getRemoteAttributes(&onRemoteAttrReceived);
94 EXPECT_TRUE(cbresult);
98 //Set remote attributes when callback is valid
99 TEST(ResourceClientTest, testSetRemoteAttributesPass)
105 object->getRemoteAttributes(&onRemoteAttrReceived);
107 ResourceAttributes::const_iterator iter = receivedResourceAttributes.begin();
108 for (unsigned int i = 0; i < receivedResourceAttributes.size(); ++i)
110 if ( iter->key() == "Temperature")
112 std::cout << "It is there" << std::endl;
113 receivedResourceAttributes["Temperature"] = 100;
114 object->setRemoteAttributes(receivedResourceAttributes, &OnRemoteAttributesSetCallback);
119 EXPECT_TRUE(cbresult);
123 //Check value of isMonitoring
124 TEST(ResourceClientTest, testIsMonitoring)
127 manager->DiscoveryManager::discoverResource("", uri, CT_DEFAULT, &onResourceDiscoveredCallback);
130 EXPECT_FALSE(object->isMonitoring());
133 //Check value of isCaching
134 TEST(ResourceClientTest, testIsCaching)
138 EXPECT_FALSE(object->isCaching());
141 TEST(ResourceClientTest, testIsObservable)
145 EXPECT_TRUE(object->isObservable());
148 //Check value of startMonitoring for NULL callback
149 TEST(ResourceClientTest, testStartMonitoringFail)
152 object->m_monitoringFlag = false;
153 object->startMonitoring(NULL);
154 EXPECT_TRUE(object->m_monitoringFlag);
155 object->stopMonitoring();
159 //Check value of startMonitoring for valid callback
160 TEST(ResourceClientTest, testStartMonitoringPass)
163 object->startMonitoring(&onResourceStateChanged);
164 EXPECT_TRUE(object->m_monitoringFlag);
165 object->stopMonitoring();
169 //Stop Monitoring when Monitoring is true
170 TEST(ResourceClientTest, testStopMonitoring)
173 object->startMonitoring(&onResourceStateChanged);
174 object->stopMonitoring();
175 EXPECT_FALSE(object->m_monitoringFlag);
176 object->startMonitoring(&onResourceStateChanged);
180 //Get state of resource
181 TEST(ResourceClientTest, testGetStatePass)
184 ResourceState result = object->getState();
185 EXPECT_TRUE(result >= ResourceState::NOT_MONITORING && result <= ResourceState::DESTROYED);
189 //Cache ID is not zero
190 TEST(ResourceClientTest, testStartCachingPass)
193 object->m_cachingFlag = false;
194 object->startCaching();
195 EXPECT_TRUE(object->m_cachingFlag);
199 TEST(ResourceClientTest, testStopCaching)
202 object->m_cachingFlag = false;
203 object->startCaching();
204 object->stopCaching();
205 EXPECT_FALSE(object->m_cachingFlag);
209 TEST(ResourceClientTest, testRefreshCache)
212 object->m_cachingFlag = false;
213 object->startCaching();
214 object->refreshCache();
215 EXPECT_TRUE(object->getResourceCacheState() == CacheState::UPDATING);
219 //Callback for start caching is valid
220 TEST(ResourceClientTest, testStartCachingCbPass)
223 object->m_cachingFlag = false;
224 object->startCaching(&onCacheUpdated);
225 EXPECT_TRUE(object->m_cachingFlag);
229 //Callback for start caching is NULL
230 TEST(ResourceClientTest, testStartCachingCbFail)
233 object->m_cachingFlag = false;
234 object->startCaching(NULL);
235 EXPECT_TRUE(object->m_cachingFlag);
240 TEST(ResourceClientTest, testGetResourceCacheState)
243 CacheState result = object->getResourceCacheState();
244 EXPECT_TRUE(result >= CacheState::READY && result <= CacheState::NONE);
248 //Get cached attributes
249 TEST(ResourceClientTest, testGetCachedAttributesWithoutCallback)
252 ResourceAttributes result = object->getCachedAttributes();
253 EXPECT_TRUE(result.empty());
257 //Check with invalid attribute value
258 TEST(ResourceClientTest, testGetCachedAttributeWithInvalidAttribute)
261 ResourceAttributes::Value result = object->getCachedAttribute("");
262 EXPECT_TRUE(result == nullptr);
266 //Get remote attributes when callback is NULL
267 TEST(ResourceClientTest, testGetRemoteAttributesFail)
271 object->getRemoteAttributes(NULL);
272 EXPECT_FALSE(cbresult);
276 TEST(ResourceClientTest, testGetUri)
279 std::string result = object->getUri();
280 EXPECT_TRUE(!result.empty());
284 TEST(ResourceClientTest, testGetAddress)
287 std::string result = object->getAddress();
288 EXPECT_TRUE(!result.empty());
292 TEST(ResourceClientTest, testGetTypes)
295 std::vector < std::string > result = object->getTypes();
296 EXPECT_TRUE(result.size() != 0);
300 TEST(ResourceClientTest, testGetInterfaces)
303 std::vector < std::string > result = object->getInterfaces();
304 EXPECT_TRUE(result.size() != 0);
308 //Check with valid attribute value
309 TEST(ResourceClientTest, testGetCachedAttribute)
312 ResourceAttributes::Value result = object->getCachedAttribute("Temperature");
313 EXPECT_TRUE(result != nullptr);
317 //Test getting instance of DiscoveryManager
318 TEST(ResourceClientTest, testGetInstance)
321 DiscoveryManager *instance = DiscoveryManager::getInstance();
322 EXPECT_TRUE(instance != NULL);
326 //Send empty resource URI
327 TEST(ResourceClientTest, testDiscoverResourceEmptyResource)
330 DiscoveryManager *instance = DiscoveryManager::getInstance();
331 EXPECT_THROW(instance->discoverResource("", "", CT_DEFAULT, &onResourceDiscoveredCallback),
332 InvalidParameterException);
337 TEST(ResourceClientTest, testDiscoverResourceEmptyCallback)
340 DiscoveryManager *instance = DiscoveryManager::getInstance();
341 EXPECT_THROW(instance->discoverResource("", uri, CT_DEFAULT, NULL), InvalidParameterException);
343 object->stopMonitoring();
346 //Send invalid ResourceAttributes object to function
347 TEST(ResourceClientTest, testSetRemoteAttributesInvalidAttributes)
351 ResourceAttributes attr;
352 //object->getRemoteAttributes(&onRemoteAttrReceived);
353 object->setRemoteAttributes(attr, &OnRemoteAttributesSetCallback);
354 EXPECT_FALSE(cbresult);