resource-manipulation: add unittest for container
authorMinji Park <minjii.park@samsung.com>
Thu, 23 Jul 2015 03:40:27 +0000 (12:40 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 23 Jul 2015 04:50:27 +0000 (04:50 +0000)
- add unittest for dynamic bundle loading and resource registration
- update sample bundle used for unittest

Change-Id: I9a59ecd5cbd49a8adbff703d58c0cb72ff58687a
Signed-off-by: Minji Park <minjii.park@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1839
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/src/resourceContainer/src/ResourceContainerImpl.cpp
service/resource-manipulation/src/resourceContainer/unittests/ResourceContainerTest.cpp
service/resource-manipulation/src/resourceContainer/unittests/TestBundle/include/TestBundleActivator.h
service/resource-manipulation/src/resourceContainer/unittests/TestBundle/src/TestBundleActivator.cpp
service/resource-manipulation/src/resourceContainer/unittests/TestBundleJava/hue-0.1-jar-with-dependencies.jar

index d1c6080..01f986a 100644 (file)
@@ -65,18 +65,18 @@ namespace OIC
 
         ResourceContainerImpl::ResourceContainerImpl()
         {
-
+            m_config = nullptr;
         }
 
         ResourceContainerImpl::~ResourceContainerImpl()
         {
-
+            m_config = nullptr;
         }
 
         bool has_suffix(const std::string &str, const std::string &suffix)
         {
             return str.size() >= suffix.size()
-                    && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
+                   && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
         }
 
         void ResourceContainerImpl::startContainer(string configFile)
@@ -107,11 +107,11 @@ namespace OIC
                         std::replace(activatorName.begin(), activatorName.end(), '.', '/');
                         ((BundleInfoInternal *) bundleInfo)->setActivatorName(activatorName);
                         ((BundleInfoInternal *) bundleInfo)->setLibraryPath(
-                                bundles[i]["libraryPath"]);
+                            bundles[i]["libraryPath"]);
 
                     }
                     info_logger() << "Init Bundle:" << bundles[i]["id"] << ";" << bundles[i]["path"]
-                            << endl;
+                                  << endl;
                     registerBundle(bundleInfo);
                     activateBundle(bundleInfo);
                 }
@@ -126,13 +126,26 @@ namespace OIC
         {
             info_logger() << "Stopping resource container.";
             for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
-                    it != m_bundles.end(); ++it)
+                 it != m_bundles.end(); ++it)
             {
                 BundleInfoInternal *bundleInfo = it->second;
                 deactivateBundle(bundleInfo);
                 unregisterBundle(bundleInfo);
             }
 
+            if (!m_mapServers.empty())
+            {
+                map< std::string, ResourceObject::Ptr >::iterator itor = m_mapServers.begin();
+
+                while (itor != m_mapServers.end())
+                {
+                    (itor++)->second.reset();
+                }
+
+                m_mapResources.clear();
+                m_mapBundleResources.clear();
+            }
+
             delete m_config;
         }
 
