add new test cases for resource container
authorMinji Park <minjii.park@samsung.com>
Thu, 9 Jul 2015 13:54:00 +0000 (22:54 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 10 Jul 2015 00:42:29 +0000 (00:42 +0000)
Change-Id: Ie5c917b14c24782b2ec0b08cf9f820bfbbac371b
Signed-off-by: Minji Park <minjii.park@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1604
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/modules/resourceContainer/unittests/ResourceContainerTest.cpp

index 3f75aff..8758e5b 100644 (file)
@@ -37,11 +37,10 @@ using namespace std;
 using namespace testing;
 using namespace OIC::Service;
 
-
 string CONFIG_FILE = "ResourceContainerTestConfig.xml";
 
-typedef ResourceObject::Ptr(*resourceObjectBuild)();
 
+/*Fake bundle resource class for testing*/
 class TestBundleResource : public BundleResource
 {
     public:
@@ -52,7 +51,9 @@ class TestBundleResource : public BundleResource
         void setAttribute(string attributeName, string value)
         {};
         void initAttributes()
-        {};
+        {
+            BundleResource::setAttribute("attri", "test");
+        };
 };
 
 class ResourceContainerTest : public Test
@@ -67,40 +68,178 @@ class ResourceContainerTest : public Test
         }
 };
 
+TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfigFile)
+{
+    m_pResourceContainer->startContainer(CONFIG_FILE);
+
+    EXPECT_GT(m_pResourceContainer->listBundles().size(), 0);
+    EXPECT_STREQ("oic.bundle.test", (*m_pResourceContainer->listBundles().begin())->getID().c_str());
+    EXPECT_STREQ("libTestBundle.so", (*m_pResourceContainer->listBundles().begin())->getPath().c_str());
+    EXPECT_STREQ("1.0.0", (*m_pResourceContainer->listBundles().begin())->getVersion().c_str());
+
+    m_pResourceContainer->stopContainer();
+}
+
+TEST_F(ResourceContainerTest, BundleLoadedWhenContainerStartedWithValidConfigFile)
+{
+    m_pResourceContainer->startContainer(CONFIG_FILE);
+
+    EXPECT_GT(m_pResourceContainer->listBundles().size(), 0);
+    EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
+    EXPECT_NE(nullptr, ((BundleInfoInternal *)(
+                            *m_pResourceContainer->listBundles().begin()))->getBundleHandle());
+
+    m_pResourceContainer->stopContainer();
+}
+
+TEST_F(ResourceContainerTest, BundleActivatedWhenContainerStartedWithValidConfigFile)
+{
+    m_pResourceContainer->startContainer(CONFIG_FILE);
+
+    EXPECT_GT(m_pResourceContainer->listBundles().size(), 0);
+    EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
+    EXPECT_NE(nullptr, ((BundleInfoInternal *)(
+                            *m_pResourceContainer->listBundles().begin()))->getBundleActivator());
+
+    m_pResourceContainer->stopContainer();
+}
+
+TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithInvalidConfigFile)
+{
+    m_pResourceContainer->startContainer("invalideConfig");
+
+    EXPECT_EQ(0, m_pResourceContainer->listBundles().size());
+}
+
+TEST_F(ResourceContainerTest, BundleUnregisteredWhenContainerStopped)
+{
+    m_pResourceContainer->startContainer(CONFIG_FILE);
+    m_pResourceContainer->stopContainer();
+
+    EXPECT_EQ(0, m_pResourceContainer->listBundles().size());
+}
+
+TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI)
+{
+    m_pResourceContainer->startContainer(CONFIG_FILE);
+    m_pResourceContainer->stopBundle("oic.bundle.test");
+
+    EXPECT_FALSE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
+}
+
+TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI)
+{
+    m_pResourceContainer->startContainer(CONFIG_FILE);
+    m_pResourceContainer->stopBundle("oic.bundle.test");
+    m_pResourceContainer->startBundle("oic.bundle.test");
+
+    EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
+}
+
 
 class ResourceContainerBundleAPITest : public Test
 {
     public:
         MockRepository mocks;
+        ResourceObject *m_pResourceObject;
         ResourceContainerBundleAPI *m_pResourceContainer;
+        TestBundleResource *m_pBundleResource;
 
     protected:
         void SetUp() override
         {
+            m_pResourceObject = mocks.Mock<ResourceObject>();
             m_pResourceContainer = ResourceContainerBundleAPI::getInstance();
+
+            m_pBundleResource = new TestBundleResource();
+            m_pBundleResource->m_bundleId = "oic.bundle.test";
+            m_pBundleResource->m_uri = "/test_resource";
+            m_pBundleResource->m_resourceType = "oic.test";
         }
 };
 
