added OCAccountManager class
authorJaewook Jung <jw0213.jung@samsung.com>
Thu, 28 Jul 2016 01:37:26 +0000 (10:37 +0900)
committerAshok Babu Channa <ashok.channa@samsung.com>
Thu, 4 Aug 2016 06:27:36 +0000 (06:27 +0000)
As there is need for API about Cloud server on client side, I added it
on OCPlatform.
(https://gerrit.iotivity.org/gerrit/#/c/9121/)
However there are plenty of APIs to be added more and just adding them
all on OCPlatform is not fine for usability.
So I make them a class like OCResource.

Below APIs will be added after this patchset.
: searchUser, createGroup, searchGroup, deleteGroup, addGroupMember,
  searchGroupMember, deleteGroupMember

Change-Id: I45e1f08b13d821346b364d38bf212b8040205a46
Signed-off-by: Jaewook Jung <jw0213.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9835
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jaehong Jo <jaehong.jo@samsung.com>
Reviewed-by: Dave Thaler <dthaler@microsoft.com>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
resource/csdk/stack/include/octypes.h
resource/include/OCAccountManager.h [new file with mode: 0644]
resource/include/OCPlatform.h
resource/include/OCPlatform_impl.h
resource/src/OCAccountManager.cpp [new file with mode: 0644]
resource/src/OCPlatform.cpp
resource/src/OCPlatform_impl.cpp
resource/src/SConscript
resource/unittests/OCAccountManagerTest.cpp [new file with mode: 0644]
resource/unittests/OCPlatformTest.cpp
resource/unittests/SConscript

index 6cc3335..c2267a5 100644 (file)
@@ -381,7 +381,7 @@ extern "C" {
 #define OC_RSRVD_ACCESS_TOKEN              "accesstoken"
 
 /** Defines status. */
-#define OC_RSRVD_STATUS                    "status"
+#define OC_RSRVD_LOGIN                     "login"
 
 /** Defines grant type. */
 #define OC_RSRVD_GRANT_TYPE                "granttype"
@@ -389,6 +389,12 @@ extern "C" {
 /** Defines refresh token. */
 #define OC_RSRVD_REFRESH_TOKEN             "refreshtoken"
 
+/** Defines user ID. */
+#define OC_RSRVD_USER_ID                   "uid"
+
+/** Defines options. */
+#define OC_RSRVD_OPTIONS                   "options"
+
 /** To represent grant type with refresh token. */
 #define OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN  "refresh_token"
 
diff --git a/resource/include/OCAccountManager.h b/resource/include/OCAccountManager.h
new file mode 100644 (file)
index 0000000..00f2b66
--- /dev/null
@@ -0,0 +1,147 @@
+/* ****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#ifndef OC_ACCOUNT_MANAGER_H_
+#define OC_ACCOUNT_MANAGER_H_
+
+#include <OCApi.h>
+#include <IClientWrapper.h>
+#include <InProcClientWrapper.h>
+
+namespace OC
+{
+    class OCAccountManager
+    {
+        friend class OCPlatform_impl;
+
+    public:
+        typedef std::shared_ptr<OCAccountManager> Ptr;
+
+        OCAccountManager(OCAccountManager&&) = default;
+        OCAccountManager(const OCAccountManager&) = delete;
+        OCAccountManager& operator=(OCAccountManager&&) = delete;
+        OCAccountManager& operator=(const OCAccountManager&) = delete;
+
+        virtual ~OCAccountManager(void);
+
+        /**
+        * Function to get the host address of account server
+        *
+        * @return std::string host address
+        */
+        std::string host() const;
+
+        /**
+        * Function to get the connectivity type for account server
+        *
+        * @return enum connectivity type (flags and adapter)
+        */
+        OCConnectivityType connectivityType() const;
+
+        /**
+         * Function for account registration to account server
+         *
+         * @param authProvider Provider name used for authentication.
+         * @param authCode The authorization code obtained by using an authorization server
+         *                 as an intermediary between the client and resource owner.
+         * @param cloudConnectHandler Callback function that will get the result of the operation.
+         *
+         * @return Returns ::OC_STACK_OK if success
+         */
+        OCStackResult signUp(const std::string& authProvider,
+                             const std::string& authCode,
+                             PostCallback cloudConnectHandler);
+
+        /**
+         * Overload
+         *
+         * @param authProvider Provider name used for authentication.
+         * @param authCode The authorization code obtained by using an authorization server
+         *                 as an intermediary between the client and resource owner.
+         * @param options The option values depends on auth provider.
+         * @param cloudConnectHandler Callback function that will get the result of the operation.
+         *
+         * @return Returns ::OC_STACK_OK if success
+         */
+        OCStackResult signUp(const std::string& authProvider,
+                             const std::string& authCode,
+                             const QueryParamsMap& options,
+                             PostCallback cloudConnectHandler);
+
+        /**
+         * Function for sign-in to account server
+         *
+         * @param userId Identifier of the user obtained by account registration.
+         * @param accessToken Identifier of the resource obtained by account registration.
+         * @param cloudConnectHandler Callback function that will get the result of the operation.
+         *
+         * @return Returns ::OC_STACK_OK if success
+         */
+        OCStackResult signIn(const std::string& userId,
+                             const std::string& accessToken,
+                             PostCallback cloudConnectHandler);
+
+        /**
+         * Function for sign-out to account server
+         *
+         * @param userId Identifier of the user obtained by account registration.
+         * @param accessToken Identifier of the resource obtained by account registration.
+         * @param cloudConnectHandler Callback function that will get the result of the operation.
+         *
+         * @return Returns ::OC_STACK_OK if success
+         */
+        OCStackResult signOut(const std::string& userId,
+                              const std::string& accessToken,
+                              PostCallback cloudConnectHandler);
+
+        /**
+         * Function for refresh access token to account server
+         *
+         * @param userId Identifier of the user obtained by account registration.
+         * @param refreshToken Refresh token used for access token refresh.
+         * @param cloudConnectHandler Callback function that will get the result of the operation.
+         *
+         * @return Returns ::OC_STACK_OK if success
+         */
+        OCStackResult refreshAccessToken(const std::string& userId,
+                                         const std::string& refreshToken,
+                                         PostCallback cloudConnectHandler);
+
+    private:
+        std::weak_ptr<IClientWrapper> m_clientWrapper;
+        std::string m_deviceID;
+        std::string m_host;
+        OCConnectivityType m_connType;
+        QualityOfService m_defaultQos;
+
+    private:
+        OCAccountManager(std::weak_ptr<IClientWrapper> clientWrapper,
+                         const std::string& host,
+                         OCConnectivityType connectivityType);
+
+        OCStackResult signInOut(const std::string& userId,
+                                const std::string& accessToken,
+                                bool isSignIn,
+                                PostCallback cloudConnectHandler);
+    };
+} // namespace OC
+
+#endif // OC_ACCOUNT_MANAGER_H_
+
index 78742bf..768f5f1 100644 (file)
@@ -578,7 +578,7 @@ namespace OC
          *
          * @param connectivityType ::OCConnectivityType type of connectivity indicating the
          *                           interface. Example: CT_DEFAULT, CT_ADAPTER_IP, CT_ADAPTER_TCP.
-         *                           if you want to use specific a Flag like IPv4
+         *                           if you want to use a specific Flag like IPv4,
          *                           you should apply OR operation for the flag in here.
          *                           Example: static_cast<OCConnectivityType>(CT_ADAPTER_TCP
          *                                                                    | OC_IP_USE_V4)
@@ -641,79 +641,25 @@ namespace OC
                                      DirectPairingCallback resultCallback);
 #ifdef WITH_CLOUD
         /**
-         * API for Account Registration to Account Server
-         * @note Not supported on client mode for now since device id is not generated yet on
-         *       client mode. So it should be server or both mode for API to work.
+         * Create an account manager object that can be used for doing request to account server.
+         * You can only create this object if OCPlatform was initialized to be a Client or
+         * Client/Server. Otherwise, this will return an empty shared ptr.
          *
-         * @param host Host IP Address of a account server.
-         * @param authProvider Provider name used for authentication.
-         * @param authCode The authorization code obtained by using an authorization server
-         *                 as an intermediary between the client and resource owner.
-         * @param connectivityType ::OCConnectivityType type of connectivity indicating the
-         *                           interface. Example: CT_DEFAULT, CT_ADAPTER_IP, CT_ADAPTER_TCP.
-         * @param cloudConnectHandler Callback function that will get the result of the operation.
-         *
-         * @return Returns ::OC_STACK_OK if success
-         */
-        OCStackResult signUp(const std::string& host,
-                             const std::string& authProvider,
-                             const std::string& authCode,
-                             OCConnectivityType connectivityType,
-                             PostCallback cloudConnectHandler);
-
-        /**
-         * API for Sign-In to Account Server
-         * @note Not supported on client mode for now since device id is not generated yet on
-         *       client mode. So it should be server or both mode for API to work.
-         *
-         * @param host Host IP Address of a account server.
-         * @param accessToken Identifier of the resource obtained by account registration.
-         * @param connectivityType ::OCConnectivityType type of connectivity indicating the
-         *                           interface. Example: CT_DEFAULT, CT_ADAPTER_IP, CT_ADAPTER_TCP.
-         * @param cloudConnectHandler Callback function that will get the result of the operation.
-         *
-         * @return Returns ::OC_STACK_OK if success
-         */
-        OCStackResult signIn(const std::string& host,
-                             const std::string& accessToken,
-                             OCConnectivityType connectivityType,
-                             PostCallback cloudConnectHandler);
-
-        /**
-         * API for Sign-Out to Account Server
-         * @note Not supported on client mode for now since device id is not generated yet on
-         *       client mode. So it should be server or both mode for API to work.
+         * @note For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
+         *       methods of this object to work since device id is not generated on Client mode.
          *
          * @param host Host IP Address of a account server.
-         * @param accessToken Identifier of the resource obtained by account registration.
          * @param connectivityType ::OCConnectivityType type of connectivity indicating the
          *                           interface. Example: CT_DEFAULT, CT_ADAPTER_IP, CT_ADAPTER_TCP.
-         * @param cloudConnectHandler Callback function that will get the result of the operation.
-         *
-         * @return Returns ::OC_STACK_OK if success
-         */
-        OCStackResult signOut(const std::string& host,
-                              const std::string& accessToken,
-                              OCConnectivityType connectivityType,
-                              PostCallback cloudConnectHandler);
-
-        /**
-         * API for Refresh Access token to Account Server
-         * @note Not supported on client mode for now since device id is not generated yet on
-         *       client mode. So it should be server or both mode for API to work.
-         *
-         * @param host Host IP Address of a account server.
-         * @param refreshToken Refresh token used for access token refresh.
-         * @param connectivityType ::OCConnectivityType type of connectivity indicating the
-         *                           interface. Example: CT_DEFAULT, CT_ADAPTER_IP, CT_ADAPTER_TCP.
-         * @param cloudConnectHandler Callback function that will get the result of the operation.
+         *                           if you want to use a specific Flag like IPv4,
+         *                           you should apply OR operation for the flag in here.
+         *                           Example: static_cast<OCConnectivityType>(CT_ADAPTER_TCP
+         *                                                                    | OC_IP_USE_V4)
          *
-         * @return Returns ::OC_STACK_OK if success
+         * @return OCAccountManager::Ptr a shared pointer to the new account manager object
          */