@@ -230,7 +243,7 @@ namespace OIC
         {
             void *bundleHandle = m_bundles[id]->getBundleHandle();
             info_logger() << "Unregister bundle: " << m_bundles[id]->getID() << ", "
-                    << m_bundles[id]->getID() << endl;
+                          << m_bundles[id]->getID() << endl;
             char *error;
             dlclose(bundleHandle);
             if ((error = dlerror()) != NULL)
@@ -250,29 +263,35 @@ namespace OIC
             string strResourceType = resource->m_resourceType;
             ResourceObject::Ptr server = nullptr;
 
-            info_logger() << "Registration of resource " << strUri << "," << strResourceType
-                    << endl;
+            info_logger() << "Registration of resource " << strUri << "," << strResourceType << endl;
 
-            server = buildResourceObject(strUri, strResourceType);
-
-            if (server != nullptr)
+            if (m_mapResources.find(strUri) == m_mapResources.end())
             {
-                m_mapServers[strUri] = server;
-                m_mapResources[strUri] = resource;
-                m_mapBundleResources[resource->m_bundleId].push_back(strUri);
+                server = buildResourceObject(strUri, strResourceType);
+
+                if (server != nullptr)
+                {
+                    m_mapServers[strUri] = server;
+                    m_mapResources[strUri] = resource;
+                    m_mapBundleResources[resource->m_bundleId].push_back(strUri);
 
-                resource->registerObserver(this);
+                    resource->registerObserver(this);
 
-                server->setGetRequestHandler(
+                    server->setGetRequestHandler(
                         std::bind(&ResourceContainerImpl::getRequestHandler, this,
-                                std::placeholders::_1, std::placeholders::_2));
+                                  std::placeholders::_1, std::placeholders::_2));
 
-                server->setSetRequestHandler(
+                    server->setSetRequestHandler(
                         std::bind(&ResourceContainerImpl::setRequestHandler, this,
-                                std::placeholders::_1, std::placeholders::_2));
+                                  std::placeholders::_1, std::placeholders::_2));
 
-                info_logger() << "Registration finished " << strUri << "," << strResourceType
-                        << endl;
+                    info_logger() << "Registration finished " << strUri << "," << strResourceType
+                                  << endl;
+                }
+            }
+            else
+            {
+                error_logger() << "resource with " << strUri << " already exists." << endl;
             }
         }
 
@@ -281,8 +300,8 @@ namespace OIC
             string strUri = resource->m_uri;
             string strResourceType = resource->m_resourceType;
 
-            info_logger() << "Unregistration of resource " << resource->m_uri << ","
-                    << resource->m_resourceType << endl;
+            info_logger() << "Unregistration of resource " << resource->m_uri << "," << resource->m_resourceType
+                          << endl;
 
             if (m_mapServers.find(strUri) != m_mapServers.end())
             {
@@ -317,12 +336,12 @@ namespace OIC
             ResourceAttributes attr;
 
             if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
-                    && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
+                && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
             {
                 for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
                 {
                     attr[attrName] = m_mapResources[request.getResourceUri()]->getAttribute(
-                            attrName);
+                                         attrName);
                 }
             }
 
@@ -335,7 +354,7 @@ namespace OIC
             ResourceAttributes attr = attributes;
 
             if (m_mapServers.find(request.getResourceUri()) != m_mapServers.end()
-                    && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
+                && m_mapResources.find(request.getResourceUri()) != m_mapResources.end())
             {
                 for (string attrName : m_mapResources[request.getResourceUri()]->getAttributeNames())
                 {
@@ -353,7 +372,7 @@ namespace OIC
         void ResourceContainerImpl::onNotificationReceived(std::string strResourceUri)
         {
             info_logger() << "ResourceContainerImpl::onNotificationReceived\n\tnotification from "
-                    << strResourceUri << ".\n";
+                          << strResourceUri << ".\n";
 
             if (m_mapServers.find(strResourceUri) != m_mapServers.end())
             {
@@ -371,23 +390,41 @@ namespace OIC
                 string strResourceType)
         {
             return ResourceObject::Builder(strUri, strResourceType, "DEFAULT_INTERFACE").setObservable(
-                    true).setDiscoverable(true).build();
+                       true).setDiscoverable(true).build();
         }
 
         void ResourceContainerImpl::startBundle(string bundleId)
         {
             if (m_bundles.find(bundleId) != m_bundles.end())
-                activateBundle(m_bundles[bundleId]);
+            {
+                if (!m_bundles[bundleId]->isActivated())
+                    activateBundle(m_bundles[bundleId]);
+                else
+                    error_logger() << "Bundle already started" << endl;
+            }
+            else
+            {
+                error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl;
+            }
         }
 
         void ResourceContainerImpl::stopBundle(string bundleId)
         {
             if (m_bundles.find(bundleId) != m_bundles.end())
-                deactivateBundle(m_bundles[bundleId]);
+            {
+                if (m_bundles[bundleId]->isActivated())
+                    deactivateBundle(m_bundles[bundleId]);
+                else
+                    error_logger() << "Bundle not activated" << endl;
+            }
+            else
+            {
+                error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered." << endl;
+            }
         }
 
         void ResourceContainerImpl::addBundle(string bundleId, string bundleUri, string bundlePath,
-                std::map< string, string > params)
+                                              std::map< string, string > params)
         {
             if (m_bundles.find(bundleId) != m_bundles.end())
                 error_logger() << "BundleId already exist" << endl;
@@ -406,7 +443,7 @@ namespace OIC
                 }
 
                 info_logger() << "Add Bundle:" << bundleInfo->getID().c_str() << ";"
-                        << bundleInfo->getPath().c_str() << endl;
+                              << bundleInfo->getPath().c_str() << endl;
 
                 registerBundle(bundleInfo);
             }
