/*[Admin] public static TM_FileStorage    load_TMServer(this TM_FileStorage tmFileStorage)
         * {
         *  return tmFileStorage.tmServer_Load();
         * }*/
        [Admin] public static TM_FileStorage    tmServer_Load(this TM_FileStorage tmFileStorage)
        {
            UserRole.Admin.demand();
            if (tmFileStorage.isNull())
            {
                return(tmFileStorage);
            }
            var tmServer = new TM_Server();

            tmServer.setDefaultData();


            var location = tmFileStorage.tmServer_Location();

            if (location.valid())
            {
                if (location.fileExists().isFalse())
                {
                    "[TM_Xml_Database][load_TMServer_Config] expected TM_Server file didn't exist, so creating it: {0}".info(location);
                    tmServer.saveAs(location);
                }
                var loadedTmServer = location.load <TM_Server>();
                if (loadedTmServer.isNull())
                {
                    "[TM_Xml_Database][load_TMServer_Config] Failed to load tmServer file: {0}   Default values will be used".error(location);
                }
                else
                {
                    tmServer = loadedTmServer;
                }
            }
            //tmDatabase.Events.After_TmServer_Load.raise();
            tmFileStorage.Server = tmServer;
            return(tmFileStorage);
        }
        [Admin] public static bool              tmServer_Save(this TM_FileStorage tmFileStorage, TM_Server tmServer)
        {
            UserRole.Admin.demand();
            if (tmFileStorage.isNull() || tmServer.isNull())
            {
                return(false);
            }
            var location = tmFileStorage.tmServer_Location();

            return((location.valid()) &&
                   tmServer.saveAs(location));
        }
        public void tm_Server_Save()
        {
            var tmFileStorage = new TM_FileStorage();

            Assert.NotNull(tmFileStorage.path_XmlDatabase());

            var tmServerLocation         = tmFileStorage.tmServer_Location();

            var tmServer_withDefaultData = new TM_Server().setDefaultData();

            Assert.IsTrue(tmServerLocation.fileExists());

               // Assert.Ignore("TO FIX (Refactor Side Effect");

            Assert.AreEqual(tmFileStorage.Server.toXml(), tmServer_withDefaultData.toXml());

            var tmpName1 = 10.randomLetters();

            tmFileStorage.Server.UserData_Configs.first().Name = tmpName1;
            Assert.IsTrue(tmFileStorage.tmServer_Save());
            Assert.AreEqual(tmServerLocation.load<TM_Server>().UserData_Configs.first().Name, tmpName1);   //check that it was  saved

            /*
             // this works but makes the test run in 10s (and the test being done is if there is no exception)

                var tmpName2 = 10.randomLetters();
                tmServerLocation.fileInfo()
                        .setAccessControl("Users", FileSystemRights.Write, AccessControlType.Deny);

                tmXmlDatabase.Server.UserData_Configs.first().Name = tmpName2;

                Assert.IsFalse(tmXmlDatabase.tmServer_Save());

                Assert.AreEqual(tmServerLocation.load<TM_Server>().UserData_Configs.first().Name, tmpName1);   //check that it was not saved

                Assert.IsTrue(tmServerLocation.delete_File());
                Assert.IsFalse(tmServerLocation.fileExists());
             */
            tmFileStorage.delete_Database();
            Assert.IsFalse(tmServerLocation.fileExists());
            Assert.IsFalse(tmFileStorage.path_XmlDatabase().dirExists());

            //check when not UsingFileStorage

            //check for nulls
            tmFileStorage.Path_XmlDatabase = null;
            Assert.IsFalse(tmFileStorage.tmServer_Save());
            Assert.IsFalse(new TM_FileStorage(false).tmServer_Save());
        }
        public void tm_Server_Load_UsingFileStorage()
        {
            var tmFileStorage = new TM_FileStorage(false);

            var baseReadOnlyDir = "_tmp_webRoot".tempDir();
            var webRootVirualPath = @"site\wwwroot";        // simulates use of AppData
            tmFileStorage.WebRoot = baseReadOnlyDir.pathCombine(webRootVirualPath).createDir();

            tmFileStorage.set_Path_XmlDatabase()
                         .set_Path_UserData()
                         .tmServer_Load();

            var tmServerFile     = tmFileStorage.tmServer_Location();
            var expectedLocation = tmFileStorage.Path_XmlDatabase.pathCombine(TMConsts.TM_SERVER_FILENAME);

            Assert.IsNotNull(tmFileStorage.path_XmlDatabase());
            Assert.IsTrue   (TM_Status.Current.TM_Database_Location_Using_AppData);
            Assert.NotNull  (tmFileStorage.Server);
            Assert.IsTrue   (tmServerFile.fileExists());
            Assert.AreEqual(tmServerFile, expectedLocation);

            //            Assert.Ignore("TO FIX (Refactor Side Effect");
             //       Assert.Ignore("TO FIX (Refactor Side Effect");
            var tmServer_withDefaultData = new TM_Server().setDefaultData();
            Assert.AreEqual(tmFileStorage.Server.toXml(), tmServer_withDefaultData.toXml());

            //make a change, saved it and ensure it gets loaded ok

            var tmpName1 = 10.randomLetters();
            var tmpName2 = 10.randomLetters();
            tmFileStorage.Server.UserData_Configs.first().Name = tmpName1;
            tmFileStorage.tmServer_Save();
            tmFileStorage.Server.UserData_Configs.first().Name = tmpName2;

            tmFileStorage.tmServer_Load();
            Assert.AreEqual   (tmFileStorage.Server.UserData_Configs.first().Name, tmpName1);
            Assert.AreNotEqual(tmFileStorage.Server.toXml(), tmServer_withDefaultData.toXml());

            //Try loading up an corrupted tmServer (whcih will default to load a default TM_Server
            "aaaa".saveAs(tmServerFile);
            tmFileStorage.tmServer_Load();
            Assert.AreNotEqual(tmFileStorage.Server.UserData_Configs.first().Name, tmpName1);
            Assert.AreEqual   (tmFileStorage.Server.toXml(), tmServer_withDefaultData.toXml());

            Files.deleteFolder(baseReadOnlyDir, true);
            Assert.IsFalse(baseReadOnlyDir.dirExists());

            //tmXmlDatabase.delete_Database();
        }