-        OCStackResult refreshAccessToken(const std::string& host,
-                                         const std::string& refreshToken,
-                                         OCConnectivityType connectivityType,
-                                         PostCallback cloudConnectHandler);
+        OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
+                                                            OCConnectivityType connectivityType);
 #endif // WITH_CLOUD
 #ifdef RD_CLIENT
         /**
index b3d1aa3..f1a84a3 100644 (file)
 #include "OCRepresentation.h"
 #include "OCDirectPairing.h"
 
+#ifdef WITH_CLOUD
+#include "OCAccountManager.h"
+#endif
+
 #include "oc_logger.hpp"
 
 namespace OC
@@ -274,32 +278,8 @@ namespace OC
                                          const std::string& pinNumber,
                                          DirectPairingCallback resultCallback);
 #ifdef WITH_CLOUD
-        OCStackResult signUp(const std::string& host,
-                             const std::string& authProvider,
-                             const std::string& authCode,
-                             OCConnectivityType connectivityType,
-                             PostCallback cloudConnectHandler);
-
-        OCStackResult signIn(const std::string& host,
-                             const std::string& accessToken,
-                             OCConnectivityType connectivityType,
-                             PostCallback cloudConnectHandler);
-
-        OCStackResult signOut(const std::string& host,
-                              const std::string& accessToken,
-                              OCConnectivityType connectivityType,
-                              PostCallback cloudConnectHandler);
-
-        OCStackResult signInOut(const std::string& host,
-                                const std::string& accessToken,
-                                bool isSignIn,
-                                OCConnectivityType connectivityType,
-                                PostCallback cloudConnectHandler);
-
-        OCStackResult refreshAccessToken(const std::string& host,
-                                         const std::string& refreshToken,
-                                         OCConnectivityType connectivityType,
-                                         PostCallback cloudConnectHandler);
+        OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
+                                                            OCConnectivityType connectivityType);
 #endif // WITH_CLOUD
     private:
         PlatformConfig m_cfg;
diff --git a/resource/src/OCAccountManager.cpp b/resource/src/OCAccountManager.cpp
new file mode 100644 (file)
index 0000000..f83d9b1
--- /dev/null
@@ -0,0 +1,148 @@
+/* ****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include "OCAccountManager.h"
+#include "OCUtilities.h"
+
+#include "ocstack.h"
+
+namespace OC {
+
+using OC::result_guard;
+using OC::checked_guard;
+
+OCAccountManager::OCAccountManager(std::weak_ptr<IClientWrapper> clientWrapper,
+                                   const std::string& host,
+                                   OCConnectivityType connectivityType)
+ : m_clientWrapper(clientWrapper), m_host(host), m_connType(connectivityType)
+{
+    if (m_host.empty() || m_clientWrapper.expired())
+    {
+        throw OCException(OC::Exception::INVALID_PARAM);
+    }
+
+    const char* di = OCGetServerInstanceIDString();
+    if (!di)
+    {
+        oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
+        throw OCException(OC::Exception::INVALID_PARAM);
+    }
+
+    m_deviceID.append(di);
+    checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, m_defaultQos);
+}
+
+OCAccountManager::~OCAccountManager()
+{
+}
+
+std::string OCAccountManager::host() const
+{
+    return m_host;
+}
+
+OCConnectivityType OCAccountManager::connectivityType() const
+{
+    return m_connType;
+}
+
+OCStackResult OCAccountManager::signUp(const std::string& authProvider,
+                                       const std::string& authCode,
+                                       PostCallback cloudConnectHandler)
+{
+    return result_guard(signUp(authProvider, authCode, QueryParamsMap(), cloudConnectHandler));
+}
+
+OCStackResult OCAccountManager::signUp(const std::string& authProvider,
+                                       const std::string& authCode,
+                                       const QueryParamsMap& options,
+                                       PostCallback cloudConnectHandler)
+{
+    std::string uri = m_host + OC_RSRVD_ACCOUNT_URI;
+
+    OCRepresentation rep;
+    rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
+    rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
+    rep.setValue(OC_RSRVD_AUTHCODE, authCode);
+
+    if (!options.empty())
+    {
+        OCRepresentation optionsRep;
+        for (auto iter : options)
+        {
+            optionsRep[iter.first] = iter.second;
+        }
+        rep.setValue(OC_RSRVD_OPTIONS, optionsRep);
+    }
+
+    return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
+                         OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
+                         m_connType, cloudConnectHandler, m_defaultQos);
+}
+
+OCStackResult OCAccountManager::signIn(const std::string& userId,
+                                       const std::string& accessToken,
+                                       PostCallback cloudConnectHandler)
+{
+    return result_guard(signInOut(userId, accessToken, true, cloudConnectHandler));
+}
+
+OCStackResult OCAccountManager::signOut(const std::string& userId,
+                                        const std::string& accessToken,
+                                        PostCallback cloudConnectHandler)
+{
+    return result_guard(signInOut(userId, accessToken, false, cloudConnectHandler));
+}
+
+OCStackResult OCAccountManager::signInOut(const std::string& userId,
+                                          const std::string& accessToken,
+                                          bool isSignIn,
+                                          PostCallback cloudConnectHandler)
+{
+    std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;
+
+    OCRepresentation rep;
+    rep.setValue(OC_RSRVD_USER_ID, userId);
+    rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
+    rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
+    rep.setValue(OC_RSRVD_LOGIN, isSignIn);
+
+    return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
+                         OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
+                         m_connType, cloudConnectHandler, m_defaultQos);
+}
+
+OCStackResult OCAccountManager::refreshAccessToken(const std::string& userId,
+                                                   const std::string& refreshToken,
+                                                   PostCallback cloudConnectHandler)
+{
+    std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
+
+    OCRepresentation rep;
+    rep.setValue(OC_RSRVD_USER_ID, userId);
+    rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
+    rep.setValue(OC_RSRVD_GRANT_TYPE, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
+    rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
+
+    return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
+                         OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
+                         m_connType, cloudConnectHandler, m_defaultQos);
+}
+} // namespace OC
index 8373b23..001133e 100644 (file)
@@ -306,41 +306,11 @@ namespace OC
                                              pinNumber, resultCallback);
         }
 #ifdef WITH_CLOUD
-        OCStackResult signUp(const std::string& host,
-                             const std::string& authProvider,
-                             const std::string& authCode,
-                             OCConnectivityType connectivityType,
-                             PostCallback cloudConnectHandler)
+        OCAccountManager::Ptr constructAccountManagerObject(const std::string& host,
+                                                            OCConnectivityType connectivityType)
         {
-            return OCPlatform_impl::Instance().signUp(host, authProvider, authCode,
-                                                      connectivityType, cloudConnectHandler);
-        }
-
-        OCStackResult signIn(const std::string& host,
-                             const std::string& accessToken,
-                             OCConnectivityType connectivityType,
-                             PostCallback cloudConnectHandler)
-        {
-            return OCPlatform_impl::Instance().signIn(host, accessToken,
-                                                      connectivityType, cloudConnectHandler);
-        }
-
-        OCStackResult signOut(const std::string& host,
-                              const std::string& accessToken,
-                              OCConnectivityType connectivityType,
-                              PostCallback cloudConnectHandler)
-        {
-            return OCPlatform_impl::Instance().signOut(host, accessToken,
-                                                       connectivityType, cloudConnectHandler);
-        }
-
-        OCStackResult refreshAccessToken(const std::string& host,
-                                         const std::string& refreshToken,
-                                         OCConnectivityType connectivityType,
-                                         PostCallback cloudConnectHandler)
-        {
-            return OCPlatform_impl::Instance().refreshAccessToken(host, refreshToken,
-                                                       connectivityType, cloudConnectHandler);
+            return OCPlatform_impl::Instance().constructAccountManagerObject(host,
+                                                                             connectivityType);
         }
 #endif // WITH_CLOUD
 #ifdef RD_CLIENT
index 4c89788..3525c33 100644 (file)
@@ -470,118 +470,17 @@ namespace OC
                              peer, pmSel, pinNumber, resultCallback);
     }
 #ifdef WITH_CLOUD
-    OCStackResult OCPlatform_impl::signUp(const std::string& host,
-                                          const std::string& authProvider,
-                                          const std::string& authCode,
-                                          OCConnectivityType connectivityType,
-                                          PostCallback cloudConnectHandler)
-    {
-        const char* di = OCGetServerInstanceIDString();
-        if (!di)
-        {
-            oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
-            return result_guard(OC_STACK_ERROR);
-        }
-        std::string deviceId(di);
-
-        OCRepresentation rep;
-        rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
-        rep.setValue(OC_RSRVD_AUTHPROVIDER, authProvider);
-        rep.setValue(OC_RSRVD_AUTHCODE, authCode);
-
-        std::string uri = host + OC_RSRVD_ACCOUNT_URI;
-
-        OCDevAddr devAddr;
-        QueryParamsMap queryParams;
-        HeaderOptions headerOptions;
-
-        QualityOfService defaultQos = OC::QualityOfService::NaQos;
-        checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
-
-        return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
-                             devAddr, uri, rep, queryParams, headerOptions,
-                             connectivityType, cloudConnectHandler, defaultQos);
-    }
-
-    OCStackResult OCPlatform_impl::signIn(const std::string& host,
-                                          const std::string& accessToken,
-                                          OCConnectivityType connectivityType,
-                                          PostCallback cloudConnectHandler)
-    {
-        return signInOut(host, accessToken, true, connectivityType, cloudConnectHandler);
-    }
-
-    OCStackResult OCPlatform_impl::signOut(const std::string& host,
-                                           const std::string& accessToken,
-                                           OCConnectivityType connectivityType,
-                                           PostCallback cloudConnectHandler)
-    {
-        return signInOut(host, accessToken, false, connectivityType, cloudConnectHandler);
-    }
-
-    OCStackResult OCPlatform_impl::signInOut(const std::string& host,
-                                             const std::string& accessToken,
-                                             bool isSignIn,
-                                             OCConnectivityType connectivityType,
-                                             PostCallback cloudConnectHandler)
+    OCAccountManager::Ptr OCPlatform_impl::constructAccountManagerObject(const std::string& host,
+                                                            OCConnectivityType connectivityType)
     {
-        const char* di = OCGetServerInstanceIDString();
-        if (!di)
+        if (!m_client)
         {
-            oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
-            return result_guard(OC_STACK_ERROR);
+            return std::shared_ptr<OCAccountManager>();
         }
-        std::string deviceId(di);
-
-        OCRepresentation rep;
-        rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
-        rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
-        rep.setValue(OC_RSRVD_STATUS, isSignIn);
-
-        std::string uri = host + OC_RSRVD_ACCOUNT_SESSION_URI;
-
-        OCDevAddr devAddr;
-        QueryParamsMap queryParams;
-        HeaderOptions headerOptions;
-
-        QualityOfService defaultQos = OC::QualityOfService::NaQos;
-        checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
-
-        return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
-                             devAddr, uri, rep, queryParams, headerOptions,
-                             connectivityType, cloudConnectHandler, defaultQos);
-    }
-
-    OCStackResult OCPlatform_impl::refreshAccessToken(const std::string& host,
-                                                      const std::string& refreshToken,
-                                                      OCConnectivityType connectivityType,
-                                                      PostCallback cloudConnectHandler)
-    {
-        const char* di = OCGetServerInstanceIDString();
-        if (!di)
-        {
-            oclog() << "The mode should be Server or Both to generate UUID" << std::flush;
-            return result_guard(OC_STACK_ERROR);
-        }
-        std::string deviceId(di);
-
-        OCRepresentation rep;
-        rep.setValue(OC_RSRVD_DEVICE_ID, deviceId);
-        rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);
-        rep.setValue(OC_RSRVD_GRANT_TYPE, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
-
-        std::string uri = host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;
-
-        OCDevAddr devAddr;
-        QueryParamsMap queryParams;
-        HeaderOptions headerOptions;
-
-        QualityOfService defaultQos = OC::QualityOfService::NaQos;
-        checked_guard(m_client, &IClientWrapper::GetDefaultQos, defaultQos);
 
-        return checked_guard(m_client, &IClientWrapper::PostResourceRepresentation,
-                             devAddr, uri, rep, queryParams, headerOptions,
-                             connectivityType, cloudConnectHandler, defaultQos);
+        return std::shared_ptr<OCAccountManager>(new OCAccountManager(m_client,
+                                                                      host,
+                                                                      connectivityType));
     }
 #endif // WITH_CLOUD
 } //namespace OC
index c5f10d2..6cd6d01 100644 (file)
@@ -29,8 +29,9 @@ lib_env = thread_env.Clone()
 SConscript('#resource/third_party_libs.scons', 'lib_env')
 
 oclib_env = lib_env.Clone()
-secured = lib_env.get('SECURED')
+secured = oclib_env.get('SECURED')
 target_os = oclib_env.get('TARGET_OS')
+with_cloud = oclib_env.get('WITH_CLOUD')
 
 ######################################################################
 # Build flags
@@ -77,7 +78,7 @@ if target_os in ['msys_nt', 'windows']:
        if secured == '1':
                oclib_env.AppendUnique(LIBS=['tinydtls'])
 
-if oclib_env.get('WITH_CLOUD'):
+if with_cloud:
        oclib_env.AppendUnique(CPPDEFINES = ['WITH_CLOUD'])
 
 if 'CLIENT' in oclib_env.get('RD_MODE'):
@@ -102,6 +103,9 @@ oclib_src = [
                'OCDirectPairing.cpp'
        ]
 
+if with_cloud:
+       oclib_src = oclib_src + ['OCAccountManager.cpp']
+
 if target_os in ['windows']:
        oclib_src = oclib_src + ['OCApi.cpp']
        # TODO: Add OC_EXPORT prefixes to enable DLL generation
@@ -140,6 +144,9 @@ oclib_env.UserInstallTargetHeader(header_dir + 'OCUtilities.h', 'resource', 'OCU
 oclib_env.UserInstallTargetHeader(header_dir + 'CAManager.h', 'resource', 'CAManager.h')
 oclib_env.UserInstallTargetHeader(header_dir + 'OCDirectPairing.h', 'resource', 'OCDirectPairing.h')
 
+if with_cloud:
+       oclib_env.UserInstallTargetHeader(header_dir + 'OCAccountManager.h', 'resource', 'OCAccountManager.h')
+
 # Add Provisioning library
 if target_os in ['linux', 'android', 'tizen'] and secured == '1':
         SConscript('../provisioning/SConscript')
diff --git a/resource/unittests/OCAccountManagerTest.cpp b/resource/unittests/OCAccountManagerTest.cpp
new file mode 100644 (file)
index 0000000..df55bd2
--- /dev/null
@@ -0,0 +1,164 @@
+/* ****************************************************************
+ *
+ * Copyright 2016 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
+#include <OCPlatform.h>
+#include <OCApi.h>
+#include <gtest/gtest.h>
+
+namespace OCAccountManagerTest
+{
+    using namespace OC;
+
+    // Callbacks
+    void accountHandler(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
+            const int /*eCode*/)
+    {
+    }
+
+    // Helper method
+    OCAccountManager::Ptr ConstructAccountManagerObject(std::string host)
+    {
+        auto ret = OCPlatform::constructAccountManagerObject(host, CT_DEFAULT);
+
+        if (!ret)
+        {
+            ADD_FAILURE() << "ConstructAccountManagerObject result was null";
+            throw std::runtime_error("ConstructAccountManagerObject result was null");
+        }
+
+        return ret;
+    }
+
+    // Host Test
+    TEST(HostTest, Host)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_TRUE(accountManager->host() == host);
+    }
+
+    // ConnectivityType Test
+    TEST(ConnectivityTypeTest, ConnectivityType)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_TRUE(accountManager->connectivityType() == CT_DEFAULT);
+    }
+
+    // SignUp Test
+    TEST(SignUpTest, DISABLED_SignUpWithoutOptionForValid)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string authProvider("AnyAuthProvider");
+        std::string authCode("AnyAuthCode");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_EQ(OC_STACK_OK, accountManager->signUp(authProvider, authCode, &accountHandler));
+    }
+
+    TEST(SignUpTest, DISABLED_SignUpForValid)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string authProvider("AnyAuthProvider");
+        std::string authCode("AnyAuthCode");
+        QueryParamsMap options = {};
+        options.insert(std::pair<std::string, std::string>("AnyOptionKey", "AnyOptionValue"));
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_EQ(OC_STACK_OK, accountManager->signUp(authProvider, authCode, options,
+                                                      &accountHandler));
+    }
+
+    TEST(SignUpTest, SignUpWithNullCallback)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string authProvider("AnyAuthProvider");
+        std::string authCode("AnyAuthCode");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_ANY_THROW(accountManager->signUp(authProvider, authCode, nullptr));
+    }
+
+    // SignIn Test
+    TEST(SignInTest, DISABLED_SignInForValid)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string userId("AnyUserId");
+        std::string accessToken("AnyAccessToken");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_EQ(OC_STACK_OK, accountManager->signIn(userId, accessToken, &accountHandler));
+    }
+
+    TEST(SignInTest, SignInWithNullCallback)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string userId("AnyUserId");
+        std::string accessToken("AnyAccessToken");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_ANY_THROW(accountManager->signIn(userId, accessToken, nullptr));
+    }
+
+    // SignOut Test
+    TEST(SignOutTest, DISABLED_SignOutForValid)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string userId("AnyUserId");
+        std::string accessToken("AnyAccessToken");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_EQ(OC_STACK_OK, accountManager->signOut(userId, accessToken, &accountHandler));
+    }
+
+    TEST(SignOutTest, SignOutWithNullCallback)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string userId("AnyUserId");
+        std::string accessToken("AnyAccessToken");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_ANY_THROW(accountManager->signOut(userId, accessToken, nullptr));
+    }
+
+    // RefreshAccessToken Test
+    TEST(RefreshAccessTokenTest, DISABLED_RefreshAccessTokenForValid)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string userId("AnyUserId");
+        std::string refreshToken("AnyRefreshToken");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_EQ(OC_STACK_OK, accountManager->refreshAccessToken(userId, refreshToken,
+                                                                  &accountHandler));
+    }
+
+    TEST(RefreshAccessTokenTest, RefreshAccessTokenWithNullCallback)
+    {
+        std::string host("coap://192.168.1.2:5000");
+        std::string userId("AnyUserId");
+        std::string refreshToken("AnyRefreshToken");
+        OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
+        EXPECT_TRUE(NULL != accountManager);
+        EXPECT_ANY_THROW(accountManager->refreshAccessToken(userId, refreshToken, nullptr));
+    }
+}
index d7e2cf2..e5b16fa 100644 (file)
@@ -75,12 +75,7 @@ namespace OCPlatformTest
     void pairedHandler(const PairedDevices& /*list*/)
     {
     }