@@ -426,7 +463,7 @@ namespace OIC
             else
             {
                 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered."
-                        << endl;
+                               << endl;
             }
         }
 
@@ -434,7 +471,7 @@ namespace OIC
         {
             std::list< BundleInfo * > ret;
             for (std::map< std::string, BundleInfoInternal * >::iterator it = m_bundles.begin();
-                    it != m_bundles.end(); ++it)
+                 it != m_bundles.end(); ++it)
             {
                 {
                     BundleInfo *bundleInfo = BundleInfo::build();
@@ -468,7 +505,7 @@ namespace OIC
             else
             {
                 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered."
-                        << endl;
+                               << endl;
             }
         }
 
@@ -484,7 +521,7 @@ namespace OIC
             else
             {
                 error_logger() << "Bundle with ID \'" << bundleId << "\' is not registered."
-                        << endl;
+                               << endl;
             }
         }
 
@@ -519,11 +556,11 @@ namespace OIC
             {
                 bundleActivator = (activator_t *) dlsym(bundleHandle, "externalActivateBundle");
                 bundleDeactivator = (deactivator_t *) dlsym(bundleHandle,
-                        "externalDeactivateBundle");
+                                    "externalDeactivateBundle");
                 resourceCreator = (resourceCreator_t *) dlsym(bundleHandle,
-                        "externalCreateResource");
+                                  "externalCreateResource");
                 resourceDestroyer = (resourceDestroyer_t *) dlsym(bundleHandle,
-                        "externalDestroyResource");
+                                    "externalDestroyResource");
 
                 if ((error = dlerror()) != NULL)
                 {
@@ -608,7 +645,7 @@ namespace OIC
             if (m_mapResources.find(resourceUri) != m_mapResources.end())
             {
                 resourceDestroyer_t *resourceDestroyer =
-                        m_bundles[bundleId]->getResourceDestroyer();
+                    m_bundles[bundleId]->getResourceDestroyer();
 
                 if (resourceDestroyer != NULL)
                 {
@@ -622,187 +659,187 @@ namespace OIC
         }
 
 #if(JAVA_SUPPORT)
-    JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
-    {
-        return m_bundleVM[bundleId];
-    }
-
-    void ResourceContainerImpl::registerJavaBundle(BundleInfo *bundleInfo)
-    {
-        info_logger() << "Registering Java bundle " << bundleInfo->getID() << endl;
-        JavaVM *jvm;
-        JNIEnv *env;
-        JavaVMInitArgs vm_args;
-        JavaVMOption options[3];
-
-        BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
-
-        if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
+        JavaVM *ResourceContainerImpl::getJavaVM(string bundleId)
         {
-            fclose(file);
-            info_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " available." << endl;
-            //return true;
+            return m_bundleVM[bundleId];
         }
-        else
+
+        void ResourceContainerImpl::registerJavaBundle(BundleInfo *bundleInfo)
         {
-            error_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " not available" << endl;
-            return;
-        }
+            info_logger() << "Registering Java bundle " << bundleInfo->getID() << endl;
+            JavaVM *jvm;
+            JNIEnv *env;
+            JavaVMInitArgs vm_args;
+            JavaVMOption options[3];
+
+            BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) bundleInfo;
 
-        char optionString[] = "-Djava.compiler=NONE";
-        options[0].optionString = optionString;
-        char classpath[1000];
-        strcpy(classpath, "-Djava.class.path=");
-        strcat(classpath, bundleInfo->getPath().c_str());
+            if (FILE *file = fopen(bundleInfo->getPath().c_str(), "r"))
+            {
+                fclose(file);
+                info_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " available." << endl;
+                //return true;
+            }
+            else
+            {
+                error_logger() << "Resource bundle " << bundleInfo->getPath().c_str() << " not available" << endl;
+                return;
+            }
 
-        info_logger() << "Configured classpath: " << classpath << "|" << endl;
+            char optionString[] = "-Djava.compiler=NONE";
+            options[0].optionString = optionString;
+            char classpath[1000];
+            strcpy(classpath, "-Djava.class.path=");
+            strcat(classpath, bundleInfo->getPath().c_str());
 
-        options[1].optionString = classpath;
+            info_logger() << "Configured classpath: " << classpath << "|" << endl;
 
-        char libraryPath[1000];
-        strcpy(libraryPath, "-Djava.library.path=");
-        strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
-        options[2].optionString = libraryPath;
+            options[1].optionString = classpath;
 
-        info_logger() << "Configured library path: " << libraryPath << "|" << endl;
+            char libraryPath[1000];
+            strcpy(libraryPath, "-Djava.library.path=");
+            strcat(libraryPath, bundleInfo->getLibraryPath().c_str());
+            options[2].optionString = libraryPath;
 
-        vm_args.version = JNI_VERSION_1_4;
-        vm_args.options = options;
-        vm_args.nOptions = 3;
-        vm_args.ignoreUnrecognized = 1;
+            info_logger() << "Configured library path: " << libraryPath << "|" << endl;
 
-        int res;
-        res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
+            vm_args.version = JNI_VERSION_1_4;
+            vm_args.options = options;
+            vm_args.nOptions = 3;
+            vm_args.ignoreUnrecognized = 1;
 
-        if (res < 0)
-        {
-            error_logger() << " cannot create JavaVM." << endl;
-            return;
-        }
-        else
-        {
-            info_logger() << "JVM successfully created " << endl;
-        }
+            int res;
+            res = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
 
-        m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
+            if (res < 0)
+            {
+                error_logger() << " cannot create JavaVM." << endl;
+                return;
+            }
+            else
+            {
+                info_logger() << "JVM successfully created " << endl;
+            }
 
-        const char *className = bundleInfoInternal->getActivatorName().c_str();
+            m_bundleVM.insert(std::pair< string, JavaVM * >(bundleInfo->getID(), jvm));
 
-        info_logger() << "Looking up class: " << bundleInfoInternal->getActivatorName() << "|" << endl;
+            const char *className = bundleInfoInternal->getActivatorName().c_str();
 
-        jclass bundleActivatorClass = env->FindClass(className);
+            info_logger() << "Looking up class: " << bundleInfoInternal->getActivatorName() << "|" << endl;
 
-        if (bundleActivatorClass == NULL)
-        {
-            error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
-            << " bundle activator(" << bundleInfoInternal->getActivatorName()
-            << ") not found " << endl;
-            return;
-        }
+            jclass bundleActivatorClass = env->FindClass(className);
 
-        jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
-                "()V");
+            if (bundleActivatorClass == NULL)
+            {
+                error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
+                               << " bundle activator(" << bundleInfoInternal->getActivatorName()
+                               << ") not found " << endl;
+                return;
+            }
 
-        if (activateMethod == NULL)
-        {
-            error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
-            << " activate bundle method not found " << endl;
-            return;
-        }
-        bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
+            jmethodID activateMethod = env->GetMethodID(bundleActivatorClass, "activateBundle",
+                                       "()V");
 
-        jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
-                "()V");
+            if (activateMethod == NULL)
+            {
+                error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
+                               << " activate bundle method not found " << endl;
+                return;
+            }
+            bundleInfoInternal->setJavaBundleActivatorMethod(activateMethod);
 