-// TC for registering resource should be implemented
-//TEST_F(ResourceContainerBundleAPITest, registerResourceTest)
-//{
-//    ResourceObject::Builder *builder = new ResourceObject::Builder("/test_resource", "oic.test", "");
-//    TestBundleResource *resource = new TestBundleResource();
-//
-//    mocks.ExpectCall(builder, ResourceObject::Builder::build).Return(nullptr);
-//
-//    m_pResourceContainer->registerResource(resource);
-//}
+TEST_F(ResourceContainerBundleAPITest, ResourceServerCreatedWhenRegisterResourceCalled)
+{
+    mocks.ExpectCallFunc(ResourceContainerImpl::buildResourceObject).With(m_pBundleResource->m_uri,
+            m_pBundleResource->m_resourceType).Return(nullptr);
 
-// TC for unregistering resource should be implemented
-//TEST_F(ResourceContainerBundleAPITest, unregisterResourceTest)
-//{
-//}
+    m_pResourceContainer->registerResource(m_pBundleResource);
+}
+
+TEST_F(ResourceContainerBundleAPITest, RequestHandlerForResourceServerSetWhenRegisterResourceCalled)
+{
+    mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(ResourceObject::Ptr(
+    m_pResourceObject, [](ResourceObject *) {}));
+
+    mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
+    mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
+
+    m_pResourceContainer->registerResource(m_pBundleResource);
+}
+
+TEST_F(ResourceContainerBundleAPITest, BundleResourceUnregisteredWhenUnregisterResourceCalled)
+{
+    mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(ResourceObject::Ptr(
+    m_pResourceObject, [](ResourceObject *) {}));
+
+    mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
+    mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
+
+    m_pResourceContainer->registerResource(m_pBundleResource);
+    m_pResourceContainer->unregisterResource(m_pBundleResource);
+
+    EXPECT_EQ(0, ((ResourceContainerImpl *)m_pResourceContainer)->listBundleResources(
+                  m_pBundleResource->m_bundleId).size());
+}
+
+TEST_F(ResourceContainerBundleAPITest,
+       ServerNotifiesToObserversWhenNotificationReceivedFromResource)
+{
+    mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(ResourceObject::Ptr(
+    m_pResourceObject, [](ResourceObject *) {}));
+
+    mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
+    mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
+
+    m_pResourceContainer->registerResource(m_pBundleResource);
+
+    mocks.ExpectCall(m_pResourceObject, ResourceObject::notify);
+
+    m_pResourceContainer->onNotificationReceived(m_pBundleResource->m_uri);
+}
+
+TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleId)
+{
+    configInfo bundle;
+    map<string, string> results;
+
+    m_pResourceContainer->getBundleConfiguration("oic.bundle.test", &bundle);
+
+    results = *bundle.begin();
+
+    EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
+    EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
+    EXPECT_STREQ("1.0.0", results["version"].c_str());
+}
+
+TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed)
+{
+    vector<resourceInfo> resourceConfig;
+    resourceInfo result;
+
+    m_pResourceContainer->getResourceConfiguration("oic.bundle.test", &resourceConfig);
+
+    result = *resourceConfig.begin();
+
+    EXPECT_STREQ("test_resource", result.name.c_str());
+    EXPECT_STREQ("oic.test", result.resourceType.c_str());
+}
 
 
 class ResourceContainerImplTest : public Test
 {
     public:
+        MockRepository mocks;
         ResourceContainerImpl *m_pResourceContainer;
         BundleInfo *m_pBundleInfo;
 
@@ -112,7 +251,7 @@ class ResourceContainerImplTest : public Test
         }
 };
 
