public void Test_CountFiles()
        {
            string applicationName = "TestApplication";

            string applicationPath = TestUtilities.GetTestApplicationPath(this, applicationName);

            string dataDirectoryPath = TestUtilities.GetTestDataPath(this, applicationName);

            string backupDirectoryPath = dataDirectoryPath + Path.DirectorySeparatorChar + "Backup";

            string exportDirectoryPath = dataDirectoryPath + Path.DirectorySeparatorChar + "Export";

            string legacyDirectoryPath = dataDirectoryPath + Path.DirectorySeparatorChar + "Legacy";

            VersionTestUtilities.CreateDummyVersion(dataDirectoryPath, "testing");

            CreateDummyFiles(dataDirectoryPath);

            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "Test";

            DataAccess.Data.Saver.Save(user);

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            DataAccess.Data.Saver.Save(role);

            TestArticle article = new TestArticle();
            article.ID = Guid.NewGuid();
            article.Title = "Test";

            DataAccess.Data.Saver.Save(article);

            TestCategory category = new TestCategory();
            category.Name = "Test";

            DataAccess.Data.Saver.Save(category);

            // Export data
            ApplicationBackup backup = new ApplicationBackup();
            backup.BackupDirectoryPath = backupDirectoryPath;
            backup.ExportDirectoryPath = exportDirectoryPath;
            backup.DataDirectoryPath = dataDirectoryPath;

            string zipFilePath = backup.Backup();

            Assert.IsTrue(File.Exists(zipFilePath), "The zip file wasn't created.");

            long total = 0;
            using (ZipAnalyzer analyzer = new ZipAnalyzer(zipFilePath))
            {
                total = analyzer.CountFiles();
            }

            Assert.AreEqual(7, total, "The total number of files in the the zip.");
        }
        public void Test_Execute_Reference()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "-- First Name -- ";
            user.LastName = "-- Last Name --";
            //user.Username = "******";

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            role.Name = "-- Test Role --";

            user.Roles = new TestRole[] {role};

            EntityReference reference = DataAccess.Data.Referencer.GetActiveReferences(user)[0];

            XmlDocument document = XmlUtilities.SerializeToDocument(reference);

            RenamePropertyCommand command = new RenamePropertyCommand();
            command.TypeName = user.ShortTypeName;
            command.PropertyName = "Roles";
            command.NewPropertyName = "GrantedRoles";

            command.Execute(document);

            EntityReference updatedReference = (EntityReference)XmlUtilities.DeserializeFromDocument(document, typeof(EntityReference));

            Assert.AreEqual("GrantedRoles", updatedReference.Property1Name, "The property name wasn't changed.");
        }
        public void Test_Delete_EntityAndReference_Sync()
        {
            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            Guid roleID = role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            user.Roles = new TestRole[] {role};

            DataAccess.Data.Saver.Save(role);
            DataAccess.Data.Saver.Save(user);

            EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(typeof(TestUser).Name, typeof(TestRole).Name);

            Assert.AreEqual(1, references.Count, "Incorrect number of references found.");

            DataAccess.Data.Deleter.Delete(user);

            EntityReferenceCollection references2 = DataAccess.Data.Referencer.GetReferences(typeof(TestUser).Name, typeof(TestRole).Name);

            Assert.AreEqual(0, references2.Count, "Reference not deleted.");
        }
        public void Test_Save_2ConvergingReferences()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            Guid userID = user.ID;
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            Guid roleID = role.ID;
            role.Name = "Test Role";

            TestUser user2 = new TestUser();
            user.ID = Guid.NewGuid();
            Guid user2ID = user.ID;
            user2.FirstName = "Test2";
            user2.LastName = "User2";

            TestRole role2 = new TestRole();
            role2.ID = Guid.NewGuid();
            Guid role2ID = role2.ID;
            role2.Name = "Test Role2";

            user.Roles = new TestRole[] {role, role2};

            DataAccess.Data.Saver.Save(role2);
            DataAccess.Data.Saver.Save(role);
            DataAccess.Data.Saver.Save(user);

            EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences("TestUser", "TestRole");

            Assert.AreEqual(2, references.Count, "Incorrect number of references found.");

            EntityReference reference1 = (EntityReference)references[0];
            EntityReference reference2 = (EntityReference)references[1];

            // Switch the references around if necessary to match (so they can be found in any order)
            if (!reference1.Includes(role.ID, "Users"))
            {
                EntityReference tmp = reference1;
                reference1 = reference2;
                reference2 = tmp;
            }

            Assert.IsTrue(reference1.Includes(user.ID, "Roles"), "First reference does not include expected user.");
            Assert.IsTrue(reference1.Includes(role.ID, "Users"), "First reference does not include expected role.");

            Assert.IsTrue(reference2.Includes(user.ID, "Roles"), "Second reference does not include expected user.");
            Assert.IsTrue(reference2.Includes(role2.ID, "Users"), "Second reference does not include expected role.");

            Assert.IsFalse(reference1.Includes(role2.ID, "Users"), "First reference includes unexpected role.");

            Assert.IsFalse(reference2.Includes(role.ID, "Users"), "Second reference includes unexpected role.");
        }
        public void Test_GetEntitiesByFilterGroup_EmptyGroup()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing a simple query with the PropertyFilter.", NLog.LogLevel.Debug))
            {
                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";
                user.Username = "******";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test role";

                user.Roles = new TestRole[]{role};

                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);

                ReferenceFilter filter = (ReferenceFilter)DataAccess.Data.CreateReferenceFilter();
                filter.PropertyName = "Roles";
                filter.ReferencedEntityID = role.ID;
                filter.ReferenceType = role.GetType();
                filter.AddType(typeof(TestUser));

                PropertyFilter filter2 = (PropertyFilter)DataAccess.Data.CreatePropertyFilter();
                filter2.PropertyName = "Username";
                filter2.PropertyValue = user.Username;
                filter2.AddType(typeof(TestUser));

                FilterGroup filterGroup = new FilterGroup();
                // Leave empty by not adding filters
                //filterGroup.Add(filter);
                //filterGroup.Add(filter2);

                IEntity[] found = (IEntity[])DataAccess.Data.Indexer.GetEntities(filterGroup);

                Assert.IsNotNull(found, "Null value returned");

                if (found != null)
                {
                    Assert.IsTrue(found.Length > 0, "No results found.");

                    if (found.Length > 0)
                    {
                        TestUser foundUser = (TestUser)found[0];

                        Assert.IsNotNull(foundUser, "foundUser == null");

                        Assert.AreEqual(user.ID, foundUser.ID, "The IDs don't match.");
                    }
                }
            }
        }
        public void Test_ExportToXml()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "Test";

            DataAccess.Data.Saver.Save(user);

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            DataAccess.Data.Saver.Save(role);

            TestArticle article = new TestArticle();
            article.ID = Guid.NewGuid();
            article.Title = "Test";

            DataAccess.Data.Saver.Save(article);

            TestCategory category = new TestCategory();
            category.Name = "Test";

            DataAccess.Data.Saver.Save(category);

            string outputDirectory = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Exported";

            int expectedCount = 0;

            foreach (string dataStoreName in DataAccess.Data.GetDataStoreNames())
            {
                foreach (IEntity entity in DataAccess.Data.Stores[dataStoreName].Indexer.GetEntities())
                    expectedCount ++;
            }

            DataExporter exporter = (DataExporter)DataAccess.Data.InitializeDataExporter();
            exporter.ExportDirectoryPath = outputDirectory;

            exporter.ExportToXml();

            Assert.IsTrue(Directory.Exists(outputDirectory), "The output directory doesn't exist.");

            int fileCount = 0;
            foreach (string directory in Directory.GetDirectories(outputDirectory))
            {
                foreach (String file in Directory.GetFiles(directory))
                {
                    fileCount++;
                }
            }

            Assert.AreEqual(expectedCount, fileCount, "Incorrect number of files found.");
        }
        public virtual void Test_Update()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the DataAccess.Data.Updater.Update function.", NLog.LogLevel.Debug))
            {
                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                Guid userID = user.ID;
                user.FirstName = "Test-Before";
                user.LastName = "User";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                Guid roleID = role.ID;
                role.Name = "Test Role";

                user.Roles = new TestRole[] { role };

                DataAccess.Data.Saver.Save(role);

                DataAccess.Data.Saver.Save(user);

                TestUser user2 = (TestUser)DataAccess.Data.Reader.GetEntity(typeof(TestUser), "ID", user.ID);

                Assert.IsNotNull(user2, "user2 == null");

                Assert.AreEqual(user.FirstName, user2.FirstName, "The name doesn't appear to have been saved.");

                DataAccess.Data.Activator.Activate(user2);

                user2.FirstName = "Test-Updated";

                //user2.Roles.RemoveAt(0);

                DataAccess.Data.Updater.Update(user2);

                // TODO: Check if its possible to remove and dispose the store, then have it automatically reload when needed
                //store.Dispose();
                //DataAccess.Data.Stores.Remove(store);

                TestUser user3 = (TestUser)DataAccess.Data.Reader.GetEntity<TestUser>("ID", user2.ID);

                Assert.IsNotNull(user3, "user3 == null");

                Assert.AreEqual(user2.ID, user3.ID, "The IDs don't match.");
                Assert.AreEqual(user2.FirstName, user3.FirstName, "The name doesn't appear to have been updated.");

                //Assert.IsNotNull(toDelete, "The toDelete list is null.");
                //if (toDelete != null)
                //	Assert.AreEqual(1, toDelete.Length, "Incorrect number of entities in toDelete list. Expecting the obsolete reference to be in the list.");

            }
        }
        public void Test_Static_Add()
        {
            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            Guid roleID = role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            user.Roles = Collection<TestRole>.Add(user.Roles, role);

            Assert.AreEqual(1, user.Roles.Length, "The role doesn't appear to have been added properly.");
        }
        public void Test_Static_GetByID_Found()
        {
            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            Guid roleID = role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            user.Roles = new TestRole[] {role};

            TestRole role2 = Collection<TestRole>.GetByID(user.Roles, role.ID);

            Assert.IsNotNull(role2, "The role wasn't retrieved properly.");
        }
        public void Test_GetActiveReferences_Multiple_Sync()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();

            TestRole role2 = new TestRole();
            role2.ID = Guid.NewGuid();

            user.Roles = new TestRole[] {role, role2};

            EntityReferenceCollection references = DataAccess.Data.Referencer.GetActiveReferences(user);

            Assert.IsNotNull(references, "The reference collection is null.");

            Assert.AreEqual(2, references.Count, "Incorrect number of references returned.");

            if (references != null && references.Count == 2)
            {
                EntityReference reference = references[0];

                Assert.AreEqual(user.ID, references[0].Entity1ID, "The entity 1 ID wasn't set correctly.");
                Assert.AreEqual(role.ID, references[0].Entity2ID, "The entity 2 ID wasn't set correctly.");

                Assert.AreEqual("TestUser", references[0].Type1Name, "The type name 1 wasn't set correctly.");
                Assert.AreEqual("TestRole", references[0].Type2Name, "The type name 2 wasn't set correctly.");

                Assert.AreEqual("Roles", references[0].Property1Name, "The property 1 name wasn't set correctly.");
                Assert.AreEqual("Users", references[0].Property2Name, "The property 2 name wasn't set correctly.");

                EntityReference reference2 = references[1];

                Assert.AreEqual(user.ID, reference2.Entity1ID, "The entity 1 ID wasn't set correctly.");
                Assert.AreEqual(role2.ID, reference2.Entity2ID, "The entity 2 ID wasn't set correctly.");

                Assert.AreEqual("TestUser", reference2.Type1Name, "The type name 1 wasn't set correctly.");
                Assert.AreEqual("TestRole", reference2.Type2Name, "The type name 2 wasn't set correctly.");

                Assert.AreEqual("Roles", reference2.Property1Name, "The property 1 name wasn't set correctly.");
                Assert.AreEqual("Users", reference2.Property2Name, "The property 2 name wasn't set correctly.");
            }
        }
        public void Test_GetOtherEntity()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();

            EntityReference reference = new EntityReference();

            reference.SourceEntity = user;
            reference.ReferenceEntity = role;

            IEntity otherEntity1 = reference.GetOtherEntity(user);
            IEntity otherEntity2 = reference.GetOtherEntity(role);

            Assert.AreEqual(role.ShortTypeName, otherEntity1.ShortTypeName, "Failed to get other entity. #1");
            Assert.AreEqual(user.ShortTypeName, otherEntity2.ShortTypeName, "Failed to get other entity. #2");
        }
        public virtual void Test_PreUpdate()
        {
            // Create the dummy objects
            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            Guid roleID = role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            // Add the role to the User.Roles collection
            user.Roles = new TestRole[] { role };

            // Save both objects
            DataAccess.Data.Saver.Save(role);
            DataAccess.Data.Saver.Save(user);

            // Load the user to another variable
            TestUser user2 = (TestUser)DataAccess.Data.Reader.GetEntity(typeof(TestUser), "ID", user.ID);

            // Activate the loaded user object
            DataAccess.Data.Activator.Activate(user2);

            // Remove the role from the list
            user2.Roles = Collection<TestRole>.RemoveAt(user2.Roles, 0);

            // Test the DataStore.PreUpdate function
            DataAccess.Data.Updater.PreUpdate(user2);

            // Load the user again to a new variable, which should now reflect the changes
            TestUser user3 = (TestUser)DataAccess.Data.Reader.GetEntity(typeof(TestUser), "ID", user2.ID);

            DataAccess.Data.Activator.Activate(user3, "Roles");

            // Check the roles list on the newly loaded user object
            // Should be Length == 0
            Assert.AreEqual(0, user3.Roles.Length, "Incorrect number of roles found on retrieved user entity.");
        }
        public void Test_CreateFilePath_Reference()
        {
            TestUser user = new TestUser();
            user.FirstName = "First";
            user.ID = Guid.NewGuid();

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();

            user.Roles = new TestRole[] {role};

            string basePath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Export";

            EntityReference reference = DataAccess.Data.Referencer.GetActiveReferences(user)[0];

            string path = new EntityFileNamer(reference, basePath).CreateFilePath();

            string expected = basePath + Path.DirectorySeparatorChar
                + role.ShortTypeName + "-" + user.ShortTypeName + Path.DirectorySeparatorChar
                + reference.ID.ToString() + ".xml";

            Assert.AreEqual(expected, path, "The path doesn't match what's expected.");
        }
        public virtual void Test_Activate()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the Active function.", NLog.LogLevel.Debug))
            {

                TestUser user = new TestUser();
                Guid userID = user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                Guid roleID = role.ID = Guid.NewGuid();
                role.Name = "Test Role";

                user.Roles = Collection<TestRole>.Add(user.Roles, role);

                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);

                user = null;
                role = null;

                TestUser user2 = DataAccess.Data.Reader.GetEntity<TestUser>("ID", userID);

                DataAccess.Data.Activator.Activate(user2);

                Assert.IsNotNull(user2.Roles, "The user.Roles property is null.");

                if (user2.Roles != null)
                {
                    Assert.AreEqual(1, user2.Roles.Length, "Wrong number of roles found.");

                    Assert.AreEqual(roleID, user2.Roles[0].ID, "ID of referenced role after activating doesn't match the original.");
                }
            }
        }
        public virtual void Test_Update_RemoveObsoleteReference_Sync()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the DataAccess.Data.Updater.Update function to ensure obsolete references are removed.", NLog.LogLevel.Debug))
            {
                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                Guid userID = user.ID;
                user.FirstName = "Test-Before";
                user.LastName = "User";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                Guid roleID = role.ID;
                role.Name = "Test Role";

                user.Roles = new TestRole[] { role };

                // Save the user and the role with a reference between them
                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);

                TestUser user2 = (TestUser)DataAccess.Data.Reader.GetEntity(typeof(TestUser), "ID", user.ID);

                Assert.IsNotNull(user2, "user2 == null");

                Assert.AreEqual(user.FirstName, user2.FirstName, "The name doesn't appear to have been saved.");

                DataAccess.Data.Activator.Activate(user2);

                // Clear the roles from the user
                user2.Roles = new TestRole[]{};

                // Update the user which should remove the obsolete reference
                DataAccess.Data.Updater.Update(user2);

                TestUser user3 = (TestUser)DataAccess.Data.Reader.GetEntity<TestUser>("ID", user2.ID);

                Assert.IsNotNull(user3, "user3 == null");

                Assert.AreEqual(user2.ID, user3.ID, "The IDs don't match.");

                EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences("TestUser", "TestRole");

                Assert.AreEqual(0, references.Count, "Invalid number of references found.");

            }
        }
        public virtual void Test_Update_MaintainReferences()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the update function to see if it maintains references.", NLog.LogLevel.Debug))
            {
                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test role";

                // This should remain commented out to check for exclusion
                user.Roles = new TestRole[]{role};

                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);

                EntityReference reference = new EntityReference();
                reference.ID = Guid.NewGuid();
                reference.Type1Name = "TestUser";
                reference.Type2Name = "TestRole";
                reference.Entity1ID = user.ID;
                reference.Entity2ID = role.ID;
                reference.Property1Name = "Roles";
                reference.Property2Name = "Users";

                TestUser user2 = DataAccess.Data.Reader.GetEntity<TestUser>("ID", user.ID);

                DataAccess.Data.Activator.Activate(user2);

                string newFirstName =  "Something else";
                user2.FirstName  = newFirstName;

                DataAccess.Data.Updater.Update(user2);

                TestUser user3 = DataAccess.Data.Reader.GetEntity<TestUser>("ID", user.ID);

                DataAccess.Data.Activator.Activate(user3);

                Assert.IsNotNull(user3.Roles);

                Assert.AreEqual(newFirstName, user3.FirstName, "First name mismatch.");

                if (user3.Roles != null)
                    Assert.AreEqual(1, user3.Roles.Length, "Incorrect number of roles.");

            }
        }
        public void Test_Update_2References_CheckLocationOfReferencedEntities()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            Guid userID = user.ID;
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            Guid roleID = role.ID;
            role.Name = "Test Role";

            TestUser user2 = new TestUser();
            user.ID = Guid.NewGuid();
            Guid user2ID = user.ID;
            user2.FirstName = "Test2";
            user2.LastName = "User2";

            TestRole role2 = new TestRole();
            role2.ID = Guid.NewGuid();
            Guid role2ID = role2.ID;
            role2.Name = "Test Role2";

            user.Roles = new TestRole[] {role};
            user2.Roles = new TestRole[] {role2};

            DataAccess.Data.Saver.Save(role2);
            DataAccess.Data.Saver.Save(role);
            DataAccess.Data.Saver.Save(user2);
            DataAccess.Data.Saver.Save(user);

            EntityReferenceCollection references = (EntityReferenceCollection)DataAccess.Data.Referencer.GetReferences("TestUser", "TestRole");

            Assert.AreEqual(2, references.Count, "Incorrect number of references found.");

            // Load the roles out of the users store (there should be none)
            IEntity[] rolesInUsersStore = DataAccess.Data.Stores[typeof(TestUser)].Indexer.GetEntities<TestRole>();

            Assert.AreEqual(0, rolesInUsersStore.Length, "Invalid number of roles found in users store after save.");

            IEntity[] rolesInRolesStore = DataAccess.Data.Stores[typeof(TestRole)].Indexer.GetEntities<TestRole>();

            Assert.AreEqual(2, rolesInRolesStore.Length, "Invalid number of roles found in roles store after save.");

            TestUser foundUser = DataAccess.Data.Reader.GetEntity<TestUser>("ID", user.ID);

            Assert.IsNotNull(foundUser, "The foundUser variable is null.");

            foundUser.Username = user.Username + " 2";

            DataAccess.Data.Updater.Update(foundUser);

            // Load the roles out of the users store (there should be none)
            IEntity[] rolesInUsersStore2 = DataAccess.Data.Stores[typeof(TestUser)].Indexer.GetEntities<TestRole>();

            Assert.AreEqual(0, rolesInUsersStore2.Length, "Role(s) found in users store after update.");

            IEntity[] rolesInRolesStore2 = DataAccess.Data.Stores[typeof(TestRole)].Indexer.GetEntities<TestRole>();

            Assert.AreEqual(2, rolesInRolesStore2.Length, "Role(s) not found in roles store after update.");
        }
        public void Test_GetReference_Sync()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the GetReference function with a synchronous reference to ensure it retrieves the correct reference.", NLog.LogLevel.Debug))
            {

                TestUtilities.CreateDummyReferences(100);

                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestUser user2 = new TestUser();
                user2.ID = Guid.NewGuid();
                user2.FirstName = "Test";
                user2.LastName = "User 2";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test Role";

                TestRole role2 = new TestRole();
                role2.ID = Guid.NewGuid();
                role2.Name = "Test Role 2";

                user.Roles = new TestRole[] {role, role2};

                role2.Users = new TestUser[] {user, user2};

                EntityReference originalReference = DataAccess.Data.Referencer.GetActiveReferences(user)[0];

                LogWriter.Debug("Original reference - Entity 1 ID: " + originalReference.Entity1ID.ToString());
                LogWriter.Debug("Original reference - Entity 2 ID: " + originalReference.Entity2ID.ToString());
                LogWriter.Debug("Original reference - Property 1 name: " + originalReference.Property1Name);
                LogWriter.Debug("Original reference - Property 2 name: " + originalReference.Property2Name);
                LogWriter.Debug("Original reference - Type 1 name: " + originalReference.Type1Name);
                LogWriter.Debug("Original reference - Type 2 name: " + originalReference.Type2Name);

                foreach (EntityReference r in DataAccess.Data.Referencer.GetActiveReferences(user))
                    DataAccess.Data.Saver.Save(r);

                foreach (EntityReference r in DataAccess.Data.Referencer.GetActiveReferences(role2))
                    DataAccess.Data.Saver.Save(r);

                EntityReference reference = DataAccess.Data.Referencer.GetReference(EntityState.GetType(originalReference.Type1Name),
                                                                                    originalReference.Entity1ID,
                                                                                    originalReference.Property1Name,
                                                                                    EntityState.GetType(originalReference.Type2Name),
                                                                                    originalReference.Entity2ID,
                                                                                    originalReference.Property2Name,
                                                                                    false);

                Assert.IsNotNull(reference, "The return value is null.");

                LogWriter.Debug("Found reference - Entity 1 ID: " + reference.Entity1ID.ToString());
                LogWriter.Debug("Found reference - Entity 2 ID: " + reference.Entity2ID.ToString());
                LogWriter.Debug("Found reference - Property 1 name: " + reference.Property1Name);
                LogWriter.Debug("Found reference - Property 2 name: " + reference.Property2Name);
                LogWriter.Debug("Found reference - Type 1 name: " + reference.Type1Name);
                LogWriter.Debug("Found reference - Type 2 name: " + reference.Type2Name);

                Assert.IsTrue(originalReference.Entity1ID.ToString() == reference.Entity1ID.ToString()
                              || originalReference.Entity2ID.ToString() == reference.Entity1ID.ToString(), "Entity 1 ID doesn't match expected.");

                Assert.IsTrue(originalReference.Entity2ID.ToString() == reference.Entity2ID.ToString()
                              || originalReference.Entity1ID.ToString() == reference.Entity2ID.ToString(), "Entity 2 ID doesn't match expected.");

                Assert.IsTrue(originalReference.Type1Name.ToString() == reference.Type1Name.ToString()
                              || originalReference.Type2Name.ToString() == reference.Type1Name.ToString(), "Type 1 name doesn't match expected.");

                Assert.IsTrue(originalReference.Type2Name.ToString() == reference.Type2Name.ToString()
                              || originalReference.Type1Name.ToString() == reference.Type2Name.ToString(), "Type 2 name doesn't match expected.");

                Assert.IsTrue(originalReference.Property1Name.ToString() == reference.Property1Name.ToString()
                              || originalReference.Property2Name.ToString() == reference.Property1Name.ToString(), "Property 1 name doesn't match expected.");

                Assert.IsTrue(originalReference.Property2Name.ToString() == reference.Property2Name.ToString()
                              || originalReference.Property1Name.ToString() == reference.Property2Name.ToString(), "Property 2 name doesn't match expected.");

            }
        }
        public void Test_GetEntitiesByReferenceFilter_Exclusion()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing a simple query with the PropertyFilter.", NLog.LogLevel.Debug))
            {

                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test role";

                // This should remain commented out to check for exclusion
                //user.Roles = new TestRole[]{role};

                DataAccess.Data.Saver.Save(user);
                DataAccess.Data.Saver.Save(role);

                ReferenceFilter filter = (ReferenceFilter)DataAccess.Data.CreateFilter(typeof(ReferenceFilter));
                filter.PropertyName = "Roles";
                filter.ReferencedEntityID = role.ID;
                //filter.ReferenceType = role.GetType();

                filter.AddType(typeof(TestUser));

                IEntity[] found = (IEntity[])DataAccess.Data.Indexer.GetEntities(filter);

                Assert.IsNotNull(found, "Null value returned");

                if (found != null)
                {
                    Assert.IsTrue(found.Length == 0, "Results returned when none should have been returned.");

                    /*if (found.Length > 0)
                    {
                        TestUser foundUser = (TestUser)found[0];

                        Assert.AreEqual(user.ID, foundUser.ID, "The IDs don't match.");
                    }*/
                }

            }
        }
        public void Test_Static_RemoveAt()
        {
            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            Guid roleID = role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            user.Roles = new TestRole[] {role};

            user.Roles = Collection<TestRole>.RemoveAt(user.Roles, 0);

            Assert.AreEqual(0, user.Roles.Length, "The role doesn't appear to have been removed properly.");
        }
        public void Test_GetReferences_2References()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the retrieval of references for an entity.", NLog.LogLevel.Debug))
            {
                TestUser user = new TestUser();
                Guid userID = user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                Guid roleID = role.ID = Guid.NewGuid();
                role.Name = "Test Role";

                TestRole role2 = new TestRole();
                Guid role2ID = role2.ID = Guid.NewGuid();
                role2.Name = "Test Role 2 ";

                user.Roles = new TestRole[] {role, role2};

                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(role2);
                DataAccess.Data.Saver.Save(user);

                EntityReferenceCollection referenceEntities = DataAccess.Data.Referencer.GetReferences("TestUser", "TestRole");

                Assert.AreEqual(2, referenceEntities.Count, "Incorrect number of references found in the store after saving entities.");

                EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(user.GetType(), user.ID, "Roles", typeof(TestRole), false);

                Assert.IsNotNull(references, "The references object returned was null.");

                if (references != null)
                {
                    Assert.AreEqual(2, references.Count, "Wrong number of references returned.");

                    EntityReference reference1 = references[0];
                    EntityReference reference2 = references[1];

                    // Switch the references around if necessary to match (so they can be found in any order)
                    if (!reference1.Includes(role.ID, "Users"))
                    {
                        EntityReference tmp = reference1;
                        reference1 = reference2;
                        reference2 = tmp;
                    }

                    Assert.IsTrue(reference1.Includes(userID, "Roles"), "The user ID wasn't found on the reference.");
                    Assert.IsTrue(reference1.Includes(roleID, "Users"), "The role ID wasn't found on the reference.");

                    Assert.IsTrue(reference2.Includes(userID, "Roles"), "The user ID wasn't found on the second reference.");
                    Assert.IsTrue(reference2.Includes(role2ID, "Users"), "The role2 ID wasn't found on the second reference.");
                }
            }
        }
        public void Test_GetEntityType()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the DataUtilities.GetEntityType function.", NLog.LogLevel.Debug))
            {

                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test Role";

                user.Roles = Collection<TestRole>.Add(user.Roles, role);
                role.Users = Collection<TestUser>.Add(role.Users, user);

                PropertyInfo rolesProperty = user.GetType().GetProperty("Roles");

                Type entityType = DataUtilities.GetEntityType(user, rolesProperty);

                Assert.AreEqual(entityType.FullName, role.GetType().FullName, "The types don't match #1.");

                PropertyInfo usersProperty = role.GetType().GetProperty("Users");

                Type entityType2 = DataUtilities.GetEntityType(role, usersProperty);

                Assert.AreEqual(entityType2.FullName, user.GetType().FullName, "The types don't match #2.");
            }
        }
        public void Test_GetReference_Sync_Exclude()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the GetReference function with a synchronous reference to ensure it retrieves the correct reference.", NLog.LogLevel.Debug))
            {
                TestUtilities.CreateDummyReferences(100);

                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestUser user2 = new TestUser();
                user2.ID = Guid.NewGuid();
                user2.FirstName = "Test";
                user2.LastName = "User 2";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test Role";

                TestRole role2 = new TestRole();
                role2.ID = Guid.NewGuid();
                role2.Name = "Test Role 2";

                user.Roles = new TestRole[] {role};
                user2.Roles = new TestRole[] { role2 };
                //role2.Users = new TestUser[] {user, user2};

                LogWriter.Debug("User 1 ID: " + user.ID.ToString());
                LogWriter.Debug("User 2 ID: " + user2.ID.ToString());
                LogWriter.Debug("Role 1 ID: " + role.ID.ToString());
                LogWriter.Debug("Role 2 ID: " + role2.ID.ToString());

                EntityReferenceCollection userReferences = DataAccess.Data.Referencer.GetActiveReferences(user);
                EntityReferenceCollection user2References = DataAccess.Data.Referencer.GetActiveReferences(user2);

                Assert.AreEqual(1, userReferences.Count, "userReferences.Length is incorrect");
                Assert.AreEqual(1, user2References.Count, "user2References.Length is incorrect");

                EntityReference originalReference1 = userReferences[0];
                EntityReference originalReference2 = user2References[0];

                LogWriter.Debug("Original reference - Entity 1 ID: " + originalReference1.Entity1ID.ToString());
                LogWriter.Debug("Original reference - Entity 2 ID: " + originalReference1.Entity2ID.ToString());
                LogWriter.Debug("Original reference - Property 1 name: " + originalReference1.Property1Name);
                LogWriter.Debug("Original reference - Property 2 name: " + originalReference1.Property2Name);
                LogWriter.Debug("Original reference - Type 1 name: " + originalReference1.Type1Name);
                LogWriter.Debug("Original reference - Type 2 name: " + originalReference1.Type2Name);

                DataAccess.Data.Saver.Save(role2);
                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);
                DataAccess.Data.Saver.Save(user2);

                string referenceStoreName = DataUtilities.GetDataStoreName("TestUser", "TestRole");

                EntityReferenceCollection referenceEntities = DataAccess.Data.Referencer.GetReferences(user.GetType().Name, role.GetType().Name);

                Assert.AreEqual(2, referenceEntities.Count, "Incorrect number of references found in the store after saving entities.");

                // Switch the references around if necessary
                if (referenceEntities[0].Entity1ID == originalReference2.Entity1ID)
                {
                    EntityReference r1 = referenceEntities[1];
                    EntityReference r2 = referenceEntities[0];

                    referenceEntities = new EntityReferenceCollection();
                    referenceEntities.Add(r1);
                    referenceEntities.Add(r2);
                }

                bool firstReferenceMatches = (((referenceEntities[0].Includes(originalReference1.Entity1ID, originalReference1.Property1Name)
                                                && referenceEntities[0].Includes(originalReference1.Entity2ID, originalReference1.Property2Name))
                                               ||
                                               ((referenceEntities[0].Includes(originalReference2.Entity1ID, originalReference2.Property1Name)
                                                 && referenceEntities[0].Includes(originalReference2.Entity2ID, originalReference2.Property2Name)))));

                bool secondReferenceMatches = (((referenceEntities[1].Includes(originalReference1.Entity1ID, originalReference1.Property1Name)
                                                 && referenceEntities[1].Includes(originalReference1.Entity2ID, originalReference1.Property2Name))
                                                ||
                                                ((referenceEntities[1].Includes(originalReference2.Entity1ID, originalReference2.Property1Name)
                                                  && referenceEntities[1].Includes(originalReference2.Entity2ID, originalReference2.Property2Name)))));

                Assert.IsTrue(firstReferenceMatches,
                              "First reference doesn't match original references.");

                Assert.IsTrue(secondReferenceMatches,
                              "Second reference doesn't match original references.");

                EntityReference reference = DataAccess.Data.Referencer.GetReference(user.GetType(),
                                                                                    user.ID,
                                                                                    "Roles",
                                                                                    role.GetType(),
                                                                                    role2.ID,
                                                                                    "Users",
                                                                                    false);

                Assert.IsNull(reference, "The return value should be null.");
            }
        }
        public virtual void Test_Delete_RemoveReferences()
        {
            using (LogGroup logGroup = LogGroup.StartDebug("Testing saving of an EntityReference."))
            {

                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                role.ID = Guid.NewGuid();
                role.Name = "Test role";

                // This should remain commented out to check for exclusion
                user.Roles = new TestRole[]{role};

                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);

                DataAccess.Data.Deleter.Delete(role);

                TestUser user2 = DataAccess.Data.Reader.GetEntity<TestUser>("ID", user.ID);

                DataAccess.Data.Activator.Activate(user2, "Roles");

                Assert.IsNotNull(user2.Roles);

                if (user2.Roles != null)
                    Assert.AreEqual(0, user2.Roles.Length, "Incorrect number of roles. The role should have been removed.");
            }
        }
        public void Test_Static_GetByID_Missing()
        {
            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            Guid roleID = role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            user.Roles = new TestRole[] {role};

            TestRole role2 = Collection<TestRole>.GetByID(user.Roles, Guid.NewGuid());

            Assert.IsNull(role2, "A role was retrieved when it shouldn't have been.");
        }
        public void Test_GetReferences()
        {
            EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences();

            Assert.IsNotNull(references);

            Assert.AreEqual(0, references.Count, "Invalid number of references found before creating one.");

            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestRole role = new TestRole();
            Guid roleID = role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            user.Roles = Collection<TestRole>.Add(user.Roles, role);

            DataAccess.Data.Saver.Save(role);
            DataAccess.Data.Saver.Save(user);

            references = DataAccess.Data.Referencer.GetReferences();

            Assert.IsNotNull(references);

            Assert.AreEqual(1, references.Count, "Invalid number of references found after creating one.");
        }
        public void Test_LoadEntitiesFileList()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "Test";

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            TestArticle article = new TestArticle();
            article.ID = Guid.NewGuid();
            article.Title = "Test";

            TestCategory category = new TestCategory();
            category.ID = Guid.NewGuid();
            category.Name = "Test";

            user.Roles = new TestRole[] {role};

            DataExporter exporter = (DataExporter)DataAccess.Data.InitializeDataExporter();

            exporter.ExportDirectoryPath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Exported";

            // IMPORTANT: Export the references before the entities, because the references are stripped from the entities upon export
            // Alternative is to reactivate the entities and their references using DataAccess.Data.Activator.
            EntityReferenceCollection references = DataAccess.Data.Referencer.GetActiveReferences(user);

            exporter.ExportEntity(references[0]);

            exporter.ExportEntity(user);
            exporter.ExportEntity(role);
            exporter.ExportEntity(article);
            exporter.ExportEntity(category);

            DataImporter importer = (DataImporter)DataAccess.Data.InitializeDataImporter();
            importer.ImportableDirectoryPath = exporter.ExportDirectoryPath;

            string[] fileList = importer.LoadEntitiesFileList();

            Assert.AreEqual(5, fileList.Length, "Incorrect number of files found.");
        }
        public void Test_ZipToFile()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "Test";

            DataAccess.Data.Saver.Save(user);

            TestRole role = new TestRole();
            role.ID = Guid.NewGuid();
            role.Name = "Test Role";

            DataAccess.Data.Saver.Save(role);

            TestArticle article = new TestArticle();
            article.ID = Guid.NewGuid();
            article.Title = "Test";

            DataAccess.Data.Saver.Save(article);

            TestCategory category = new TestCategory();
            category.Name = "Test";

            DataAccess.Data.Saver.Save(category);

            string outputDirectory = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Exported";

            int expectedCount = 0;

            foreach (string dataStoreName in DataAccess.Data.GetDataStoreNames())
            {
                foreach (IEntity entity in DataAccess.Data.Stores[dataStoreName].Indexer.GetEntities())
                    expectedCount ++;
            }

            DataExporter exporter = (DataExporter)DataAccess.Data.InitializeDataExporter();
            exporter.ExportDirectoryPath = outputDirectory;

            exporter.ExportToXml();

            int fileCount = 0;
            foreach (string directory in Directory.GetDirectories(outputDirectory))
            {
                foreach (String file in Directory.GetFiles(directory))
                {
                    fileCount++;
                }
            }

            Assert.AreEqual(expectedCount, fileCount, "Incorrect number of files found.");

            DirectoryZipper zipper = new DirectoryZipper(exporter.ExportDirectoryPath);

            string zipFileName = @"Backup--" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "--" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + ".zip";
            string zipFilePath = TestUtilities.GetTestingPath(this) + Path.DirectorySeparatorChar + "Backups" + Path.DirectorySeparatorChar + zipFileName;
            //zipShortPath;//Config.Application.PhysicalApplicationPath + zipShortPath;

            zipper.ZipToFile(zipFilePath);

            Assert.IsTrue(File.Exists(zipFilePath), "The zip file wasn't created.");

            // TODO: Open the zip file and check the contents
        }
        public virtual void Test_GetPageOfEntitiesWithReference_EmptyReferencedEntityID_NotFound()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the index retrieval of entities that don't have any references on a particular property.", NLog.LogLevel.Debug))
            {

                TestUser user = new TestUser();
                Guid userID = user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                Guid roleID = role.ID = Guid.NewGuid();
                role.Name = "Test Role";

                // Assign a user to the role
                role.Users = new TestUser[] {user};

                DataAccess.Data.Saver.Save(user);
                DataAccess.Data.Saver.Save(role);

                PagingLocation location = new PagingLocation(0, 10);
                string sortExpression = "UsernameAscending";

                TestRole[] foundRoles = DataAccess.Data.Indexer.GetPageOfEntitiesWithReference<TestRole>("Users", typeof(TestUser), Guid.Empty, location, sortExpression);

                Assert.IsNotNull(foundRoles, "The found roles object returned was null.");

                Assert.AreEqual(0, foundRoles.Length, "Invalid number of roles found.");
            }
        }
        public void Test_GetReference_EmptyReferencedEntityID_Found()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the retrieval of references for an entity when specifying a Guid.Empty referenced entity ID.", NLog.LogLevel.Debug))
            {

                TestUser user = new TestUser();
                Guid userID = user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";

                TestRole role = new TestRole();
                Guid roleID = role.ID = Guid.NewGuid();
                role.Name = "Test Role";

                user.Roles = Collection<TestRole>.Add(user.Roles, role);

                DataAccess.Data.Saver.Save(role);
                DataAccess.Data.Saver.Save(user);

                EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(user.GetType(), user.ID, "Roles", typeof(TestRole), false);

                Assert.IsNotNull(references, "The references object returned was null.");

                if (references != null)
                {
                    Assert.AreEqual(1, references.Count, "Wrong number of references returned.");

                    Assert.IsTrue(references[0].Includes(userID, "Roles"), "The user ID wasn't found on the reference.");
                }
            }
        }