-        if (deactivateMethod == NULL)
-        {
-            error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
-            << " deactivate bundle method not found " << endl;
-            return;
-        }
+            jmethodID deactivateMethod = env->GetMethodID(bundleActivatorClass, "deactivateBundle",
+                                         "()V");
 
-        bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
+            if (deactivateMethod == NULL)
+            {
+                error_logger() << "Cannot register bundle " << bundleInfoInternal->getID()
+                               << " deactivate bundle method not found " << endl;
+                return;
+            }
 
-        jmethodID constructor;
+            bundleInfoInternal->setJavaBundleDeactivatorMethod(deactivateMethod);
 
-        constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
+            jmethodID constructor;
 
-        jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
+            constructor = env->GetMethodID(bundleActivatorClass, "<init>", "(Ljava/lang/String;)V");
 
-        jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
+            jstring bundleID = env->NewStringUTF(bundleInfoInternal->getID().c_str());
 
-        bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
+            jobject bundleActivator = env->NewObject(bundleActivatorClass, constructor, bundleID);
 
-        bundleInfoInternal->setLoaded(true);
+            bundleInfoInternal->setJavaBundleActivatorObject(bundleActivator);
 
-        m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
+            bundleInfoInternal->setLoaded(true);
 
-        info_logger() << "Bundle registered" << endl;
-    }
+            m_bundles[bundleInfo->getID()] = ((BundleInfoInternal *)bundleInfo);
 