-TEST_F(ResourceContainerImplTest, RegisterBundleTest)
+TEST_F(ResourceContainerImplTest, SoBundleLoadedWhenRegisteredWithRegisterBundleAPI)
 {
     m_pBundleInfo->setPath("libTestBundle.so");
     m_pBundleInfo->setVersion("1.0");
@@ -120,10 +259,14 @@ TEST_F(ResourceContainerImplTest, RegisterBundleTest)
 
     m_pResourceContainer->registerBundle(m_pBundleInfo);
 
-    EXPECT_NE(((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle(), nullptr);
+    EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
 }
 
-TEST_F(ResourceContainerImplTest, RegisterBundleTestWithInvalidPath)
+//TEST_F(ResourceContainerImplTest, JavaBundleLoadedWhenRegisteredWithRegisterBundleAPI)
+//{
+//}
+
+TEST_F(ResourceContainerImplTest, BundleNotRegisteredIfBundlePathIsInvalid)
 {
     m_pBundleInfo->setPath("");
     m_pBundleInfo->setVersion("1.0");
@@ -131,10 +274,10 @@ TEST_F(ResourceContainerImplTest, RegisterBundleTestWithInvalidPath)
 
     m_pResourceContainer->registerBundle(m_pBundleInfo);
 
-    EXPECT_EQ(((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator(), nullptr);
+    EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
 }
 
-TEST_F(ResourceContainerImplTest, ActivateBundle)
+TEST_F(ResourceContainerImplTest, SoBundleActivatedWithValidBundleInfo)
 {
     m_pBundleInfo->setPath("libTestBundle.so");
     m_pBundleInfo->setVersion("1.0");
@@ -143,44 +286,94 @@ TEST_F(ResourceContainerImplTest, ActivateBundle)
     m_pResourceContainer->registerBundle(m_pBundleInfo);
     m_pResourceContainer->activateBundle(m_pBundleInfo);
 
-    EXPECT_NE(((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator(), nullptr);
+    EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
+}
+
+//TEST_F(ResourceContainerImplTest, JavaBundleActivatedWithValidBundleInfo)
+//{
+//}
+
+TEST_F(ResourceContainerImplTest, BundleNotActivatedWhenNotRegistered)
+{
+    m_pBundleInfo->setPath("libTestBundle.so");
+    m_pBundleInfo->setVersion("1.0");
+    m_pBundleInfo->setID("oic.bundle.test");
+
+    m_pResourceContainer->activateBundle(m_pBundleInfo);
+
+    EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
+}
+
+TEST_F(ResourceContainerImplTest, SoBundleActivatedWithBundleID)
+{
+    m_pBundleInfo->setPath("libTestBundle.so");
+    m_pBundleInfo->setVersion("1.0");
+    m_pBundleInfo->setID("oic.bundle.test");
+
+    m_pResourceContainer->registerBundle(m_pBundleInfo);
+    m_pResourceContainer->activateBundle(m_pBundleInfo->getID());
+
+    EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
+    EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
 }
 
-TEST_F(ResourceContainerImplTest, ActivateUnregisteredBundle)
+//TEST_F(ResourceContainerImplTest, JavaBundleActivatedWithBundleID)
+//{
+//}
+
+TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfo)
 {
     m_pBundleInfo->setPath("libTestBundle.so");
     m_pBundleInfo->setVersion("1.0");
     m_pBundleInfo->setID("oic.bundle.test");
 
+    m_pResourceContainer->registerBundle(m_pBundleInfo);
     m_pResourceContainer->activateBundle(m_pBundleInfo);
+    m_pResourceContainer->deactivateBundle(m_pBundleInfo);
 
-    EXPECT_EQ(((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator(), nullptr);
+    EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleDeactivator());
+    EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
 }
 
-// TC for deactivating bundle should be implemented
+//TEST_F(ResourceContainerImplTest, DeactivateBundleNotWorkingIfBundleNotDeactivated)
+//{
+//}
+
+TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID)
+{
+    m_pBundleInfo->setPath("libTestBundle.so");
+    m_pBundleInfo->setVersion("1.0");
+    m_pBundleInfo->setID("oic.bundle.test");
 
-//TEST_F(ResourceContainerImplTest, DeactivateBundle)
+    m_pResourceContainer->registerBundle(m_pBundleInfo);
+    m_pResourceContainer->activateBundle(m_pBundleInfo);
+
+    m_pResourceContainer->deactivateBundle(m_pBundleInfo->getID());
+
+    EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
+}
+
+//TEST_F(ResourceContainerImplTest, JavaBundleDeactivatedWithBundleID)
 //{
-//
 //}
 
 
 /* Test for Configuration */
-TEST(ConfigurationTest, LoadConfigFileWithValidPath)
+TEST(ConfigurationTest, ConfigFileLoadedWithValidPath)
 {
     Configuration *config = new Configuration(CONFIG_FILE);
 
-    EXPECT_EQ(config->isLoaded(), true);
+    EXPECT_TRUE(config->isLoaded());
 }
 
-TEST(ConfigurationTest, LoadConfigFileWithInvalidPath)
+TEST(ConfigurationTest, ConfigFileNotLoadedWithInvalidPath)
 {
-    Configuration *config = new Configuration("test");
+    Configuration *config = new Configuration("InvalidPath");
 
-    EXPECT_EQ(config->isLoaded(), false);
+    EXPECT_FALSE(config->isLoaded());
 }
 
-TEST(ConfigurationTest, getConfiguredBundlesTest)
+TEST(ConfigurationTest, BundleConfigurationListParsed)
 {
     Configuration *config = new Configuration(CONFIG_FILE);
 
@@ -191,12 +384,12 @@ TEST(ConfigurationTest, getConfiguredBundlesTest)
 
     results = *bundles.begin();
 
-    EXPECT_STREQ(results["id"].c_str(), "oic.bundle.test");
-    EXPECT_STREQ(results["path"].c_str(), "libTestBundle.so");
-    EXPECT_STREQ(results["version"].c_str(), "1.0.0");
+    EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
+    EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
+    EXPECT_STREQ("1.0.0", results["version"].c_str());
 }
 
-TEST(ConfigurationTest, getBundleConfigurationTest)
+TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId)
 {
     Configuration *config = new Configuration(CONFIG_FILE);
 
@@ -207,22 +400,22 @@ TEST(ConfigurationTest, getBundleConfigurationTest)
 
     results = *bundle.begin();
 
-    EXPECT_STREQ(results["id"].c_str(), "oic.bundle.test");
-    EXPECT_STREQ(results["path"].c_str(), "libTestBundle.so");
-    EXPECT_STREQ(results["version"].c_str(), "1.0.0");
+    EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
+    EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
+    EXPECT_STREQ("1.0.0", results["version"].c_str());
 }
 
-TEST(ConfigurationTest, getBundleConfigurationTestWithInvalidBundleId)
+TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId)
 {
     Configuration *config = new Configuration(CONFIG_FILE);
 
     configInfo bundles;
     config->getBundleConfiguration("test", &bundles);
 
-    EXPECT_EQ(bundles.empty(), true);
+    EXPECT_TRUE(bundles.empty());
 }
 
-TEST(ConfigurationTest, getResourceConfigurationTest)
+TEST(ConfigurationTest, BundleResourceConfigurationListParsed)
 {
     Configuration *config = new Configuration(CONFIG_FILE);
 
@@ -233,11 +426,11 @@ TEST(ConfigurationTest, getResourceConfigurationTest)
 
     result = *resourceConfig.begin();
 
-    EXPECT_STREQ(result.name.c_str(), "test_resource");
-    EXPECT_STREQ(result.resourceType.c_str(), "oic.test");
+    EXPECT_STREQ("test_resource", result.name.c_str());
+    EXPECT_STREQ("oic.test", result.resourceType.c_str());
 }
 
-TEST(ConfigurationTest, getResourceConfigurationTestWithInvalideBundleId)
+TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId)
 {
     Configuration *config = new Configuration(CONFIG_FILE);
 
@@ -245,5 +438,5 @@ TEST(ConfigurationTest, getResourceConfigurationTestWithInvalideBundleId)
     vector<resourceInfo> resourceConfig;
     config->getResourceConfiguration("test", &resourceConfig);
 
-    EXPECT_EQ(bundles.empty(), true);
-}
+    EXPECT_TRUE(bundles.empty());
+}
\ No newline at end of file