-#ifdef WITH_CLOUD
-    void accountHandler(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
-            const int /*eCode*/)
-    {
-    }
-#endif
+
     //Helper methods
     void DeleteStringLL(OCStringLL* ll)
     {
@@ -923,70 +918,4 @@ namespace OCPlatformTest
         std::shared_ptr<OCDirectPairing> s_dp(new OCDirectPairing(&peer));
         EXPECT_ANY_THROW(OCPlatform::doDirectPairing(nullptr, pmSel, pin, nullptr));
     }
-#ifdef WITH_CLOUD
-    //SignUp Test
-    TEST(SignUpTest, DISABLED_SignUpWithValidParameters)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string authProvider("AnyAuthProvider");
-        std::string authCode("AnyAuthCode");
-        EXPECT_EQ(OC_STACK_OK, OCPlatform::signUp(host, authProvider, authCode,
-                                                  CT_DEFAULT, &accountHandler));
-    }
-
-    TEST(SignUpTest, SignUpWithNullCallback)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string authProvider("AnyAuthProvider");
-        std::string authCode("AnyAuthCode");
-        EXPECT_ANY_THROW(OCPlatform::signUp(host, authProvider, authCode, CT_DEFAULT, nullptr));
-    }
-
-    //SignIn Test
-    TEST(SignInTest, DISABLED_SignInWithValidParameters)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string accessToken("AnyAccessToken");
-        EXPECT_EQ(OC_STACK_OK, OCPlatform::signIn(host, accessToken, CT_DEFAULT, &accountHandler));
-    }
-
-    TEST(SignInTest, SignInWithNullCallback)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string accessToken("AnyAccessToken");
-        EXPECT_ANY_THROW(OCPlatform::signIn(host, accessToken, CT_DEFAULT, nullptr));
-    }
-
-    //SignOut Test
-    TEST(SignOutTest, DISABLED_SignOutWithValidParameters)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string accessToken("AnyAccessToken");
-        EXPECT_EQ(OC_STACK_OK, OCPlatform::signOut(host, accessToken,
-                                                   CT_DEFAULT, &accountHandler));
-    }
-
-    TEST(SignOutTest, SignOutWithNullCallback)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string accessToken("AnyAccessToken");
-        EXPECT_ANY_THROW(OCPlatform::signOut(host, accessToken, CT_DEFAULT, nullptr));
-    }
-
-    //RefreshAccessToken Test
-    TEST(RefreshAccessTokenTest, DISABLED_RefreshAccessTokenWithValidParameters)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string refreshToken("AnyRefreshToken");
-        EXPECT_EQ(OC_STACK_OK, OCPlatform::refreshAccessToken(host, refreshToken,
-                                                              CT_DEFAULT, &accountHandler));
-    }
-
-    TEST(RefreshAccessTokenTest, RefreshAccessTokenWithNullCallback)
-    {
-        std::string host("coap+tcp://192.168.1.2:5000");
-        std::string refreshToken("AnyRefreshToken");
-        EXPECT_ANY_THROW(OCPlatform::refreshAccessToken(host, refreshToken, CT_DEFAULT, nullptr));
-    }
-#endif // WITH_CLOUD
 }