-    void ResourceContainerImpl::activateJavaBundle(string bundleId)
-    {
-        info_logger() << "Activating java bundle" << endl;
-        JavaVM *vm = getJavaVM(bundleId);
-        BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
-        JNIEnv *env;
-        int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+            info_logger() << "Bundle registered" << endl;
+        }
 
-        if (envStat == JNI_EDETACHED)
+        void ResourceContainerImpl::activateJavaBundle(string bundleId)
         {
-            if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
+            info_logger() << "Activating java bundle" << endl;
+            JavaVM *vm = getJavaVM(bundleId);
+            BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
+            JNIEnv *env;
+            int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+
+            if (envStat == JNI_EDETACHED)
+            {
+                if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
+                {
+                    error_logger() << "Failed to attach " << endl;
+                }
+            }
+            else if (envStat == JNI_EVERSION)
             {
-                error_logger() << "Failed to attach " << endl;
+                error_logger() << "Env: version not supported " << endl;
             }
-        }
-        else if (envStat == JNI_EVERSION)
-        {
-            error_logger() << "Env: version not supported " << endl;
-        }
-
-        env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
-                bundleInfoInternal->getJavaBundleActivatorMethod());
 
-        m_bundles[bundleId]->setActivated(true);
-    }
+            env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
+                                bundleInfoInternal->getJavaBundleActivatorMethod());
 
-    void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
-    {
-        info_logger() << "Deactivating java bundle" << endl;
-        JavaVM *vm = getJavaVM(bundleId);
-        BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
-        JNIEnv *env;
-        int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+            m_bundles[bundleId]->setActivated(true);
+        }
 
-        if (envStat == JNI_EDETACHED)
+        void ResourceContainerImpl::deactivateJavaBundle(string bundleId)
         {
-            if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
+            info_logger() << "Deactivating java bundle" << endl;
+            JavaVM *vm = getJavaVM(bundleId);
+            BundleInfoInternal *bundleInfoInternal = (BundleInfoInternal *) m_bundles[bundleId];
+            JNIEnv *env;
+            int envStat = vm->GetEnv((void **) &env, JNI_VERSION_1_4);
+
+            if (envStat == JNI_EDETACHED)
+            {
+                if (vm->AttachCurrentThread((void **) &env, NULL) != 0)
+                {
+                    error_logger() << "Failed to attach " << endl;
+                }
+            }
+            else if (envStat == JNI_EVERSION)
             {
-                error_logger() << "Failed to attach " << endl;
+                error_logger() << "Env: version not supported " << endl;
             }
-        }
-        else if (envStat == JNI_EVERSION)
-        {
-            error_logger() << "Env: version not supported " << endl;
-        }
 
