Create the default object for openldap and AD.

This commit introduces the following functionalities
=> Default AD and openldap config object would always be there.
=> User should not be able to change the type of the ldap
   once it is created.

This change is to align with redfish sehema
(https://redfish.dmtf.org/schemas/AccountService.v1_4_0.json),
In the schema AD and LDAP is a property which user can PATCH,
Now with the current code which doesn't have the default config
so for the PATCH, We were forcing the user to give all the
properties and then create the object which is against the
PATCH semantics.

TestedBy: Unit tested
          Default Object gets created when service starts.
          change of ldap type gets the error back.

Signed-off-by: Ratan Gupta <ratagupt@linux.vnet.ibm.com>
Change-Id: I0ce951a13ee525df022fb0716f0aea10d1909781
diff --git a/test/ldap_config_test.cpp b/test/ldap_config_test.cpp
index 05cea20..74c8171 100644
--- a/test/ldap_config_test.cpp
+++ b/test/ldap_config_test.cpp
@@ -71,22 +71,32 @@
     }
     MOCK_METHOD1(restartService, void(const std::string& service));
     MOCK_METHOD1(stopService, void(const std::string& service));
-    std::unique_ptr<Config>& getConfigPtr()
+    std::unique_ptr<Config>& getOpenLdapConfigPtr()
     {
-        return configPtr;
+        return openLDAPConfigPtr;
     }
 
     std::string configBindPassword()
     {
-        return getConfigPtr()->lDAPBindPassword;
+        return getADConfigPtr()->lDAPBindPassword;
     }
 
-    void restore(const char* filePath)
+    std::unique_ptr<Config>& getADConfigPtr()
     {
-        phosphor::ldap::ConfigMgr::restore(filePath);
+        return ADConfigPtr;
+    }
+    void restore()
+    {
+        // TODO enable it in later commit.
+        // phosphor::ldap::ConfigMgr::restore();
         return;
     }
 
+    void createDefaultObjects()
+    {
+        phosphor::ldap::ConfigMgr::createDefaultObjects();
+    }
+
     friend class TestLDAPConfig;
 };
 
@@ -105,32 +115,65 @@
     MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
                           dbusPersistentFilePath.c_str(),
                           tlsCacertfile.c_str());
+
+    EXPECT_CALL(manager, stopService("nslcd.service")).Times(1);
     EXPECT_CALL(manager, restartService("nslcd.service")).Times(2);
     EXPECT_CALL(manager, restartService("nscd.service")).Times(1);
     manager.createConfig(
         "ldap://9.194.251.136/", "cn=Users,dc=com", "cn=Users,dc=corp",
         "MyLdap12", ldap_base::Create::SearchScope::sub,
         ldap_base::Create::Type::ActiveDirectory, "uid", "gid");
-    manager.getConfigPtr()->enabled(true);
+    manager.getADConfigPtr()->enabled(true);
 
     EXPECT_TRUE(fs::exists(configFilePath));
-    EXPECT_EQ(manager.getConfigPtr()->lDAPServerURI(), "ldap://9.194.251.136/");
-    EXPECT_EQ(manager.getConfigPtr()->lDAPBindDN(), "cn=Users,dc=com");
-    EXPECT_EQ(manager.getConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp");
-    EXPECT_EQ(manager.getConfigPtr()->lDAPSearchScope(),
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPServerURI(),
+              "ldap://9.194.251.136/");
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPBindDN(), "cn=Users,dc=com");
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPBaseDN(), "cn=Users,dc=corp");
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPSearchScope(),
               ldap_base::Config::SearchScope::sub);
-    EXPECT_EQ(manager.getConfigPtr()->lDAPType(),
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPType(),
               ldap_base::Config::Type::ActiveDirectory);
-    EXPECT_EQ(manager.getConfigPtr()->userNameAttribute(), "uid");
-    EXPECT_EQ(manager.getConfigPtr()->groupNameAttribute(), "gid");
-    EXPECT_EQ(manager.getConfigPtr()->lDAPBindDNPassword(), "");
+
+    EXPECT_EQ(manager.getADConfigPtr()->userNameAttribute(), "uid");
+    EXPECT_EQ(manager.getADConfigPtr()->groupNameAttribute(), "gid");
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPBindDNPassword(), "");
     EXPECT_EQ(manager.configBindPassword(), "MyLdap12");
     // change the password
-    manager.getConfigPtr()->lDAPBindDNPassword("MyLdap14");
-    EXPECT_EQ(manager.getConfigPtr()->lDAPBindDNPassword(), "");
+    manager.getADConfigPtr()->lDAPBindDNPassword("MyLdap14");
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPBindDNPassword(), "");
     EXPECT_EQ(manager.configBindPassword(), "MyLdap14");
 }
 
+TEST_F(TestLDAPConfig, testDefaultObject)
+{
+    auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
+    auto tlsCacertfile = std::string(dir.c_str()) + "/" + tslCacertFile;
+    auto dbusPersistentFilePath =
+        std::string(dir.c_str()) + "/" + dbusPersistFile;
+
+    if (fs::exists(configFilePath))
+    {
+        fs::remove(configFilePath);
+    }
+    EXPECT_FALSE(fs::exists(configFilePath));
+
+    MockConfigMgr manager(bus, LDAP_CONFIG_ROOT, configFilePath.c_str(),
+                          dbusPersistentFilePath.c_str(),
+                          tlsCacertfile.c_str());
+
+    EXPECT_CALL(manager, stopService("nslcd.service")).Times(2);
+
+    manager.createDefaultObjects();
+
+    EXPECT_NE(nullptr, manager.getADConfigPtr());
+    EXPECT_NE(nullptr, manager.getOpenLdapConfigPtr());
+    EXPECT_EQ(manager.getADConfigPtr()->lDAPType(),
+              ldap_base::Config::Type::ActiveDirectory);
+    EXPECT_EQ(manager.getOpenLdapConfigPtr()->lDAPType(),
+              ldap_base::Config::Type::OpenLdap);
+}
+/*
 TEST_F(TestLDAPConfig, testRestores)
 {
     auto configFilePath = std::string(dir.c_str()) + "/" + ldapconfFile;
@@ -399,5 +442,6 @@
               ldap_base::Config::Type::OpenLdap);
     delete managerPtr;
 }
+*/
 } // namespace ldap
 } // namespace phosphor