index a3a78cd..70b258e 100644 (file)
@@ -55,7 +55,7 @@ unittests_env.PrependUnique(LIBS = [
                ])
 
 if unittests_env.get('SECURED') == '1':
-    unittests_env.AppendUnique(LIBS = ['tinydtls'])
+       unittests_env.AppendUnique(LIBS = ['tinydtls'])
 
 if unittests_env.get('LOGGING'):
        unittests_env.AppendUnique(CPPDEFINES = ['TB_LOG'])
@@ -66,14 +66,22 @@ if unittests_env.get('WITH_CLOUD'):
 ######################################################################
 # Source files and Targets
 ######################################################################
-unittests = unittests_env.Program('unittests', ['ConstructResourceTest.cpp',
-                                                'OCPlatformTest.cpp',
-                                                'OCRepresentationTest.cpp',
-                                                'OCRepresentationEncodingTest.cpp',
-                                                'OCResourceTest.cpp',
-                                                'OCExceptionTest.cpp',
-                                                'OCResourceResponseTest.cpp',
-                                                'OCHeaderOptionTest.cpp'])
+
+unittests_src = [
+               'ConstructResourceTest.cpp',
+               'OCPlatformTest.cpp',
+               'OCRepresentationTest.cpp',
+               'OCRepresentationEncodingTest.cpp',
+               'OCResourceTest.cpp',
+               'OCExceptionTest.cpp',
+               'OCResourceResponseTest.cpp',
+               'OCHeaderOptionTest.cpp'
+       ]
+
+if unittests_env.get('WITH_CLOUD'):
+       unittests_src = unittests_src + ['OCAccountManagerTest.cpp']
+
+unittests = unittests_env.Program('unittests', unittests_src)
 
 Alias("unittests", [unittests])