-        env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
-                bundleInfoInternal->getJavaBundleDeactivatorMethod());
+            env->CallVoidMethod(bundleInfoInternal->getJavaBundleActivatorObject(),
+                                bundleInfoInternal->getJavaBundleDeactivatorMethod());
 
-        m_bundles[bundleId]->setActivated(false);
-    }
+            m_bundles[bundleId]->setActivated(false);
+        }
 
-    void ResourceContainerImpl::unregisterBundleJava(string id)
-    {
-        info_logger() << "Unregister Java bundle: " << m_bundles[id]->getID() << ", "
-        << m_bundles[id]->getID() << endl;
+        void ResourceContainerImpl::unregisterBundleJava(string id)
+        {
+            info_logger() << "Unregister Java bundle: " << m_bundles[id]->getID() << ", "
+                          << m_bundles[id]->getID() << endl;
 
-        info_logger() << "Destroying JVM" << endl;
-        m_bundleVM[id]->DestroyJavaVM();
+            info_logger() << "Destroying JVM" << endl;
+            m_bundleVM[id]->DestroyJavaVM();
 
-        delete m_bundles[id];
-        m_bundles.erase(id);
-    }
+            delete m_bundles[id];
+            m_bundles.erase(id);
+        }
 #endif
     }
 
index 92e1178..c4c7aae 100644 (file)
@@ -26,6 +26,7 @@
 #include <HippoMocks/hippomocks.h>
 
 #include "Configuration.h"
+#include "BundleActivator.h"
 #include "BundleResource.h"
 #include "ResourceContainer.h"
 #include "ResourceContainerBundleAPI.h"
@@ -62,12 +63,14 @@ class TestBundleResource: public BundleResource
 class ResourceContainerTest: public Test
 {
     public:
-        ~ResourceContainerTest() noexcept(true){
+        ~ResourceContainerTest() noexcept(true)
+        {
 
         }
 
 
     public:
+        MockRepository mocks;
         ResourceContainer *m_pResourceContainer;
 
     protected:
@@ -138,6 +141,8 @@ TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI)
 
     EXPECT_FALSE(
         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
+
+    m_pResourceContainer->stopContainer();
 }
 
 TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI)
