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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include <gtest/gtest.h>
26 #include <HippoMocks/hippomocks.h>
28 #include "Configuration.h"
29 #include "BundleResource.h"
30 #include "ResourceContainer.h"
31 #include "ResourceContainerBundleAPI.h"
32 #include "ResourceContainerImpl.h"
34 #include "ResourceObject.h"
37 using namespace testing;
38 using namespace OIC::Service;
40 string CONFIG_FILE = "ResourceContainerTestConfig.xml";
42 /*Fake bundle resource class for testing*/
43 class TestBundleResource: public BundleResource
46 string getAttribute(string attributeName)
51 void setAttribute(string attributeName, string value)
57 BundleResource::setAttribute("attri", "test");
62 class ResourceContainerTest: public Test
65 ResourceContainer *m_pResourceContainer;
70 m_pResourceContainer = ResourceContainer::getInstance();
74 TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfigFile)
76 m_pResourceContainer->startContainer(CONFIG_FILE);
78 EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
79 EXPECT_STREQ("oic.bundle.test",
80 (*m_pResourceContainer->listBundles().begin())->getID().c_str());
81 EXPECT_STREQ("libTestBundle.so",
82 (*m_pResourceContainer->listBundles().begin())->getPath().c_str());
83 EXPECT_STREQ("1.0.0", (*m_pResourceContainer->listBundles().begin())->getVersion().c_str());
85 m_pResourceContainer->stopContainer();
88 TEST_F(ResourceContainerTest, BundleLoadedWhenContainerStartedWithValidConfigFile)
90 m_pResourceContainer->startContainer(CONFIG_FILE);
92 EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
93 EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
95 ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleHandle());
97 m_pResourceContainer->stopContainer();
100 TEST_F(ResourceContainerTest, BundleActivatedWhenContainerStartedWithValidConfigFile)
102 m_pResourceContainer->startContainer(CONFIG_FILE);
104 EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
106 ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
108 ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleActivator());
110 m_pResourceContainer->stopContainer();
113 TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithInvalidConfigFile)
115 m_pResourceContainer->startContainer("invalideConfig");
117 EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
120 TEST_F(ResourceContainerTest, BundleUnregisteredWhenContainerStopped)
122 m_pResourceContainer->startContainer(CONFIG_FILE);
123 m_pResourceContainer->stopContainer();
125 EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
128 TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI)
130 m_pResourceContainer->startContainer(CONFIG_FILE);
131 m_pResourceContainer->stopBundle("oic.bundle.test");
134 ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
137 TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI)
139 m_pResourceContainer->startContainer(CONFIG_FILE);
140 m_pResourceContainer->stopBundle("oic.bundle.test");
141 m_pResourceContainer->startBundle("oic.bundle.test");
144 ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
147 class ResourceContainerBundleAPITest: public Test
150 MockRepository mocks;
151 ResourceObject *m_pResourceObject;
152 ResourceContainerBundleAPI *m_pResourceContainer;
153 TestBundleResource *m_pBundleResource;
156 void SetUp() override
158 m_pResourceObject = mocks.Mock<ResourceObject>();
159 m_pResourceContainer = ResourceContainerBundleAPI::getInstance();
161 m_pBundleResource = new TestBundleResource();
162 m_pBundleResource->m_bundleId = "oic.bundle.test";
163 m_pBundleResource->m_uri = "/test_resource";
164 m_pBundleResource->m_resourceType = "oic.test";
168 TEST_F(ResourceContainerBundleAPITest, ResourceServerCreatedWhenRegisterResourceCalled)
170 mocks.ExpectCallFunc(ResourceContainerImpl::buildResourceObject).With(m_pBundleResource->m_uri,
171 m_pBundleResource->m_resourceType).Return(nullptr);
173 m_pResourceContainer->registerResource(m_pBundleResource);
176 TEST_F(ResourceContainerBundleAPITest, RequestHandlerForResourceServerSetWhenRegisterResourceCalled)
178 mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
179 ResourceObject::Ptr(m_pResourceObject, [](ResourceObject *)
182 mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
183 mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
185 m_pResourceContainer->registerResource(m_pBundleResource);
188 TEST_F(ResourceContainerBundleAPITest, BundleResourceUnregisteredWhenUnregisterResourceCalled)
190 mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
191 ResourceObject::Ptr(m_pResourceObject, [](ResourceObject *)
194 mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
195 mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
197 m_pResourceContainer->registerResource(m_pBundleResource);
198 m_pResourceContainer->unregisterResource(m_pBundleResource);
200 EXPECT_EQ((unsigned int) 0,
201 ((ResourceContainerImpl *)m_pResourceContainer)->listBundleResources(
202 m_pBundleResource->m_bundleId).size());
205 TEST_F(ResourceContainerBundleAPITest,
206 ServerNotifiesToObserversWhenNotificationReceivedFromResource)
208 mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
209 ResourceObject::Ptr(m_pResourceObject, [](ResourceObject *)
212 mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
213 mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
215 m_pResourceContainer->registerResource(m_pBundleResource);
217 mocks.ExpectCall(m_pResourceObject, ResourceObject::notify);
219 m_pResourceContainer->onNotificationReceived(m_pBundleResource->m_uri);
222 TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleId)
225 map< string, string > results;
227 m_pResourceContainer->getBundleConfiguration("oic.bundle.test", &bundle);
229 results = *bundle.begin();
231 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
232 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
233 EXPECT_STREQ("1.0.0", results["version"].c_str());
236 TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed)
238 vector< resourceInfo > resourceConfig;
241 m_pResourceContainer->getResourceConfiguration("oic.bundle.test", &resourceConfig);
243 result = *resourceConfig.begin();
245 EXPECT_STREQ("test_resource", result.name.c_str());
246 EXPECT_STREQ("oic.test", result.resourceType.c_str());
249 class ResourceContainerImplTest: public Test
252 MockRepository mocks;
253 ResourceContainerImpl *m_pResourceContainer;
254 BundleInfo *m_pBundleInfo;
257 void SetUp() override
259 m_pResourceContainer = ResourceContainerImpl::getImplInstance();
260 m_pBundleInfo = BundleInfo::build();
264 TEST_F(ResourceContainerImplTest, SoBundleLoadedWhenRegisteredWithRegisterBundleAPI)
266 m_pBundleInfo->setPath("libTestBundle.so");
267 m_pBundleInfo->setVersion("1.0");
268 m_pBundleInfo->setLibraryPath(".");
269 m_pBundleInfo->setID("oic.bundle.test");
271 m_pResourceContainer->registerBundle(m_pBundleInfo);
273 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
276 TEST_F(ResourceContainerImplTest, JavaBundleLoadedWhenRegisteredWithRegisterBundleAPIWrongPath)
278 m_pBundleInfo->setPath("wrong_path.jar");
279 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
280 m_pBundleInfo->setLibraryPath("../.");
281 m_pBundleInfo->setVersion("1.0");
282 m_pBundleInfo->setID("oic.bundle.java.test");
284 m_pResourceContainer->registerBundle(m_pBundleInfo);
285 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
288 TEST_F(ResourceContainerImplTest, JavaBundleTest)
290 m_pBundleInfo->setPath("TestBundleJava/hue-0.1-jar-with-dependencies.jar");
291 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
292 m_pBundleInfo->setLibraryPath("../.");
293 m_pBundleInfo->setVersion("1.0");
294 m_pBundleInfo->setID("oic.bundle.java.test");
296 m_pResourceContainer->registerBundle(m_pBundleInfo);
297 EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
299 m_pResourceContainer->activateBundle(m_pBundleInfo);
300 EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
302 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
303 EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
306 TEST_F(ResourceContainerImplTest, BundleNotRegisteredIfBundlePathIsInvalid)
308 m_pBundleInfo->setPath("");
309 m_pBundleInfo->setVersion("1.0");
310 m_pBundleInfo->setLibraryPath("../.");
311 m_pBundleInfo->setID("oic.bundle.test");
313 m_pResourceContainer->registerBundle(m_pBundleInfo);
315 EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
319 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithValidBundleInfo)
321 m_pBundleInfo->setPath("libTestBundle.so");
322 m_pBundleInfo->setVersion("1.0");
323 m_pBundleInfo->setLibraryPath("../.");
324 m_pBundleInfo->setID("oic.bundle.test");
326 m_pResourceContainer->registerBundle(m_pBundleInfo);
327 m_pResourceContainer->activateBundle(m_pBundleInfo);
329 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
332 /*TEST_F(ResourceContainerImplTest, JavaBundleActivatedWithValidBundleInfo)
334 m_pBundleInfo->setPath("TestBundleJava/hue-0.1-jar-with-dependencies.jar");
335 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
336 m_pBundleInfo->setLibraryPath("../.");
337 m_pBundleInfo->setVersion("1.0");
338 m_pBundleInfo->setID("oic.bundle.java.test2");
340 m_pResourceContainer->registerBundle(m_pBundleInfo);
341 m_pResourceContainer->activateBundle(m_pBundleInfo);
342 EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
346 TEST_F(ResourceContainerImplTest, BundleNotActivatedWhenNotRegistered)
348 m_pBundleInfo->setPath("libTestBundle.so");
349 m_pBundleInfo->setVersion("1.0");
350 m_pBundleInfo->setLibraryPath("../.");
351 m_pBundleInfo->setID("oic.bundle.test");
353 m_pResourceContainer->activateBundle(m_pBundleInfo);
355 EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
358 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithBundleID)
360 m_pBundleInfo->setPath("libTestBundle.so");
361 m_pBundleInfo->setVersion("1.0");
362 m_pBundleInfo->setLibraryPath("../.");
363 m_pBundleInfo->setID("oic.bundle.test");
365 m_pResourceContainer->registerBundle(m_pBundleInfo);
366 m_pResourceContainer->activateBundle(m_pBundleInfo->getID());
368 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
369 EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
372 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfo)
374 m_pBundleInfo->setPath("libTestBundle.so");
375 m_pBundleInfo->setVersion("1.0");
376 m_pBundleInfo->setLibraryPath("../.");
377 m_pBundleInfo->setID("oic.bundle.test");
379 m_pResourceContainer->registerBundle(m_pBundleInfo);
380 m_pResourceContainer->activateBundle(m_pBundleInfo);
381 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
383 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleDeactivator());
384 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
387 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfoJava)
389 m_pBundleInfo->setPath("TestBundle/hue-0.1-jar-with-dependencies.jar");
390 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
391 m_pBundleInfo->setLibraryPath("../.");
392 m_pBundleInfo->setVersion("1.0");
393 m_pBundleInfo->setID("oic.bundle.java.test");
395 m_pResourceContainer->registerBundle(m_pBundleInfo);
396 m_pResourceContainer->activateBundle(m_pBundleInfo);
397 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
398 EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
401 TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID)
403 m_pBundleInfo->setPath("libTestBundle.so");
404 m_pBundleInfo->setVersion("1.0");
405 m_pBundleInfo->setLibraryPath("../.");
406 m_pBundleInfo->setID("oic.bundle.test");
408 m_pResourceContainer->registerBundle(m_pBundleInfo);
409 m_pResourceContainer->activateBundle(m_pBundleInfo);
411 m_pResourceContainer->deactivateBundle(m_pBundleInfo->getID());
413 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
416 //TEST_F(ResourceContainerImplTest, JavaBundleDeactivatedWithBundleID)
420 /* Test for Configuration */
421 TEST(ConfigurationTest, ConfigFileLoadedWithValidPath)
423 Configuration *config = new Configuration(CONFIG_FILE);
425 EXPECT_TRUE(config->isLoaded());
428 TEST(ConfigurationTest, ConfigFileNotLoadedWithInvalidPath)
430 Configuration *config = new Configuration("InvalidPath");
432 EXPECT_FALSE(config->isLoaded());
435 TEST(ConfigurationTest, BundleConfigurationListParsed)
437 Configuration *config = new Configuration(CONFIG_FILE);
440 map< string, string > results;
442 config->getConfiguredBundles(&bundles);
444 results = *bundles.begin();
446 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
447 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
448 EXPECT_STREQ("1.0.0", results["version"].c_str());
451 TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId)
453 Configuration *config = new Configuration(CONFIG_FILE);
456 map< string, string > results;
458 config->getBundleConfiguration("oic.bundle.test", &bundle);
460 results = *bundle.begin();
462 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
463 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
464 EXPECT_STREQ("1.0.0", results["version"].c_str());
467 TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId)
469 Configuration *config = new Configuration(CONFIG_FILE);
472 config->getBundleConfiguration("test", &bundles);
474 EXPECT_TRUE(bundles.empty());
477 TEST(ConfigurationTest, BundleResourceConfigurationListParsed)
479 Configuration *config = new Configuration(CONFIG_FILE);
481 vector< resourceInfo > resourceConfig;
484 config->getResourceConfiguration("oic.bundle.test", &resourceConfig);
486 result = *resourceConfig.begin();
488 EXPECT_STREQ("test_resource", result.name.c_str());
489 EXPECT_STREQ("oic.test", result.resourceType.c_str());
492 TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId)
494 Configuration *config = new Configuration(CONFIG_FILE);
497 vector< resourceInfo > resourceConfig;
498 config->getResourceConfiguration("test", &resourceConfig);
500 EXPECT_TRUE(bundles.empty());