public TestUser CreateTestUser(string testingDirectory)
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "First";
            user.LastName = "Last";
            user.Username = "******";
            user.Email = "*****@*****.**";

            string filePath = testingDirectory + Path.DirectorySeparatorChar
                + user.GetType().FullName + Path.DirectorySeparatorChar
                + user.ID.ToString() + ".xml";

            string dirPath = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);

            using (StreamWriter writer = File.CreateText(filePath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TestUser));

                serializer.Serialize(writer, user);

                writer.Close();
            }

            return user;
        }
        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_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_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_GetMirrorPropertyName_Multiple_Implicit_Sync()
        {
            TestUser user = new TestUser();

            string mirrorPropertyName = EntitiesUtilities.GetMirrorPropertyName(user, user.GetType().GetProperty("Roles"));

            Assert.AreEqual("Users", mirrorPropertyName, "The mirror property name wasn't determined correctly.");
        }
        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 void Test_CopyTo_TestUser()
        {
            TestUser user1 = new TestUser();
            user1.ID = Guid.NewGuid();
            user1.FirstName = "Before";

            TestUser user2 = new TestUser();

            user1.CopyTo(user2);

            Assert.AreEqual(user1.FirstName, user2.FirstName, "The first name wasn't transferred.");
        }
        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_CreateFilePath_Entity()
        {
            TestUser user = new TestUser();
            user.FirstName = "First";
            user.ID = Guid.NewGuid();

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

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

            string expected = basePath + Path.DirectorySeparatorChar + user.GetType() + Path.DirectorySeparatorChar
                + user.ID.ToString() + ".xml";

            Assert.AreEqual(expected, path, "The path doesn't match what's expected.");
        }
        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_GetDataStoreNames()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "First";
            user.LastName = "Last";
            user.Email = "Email";
            user.Username = "******";

            DataAccess.Data.Saver.Save(user);

            string[] names = DataAccess.Data.GetDataStoreNames();

            string expected0 = "TestRole-TestUser";
            string expected1 = "TestUser";

            Assert.AreEqual(2, names.Length, "Invalid number of data store names returned.");

            Assert.AreEqual(expected0, names[0], "The first data store name doesn't match what's expected.");
            Assert.AreEqual(expected1, names[1], "The second data store name doesn't match what's expected.");
        }
        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_Execute_Entity()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "-- First Name -- ";
            user.LastName = "-- Last Name --";
            //user.Username = "******";

            XmlDocument document = XmlUtilities.SerializeToDocument(user);

            RenameTypeCommand command = new RenameTypeCommand();
            command.TypeName = user.ShortTypeName;
            command.NewTypeName = "TestAccount";

            command.Execute(document);

            Assert.AreEqual("TestAccount", document.DocumentElement.Name, "The type name wasn't updated on the document element node.");

            TestAccount account = (TestAccount)XmlUtilities.DeserializeFromDocument(document, typeof(TestAccount));

            Assert.AreEqual(user.FirstName, account.FirstName, "The properties don't match.");
        }
        public void Test_Execute_Entity()
        {
            TestUser user = new TestUser();
            user.ID = Guid.NewGuid();
            user.FirstName = "-- First Name -- ";
            user.LastName = "-- Last Name --";
            //user.Username = "******";

            XmlDocument document = XmlUtilities.SerializeToDocument(user);

            RenamePropertyCommand command = new RenamePropertyCommand();
            command.TypeName = user.ShortTypeName;
            command.PropertyName = "FirstName";
            command.NewPropertyName = "Username";

            command.Execute(document);

            TestUser updatedUser = (TestUser)XmlUtilities.DeserializeFromDocument(document, typeof(TestUser));

            // This should have moved the "FirstName" property to "Username" and therefore the new "Username" property value
            // should be equal with the original "FirstName" property
            Assert.AreEqual(user.FirstName, updatedUser.Username, "The property wasn't renamed properly.");
        }
        public void Test_Delete()
        {
            TestUser user = new TestUser();
            Guid userID = user.ID = Guid.NewGuid();
            user.FirstName = "Test";
            user.LastName = "User";

            TestUser[] foundUsers1 = DataAccess.Data.Indexer.GetEntities<TestUser>();

            Assert.AreEqual(0, foundUsers1.Length, "There should be no users found yet.");

            DataAccess.Data.Saver.Save(user);

            TestUser[] foundUsers2 = DataAccess.Data.Indexer.GetEntities<TestUser>();

            Assert.AreEqual(1, foundUsers2.Length, "User wasn't saved or too many found.");

            DataAccess.Data.Deleter.Delete(user);

            TestUser[] foundUsers3 = DataAccess.Data.Indexer.GetEntities<TestUser>();

            Assert.AreEqual(0, foundUsers3.Length, "Invalid number of users.");
        }
        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_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 virtual void Test_GetEntitiesByType()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing a query of all entities of a specific type.", NLog.LogLevel.Debug))
            {
                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                user.FirstName = "Test";
                user.LastName = "User";
                user.Username = "******";

                DataAccess.Data.Saver.Save(user);

                IEntity[] entities = DataAccess.Data.Indexer.GetEntities(user.GetType());

                Assert.AreEqual(1, entities.Length, "Invalid number found.");
            }
        }
        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_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_Activate_2References_Async_Converging()
        {
            using (LogGroup logGroup = LogGroup.Start("Testing the Activate function with 2 asynchronous, converging references.", NLog.LogLevel.Debug))
            {

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

                TestUser user = new TestUser();
                user.ID = Guid.NewGuid();
                Guid userID = user.ID;
                user.FirstName = "First";
                user.LastName = "Last";
                user.Username ="******";
                user.Password = "******";

                /*TestArticlePage page = new TestArticlePage();
                page.ID = Guid.NewGuid();
                Guid pageID = page.ID;
                page.Title = "Test Page";

                TestArticlePage page2 = new TestArticlePage();
                page2.ID = Guid.NewGuid();
                Guid page2ID = page2.ID;
                page2.Title = "Test Page 2";
                */
                //article.Pages = new TestArticlePage[] {page, page2};
                //user.Roles = Collection<TestRole>.Add(user.Roles, role);

                article.Editors = new TestUser[] {user};

                //DataAccess.Data.Saver.Save(page);
                //DataAccess.Data.Saver.Save(page2);

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

                //  TODO: Check if needed
                //page = null;
                //page2 = null;
                //article = null;

                EntityReferenceCollection references = DataAccess.Data.Referencer.GetReferences(article.GetType().Name, user.GetType().Name);

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

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

                TestArticle foundArticle = DataAccess.Data.Reader.GetEntity<TestArticle>("ID", articleID);

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

                DataAccess.Data.Activator.Activate(foundArticle, "Editors");
                DataAccess.Data.Activator.Activate(foundArticle, "Authors");

                Assert.IsNotNull(foundArticle.Editors, "The article.Editors property is null.");
                Assert.AreEqual(1, foundArticle.Editors.Length, "article.Editors.Length != 1.");

                if (foundArticle.Editors != null && foundArticle.Editors.Length == 1)
                {
                    TestUser foundUser1 = foundArticle.Editors[0];

                    // Use an array and IndexOf function so the match will work in any order
                    Guid[] userIDs = new Guid[] {userID};

                    Assert.AreEqual(userID.ToString(), foundUser1.ID.ToString(), "The IDs of the user in Editors property doesn't match expected.");

                }

                Assert.AreEqual(0, foundArticle.Authors.Length, "Authors found when they shouldn't be. The two properties must be getting mixed up.");

            }
        }
        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 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 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.");

            }
        }