@@ -148,13 +153,81 @@ TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI)
 
     EXPECT_TRUE(
         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
+
+    m_pResourceContainer->stopContainer();
+}
+
+TEST_F(ResourceContainerTest, AddNewSoBundleToContainer)
+{
+    std::map<string, string> bundleParams;
+    std::list<BundleInfo *> bundles;
+
+    bundles = m_pResourceContainer->listBundles();
+    m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", bundleParams);
+
+    EXPECT_EQ(bundles.size() + 1, m_pResourceContainer->listBundles().size());
+    EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
+}
+
+TEST_F(ResourceContainerTest, RemoveSoBundleFromContainer)
+{
+    std::map<string, string> bundleParams;
+    std::list<BundleInfo *> bundles;
+
+    bundles = m_pResourceContainer->listBundles();
+    m_pResourceContainer->removeBundle("oic.bundle.test");
+
+    EXPECT_EQ(bundles.size() - 1, m_pResourceContainer->listBundles().size());
+}
+
+TEST_F(ResourceContainerTest, AddBundleAlreadyRegistered)
+{
+    std::map<string, string> bundleParams;
+    std::list<BundleInfo *> bundles;
+
+    m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", bundleParams);
+    bundles = m_pResourceContainer->listBundles();
+    m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", bundleParams);
+
+    EXPECT_EQ(bundles.size(), m_pResourceContainer->listBundles().size());
+}
+
+TEST_F(ResourceContainerTest, AddAndRemoveSoBundleResource)
+{
+    std::list<string> resources;
+    std::map<string, string> resourceParams;
+    resourceParams["resourceType"] = "oic.test";
+
+    m_pResourceContainer->startContainer(CONFIG_FILE);
+    resources = m_pResourceContainer->listBundleResources("oic.bundle.test");
+
+    m_pResourceContainer->addResourceConfig("oic.bundle.test", "/test_resource", resourceParams);
+
+    EXPECT_EQ(resources.size() + 1,
+              m_pResourceContainer->listBundleResources("oic.bundle.test").size());
+
+    m_pResourceContainer->removeResourceConfig("oic.bundle.test", "/test_resource");
+
+    EXPECT_EQ(resources.size(), m_pResourceContainer->listBundleResources("oic.bundle.test").size());
+
+    m_pResourceContainer->stopContainer();
+}
+
+TEST_F(ResourceContainerTest, TryAddingSoBundleResourceToNotRegisteredBundle)
+{
+    std::map<string, string> resourceParams;
+
+    mocks.NeverCallFunc(ResourceContainerImpl::buildResourceObject);
+
+    m_pResourceContainer->addResourceConfig("unvalidBundleId", "", resourceParams);
 }
 
 class ResourceContainerBundleAPITest: public Test
 {
 
     public:
-        ~ResourceContainerBundleAPITest() noexcept(true){
+        ~ResourceContainerBundleAPITest() noexcept(true)
+        {
 
         }
 
@@ -179,6 +252,11 @@ class ResourceContainerBundleAPITest: public Test
 
 TEST_F(ResourceContainerBundleAPITest, ResourceServerCreatedWhenRegisterResourceCalled)
 {
+    m_pBundleResource = new TestBundleResource();
+    m_pBundleResource->m_bundleId = "oic.bundle.test";
+    m_pBundleResource->m_uri = "/test_resource/test";
+    m_pBundleResource->m_resourceType = "oic.test";
+
     mocks.ExpectCallFunc(ResourceContainerImpl::buildResourceObject).With(m_pBundleResource->m_uri,
             m_pBundleResource->m_resourceType).Return(nullptr);
 
@@ -195,6 +273,8 @@ TEST_F(ResourceContainerBundleAPITest, RequestHandlerForResourceServerSetWhenReg
     mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
 
     m_pResourceContainer->registerResource(m_pBundleResource);
+
+    m_pResourceContainer->unregisterResource(m_pBundleResource);
 }
 
 TEST_F(ResourceContainerBundleAPITest, BundleResourceUnregisteredWhenUnregisterResourceCalled)
@@ -229,6 +309,8 @@ TEST_F(ResourceContainerBundleAPITest,
     mocks.ExpectCall(m_pResourceObject, ResourceObject::notify);
 
     m_pResourceContainer->onNotificationReceived(m_pBundleResource->m_uri);
+
+    m_pResourceContainer->unregisterResource(m_pBundleResource);
 }
 
 TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleId)
@@ -236,6 +318,7 @@ TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleI
     configInfo bundle;
     map< string, string > results;
 
+    ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(CONFIG_FILE);
     m_pResourceContainer->getBundleConfiguration("oic.bundle.test", &bundle);
 
     results = *bundle.begin();
@@ -243,6 +326,8 @@ TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleI
     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());
+
+    ((ResourceContainerImpl *)m_pResourceContainer)->stopContainer();
 }
 
 TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed)
@@ -250,18 +335,22 @@ TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed)
     vector< resourceInfo > resourceConfig;
     resourceInfo result;
 
+    ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(CONFIG_FILE);
     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());
+
+    ((ResourceContainerImpl *)m_pResourceContainer)->stopContainer();
 }
 
 class ResourceContainerImplTest: public Test
 {
     public:
-        ~ResourceContainerImplTest() noexcept(true){
+        ~ResourceContainerImplTest() noexcept(true)
+        {
 
         }
 
@@ -348,20 +437,6 @@ TEST_F(ResourceContainerImplTest, SoBundleActivatedWithValidBundleInfo)
     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
 }
 
-/*TEST_F(ResourceContainerImplTest, JavaBundleActivatedWithValidBundleInfo)
-{
-    m_pBundleInfo->setPath("TestBundleJava/hue-0.1-jar-with-dependencies.jar");
-    m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
-    m_pBundleInfo->setLibraryPath("../.");
-    m_pBundleInfo->setVersion("1.0");
-    m_pBundleInfo->setID("oic.bundle.java.test2");
-
-    m_pResourceContainer->registerBundle(m_pBundleInfo);
-    m_pResourceContainer->activateBundle(m_pBundleInfo);
-    EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
-
-}*/
-
 TEST_F(ResourceContainerImplTest, BundleNotActivatedWhenNotRegistered)
 {
     m_pBundleInfo->setPath("libTestBundle.so");
@@ -432,10 +507,6 @@ TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID)
     EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
 }
 
-//TEST_F(ResourceContainerImplTest, JavaBundleDeactivatedWithBundleID)
-//{
-//}
-
 /* Test for Configuration */
 TEST(ConfigurationTest, ConfigFileLoadedWithValidPath)
 {
@@ -517,4 +588,4 @@ TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId)
     config->getResourceConfiguration("test", &resourceConfig);
 
     EXPECT_TRUE(bundles.empty());
-}
+}
\ No newline at end of file
index 7b45ba5..5a2e254 100644 (file)
@@ -40,6 +40,17 @@ class TestBundleActivator : public BundleActivator
 
         void createResource(resourceInfo resourceInfo);
         void destroyResource(BundleResource *pBundleResource);
+
+        ResourceContainerBundleAPI *m_pResourceContainer;
+        std::string m_bundleId;
+        BundleResource *m_pTestResource;
+};
+
+/*Fake bundle resource class for testing*/
+class TestBundleResource : public BundleResource
+{
+    public:
+        void initAttributes() { };
 };
 
 #endif /* TESTBUNDLE_H_ */
index 853fffd..47b1afa 100644 (file)
@@ -24,31 +24,48 @@ TestBundleActivator *bundle;
 
 TestBundleActivator::TestBundleActivator()
 {
+    m_pResourceContainer = nullptr;
+    m_pTestResource = nullptr;
 }
 
 TestBundleActivator::~TestBundleActivator()
 {
+    m_pResourceContainer = nullptr;
+    m_pTestResource = nullptr;
 }
 
 void TestBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
         std::string bundleId)
 {
     std::cout << "TestBundleActivator::activateBundle .. " << std::endl;
+    m_pResourceContainer = resourceContainer;
+    m_bundleId = bundleId;
 }
 
 void TestBundleActivator::deactivateBundle()
 {
     std::cout << "TestBundleActivator::deactivateBundle .. " << std::endl;
+    m_pResourceContainer = nullptr;
 }
 
 void TestBundleActivator::createResource(resourceInfo resourceInfo)
 {
     std::cout << "TestBundleActivator::createResource .. " << std::endl;
+
+    TestBundleResource *m_pTestResource = new TestBundleResource();
+
+    m_pTestResource->m_bundleId = m_bundleId;
+    m_pTestResource->m_uri = resourceInfo.uri;
+    m_pTestResource->m_resourceType = resourceInfo.resourceType;
+
+    m_pResourceContainer->registerResource(m_pTestResource);
 }
 
 void TestBundleActivator::destroyResource(BundleResource *pBundleResource)
 {
     std::cout << "TestBundleActivator::destroyResource .. " << std::endl;
+
+    m_pResourceContainer->unregisterResource(pBundleResource);
 }
 
 extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
@@ -60,10 +77,8 @@ extern "C" void externalActivateBundle(ResourceContainerBundleAPI *resourceConta
 
 extern "C" void externalDeactivateBundle()
 {
-    if (!bundle)
-    {
-        bundle->deactivateBundle();
-    }
+    bundle->deactivateBundle();
+    delete bundle;
 }
 
 extern "C" void externalCreateResource(resourceInfo resourceInfo)
index be303a1..f716b43 100644 (file)
Binary files a/service/resource-manipulation/src/resourceContainer/unittests/TestBundleJava/hue-0.1-jar-with-dependencies.jar and b/service/resource-manipulation/src/resourceContainer/unittests/TestBundleJava/hue-0.1-jar-with-dependencies.jar differ